daemon_unix.go 27 KB

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