daemon_unix.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. "github.com/docker/docker/container"
  13. "github.com/docker/docker/daemon/graphdriver"
  14. derr "github.com/docker/docker/errors"
  15. "github.com/docker/docker/image"
  16. "github.com/docker/docker/layer"
  17. pblkiodev "github.com/docker/docker/pkg/blkiodev"
  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 *runconfig.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 *runconfig.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 *runconfig.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 *runconfig.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 *runconfig.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 *runconfig.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 *runconfig.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 *runconfig.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. // cpu subsystem checks and adjustments
  241. if resources.CPUShares > 0 && !sysInfo.CPUShares {
  242. warnings = append(warnings, "Your kernel does not support CPU shares. Shares discarded.")
  243. logrus.Warnf("Your kernel does not support CPU shares. Shares discarded.")
  244. resources.CPUShares = 0
  245. }
  246. if resources.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
  247. warnings = append(warnings, "Your kernel does not support CPU cfs period. Period discarded.")
  248. logrus.Warnf("Your kernel does not support CPU cfs period. Period discarded.")
  249. resources.CPUPeriod = 0
  250. }
  251. if resources.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
  252. warnings = append(warnings, "Your kernel does not support CPU cfs quota. Quota discarded.")
  253. logrus.Warnf("Your kernel does not support CPU cfs quota. Quota discarded.")
  254. resources.CPUQuota = 0
  255. }
  256. // cpuset subsystem checks and adjustments
  257. if (resources.CpusetCpus != "" || resources.CpusetMems != "") && !sysInfo.Cpuset {
  258. warnings = append(warnings, "Your kernel does not support cpuset. Cpuset discarded.")
  259. logrus.Warnf("Your kernel does not support cpuset. Cpuset discarded.")
  260. resources.CpusetCpus = ""
  261. resources.CpusetMems = ""
  262. }
  263. cpusAvailable, err := sysInfo.IsCpusetCpusAvailable(resources.CpusetCpus)
  264. if err != nil {
  265. return warnings, derr.ErrorCodeInvalidCpusetCpus.WithArgs(resources.CpusetCpus)
  266. }
  267. if !cpusAvailable {
  268. return warnings, derr.ErrorCodeNotAvailableCpusetCpus.WithArgs(resources.CpusetCpus, sysInfo.Cpus)
  269. }
  270. memsAvailable, err := sysInfo.IsCpusetMemsAvailable(resources.CpusetMems)
  271. if err != nil {
  272. return warnings, derr.ErrorCodeInvalidCpusetMems.WithArgs(resources.CpusetMems)
  273. }
  274. if !memsAvailable {
  275. return warnings, derr.ErrorCodeNotAvailableCpusetMems.WithArgs(resources.CpusetMems, sysInfo.Mems)
  276. }
  277. // blkio subsystem checks and adjustments
  278. if resources.BlkioWeight > 0 && !sysInfo.BlkioWeight {
  279. warnings = append(warnings, "Your kernel does not support Block I/O weight. Weight discarded.")
  280. logrus.Warnf("Your kernel does not support Block I/O weight. Weight discarded.")
  281. resources.BlkioWeight = 0
  282. }
  283. if resources.BlkioWeight > 0 && (resources.BlkioWeight < 10 || resources.BlkioWeight > 1000) {
  284. return warnings, fmt.Errorf("Range of blkio weight is from 10 to 1000.")
  285. }
  286. if len(resources.BlkioWeightDevice) > 0 && !sysInfo.BlkioWeightDevice {
  287. warnings = append(warnings, "Your kernel does not support Block I/O weight_device.")
  288. logrus.Warnf("Your kernel does not support Block I/O weight_device. Weight-device discarded.")
  289. resources.BlkioWeightDevice = []*pblkiodev.WeightDevice{}
  290. }
  291. if len(resources.BlkioDeviceReadBps) > 0 && !sysInfo.BlkioReadBpsDevice {
  292. warnings = append(warnings, "Your kernel does not support Block read limit in bytes per second.")
  293. logrus.Warnf("Your kernel does not support Block I/O read limit in bytes per second. --device-read-bps discarded.")
  294. resources.BlkioDeviceReadBps = []*pblkiodev.ThrottleDevice{}
  295. }
  296. if len(resources.BlkioDeviceWriteBps) > 0 && !sysInfo.BlkioWriteBpsDevice {
  297. warnings = append(warnings, "Your kernel does not support Block write limit in bytes per second.")
  298. logrus.Warnf("Your kernel does not support Block I/O write limit in bytes per second. --device-write-bps discarded.")
  299. resources.BlkioDeviceWriteBps = []*pblkiodev.ThrottleDevice{}
  300. }
  301. if len(resources.BlkioDeviceReadIOps) > 0 && !sysInfo.BlkioReadIOpsDevice {
  302. warnings = append(warnings, "Your kernel does not support Block read limit in IO per second.")
  303. logrus.Warnf("Your kernel does not support Block I/O read limit in IO per second. -device-read-iops discarded.")
  304. resources.BlkioDeviceReadIOps = []*pblkiodev.ThrottleDevice{}
  305. }
  306. if len(resources.BlkioDeviceWriteIOps) > 0 && !sysInfo.BlkioWriteIOpsDevice {
  307. warnings = append(warnings, "Your kernel does not support Block write limit in IO per second.")
  308. logrus.Warnf("Your kernel does not support Block I/O write limit in IO per second. --device-write-iops discarded.")
  309. resources.BlkioDeviceWriteIOps = []*pblkiodev.ThrottleDevice{}
  310. }
  311. return warnings, nil
  312. }
  313. // verifyPlatformContainerSettings performs platform-specific validation of the
  314. // hostconfig and config structures.
  315. func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *runconfig.HostConfig, config *runconfig.Config) ([]string, error) {
  316. warnings := []string{}
  317. sysInfo := sysinfo.New(true)
  318. warnings, err := daemon.verifyExperimentalContainerSettings(hostConfig, config)
  319. if err != nil {
  320. return warnings, err
  321. }
  322. w, err := verifyContainerResources(&hostConfig.Resources)
  323. if err != nil {
  324. return warnings, err
  325. }
  326. warnings = append(warnings, w...)
  327. if hostConfig.ShmSize != nil && *hostConfig.ShmSize <= 0 {
  328. return warnings, fmt.Errorf("SHM size must be greater then 0")
  329. }
  330. if hostConfig.OomKillDisable && !sysInfo.OomKillDisable {
  331. hostConfig.OomKillDisable = false
  332. return warnings, fmt.Errorf("Your kernel does not support oom kill disable.")
  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. // MigrateIfDownlevel is a wrapper for AUFS migration for downlevel
  382. func migrateIfDownlevel(driver graphdriver.Driver, root string) error {
  383. return migrateIfAufs(driver, root)
  384. }
  385. func isBridgeNetworkDisabled(config *Config) bool {
  386. return config.Bridge.Iface == disableNetworkBridge
  387. }
  388. func (daemon *Daemon) networkOptions(dconfig *Config) ([]nwconfig.Option, error) {
  389. options := []nwconfig.Option{}
  390. if dconfig == nil {
  391. return options, nil
  392. }
  393. options = append(options, nwconfig.OptionDataDir(dconfig.Root))
  394. dd := runconfig.DefaultDaemonNetworkMode()
  395. dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
  396. options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
  397. options = append(options, nwconfig.OptionDefaultNetwork(dn))
  398. if strings.TrimSpace(dconfig.ClusterStore) != "" {
  399. kv := strings.Split(dconfig.ClusterStore, "://")
  400. if len(kv) != 2 {
  401. return nil, fmt.Errorf("kv store daemon config must be of the form KV-PROVIDER://KV-URL")
  402. }
  403. options = append(options, nwconfig.OptionKVProvider(kv[0]))
  404. options = append(options, nwconfig.OptionKVProviderURL(kv[1]))
  405. }
  406. if len(dconfig.ClusterOpts) > 0 {
  407. options = append(options, nwconfig.OptionKVOpts(dconfig.ClusterOpts))
  408. }
  409. if daemon.discoveryWatcher != nil {
  410. options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher))
  411. }
  412. if dconfig.ClusterAdvertise != "" {
  413. options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise))
  414. }
  415. options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  416. options = append(options, driverOptions(dconfig)...)
  417. return options, nil
  418. }
  419. func (daemon *Daemon) initNetworkController(config *Config) (libnetwork.NetworkController, error) {
  420. netOptions, err := daemon.networkOptions(config)
  421. if err != nil {
  422. return nil, err
  423. }
  424. controller, err := libnetwork.New(netOptions...)
  425. if err != nil {
  426. return nil, fmt.Errorf("error obtaining controller instance: %v", err)
  427. }
  428. // Initialize default network on "null"
  429. if _, err := controller.NewNetwork("null", "none", libnetwork.NetworkOptionPersist(false)); err != nil {
  430. return nil, fmt.Errorf("Error creating default \"null\" network: %v", err)
  431. }
  432. // Initialize default network on "host"
  433. if _, err := controller.NewNetwork("host", "host", libnetwork.NetworkOptionPersist(false)); err != nil {
  434. return nil, fmt.Errorf("Error creating default \"host\" network: %v", err)
  435. }
  436. if !config.DisableBridge {
  437. // Initialize default driver "bridge"
  438. if err := initBridgeDriver(controller, config); err != nil {
  439. return nil, err
  440. }
  441. }
  442. return controller, nil
  443. }
  444. func driverOptions(config *Config) []nwconfig.Option {
  445. bridgeConfig := options.Generic{
  446. "EnableIPForwarding": config.Bridge.EnableIPForward,
  447. "EnableIPTables": config.Bridge.EnableIPTables,
  448. "EnableUserlandProxy": config.Bridge.EnableUserlandProxy}
  449. bridgeOption := options.Generic{netlabel.GenericData: bridgeConfig}
  450. dOptions := []nwconfig.Option{}
  451. dOptions = append(dOptions, nwconfig.OptionDriverConfig("bridge", bridgeOption))
  452. return dOptions
  453. }
  454. func initBridgeDriver(controller libnetwork.NetworkController, config *Config) error {
  455. if n, err := controller.NetworkByName("bridge"); err == nil {
  456. if err = n.Delete(); err != nil {
  457. return fmt.Errorf("could not delete the default bridge network: %v", err)
  458. }
  459. }
  460. bridgeName := bridge.DefaultBridgeName
  461. if config.Bridge.Iface != "" {
  462. bridgeName = config.Bridge.Iface
  463. }
  464. netOption := map[string]string{
  465. bridge.BridgeName: bridgeName,
  466. bridge.DefaultBridge: strconv.FormatBool(true),
  467. netlabel.DriverMTU: strconv.Itoa(config.Mtu),
  468. bridge.EnableIPMasquerade: strconv.FormatBool(config.Bridge.EnableIPMasq),
  469. bridge.EnableICC: strconv.FormatBool(config.Bridge.InterContainerCommunication),
  470. }
  471. // --ip processing
  472. if config.Bridge.DefaultIP != nil {
  473. netOption[bridge.DefaultBindingIP] = config.Bridge.DefaultIP.String()
  474. }
  475. ipamV4Conf := libnetwork.IpamConf{}
  476. ipamV4Conf.AuxAddresses = make(map[string]string)
  477. if nw, _, err := ipamutils.ElectInterfaceAddresses(bridgeName); err == nil {
  478. ipamV4Conf.PreferredPool = nw.String()
  479. hip, _ := types.GetHostPartIP(nw.IP, nw.Mask)
  480. if hip.IsGlobalUnicast() {
  481. ipamV4Conf.Gateway = nw.IP.String()
  482. }
  483. }
  484. if config.Bridge.IP != "" {
  485. ipamV4Conf.PreferredPool = config.Bridge.IP
  486. ip, _, err := net.ParseCIDR(config.Bridge.IP)
  487. if err != nil {
  488. return err
  489. }
  490. ipamV4Conf.Gateway = ip.String()
  491. } else if bridgeName == bridge.DefaultBridgeName && ipamV4Conf.PreferredPool != "" {
  492. 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)
  493. }
  494. if config.Bridge.FixedCIDR != "" {
  495. _, fCIDR, err := net.ParseCIDR(config.Bridge.FixedCIDR)
  496. if err != nil {
  497. return err
  498. }
  499. ipamV4Conf.SubPool = fCIDR.String()
  500. }
  501. if config.Bridge.DefaultGatewayIPv4 != nil {
  502. ipamV4Conf.AuxAddresses["DefaultGatewayIPv4"] = config.Bridge.DefaultGatewayIPv4.String()
  503. }
  504. var (
  505. ipamV6Conf *libnetwork.IpamConf
  506. deferIPv6Alloc bool
  507. )
  508. if config.Bridge.FixedCIDRv6 != "" {
  509. _, fCIDRv6, err := net.ParseCIDR(config.Bridge.FixedCIDRv6)
  510. if err != nil {
  511. return err
  512. }
  513. // In case user has specified the daemon flag --fixed-cidr-v6 and the passed network has
  514. // at least 48 host bits, we need to guarantee the current behavior where the containers'
  515. // IPv6 addresses will be constructed based on the containers' interface MAC address.
  516. // We do so by telling libnetwork to defer the IPv6 address allocation for the endpoints
  517. // on this network until after the driver has created the endpoint and returned the
  518. // constructed address. Libnetwork will then reserve this address with the ipam driver.
  519. ones, _ := fCIDRv6.Mask.Size()
  520. deferIPv6Alloc = ones <= 80
  521. if ipamV6Conf == nil {
  522. ipamV6Conf = &libnetwork.IpamConf{}
  523. }
  524. ipamV6Conf.PreferredPool = fCIDRv6.String()
  525. }
  526. if config.Bridge.DefaultGatewayIPv6 != nil {
  527. if ipamV6Conf == nil {
  528. ipamV6Conf = &libnetwork.IpamConf{}
  529. }
  530. ipamV6Conf.AuxAddresses["DefaultGatewayIPv6"] = config.Bridge.DefaultGatewayIPv6.String()
  531. }
  532. v4Conf := []*libnetwork.IpamConf{&ipamV4Conf}
  533. v6Conf := []*libnetwork.IpamConf{}
  534. if ipamV6Conf != nil {
  535. v6Conf = append(v6Conf, ipamV6Conf)
  536. }
  537. // Initialize default network on "bridge" with the same name
  538. _, err := controller.NewNetwork("bridge", "bridge",
  539. libnetwork.NetworkOptionGeneric(options.Generic{
  540. netlabel.GenericData: netOption,
  541. netlabel.EnableIPv6: config.Bridge.EnableIPv6,
  542. }),
  543. libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf),
  544. libnetwork.NetworkOptionDeferIPv6Alloc(deferIPv6Alloc))
  545. if err != nil {
  546. return fmt.Errorf("Error creating default \"bridge\" network: %v", err)
  547. }
  548. return nil
  549. }
  550. // setupInitLayer populates a directory with mountpoints suitable
  551. // for bind-mounting dockerinit into the container. The mountpoint is simply an
  552. // empty file at /.dockerinit
  553. //
  554. // This extra layer is used by all containers as the top-most ro layer. It protects
  555. // the container from unwanted side-effects on the rw layer.
  556. func setupInitLayer(initLayer string, rootUID, rootGID int) error {
  557. for pth, typ := range map[string]string{
  558. "/dev/pts": "dir",
  559. "/dev/shm": "dir",
  560. "/proc": "dir",
  561. "/sys": "dir",
  562. "/.dockerinit": "file",
  563. "/.dockerenv": "file",
  564. "/etc/resolv.conf": "file",
  565. "/etc/hosts": "file",
  566. "/etc/hostname": "file",
  567. "/dev/console": "file",
  568. "/etc/mtab": "/proc/mounts",
  569. } {
  570. parts := strings.Split(pth, "/")
  571. prev := "/"
  572. for _, p := range parts[1:] {
  573. prev = filepath.Join(prev, p)
  574. syscall.Unlink(filepath.Join(initLayer, prev))
  575. }
  576. if _, err := os.Stat(filepath.Join(initLayer, pth)); err != nil {
  577. if os.IsNotExist(err) {
  578. if err := idtools.MkdirAllNewAs(filepath.Join(initLayer, filepath.Dir(pth)), 0755, rootUID, rootGID); err != nil {
  579. return err
  580. }
  581. switch typ {
  582. case "dir":
  583. if err := idtools.MkdirAllNewAs(filepath.Join(initLayer, pth), 0755, rootUID, rootGID); err != nil {
  584. return err
  585. }
  586. case "file":
  587. f, err := os.OpenFile(filepath.Join(initLayer, pth), os.O_CREATE, 0755)
  588. if err != nil {
  589. return err
  590. }
  591. f.Chown(rootUID, rootGID)
  592. f.Close()
  593. default:
  594. if err := os.Symlink(typ, filepath.Join(initLayer, pth)); err != nil {
  595. return err
  596. }
  597. }
  598. } else {
  599. return err
  600. }
  601. }
  602. }
  603. // Layer is ready to use, if it wasn't before.
  604. return nil
  605. }
  606. // registerLinks writes the links to a file.
  607. func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *runconfig.HostConfig) error {
  608. if hostConfig == nil || hostConfig.Links == nil {
  609. return nil
  610. }
  611. for _, l := range hostConfig.Links {
  612. name, alias, err := runconfig.ParseLink(l)
  613. if err != nil {
  614. return err
  615. }
  616. child, err := daemon.GetContainer(name)
  617. if err != nil {
  618. //An error from daemon.GetContainer() means this name could not be found
  619. return fmt.Errorf("Could not get container for %s", name)
  620. }
  621. for child.HostConfig.NetworkMode.IsContainer() {
  622. parts := strings.SplitN(string(child.HostConfig.NetworkMode), ":", 2)
  623. child, err = daemon.GetContainer(parts[1])
  624. if err != nil {
  625. return fmt.Errorf("Could not get container for %s", parts[1])
  626. }
  627. }
  628. if child.HostConfig.NetworkMode.IsHost() {
  629. return runconfig.ErrConflictHostNetworkAndLinks
  630. }
  631. if err := daemon.registerLink(container, child, alias); err != nil {
  632. return err
  633. }
  634. }
  635. // After we load all the links into the daemon
  636. // set them to nil on the hostconfig
  637. hostConfig.Links = nil
  638. if err := container.WriteHostConfig(); err != nil {
  639. return err
  640. }
  641. return nil
  642. }
  643. // conditionalMountOnStart is a platform specific helper function during the
  644. // container start to call mount.
  645. func (daemon *Daemon) conditionalMountOnStart(container *container.Container) error {
  646. return daemon.Mount(container)
  647. }
  648. // conditionalUnmountOnCleanup is a platform specific helper function called
  649. // during the cleanup of a container to unmount.
  650. func (daemon *Daemon) conditionalUnmountOnCleanup(container *container.Container) {
  651. daemon.Unmount(container)
  652. }
  653. func restoreCustomImage(driver graphdriver.Driver, is image.Store, ls layer.Store, rs reference.Store) error {
  654. // Unix has no custom images to register
  655. return nil
  656. }