2013-01-19 00:13:39 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
const LxcTemplate = `
|
|
|
|
# hostname
|
|
|
|
{{if .Config.Hostname}}
|
|
|
|
lxc.utsname = {{.Config.Hostname}}
|
|
|
|
{{else}}
|
2013-01-22 02:39:52 +00:00
|
|
|
lxc.utsname = {{.Id}}
|
2013-01-19 00:13:39 +00:00
|
|
|
{{end}}
|
|
|
|
#lxc.aa_profile = unconfined
|
|
|
|
|
2013-07-23 02:00:35 +00:00
|
|
|
{{if .Config.NetworkDisabled}}
|
|
|
|
# network is disabled (-n=false)
|
|
|
|
lxc.network.type = empty
|
|
|
|
{{else}}
|
2013-01-19 00:13:39 +00:00
|
|
|
# network configuration
|
2013-02-21 01:47:09 +00:00
|
|
|
lxc.network.type = veth
|
|
|
|
lxc.network.flags = up
|
2013-04-03 22:05:03 +00:00
|
|
|
lxc.network.link = {{.NetworkSettings.Bridge}}
|
2013-02-21 01:47:09 +00:00
|
|
|
lxc.network.name = eth0
|
|
|
|
lxc.network.mtu = 1500
|
2013-06-04 18:00:22 +00:00
|
|
|
lxc.network.ipv4 = {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen}}
|
2013-07-22 00:11:47 +00:00
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
# root filesystem
|
2013-03-21 07:25:00 +00:00
|
|
|
{{$ROOTFS := .RootfsPath}}
|
2013-02-13 21:56:19 +00:00
|
|
|
lxc.rootfs = {{$ROOTFS}}
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-09-16 17:53:24 +00:00
|
|
|
{{if and .HostnamePath .HostsPath}}
|
2013-09-09 18:57:25 +00:00
|
|
|
# enable domain name support
|
|
|
|
lxc.mount.entry = {{.HostnamePath}} {{$ROOTFS}}/etc/hostname none bind,ro 0 0
|
|
|
|
lxc.mount.entry = {{.HostsPath}} {{$ROOTFS}}/etc/hosts none bind,ro 0 0
|
2013-09-16 17:53:24 +00:00
|
|
|
{{end}}
|
2013-09-09 18:57:25 +00:00
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
# use a dedicated pts for the container (and limit the number of pseudo terminal
|
|
|
|
# available)
|
|
|
|
lxc.pts = 1024
|
|
|
|
|
|
|
|
# disable the main console
|
|
|
|
lxc.console = none
|
|
|
|
|
|
|
|
# no controlling tty at all
|
|
|
|
lxc.tty = 1
|
|
|
|
|
2013-08-09 22:53:02 +00:00
|
|
|
{{if .Config.Privileged}}
|
|
|
|
lxc.cgroup.devices.allow = a
|
|
|
|
{{else}}
|
2013-01-19 00:13:39 +00:00
|
|
|
# no implicit access to devices
|
|
|
|
lxc.cgroup.devices.deny = a
|
|
|
|
|
|
|
|
# /dev/null and zero
|
|
|
|
lxc.cgroup.devices.allow = c 1:3 rwm
|
|
|
|
lxc.cgroup.devices.allow = c 1:5 rwm
|
|
|
|
|
|
|
|
# consoles
|
|
|
|
lxc.cgroup.devices.allow = c 5:1 rwm
|
|
|
|
lxc.cgroup.devices.allow = c 5:0 rwm
|
|
|
|
lxc.cgroup.devices.allow = c 4:0 rwm
|
|
|
|
lxc.cgroup.devices.allow = c 4:1 rwm
|
|
|
|
|
|
|
|
# /dev/urandom,/dev/random
|
|
|
|
lxc.cgroup.devices.allow = c 1:9 rwm
|
|
|
|
lxc.cgroup.devices.allow = c 1:8 rwm
|
|
|
|
|
|
|
|
# /dev/pts/* - pts namespaces are "coming soon"
|
|
|
|
lxc.cgroup.devices.allow = c 136:* rwm
|
|
|
|
lxc.cgroup.devices.allow = c 5:2 rwm
|
|
|
|
|
|
|
|
# tuntap
|
|
|
|
lxc.cgroup.devices.allow = c 10:200 rwm
|
|
|
|
|
|
|
|
# fuse
|
|
|
|
#lxc.cgroup.devices.allow = c 10:229 rwm
|
|
|
|
|
|
|
|
# rtc
|
|
|
|
#lxc.cgroup.devices.allow = c 254:0 rwm
|
2013-08-09 22:53:02 +00:00
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
# standard mount point
|
2013-10-09 20:40:46 +00:00
|
|
|
# Use mnt.putold as per https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/986385
|
|
|
|
lxc.pivotdir = lxc_putold
|
2013-06-19 17:37:08 +00:00
|
|
|
# WARNING: procfs is a known attack vector and should probably be disabled
|
|
|
|
# if your userspace allows it. eg. see http://blog.zx2c4.com/749
|
2013-02-13 21:56:19 +00:00
|
|
|
lxc.mount.entry = proc {{$ROOTFS}}/proc proc nosuid,nodev,noexec 0 0
|
2013-06-19 17:37:08 +00:00
|
|
|
# WARNING: sysfs is a known attack vector and should probably be disabled
|
|
|
|
# if your userspace allows it. eg. see http://bit.ly/T9CkqJ
|
2013-02-13 21:56:19 +00:00
|
|
|
lxc.mount.entry = sysfs {{$ROOTFS}}/sys sysfs nosuid,nodev,noexec 0 0
|
|
|
|
lxc.mount.entry = devpts {{$ROOTFS}}/dev/pts devpts newinstance,ptmxmode=0666,nosuid,noexec 0 0
|
|
|
|
#lxc.mount.entry = varrun {{$ROOTFS}}/var/run tmpfs mode=755,size=4096k,nosuid,nodev,noexec 0 0
|
|
|
|
#lxc.mount.entry = varlock {{$ROOTFS}}/var/lock tmpfs size=1024k,nosuid,nodev,noexec 0 0
|
2013-07-05 23:16:58 +00:00
|
|
|
lxc.mount.entry = shm {{$ROOTFS}}/dev/shm tmpfs size=65536k,nosuid,nodev,noexec 0 0
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-10-18 05:39:57 +00:00
|
|
|
# Inject dockerinit
|
2013-06-14 23:56:08 +00:00
|
|
|
lxc.mount.entry = {{.SysInitPath}} {{$ROOTFS}}/.dockerinit none bind,ro 0 0
|
2013-02-13 22:01:44 +00:00
|
|
|
|
2013-01-29 01:32:15 +00:00
|
|
|
# In order to get a working DNS environment, mount bind (ro) the host's /etc/resolv.conf into the container
|
2013-04-11 01:23:34 +00:00
|
|
|
lxc.mount.entry = {{.ResolvConfPath}} {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0
|
2013-04-10 23:10:53 +00:00
|
|
|
{{if .Volumes}}
|
2013-06-26 15:43:53 +00:00
|
|
|
{{ $rw := .VolumesRW }}
|
|
|
|
{{range $virtualPath, $realPath := .Volumes}}
|
|
|
|
lxc.mount.entry = {{$realPath}} {{$ROOTFS}}/{{$virtualPath}} none bind,{{ if index $rw $virtualPath }}rw{{else}}ro{{end}} 0 0
|
2013-05-13 23:39:54 +00:00
|
|
|
{{end}}
|
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-08-09 22:53:02 +00:00
|
|
|
{{if .Config.Privileged}}
|
|
|
|
# retain all capabilities; no lxc.cap.drop line
|
|
|
|
{{else}}
|
2013-01-19 00:13:39 +00:00
|
|
|
# drop linux capabilities (apply mainly to the user root in the container)
|
2013-06-19 17:39:35 +00:00
|
|
|
# (Note: 'lxc.cap.keep' is coming soon and should replace this under the
|
|
|
|
# security principle 'deny all unless explicitly permitted', see
|
|
|
|
# http://sourceforge.net/mailarchive/message.php?msg_id=31054627 )
|
2013-09-26 19:41:45 +00:00
|
|
|
lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod setpcap sys_admin sys_boot sys_module sys_nice sys_pacct sys_rawio sys_resource sys_time sys_tty_config
|
2013-08-09 22:53:02 +00:00
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
# limits
|
2013-03-12 02:25:02 +00:00
|
|
|
{{if .Config.Memory}}
|
|
|
|
lxc.cgroup.memory.limit_in_bytes = {{.Config.Memory}}
|
|
|
|
lxc.cgroup.memory.soft_limit_in_bytes = {{.Config.Memory}}
|
2013-03-12 02:55:14 +00:00
|
|
|
{{with $memSwap := getMemorySwap .Config}}
|
|
|
|
lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
|
2013-03-12 00:40:54 +00:00
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
{{end}}
|
2013-05-07 18:16:30 +00:00
|
|
|
{{if .Config.CpuShares}}
|
|
|
|
lxc.cgroup.cpu.shares = {{.Config.CpuShares}}
|
|
|
|
{{end}}
|
2013-01-19 00:13:39 +00:00
|
|
|
`
|
|
|
|
|
2013-08-15 23:35:03 +00:00
|
|
|
const LxcHostConfigTemplate = `
|
|
|
|
{{if .LxcConf}}
|
|
|
|
{{range $pair := .LxcConf}}
|
|
|
|
{{$pair.Key}} = {{$pair.Value}}
|
|
|
|
{{end}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
var LxcTemplateCompiled *template.Template
|
2013-08-15 23:35:03 +00:00
|
|
|
var LxcHostConfigTemplateCompiled *template.Template
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-03-12 02:25:02 +00:00
|
|
|
func getMemorySwap(config *Config) int64 {
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
|
|
|
// If you want to omit MemorySwap, set it to `-1'.
|
|
|
|
if config.MemorySwap < 0 {
|
2013-03-12 00:40:54 +00:00
|
|
|
return 0
|
|
|
|
}
|
2013-03-12 02:25:02 +00:00
|
|
|
return config.Memory * 2
|
2013-03-12 00:40:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
func init() {
|
|
|
|
var err error
|
2013-03-12 00:40:54 +00:00
|
|
|
funcMap := template.FuncMap{
|
2013-03-12 02:25:02 +00:00
|
|
|
"getMemorySwap": getMemorySwap,
|
2013-03-12 00:40:54 +00:00
|
|
|
}
|
|
|
|
LxcTemplateCompiled, err = template.New("lxc").Funcs(funcMap).Parse(LxcTemplate)
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-08-15 23:35:03 +00:00
|
|
|
LxcHostConfigTemplateCompiled, err = template.New("lxc-hostconfig").Funcs(funcMap).Parse(LxcHostConfigTemplate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|