From 934119dc914a9e89a53ca714bc737be67a700a72 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 11 Mar 2013 17:40:54 -0700 Subject: [PATCH] Added support for RamSwap in the generated LXC config (to limit the swap and have the right default settings) Upstream-commit: 75d04a5a7561cabba0aaf2875ef4479381096f91 Component: engine --- components/engine/container.go | 3 ++- components/engine/lxc_template.go | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/components/engine/container.go b/components/engine/container.go index 530c08bc39..c837500549 100644 --- a/components/engine/container.go +++ b/components/engine/container.go @@ -53,7 +53,8 @@ type Container struct { type Config struct { Hostname string User string - Ram int64 + Ram int64 // Memory limit (in bytes) + RamSwap int64 // Total memory usage (ram + swap); set `-1' to disable swap Ports []int Tty bool // Attach standard streams to a tty, including stdin if it is not closed. OpenStdin bool // Open stdin diff --git a/components/engine/lxc_template.go b/components/engine/lxc_template.go index 47f2058dc4..5a37624a32 100755 --- a/components/engine/lxc_template.go +++ b/components/engine/lxc_template.go @@ -88,14 +88,29 @@ lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod net_raw se {{if .Config.Ram}} lxc.cgroup.memory.limit_in_bytes = {{.Config.Ram}} lxc.cgroup.memory.soft_limit_in_bytes = {{.Config.Ram}} +{{with $ramSwap := getRamSwap .Config}} +lxc.cgroup.memory.memsw.limit_in_bytes = {{$ramSwap}} +{{end}} {{end}} ` var LxcTemplateCompiled *template.Template +func getRamSwap(config *Config) int64 { + // By default, RamSwap is set to twice the size of RAM. + // If you want to omit RamSwap, set it to `-1'. + if config.RamSwap < 0 { + return 0 + } + return config.Ram * 2 +} + func init() { var err error - LxcTemplateCompiled, err = template.New("lxc").Parse(LxcTemplate) + funcMap := template.FuncMap{ + "getRamSwap": getRamSwap, + } + LxcTemplateCompiled, err = template.New("lxc").Funcs(funcMap).Parse(LxcTemplate) if err != nil { panic(err) }