github.com/circular-dark/docker@v1.7.0/daemon/execdriver/lxc/lxc_template.go (about)

     1  // +build linux
     2  
     3  package lxc
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"strings"
     9  	"text/template"
    10  
    11  	"github.com/Sirupsen/logrus"
    12  	"github.com/docker/docker/daemon/execdriver"
    13  	nativeTemplate "github.com/docker/docker/daemon/execdriver/native/template"
    14  	"github.com/docker/docker/pkg/stringutils"
    15  	"github.com/docker/libcontainer/label"
    16  )
    17  
    18  const LxcTemplate = `
    19  lxc.network.type = none
    20  # root filesystem
    21  {{$ROOTFS := .Rootfs}}
    22  lxc.rootfs = {{$ROOTFS}}
    23  
    24  # use a dedicated pts for the container (and limit the number of pseudo terminal
    25  # available)
    26  lxc.pts = 1024
    27  
    28  # disable the main console
    29  lxc.console = none
    30  
    31  # no controlling tty at all
    32  lxc.tty = 1
    33  
    34  {{if .ProcessConfig.Privileged}}
    35  lxc.cgroup.devices.allow = a
    36  {{else}}
    37  # no implicit access to devices
    38  lxc.cgroup.devices.deny = a
    39  #Allow the devices passed to us in the AllowedDevices list.
    40  {{range $allowedDevice := .AllowedDevices}}
    41  lxc.cgroup.devices.allow = {{$allowedDevice.CgroupString}}
    42  {{end}}
    43  {{end}}
    44  
    45  # standard mount point
    46  # Use mnt.putold as per https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/986385
    47  lxc.pivotdir = lxc_putold
    48  
    49  # NOTICE: These mounts must be applied within the namespace
    50  {{if .ProcessConfig.Privileged}}
    51  # WARNING: mounting procfs and/or sysfs read-write is a known attack vector.
    52  # See e.g. http://blog.zx2c4.com/749 and https://bit.ly/T9CkqJ
    53  # We mount them read-write here, but later, dockerinit will call the Restrict() function to remount them read-only.
    54  # We cannot mount them directly read-only, because that would prevent loading AppArmor profiles.
    55  lxc.mount.entry = proc {{escapeFstabSpaces $ROOTFS}}/proc proc nosuid,nodev,noexec 0 0
    56  lxc.mount.entry = sysfs {{escapeFstabSpaces $ROOTFS}}/sys sysfs nosuid,nodev,noexec 0 0
    57  	{{if .AppArmor}}
    58  lxc.aa_profile = unconfined
    59  	{{end}}
    60  {{else}}
    61  # In non-privileged mode, lxc will automatically mount /proc and /sys in readonly mode
    62  # for security. See: http://man7.org/linux/man-pages/man5/lxc.container.conf.5.html
    63  lxc.mount.auto = proc sys
    64  	{{if .AppArmorProfile}}
    65  lxc.aa_profile = {{.AppArmorProfile}}
    66  	{{end}}
    67  {{end}}
    68  
    69  {{if .ProcessConfig.Tty}}
    70  lxc.mount.entry = {{.ProcessConfig.Console}} {{escapeFstabSpaces $ROOTFS}}/dev/console none bind,rw 0 0
    71  {{end}}
    72  
    73  lxc.mount.entry = devpts {{escapeFstabSpaces $ROOTFS}}/dev/pts devpts {{formatMountLabel "newinstance,ptmxmode=0666,nosuid,noexec" ""}} 0 0
    74  lxc.mount.entry = shm {{escapeFstabSpaces $ROOTFS}}/dev/shm tmpfs {{formatMountLabel "size=65536k,nosuid,nodev,noexec" ""}} 0 0
    75  
    76  {{range $value := .Mounts}}
    77  {{$createVal := isDirectory $value.Source}}
    78  {{if $value.Writable}}
    79  lxc.mount.entry = {{$value.Source}} {{escapeFstabSpaces $ROOTFS}}/{{escapeFstabSpaces $value.Destination}} none rbind,rw,create={{$createVal}} 0 0
    80  {{else}}
    81  lxc.mount.entry = {{$value.Source}} {{escapeFstabSpaces $ROOTFS}}/{{escapeFstabSpaces $value.Destination}} none rbind,ro,create={{$createVal}} 0 0
    82  {{end}}
    83  {{end}}
    84  
    85  # limits
    86  {{if .Resources}}
    87  {{if .Resources.Memory}}
    88  lxc.cgroup.memory.limit_in_bytes = {{.Resources.Memory}}
    89  lxc.cgroup.memory.soft_limit_in_bytes = {{.Resources.Memory}}
    90  {{with $memSwap := getMemorySwap .Resources}}
    91  lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
    92  {{end}}
    93  {{end}}
    94  {{if .Resources.CpuShares}}
    95  lxc.cgroup.cpu.shares = {{.Resources.CpuShares}}
    96  {{end}}
    97  {{if .Resources.CpuPeriod}}
    98  lxc.cgroup.cpu.cfs_period_us = {{.Resources.CpuPeriod}}
    99  {{end}}
   100  {{if .Resources.CpusetCpus}}
   101  lxc.cgroup.cpuset.cpus = {{.Resources.CpusetCpus}}
   102  {{end}}
   103  {{if .Resources.CpusetMems}}
   104  lxc.cgroup.cpuset.mems = {{.Resources.CpusetMems}}
   105  {{end}}
   106  {{if .Resources.CpuQuota}}
   107  lxc.cgroup.cpu.cfs_quota_us = {{.Resources.CpuQuota}}
   108  {{end}}
   109  {{if .Resources.BlkioWeight}}
   110  lxc.cgroup.blkio.weight = {{.Resources.BlkioWeight}}
   111  {{end}}
   112  {{if .Resources.OomKillDisable}}
   113  lxc.cgroup.memory.oom_control = {{.Resources.OomKillDisable}}
   114  {{end}}
   115  {{end}}
   116  
   117  {{if .LxcConfig}}
   118  {{range $value := .LxcConfig}}
   119  lxc.{{$value}}
   120  {{end}}
   121  {{end}}
   122  
   123  {{if .Network.Interface}}
   124  {{if .Network.Interface.IPAddress}}
   125  lxc.network.ipv4 = {{.Network.Interface.IPAddress}}/{{.Network.Interface.IPPrefixLen}}
   126  {{end}}
   127  {{if .Network.Interface.Gateway}}
   128  lxc.network.ipv4.gateway = {{.Network.Interface.Gateway}}
   129  {{end}}
   130  {{if .Network.Interface.MacAddress}}
   131  lxc.network.hwaddr = {{.Network.Interface.MacAddress}}
   132  {{end}}
   133  {{end}}
   134  {{if .ProcessConfig.Env}}
   135  lxc.utsname = {{getHostname .ProcessConfig.Env}}
   136  {{end}}
   137  
   138  {{if .ProcessConfig.Privileged}}
   139  # No cap values are needed, as lxc is starting in privileged mode
   140  {{else}}
   141  	{{ with keepCapabilities .CapAdd .CapDrop }}
   142  		{{range .}}
   143  lxc.cap.keep = {{.}}
   144  		{{end}}
   145  	{{else}}
   146  		{{ with dropList .CapDrop }}
   147  		{{range .}}
   148  lxc.cap.drop = {{.}}
   149  		{{end}}
   150  		{{end}}
   151  	{{end}}
   152  {{end}}
   153  `
   154  
   155  var LxcTemplateCompiled *template.Template
   156  
   157  // Escape spaces in strings according to the fstab documentation, which is the
   158  // format for "lxc.mount.entry" lines in lxc.conf. See also "man 5 fstab".
   159  func escapeFstabSpaces(field string) string {
   160  	return strings.Replace(field, " ", "\\040", -1)
   161  }
   162  
   163  func keepCapabilities(adds []string, drops []string) ([]string, error) {
   164  	container := nativeTemplate.New()
   165  	logrus.Debugf("adds %s drops %s\n", adds, drops)
   166  	caps, err := execdriver.TweakCapabilities(container.Capabilities, adds, drops)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	var newCaps []string
   171  	for _, cap := range caps {
   172  		logrus.Debugf("cap %s\n", cap)
   173  		realCap := execdriver.GetCapability(cap)
   174  		numCap := fmt.Sprintf("%d", realCap.Value)
   175  		newCaps = append(newCaps, numCap)
   176  	}
   177  
   178  	return newCaps, nil
   179  }
   180  
   181  func dropList(drops []string) ([]string, error) {
   182  	if stringutils.InSlice(drops, "all") {
   183  		var newCaps []string
   184  		for _, capName := range execdriver.GetAllCapabilities() {
   185  			cap := execdriver.GetCapability(capName)
   186  			logrus.Debugf("drop cap %s\n", cap.Key)
   187  			numCap := fmt.Sprintf("%d", cap.Value)
   188  			newCaps = append(newCaps, numCap)
   189  		}
   190  		return newCaps, nil
   191  	}
   192  	return []string{}, nil
   193  }
   194  
   195  func isDirectory(source string) string {
   196  	f, err := os.Stat(source)
   197  	logrus.Debugf("dir: %s\n", source)
   198  	if err != nil {
   199  		if os.IsNotExist(err) {
   200  			return "dir"
   201  		}
   202  		return ""
   203  	}
   204  	if f.IsDir() {
   205  		return "dir"
   206  	}
   207  	return "file"
   208  }
   209  
   210  func getMemorySwap(v *execdriver.Resources) int64 {
   211  	// By default, MemorySwap is set to twice the size of RAM.
   212  	// If you want to omit MemorySwap, set it to `-1'.
   213  	if v.MemorySwap < 0 {
   214  		return 0
   215  	}
   216  	return v.Memory * 2
   217  }
   218  
   219  func getLabel(c map[string][]string, name string) string {
   220  	label := c["label"]
   221  	for _, l := range label {
   222  		parts := strings.SplitN(l, "=", 2)
   223  		if strings.TrimSpace(parts[0]) == name {
   224  			return strings.TrimSpace(parts[1])
   225  		}
   226  	}
   227  	return ""
   228  }
   229  
   230  func getHostname(env []string) string {
   231  	for _, kv := range env {
   232  		parts := strings.SplitN(kv, "=", 2)
   233  		if parts[0] == "HOSTNAME" && len(parts) == 2 {
   234  			return parts[1]
   235  		}
   236  	}
   237  	return ""
   238  }
   239  
   240  func init() {
   241  	var err error
   242  	funcMap := template.FuncMap{
   243  		"getMemorySwap":     getMemorySwap,
   244  		"escapeFstabSpaces": escapeFstabSpaces,
   245  		"formatMountLabel":  label.FormatMountLabel,
   246  		"isDirectory":       isDirectory,
   247  		"keepCapabilities":  keepCapabilities,
   248  		"dropList":          dropList,
   249  		"getHostname":       getHostname,
   250  	}
   251  	LxcTemplateCompiled, err = template.New("lxc").Funcs(funcMap).Parse(LxcTemplate)
   252  	if err != nil {
   253  		panic(err)
   254  	}
   255  }