diff --git a/container.go b/container.go index 530c08bc39..c837500549 100644 --- a/container.go +++ b/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/lxc_template.go b/lxc_template.go index 47f2058dc4..5a37624a32 100755 --- a/lxc_template.go +++ b/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) }