daemon_unix.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "net"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "github.com/Sirupsen/logrus"
  12. pblkiodev "github.com/docker/docker/api/types/blkiodev"
  13. containertypes "github.com/docker/docker/api/types/container"
  14. "github.com/docker/docker/container"
  15. derr "github.com/docker/docker/errors"
  16. "github.com/docker/docker/image"
  17. "github.com/docker/docker/layer"
  18. "github.com/docker/docker/pkg/idtools"
  19. "github.com/docker/docker/pkg/parsers/kernel"
  20. "github.com/docker/docker/pkg/sysinfo"
  21. "github.com/docker/docker/reference"
  22. "github.com/docker/docker/runconfig"
  23. runconfigopts "github.com/docker/docker/runconfig/opts"
  24. "github.com/docker/libnetwork"
  25. nwconfig "github.com/docker/libnetwork/config"
  26. "github.com/docker/libnetwork/drivers/bridge"
  27. "github.com/docker/libnetwork/ipamutils"
  28. "github.com/docker/libnetwork/netlabel"
  29. "github.com/docker/libnetwork/options"
  30. "github.com/docker/libnetwork/types"
  31. blkiodev "github.com/opencontainers/runc/libcontainer/configs"
  32. "github.com/opencontainers/runc/libcontainer/label"
  33. )
  34. const (
  35. // See https://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
  36. linuxMinCPUShares = 2
  37. linuxMaxCPUShares = 262144
  38. platformSupported = true
  39. // It's not kernel limit, we want this 4M limit to supply a reasonable functional container
  40. linuxMinMemory = 4194304
  41. )
  42. func getBlkioWeightDevices(config *containertypes.HostConfig) ([]*blkiodev.WeightDevice, error) {
  43. var stat syscall.Stat_t
  44. var blkioWeightDevices []*blkiodev.WeightDevice
  45. for _, weightDevice := range config.BlkioWeightDevice {
  46. if err := syscall.Stat(weightDevice.Path, &stat); err != nil {
  47. return nil, err
  48. }
  49. weightDevice := blkiodev.NewWeightDevice(int64(stat.Rdev/256), int64(stat.Rdev%256), weightDevice.Weight, 0)
  50. blkioWeightDevices = append(blkioWeightDevices, weightDevice)
  51. }
  52. return blkioWeightDevices, nil
  53. }
  54. func parseSecurityOpt(container *container.Container, config *containertypes.HostConfig) error {
  55. var (
  56. labelOpts []string
  57. err error
  58. )
  59. for _, opt := range config.SecurityOpt {
  60. con := strings.SplitN(opt, ":", 2)
  61. if len(con) == 1 {
  62. return fmt.Errorf("Invalid --security-opt: %q", opt)
  63. }
  64. switch con[0] {
  65. case "label":
  66. labelOpts = append(labelOpts, con[1])
  67. case "apparmor":
  68. container.AppArmorProfile = con[1]
  69. case "seccomp":
  70. container.SeccompProfile = con[1]
  71. default:
  72. return fmt.Errorf("Invalid --security-opt: %q", opt)
  73. }
  74. }
  75. container.ProcessLabel, container.MountLabel, err = label.InitLabels(labelOpts)
  76. return err
  77. }
  78. func getBlkioReadIOpsDevices(config *containertypes.HostConfig) ([]*blkiodev.ThrottleDevice, error) {
  79. var blkioReadIOpsDevice []*blkiodev.ThrottleDevice
  80. var stat syscall.Stat_t
  81. for _, iopsDevice := range config.BlkioDeviceReadIOps {
  82. if err := syscall.Stat(iopsDevice.Path, &stat); err != nil {
  83. return nil, err
  84. }
  85. readIOpsDevice := blkiodev.NewThrottleDevice(int64(stat.Rdev/256), int64(stat.Rdev%256), iopsDevice.Rate)
  86. blkioReadIOpsDevice = append(blkioReadIOpsDevice, readIOpsDevice)
  87. }
  88. return blkioReadIOpsDevice, nil
  89. }
  90. func getBlkioWriteIOpsDevices(config *containertypes.HostConfig) ([]*blkiodev.ThrottleDevice, error) {
  91. var blkioWriteIOpsDevice []*blkiodev.ThrottleDevice
  92. var stat syscall.Stat_t
  93. for _, iopsDevice := range config.BlkioDeviceWriteIOps {
  94. if err := syscall.Stat(iopsDevice.Path, &stat); err != nil {
  95. return nil, err
  96. }
  97. writeIOpsDevice := blkiodev.NewThrottleDevice(int64(stat.Rdev/256), int64(stat.Rdev%256), iopsDevice.Rate)
  98. blkioWriteIOpsDevice = append(blkioWriteIOpsDevice, writeIOpsDevice)
  99. }
  100. return blkioWriteIOpsDevice, nil
  101. }
  102. func getBlkioReadBpsDevices(config *containertypes.HostConfig) ([]*blkiodev.ThrottleDevice, error) {
  103. var blkioReadBpsDevice []*blkiodev.ThrottleDevice
  104. var stat syscall.Stat_t
  105. for _, bpsDevice := range config.BlkioDeviceReadBps {
  106. if err := syscall.Stat(bpsDevice.Path, &stat); err != nil {
  107. return nil, err
  108. }
  109. readBpsDevice := blkiodev.NewThrottleDevice(int64(stat.Rdev/256), int64(stat.Rdev%256), bpsDevice.Rate)
  110. blkioReadBpsDevice = append(blkioReadBpsDevice, readBpsDevice)
  111. }
  112. return blkioReadBpsDevice, nil
  113. }
  114. func getBlkioWriteBpsDevices(config *containertypes.HostConfig) ([]*blkiodev.ThrottleDevice, error) {
  115. var blkioWriteBpsDevice []*blkiodev.ThrottleDevice
  116. var stat syscall.Stat_t
  117. for _, bpsDevice := range config.BlkioDeviceWriteBps {
  118. if err := syscall.Stat(bpsDevice.Path, &stat); err != nil {
  119. return nil, err
  120. }
  121. writeBpsDevice := blkiodev.NewThrottleDevice(int64(stat.Rdev/256), int64(stat.Rdev%256), bpsDevice.Rate)
  122. blkioWriteBpsDevice = append(blkioWriteBpsDevice, writeBpsDevice)
  123. }
  124. return blkioWriteBpsDevice, nil
  125. }
  126. func checkKernelVersion(k, major, minor int) bool {
  127. if v, err := kernel.GetKernelVersion(); err != nil {
  128. logrus.Warnf("%s", err)
  129. } else {
  130. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: k, Major: major, Minor: minor}) < 0 {
  131. return false
  132. }
  133. }
  134. return true
  135. }
  136. func checkKernel() error {
  137. // Check for unsupported kernel versions
  138. // FIXME: it would be cleaner to not test for specific versions, but rather
  139. // test for specific functionalities.
  140. // Unfortunately we can't test for the feature "does not cause a kernel panic"
  141. // without actually causing a kernel panic, so we need this workaround until
  142. // the circumstances of pre-3.10 crashes are clearer.
  143. // For details see https://github.com/docker/docker/issues/407
  144. if !checkKernelVersion(3, 10, 0) {
  145. v, _ := kernel.GetKernelVersion()
  146. if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
  147. logrus.Warnf("Your Linux kernel version %s can be unstable running docker. Please upgrade your kernel to 3.10.0.", v.String())
  148. }
  149. }
  150. return nil
  151. }
  152. // adaptContainerSettings is called during container creation to modify any
  153. // settings necessary in the HostConfig structure.
  154. func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConfig, adjustCPUShares bool) error {
  155. if adjustCPUShares && hostConfig.CPUShares > 0 {
  156. // Handle unsupported CPUShares
  157. if hostConfig.CPUShares < linuxMinCPUShares {
  158. logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
  159. hostConfig.CPUShares = linuxMinCPUShares
  160. } else if hostConfig.CPUShares > linuxMaxCPUShares {
  161. logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
  162. hostConfig.CPUShares = linuxMaxCPUShares
  163. }
  164. }
  165. if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
  166. // By default, MemorySwap is set to twice the size of Memory.
  167. hostConfig.MemorySwap = hostConfig.Memory * 2
  168. }
  169. if hostConfig.ShmSize == nil {
  170. shmSize := container.DefaultSHMSize
  171. hostConfig.ShmSize = &shmSize
  172. }
  173. var err error
  174. if hostConfig.SecurityOpt == nil {
  175. hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
  176. if err != nil {
  177. return err
  178. }
  179. }
  180. if hostConfig.MemorySwappiness == nil {
  181. defaultSwappiness := int64(-1)
  182. hostConfig.MemorySwappiness = &defaultSwappiness
  183. }
  184. return nil
  185. }
  186. func verifyContainerResources(resources *containertypes.Resources) ([]string, error) {
  187. warnings := []string{}
  188. sysInfo := sysinfo.New(true)
  189. // memory subsystem checks and adjustments
  190. if resources.Memory != 0 && resources.Memory < linuxMinMemory {
  191. return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
  192. }
  193. if resources.Memory > 0 && !sysInfo.MemoryLimit {
  194. warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
  195. logrus.Warnf("Your kernel does not support memory limit capabilities. Limitation discarded.")
  196. resources.Memory = 0
  197. resources.MemorySwap = -1
  198. }
  199. if resources.Memory > 0 && resources.MemorySwap != -1 && !sysInfo.SwapLimit {
  200. warnings = append(warnings, "Your kernel does not support swap limit capabilities, memory limited without swap.")
  201. logrus.Warnf("Your kernel does not support swap limit capabilities, memory limited without swap.")
  202. resources.MemorySwap = -1
  203. }
  204. if resources.Memory > 0 && resources.MemorySwap > 0 && resources.MemorySwap < resources.Memory {
  205. return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.")
  206. }
  207. if resources.Memory == 0 && resources.MemorySwap > 0 {
  208. return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.")
  209. }
  210. if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
  211. warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  212. logrus.Warnf("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  213. resources.MemorySwappiness = nil
  214. }
  215. if resources.MemorySwappiness != nil {
  216. swappiness := *resources.MemorySwappiness
  217. if swappiness < -1 || swappiness > 100 {
  218. return warnings, fmt.Errorf("Invalid value: %v, valid memory swappiness range is 0-100.", swappiness)
  219. }
  220. }
  221. if resources.MemoryReservation > 0 && !sysInfo.MemoryReservation {
  222. warnings = append(warnings, "Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  223. logrus.Warnf("Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  224. resources.MemoryReservation = 0
  225. }
  226. if resources.Memory > 0 && resources.MemoryReservation > 0 && resources.Memory < resources.MemoryReservation {
  227. return warnings, fmt.Errorf("Minimum memory limit should be larger than memory reservation limit, see usage.")
  228. }
  229. if resources.KernelMemory > 0 && !sysInfo.KernelMemory {
  230. warnings = append(warnings, "Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  231. logrus.Warnf("Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  232. resources.KernelMemory = 0
  233. }
  234. if resources.KernelMemory > 0 && resources.KernelMemory < linuxMinMemory {
  235. return warnings, fmt.Errorf("Minimum kernel memory limit allowed is 4MB")
  236. }
  237. if resources.KernelMemory > 0 && !checkKernelVersion(4, 0, 0) {
  238. warnings = append(warnings, "You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
  239. logrus.Warnf("You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
  240. }
  241. if resources.OomKillDisable && !sysInfo.OomKillDisable {
  242. resources.OomKillDisable = false
  243. return warnings, fmt.Errorf("Your kernel does not support oom kill disable.")
  244. }
  245. // cpu subsystem checks and adjustments
  246. if resources.CPUShares > 0 && !sysInfo.CPUShares {
  247. warnings = append(warnings, "Your kernel does not support CPU shares. Shares discarded.")
  248. logrus.Warnf("Your kernel does not support CPU shares. Shares discarded.")
  249. resources.CPUShares = 0
  250. }
  251. if resources.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
  252. warnings = append(warnings, "Your kernel does not support CPU cfs period. Period discarded.")
  253. logrus.Warnf("Your kernel does not support CPU cfs period. Period discarded.")
  254. resources.CPUPeriod = 0
  255. }
  256. if resources.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
  257. warnings = append(warnings, "Your kernel does not support CPU cfs quota. Quota discarded.")
  258. logrus.Warnf("Your kernel does not support CPU cfs quota. Quota discarded.")
  259. resources.CPUQuota = 0
  260. }
  261. // cpuset subsystem checks and adjustments
  262. if (resources.CpusetCpus != "" || resources.CpusetMems != "") && !sysInfo.Cpuset {
  263. warnings = append(warnings, "Your kernel does not support cpuset. Cpuset discarded.")
  264. logrus.Warnf("Your kernel does not support cpuset. Cpuset discarded.")
  265. resources.CpusetCpus = ""
  266. resources.CpusetMems = ""
  267. }
  268. cpusAvailable, err := sysInfo.IsCpusetCpusAvailable(resources.CpusetCpus)
  269. if err != nil {
  270. return warnings, derr.ErrorCodeInvalidCpusetCpus.WithArgs(resources.CpusetCpus)
  271. }
  272. if !cpusAvailable {
  273. return warnings, derr.ErrorCodeNotAvailableCpusetCpus.WithArgs(resources.CpusetCpus, sysInfo.Cpus)
  274. }
  275. memsAvailable, err := sysInfo.IsCpusetMemsAvailable(resources.CpusetMems)
  276. if err != nil {
  277. return warnings, derr.ErrorCodeInvalidCpusetMems.WithArgs(resources.CpusetMems)
  278. }
  279. if !memsAvailable {
  280. return warnings, derr.ErrorCodeNotAvailableCpusetMems.WithArgs(resources.CpusetMems, sysInfo.Mems)
  281. }
  282. // blkio subsystem checks and adjustments
  283. if resources.BlkioWeight > 0 && !sysInfo.BlkioWeight {
  284. warnings = append(warnings, "Your kernel does not support Block I/O weight. Weight discarded.")
  285. logrus.Warnf("Your kernel does not support Block I/O weight. Weight discarded.")
  286. resources.BlkioWeight = 0
  287. }
  288. if resources.BlkioWeight > 0 && (resources.BlkioWeight < 10 || resources.BlkioWeight > 1000) {
  289. return warnings, fmt.Errorf("Range of blkio weight is from 10 to 1000.")
  290. }
  291. if len(resources.BlkioWeightDevice) > 0 && !sysInfo.BlkioWeightDevice {
  292. warnings = append(warnings, "Your kernel does not support Block I/O weight_device.")
  293. logrus.Warnf("Your kernel does not support Block I/O weight_device. Weight-device discarded.")
  294. resources.BlkioWeightDevice = []*pblkiodev.WeightDevice{}
  295. }
  296. if len(resources.BlkioDeviceReadBps) > 0 && !sysInfo.BlkioReadBpsDevice {
  297. warnings = append(warnings, "Your kernel does not support Block read limit in bytes per second.")
  298. logrus.Warnf("Your kernel does not support Block I/O read limit in bytes per second. --device-read-bps discarded.")
  299. resources.BlkioDeviceReadBps = []*pblkiodev.ThrottleDevice{}
  300. }
  301. if len(resources.BlkioDeviceWriteBps) > 0 && !sysInfo.BlkioWriteBpsDevice {
  302. warnings = append(warnings, "Your kernel does not support Block write limit in bytes per second.")
  303. logrus.Warnf("Your kernel does not support Block I/O write limit in bytes per second. --device-write-bps discarded.")
  304. resources.BlkioDeviceWriteBps = []*pblkiodev.ThrottleDevice{}
  305. }
  306. if len(resources.BlkioDeviceReadIOps) > 0 && !sysInfo.BlkioReadIOpsDevice {
  307. warnings = append(warnings, "Your kernel does not support Block read limit in IO per second.")
  308. logrus.Warnf("Your kernel does not support Block I/O read limit in IO per second. -device-read-iops discarded.")
  309. resources.BlkioDeviceReadIOps = []*pblkiodev.ThrottleDevice{}
  310. }
  311. if len(resources.BlkioDeviceWriteIOps) > 0 && !sysInfo.BlkioWriteIOpsDevice {
  312. warnings = append(warnings, "Your kernel does not support Block write limit in IO per second.")
  313. logrus.Warnf("Your kernel does not support Block I/O write limit in IO per second. --device-write-iops discarded.")
  314. resources.BlkioDeviceWriteIOps = []*pblkiodev.ThrottleDevice{}
  315. }
  316. return warnings, nil
  317. }
  318. // verifyPlatformContainerSettings performs platform-specific validation of the
  319. // hostconfig and config structures.
  320. func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config) ([]string, error) {
  321. warnings := []string{}
  322. sysInfo := sysinfo.New(true)
  323. warnings, err := daemon.verifyExperimentalContainerSettings(hostConfig, config)
  324. if err != nil {
  325. return warnings, err
  326. }
  327. w, err := verifyContainerResources(&hostConfig.Resources)
  328. if err != nil {
  329. return warnings, err
  330. }
  331. warnings = append(warnings, w...)
  332. if hostConfig.ShmSize != nil && *hostConfig.ShmSize <= 0 {
  333. return warnings, fmt.Errorf("SHM size must be greater then 0")
  334. }
  335. if hostConfig.OomScoreAdj < -1000 || hostConfig.OomScoreAdj > 1000 {
  336. return warnings, fmt.Errorf("Invalid value %d, range for oom score adj is [-1000, 1000].", hostConfig.OomScoreAdj)
  337. }
  338. if sysInfo.IPv4ForwardingDisabled {
  339. warnings = append(warnings, "IPv4 forwarding is disabled. Networking will not work.")
  340. logrus.Warnf("IPv4 forwarding is disabled. Networking will not work")
  341. }
  342. return warnings, nil
  343. }
  344. // checkConfigOptions checks for mutually incompatible config options
  345. func checkConfigOptions(config *Config) error {
  346. // Check for mutually incompatible config options
  347. if config.Bridge.Iface != "" && config.Bridge.IP != "" {
  348. return fmt.Errorf("You specified -b & --bip, mutually exclusive options. Please specify only one.")
  349. }
  350. if !config.Bridge.EnableIPTables && !config.Bridge.InterContainerCommunication {
  351. return fmt.Errorf("You specified --iptables=false with --icc=false. ICC=false uses iptables to function. Please set --icc or --iptables to true.")
  352. }
  353. if !config.Bridge.EnableIPTables && config.Bridge.EnableIPMasq {
  354. config.Bridge.EnableIPMasq = false
  355. }
  356. return nil
  357. }
  358. // checkSystem validates platform-specific requirements
  359. func checkSystem() error {
  360. if os.Geteuid() != 0 {
  361. return fmt.Errorf("The Docker daemon needs to be run as root")
  362. }
  363. return checkKernel()
  364. }
  365. // configureKernelSecuritySupport configures and validate security support for the kernel
  366. func configureKernelSecuritySupport(config *Config, driverName string) error {
  367. if config.EnableSelinuxSupport {
  368. if selinuxEnabled() {
  369. // As Docker on overlayFS and SELinux are incompatible at present, error on overlayfs being enabled
  370. if driverName == "overlay" {
  371. return fmt.Errorf("SELinux is not supported with the %s graph driver", driverName)
  372. }
  373. logrus.Debug("SELinux enabled successfully")
  374. } else {
  375. logrus.Warn("Docker could not enable SELinux on the host system")
  376. }
  377. } else {
  378. selinuxSetDisabled()
  379. }
  380. return nil
  381. }
  382. func isBridgeNetworkDisabled(config *Config) bool {
  383. return config.Bridge.Iface == disableNetworkBridge
  384. }
  385. func (daemon *Daemon) networkOptions(dconfig *Config) ([]nwconfig.Option, error) {
  386. options := []nwconfig.Option{}
  387. if dconfig == nil {
  388. return options, nil
  389. }
  390. options = append(options, nwconfig.OptionDataDir(dconfig.Root))
  391. dd := runconfig.DefaultDaemonNetworkMode()
  392. dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
  393. options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
  394. options = append(options, nwconfig.OptionDefaultNetwork(dn))
  395. if strings.TrimSpace(dconfig.ClusterStore) != "" {
  396. kv := strings.Split(dconfig.ClusterStore, "://")
  397. if len(kv) != 2 {
  398. return nil, fmt.Errorf("kv store daemon config must be of the form KV-PROVIDER://KV-URL")
  399. }
  400. options = append(options, nwconfig.OptionKVProvider(kv[0]))
  401. options = append(options, nwconfig.OptionKVProviderURL(kv[1]))
  402. }
  403. if len(dconfig.ClusterOpts) > 0 {
  404. options = append(options, nwconfig.OptionKVOpts(dconfig.ClusterOpts))
  405. }
  406. if daemon.discoveryWatcher != nil {
  407. options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher))
  408. }
  409. if dconfig.ClusterAdvertise != "" {
  410. options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise))
  411. }
  412. options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  413. options = append(options, driverOptions(dconfig)...)
  414. return options, nil
  415. }
  416. func (daemon *Daemon) initNetworkController(config *Config) (libnetwork.NetworkController, error) {
  417. netOptions, err := daemon.networkOptions(config)
  418. if err != nil {
  419. return nil, err
  420. }
  421. controller, err := libnetwork.New(netOptions...)
  422. if err != nil {
  423. return nil, fmt.Errorf("error obtaining controller instance: %v", err)
  424. }
  425. // Initialize default network on "null"
  426. if _, err := controller.NewNetwork("null", "none", libnetwork.NetworkOptionPersist(false)); err != nil {
  427. return nil, fmt.Errorf("Error creating default \"null\" network: %v", err)
  428. }
  429. // Initialize default network on "host"
  430. if _, err := controller.NewNetwork("host", "host", libnetwork.NetworkOptionPersist(false)); err != nil {
  431. return nil, fmt.Errorf("Error creating default \"host\" network: %v", err)
  432. }
  433. if !config.DisableBridge {
  434. // Initialize default driver "bridge"
  435. if err := initBridgeDriver(controller, config); err != nil {
  436. return nil, err
  437. }
  438. }
  439. return controller, nil
  440. }
  441. func driverOptions(config *Config) []nwconfig.Option {
  442. bridgeConfig := options.Generic{
  443. "EnableIPForwarding": config.Bridge.EnableIPForward,
  444. "EnableIPTables": config.Bridge.EnableIPTables,
  445. "EnableUserlandProxy": config.Bridge.EnableUserlandProxy}
  446. bridgeOption := options.Generic{netlabel.GenericData: bridgeConfig}
  447. dOptions := []nwconfig.Option{}
  448. dOptions = append(dOptions, nwconfig.OptionDriverConfig("bridge", bridgeOption))
  449. return dOptions
  450. }
  451. func initBridgeDriver(controller libnetwork.NetworkController, config *Config) error {
  452. if n, err := controller.NetworkByName("bridge"); err == nil {
  453. if err = n.Delete(); err != nil {
  454. return fmt.Errorf("could not delete the default bridge network: %v", err)
  455. }
  456. }
  457. bridgeName := bridge.DefaultBridgeName
  458. if config.Bridge.Iface != "" {
  459. bridgeName = config.Bridge.Iface
  460. }
  461. netOption := map[string]string{
  462. bridge.BridgeName: bridgeName,
  463. bridge.DefaultBridge: strconv.FormatBool(true),
  464. netlabel.DriverMTU: strconv.Itoa(config.Mtu),
  465. bridge.EnableIPMasquerade: strconv.FormatBool(config.Bridge.EnableIPMasq),
  466. bridge.EnableICC: strconv.FormatBool(config.Bridge.InterContainerCommunication),
  467. }
  468. // --ip processing
  469. if config.Bridge.DefaultIP != nil {
  470. netOption[bridge.DefaultBindingIP] = config.Bridge.DefaultIP.String()
  471. }
  472. ipamV4Conf := libnetwork.IpamConf{}
  473. ipamV4Conf.AuxAddresses = make(map[string]string)
  474. if nw, _, err := ipamutils.ElectInterfaceAddresses(bridgeName); err == nil {
  475. ipamV4Conf.PreferredPool = nw.String()
  476. hip, _ := types.GetHostPartIP(nw.IP, nw.Mask)
  477. if hip.IsGlobalUnicast() {
  478. ipamV4Conf.Gateway = nw.IP.String()
  479. }
  480. }
  481. if config.Bridge.IP != "" {
  482. ipamV4Conf.PreferredPool = config.Bridge.IP
  483. ip, _, err := net.ParseCIDR(config.Bridge.IP)
  484. if err != nil {
  485. return err
  486. }
  487. ipamV4Conf.Gateway = ip.String()
  488. } else if bridgeName == bridge.DefaultBridgeName && ipamV4Conf.PreferredPool != "" {
  489. logrus.Infof("Default bridge (%s) is assigned with an IP address %s. Daemon option --bip can be used to set a preferred IP address", bridgeName, ipamV4Conf.PreferredPool)
  490. }
  491. if config.Bridge.FixedCIDR != "" {
  492. _, fCIDR, err := net.ParseCIDR(config.Bridge.FixedCIDR)
  493. if err != nil {
  494. return err
  495. }
  496. ipamV4Conf.SubPool = fCIDR.String()
  497. }
  498. if config.Bridge.DefaultGatewayIPv4 != nil {
  499. ipamV4Conf.AuxAddresses["DefaultGatewayIPv4"] = config.Bridge.DefaultGatewayIPv4.String()
  500. }
  501. var (
  502. ipamV6Conf *libnetwork.IpamConf
  503. deferIPv6Alloc bool
  504. )
  505. if config.Bridge.FixedCIDRv6 != "" {
  506. _, fCIDRv6, err := net.ParseCIDR(config.Bridge.FixedCIDRv6)
  507. if err != nil {
  508. return err
  509. }
  510. // In case user has specified the daemon flag --fixed-cidr-v6 and the passed network has
  511. // at least 48 host bits, we need to guarantee the current behavior where the containers'
  512. // IPv6 addresses will be constructed based on the containers' interface MAC address.
  513. // We do so by telling libnetwork to defer the IPv6 address allocation for the endpoints
  514. // on this network until after the driver has created the endpoint and returned the
  515. // constructed address. Libnetwork will then reserve this address with the ipam driver.
  516. ones, _ := fCIDRv6.Mask.Size()
  517. deferIPv6Alloc = ones <= 80
  518. if ipamV6Conf == nil {
  519. ipamV6Conf = &libnetwork.IpamConf{AuxAddresses: make(map[string]string)}
  520. }
  521. ipamV6Conf.PreferredPool = fCIDRv6.String()
  522. }
  523. if config.Bridge.DefaultGatewayIPv6 != nil {
  524. if ipamV6Conf == nil {
  525. ipamV6Conf = &libnetwork.IpamConf{AuxAddresses: make(map[string]string)}
  526. }
  527. ipamV6Conf.AuxAddresses["DefaultGatewayIPv6"] = config.Bridge.DefaultGatewayIPv6.String()
  528. }
  529. v4Conf := []*libnetwork.IpamConf{&ipamV4Conf}
  530. v6Conf := []*libnetwork.IpamConf{}
  531. if ipamV6Conf != nil {
  532. v6Conf = append(v6Conf, ipamV6Conf)
  533. }
  534. // Initialize default network on "bridge" with the same name
  535. _, err := controller.NewNetwork("bridge", "bridge",
  536. libnetwork.NetworkOptionGeneric(options.Generic{
  537. netlabel.GenericData: netOption,
  538. netlabel.EnableIPv6: config.Bridge.EnableIPv6,
  539. }),
  540. libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf),
  541. libnetwork.NetworkOptionDeferIPv6Alloc(deferIPv6Alloc))
  542. if err != nil {
  543. return fmt.Errorf("Error creating default \"bridge\" network: %v", err)
  544. }
  545. return nil
  546. }
  547. // setupInitLayer populates a directory with mountpoints suitable
  548. // for bind-mounting dockerinit into the container. The mountpoint is simply an
  549. // empty file at /.dockerinit
  550. //
  551. // This extra layer is used by all containers as the top-most ro layer. It protects
  552. // the container from unwanted side-effects on the rw layer.
  553. func setupInitLayer(initLayer string, rootUID, rootGID int) error {
  554. for pth, typ := range map[string]string{
  555. "/dev/pts": "dir",
  556. "/dev/shm": "dir",
  557. "/proc": "dir",
  558. "/sys": "dir",
  559. "/.dockerinit": "file",
  560. "/.dockerenv": "file",
  561. "/etc/resolv.conf": "file",
  562. "/etc/hosts": "file",
  563. "/etc/hostname": "file",
  564. "/dev/console": "file",
  565. "/etc/mtab": "/proc/mounts",
  566. } {
  567. parts := strings.Split(pth, "/")
  568. prev := "/"
  569. for _, p := range parts[1:] {
  570. prev = filepath.Join(prev, p)
  571. syscall.Unlink(filepath.Join(initLayer, prev))
  572. }
  573. if _, err := os.Stat(filepath.Join(initLayer, pth)); err != nil {
  574. if os.IsNotExist(err) {
  575. if err := idtools.MkdirAllNewAs(filepath.Join(initLayer, filepath.Dir(pth)), 0755, rootUID, rootGID); err != nil {
  576. return err
  577. }
  578. switch typ {
  579. case "dir":
  580. if err := idtools.MkdirAllNewAs(filepath.Join(initLayer, pth), 0755, rootUID, rootGID); err != nil {
  581. return err
  582. }
  583. case "file":
  584. f, err := os.OpenFile(filepath.Join(initLayer, pth), os.O_CREATE, 0755)
  585. if err != nil {
  586. return err
  587. }
  588. f.Chown(rootUID, rootGID)
  589. f.Close()
  590. default:
  591. if err := os.Symlink(typ, filepath.Join(initLayer, pth)); err != nil {
  592. return err
  593. }
  594. }
  595. } else {
  596. return err
  597. }
  598. }
  599. }
  600. // Layer is ready to use, if it wasn't before.
  601. return nil
  602. }
  603. // registerLinks writes the links to a file.
  604. func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *containertypes.HostConfig) error {
  605. if hostConfig == nil || hostConfig.Links == nil {
  606. return nil
  607. }
  608. for _, l := range hostConfig.Links {
  609. name, alias, err := runconfigopts.ParseLink(l)
  610. if err != nil {
  611. return err
  612. }
  613. child, err := daemon.GetContainer(name)
  614. if err != nil {
  615. //An error from daemon.GetContainer() means this name could not be found
  616. return fmt.Errorf("Could not get container for %s", name)
  617. }
  618. for child.HostConfig.NetworkMode.IsContainer() {
  619. parts := strings.SplitN(string(child.HostConfig.NetworkMode), ":", 2)
  620. child, err = daemon.GetContainer(parts[1])
  621. if err != nil {
  622. return fmt.Errorf("Could not get container for %s", parts[1])
  623. }
  624. }
  625. if child.HostConfig.NetworkMode.IsHost() {
  626. return runconfig.ErrConflictHostNetworkAndLinks
  627. }
  628. if err := daemon.registerLink(container, child, alias); err != nil {
  629. return err
  630. }
  631. }
  632. // After we load all the links into the daemon
  633. // set them to nil on the hostconfig
  634. hostConfig.Links = nil
  635. if err := container.WriteHostConfig(); err != nil {
  636. return err
  637. }
  638. return nil
  639. }
  640. // conditionalMountOnStart is a platform specific helper function during the
  641. // container start to call mount.
  642. func (daemon *Daemon) conditionalMountOnStart(container *container.Container) error {
  643. return daemon.Mount(container)
  644. }
  645. // conditionalUnmountOnCleanup is a platform specific helper function called
  646. // during the cleanup of a container to unmount.
  647. func (daemon *Daemon) conditionalUnmountOnCleanup(container *container.Container) {
  648. daemon.Unmount(container)
  649. }
  650. func restoreCustomImage(is image.Store, ls layer.Store, rs reference.Store) error {
  651. // Unix has no custom images to register
  652. return nil
  653. }