lxc_template.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package docker
  2. import (
  3. "strings"
  4. "text/template"
  5. )
  6. const LxcTemplate = `
  7. {{if .Config.NetworkDisabled}}
  8. # network is disabled (-n=false)
  9. lxc.network.type = empty
  10. {{else}}
  11. # network configuration
  12. lxc.network.type = veth
  13. lxc.network.link = {{.NetworkSettings.Bridge}}
  14. lxc.network.name = eth0
  15. {{end}}
  16. # root filesystem
  17. {{$ROOTFS := .RootfsPath}}
  18. lxc.rootfs = {{$ROOTFS}}
  19. # use a dedicated pts for the container (and limit the number of pseudo terminal
  20. # available)
  21. lxc.pts = 1024
  22. # disable the main console
  23. lxc.console = none
  24. # no controlling tty at all
  25. lxc.tty = 1
  26. {{if (getHostConfig .).Privileged}}
  27. lxc.cgroup.devices.allow = a
  28. {{else}}
  29. # no implicit access to devices
  30. lxc.cgroup.devices.deny = a
  31. # /dev/null and zero
  32. lxc.cgroup.devices.allow = c 1:3 rwm
  33. lxc.cgroup.devices.allow = c 1:5 rwm
  34. # consoles
  35. lxc.cgroup.devices.allow = c 5:1 rwm
  36. lxc.cgroup.devices.allow = c 5:0 rwm
  37. lxc.cgroup.devices.allow = c 4:0 rwm
  38. lxc.cgroup.devices.allow = c 4:1 rwm
  39. # /dev/urandom,/dev/random
  40. lxc.cgroup.devices.allow = c 1:9 rwm
  41. lxc.cgroup.devices.allow = c 1:8 rwm
  42. # /dev/pts/ - pts namespaces are "coming soon"
  43. lxc.cgroup.devices.allow = c 136:* rwm
  44. lxc.cgroup.devices.allow = c 5:2 rwm
  45. # tuntap
  46. lxc.cgroup.devices.allow = c 10:200 rwm
  47. # fuse
  48. #lxc.cgroup.devices.allow = c 10:229 rwm
  49. # rtc
  50. #lxc.cgroup.devices.allow = c 254:0 rwm
  51. {{end}}
  52. # standard mount point
  53. # Use mnt.putold as per https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/986385
  54. lxc.pivotdir = lxc_putold
  55. # NOTICE: These mounts must be applied within the namespace
  56. # WARNING: procfs is a known attack vector and should probably be disabled
  57. # if your userspace allows it. eg. see http://blog.zx2c4.com/749
  58. lxc.mount.entry = proc {{escapeFstabSpaces $ROOTFS}}/proc proc nosuid,nodev,noexec 0 0
  59. # WARNING: sysfs is a known attack vector and should probably be disabled
  60. # if your userspace allows it. eg. see http://bit.ly/T9CkqJ
  61. lxc.mount.entry = sysfs {{escapeFstabSpaces $ROOTFS}}/sys sysfs nosuid,nodev,noexec 0 0
  62. lxc.mount.entry = devpts {{escapeFstabSpaces $ROOTFS}}/dev/pts devpts newinstance,ptmxmode=0666,nosuid,noexec 0 0
  63. lxc.mount.entry = shm {{escapeFstabSpaces $ROOTFS}}/dev/shm tmpfs size=65536k,nosuid,nodev,noexec 0 0
  64. {{if (getHostConfig .).Privileged}}
  65. {{if (getCapabilities .).AppArmor}}
  66. lxc.aa_profile = unconfined
  67. {{else}}
  68. #lxc.aa_profile = unconfined
  69. {{end}}
  70. {{end}}
  71. # limits
  72. {{if .Config.Memory}}
  73. lxc.cgroup.memory.limit_in_bytes = {{.Config.Memory}}
  74. lxc.cgroup.memory.soft_limit_in_bytes = {{.Config.Memory}}
  75. {{with $memSwap := getMemorySwap .Config}}
  76. lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
  77. {{end}}
  78. {{end}}
  79. {{if .Config.CpuShares}}
  80. lxc.cgroup.cpu.shares = {{.Config.CpuShares}}
  81. {{end}}
  82. {{if (getHostConfig .).LxcConf}}
  83. {{range $pair := (getHostConfig .).LxcConf}}
  84. {{$pair.Key}} = {{$pair.Value}}
  85. {{end}}
  86. {{end}}
  87. `
  88. var LxcTemplateCompiled *template.Template
  89. // Escape spaces in strings according to the fstab documentation, which is the
  90. // format for "lxc.mount.entry" lines in lxc.conf. See also "man 5 fstab".
  91. func escapeFstabSpaces(field string) string {
  92. return strings.Replace(field, " ", "\\040", -1)
  93. }
  94. func getMemorySwap(config *Config) int64 {
  95. // By default, MemorySwap is set to twice the size of RAM.
  96. // If you want to omit MemorySwap, set it to `-1'.
  97. if config.MemorySwap < 0 {
  98. return 0
  99. }
  100. return config.Memory * 2
  101. }
  102. func getHostConfig(container *Container) *HostConfig {
  103. return container.hostConfig
  104. }
  105. func getCapabilities(container *Container) *Capabilities {
  106. return container.runtime.capabilities
  107. }
  108. func init() {
  109. var err error
  110. funcMap := template.FuncMap{
  111. "getMemorySwap": getMemorySwap,
  112. "getHostConfig": getHostConfig,
  113. "getCapabilities": getCapabilities,
  114. "escapeFstabSpaces": escapeFstabSpaces,
  115. }
  116. LxcTemplateCompiled, err = template.New("lxc").Funcs(funcMap).Parse(LxcTemplate)
  117. if err != nil {
  118. panic(err)
  119. }
  120. }