container_operations_unix.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "time"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/daemon/execdriver"
  15. "github.com/docker/docker/daemon/links"
  16. "github.com/docker/docker/daemon/network"
  17. derr "github.com/docker/docker/errors"
  18. "github.com/docker/docker/pkg/fileutils"
  19. "github.com/docker/docker/pkg/idtools"
  20. "github.com/docker/docker/pkg/mount"
  21. "github.com/docker/docker/pkg/parsers"
  22. "github.com/docker/docker/pkg/stringid"
  23. "github.com/docker/docker/runconfig"
  24. containertypes "github.com/docker/engine-api/types/container"
  25. networktypes "github.com/docker/engine-api/types/network"
  26. "github.com/docker/go-units"
  27. "github.com/docker/libnetwork"
  28. "github.com/docker/libnetwork/netlabel"
  29. "github.com/docker/libnetwork/options"
  30. "github.com/opencontainers/runc/libcontainer/configs"
  31. "github.com/opencontainers/runc/libcontainer/devices"
  32. "github.com/opencontainers/runc/libcontainer/label"
  33. )
  34. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  35. var env []string
  36. children, err := daemon.children(container.Name)
  37. if err != nil {
  38. return nil, err
  39. }
  40. bridgeSettings := container.NetworkSettings.Networks["bridge"]
  41. if bridgeSettings == nil {
  42. return nil, nil
  43. }
  44. if len(children) > 0 {
  45. for linkAlias, child := range children {
  46. if !child.IsRunning() {
  47. return nil, derr.ErrorCodeLinkNotRunning.WithArgs(child.Name, linkAlias)
  48. }
  49. childBridgeSettings := child.NetworkSettings.Networks["bridge"]
  50. if childBridgeSettings == nil {
  51. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  52. }
  53. link := links.NewLink(
  54. bridgeSettings.IPAddress,
  55. childBridgeSettings.IPAddress,
  56. linkAlias,
  57. child.Config.Env,
  58. child.Config.ExposedPorts,
  59. )
  60. for _, envVar := range link.ToEnv() {
  61. env = append(env, envVar)
  62. }
  63. }
  64. }
  65. return env, nil
  66. }
  67. func (daemon *Daemon) populateCommand(c *container.Container, env []string) error {
  68. var en *execdriver.Network
  69. if !c.Config.NetworkDisabled {
  70. en = &execdriver.Network{}
  71. if !daemon.execDriver.SupportsHooks() || c.HostConfig.NetworkMode.IsHost() {
  72. en.NamespacePath = c.NetworkSettings.SandboxKey
  73. }
  74. if c.HostConfig.NetworkMode.IsContainer() {
  75. nc, err := daemon.getNetworkedContainer(c.ID, c.HostConfig.NetworkMode.ConnectedContainer())
  76. if err != nil {
  77. return err
  78. }
  79. en.ContainerID = nc.ID
  80. }
  81. }
  82. ipc := &execdriver.Ipc{}
  83. var err error
  84. c.ShmPath, err = c.ShmResourcePath()
  85. if err != nil {
  86. return err
  87. }
  88. c.MqueuePath, err = c.MqueueResourcePath()
  89. if err != nil {
  90. return err
  91. }
  92. if c.HostConfig.IpcMode.IsContainer() {
  93. ic, err := daemon.getIpcContainer(c)
  94. if err != nil {
  95. return err
  96. }
  97. ipc.ContainerID = ic.ID
  98. c.ShmPath = ic.ShmPath
  99. c.MqueuePath = ic.MqueuePath
  100. } else {
  101. ipc.HostIpc = c.HostConfig.IpcMode.IsHost()
  102. if ipc.HostIpc {
  103. if _, err := os.Stat("/dev/shm"); err != nil {
  104. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  105. }
  106. if _, err := os.Stat("/dev/mqueue"); err != nil {
  107. return fmt.Errorf("/dev/mqueue is not mounted, but must be for --ipc=host")
  108. }
  109. c.ShmPath = "/dev/shm"
  110. c.MqueuePath = "/dev/mqueue"
  111. }
  112. }
  113. pid := &execdriver.Pid{}
  114. pid.HostPid = c.HostConfig.PidMode.IsHost()
  115. uts := &execdriver.UTS{
  116. HostUTS: c.HostConfig.UTSMode.IsHost(),
  117. }
  118. // Build lists of devices allowed and created within the container.
  119. var userSpecifiedDevices []*configs.Device
  120. for _, deviceMapping := range c.HostConfig.Devices {
  121. devs, err := getDevicesFromPath(deviceMapping)
  122. if err != nil {
  123. return err
  124. }
  125. userSpecifiedDevices = append(userSpecifiedDevices, devs...)
  126. }
  127. allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
  128. autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
  129. var rlimits []*units.Rlimit
  130. ulimits := c.HostConfig.Ulimits
  131. // Merge ulimits with daemon defaults
  132. ulIdx := make(map[string]*units.Ulimit)
  133. for _, ul := range ulimits {
  134. ulIdx[ul.Name] = ul
  135. }
  136. for name, ul := range daemon.configStore.Ulimits {
  137. if _, exists := ulIdx[name]; !exists {
  138. ulimits = append(ulimits, ul)
  139. }
  140. }
  141. weightDevices, err := getBlkioWeightDevices(c.HostConfig)
  142. if err != nil {
  143. return err
  144. }
  145. readBpsDevice, err := getBlkioReadBpsDevices(c.HostConfig)
  146. if err != nil {
  147. return err
  148. }
  149. writeBpsDevice, err := getBlkioWriteBpsDevices(c.HostConfig)
  150. if err != nil {
  151. return err
  152. }
  153. readIOpsDevice, err := getBlkioReadIOpsDevices(c.HostConfig)
  154. if err != nil {
  155. return err
  156. }
  157. writeIOpsDevice, err := getBlkioWriteIOpsDevices(c.HostConfig)
  158. if err != nil {
  159. return err
  160. }
  161. for _, limit := range ulimits {
  162. rl, err := limit.GetRlimit()
  163. if err != nil {
  164. return err
  165. }
  166. rlimits = append(rlimits, rl)
  167. }
  168. resources := &execdriver.Resources{
  169. CommonResources: execdriver.CommonResources{
  170. Memory: c.HostConfig.Memory,
  171. MemoryReservation: c.HostConfig.MemoryReservation,
  172. CPUShares: c.HostConfig.CPUShares,
  173. BlkioWeight: c.HostConfig.BlkioWeight,
  174. },
  175. MemorySwap: c.HostConfig.MemorySwap,
  176. KernelMemory: c.HostConfig.KernelMemory,
  177. CpusetCpus: c.HostConfig.CpusetCpus,
  178. CpusetMems: c.HostConfig.CpusetMems,
  179. CPUPeriod: c.HostConfig.CPUPeriod,
  180. CPUQuota: c.HostConfig.CPUQuota,
  181. Rlimits: rlimits,
  182. BlkioWeightDevice: weightDevices,
  183. BlkioThrottleReadBpsDevice: readBpsDevice,
  184. BlkioThrottleWriteBpsDevice: writeBpsDevice,
  185. BlkioThrottleReadIOpsDevice: readIOpsDevice,
  186. BlkioThrottleWriteIOpsDevice: writeIOpsDevice,
  187. OomKillDisable: c.HostConfig.OomKillDisable,
  188. MemorySwappiness: -1,
  189. }
  190. if c.HostConfig.MemorySwappiness != nil {
  191. resources.MemorySwappiness = *c.HostConfig.MemorySwappiness
  192. }
  193. processConfig := execdriver.ProcessConfig{
  194. CommonProcessConfig: execdriver.CommonProcessConfig{
  195. Entrypoint: c.Path,
  196. Arguments: c.Args,
  197. Tty: c.Config.Tty,
  198. },
  199. Privileged: c.HostConfig.Privileged,
  200. User: c.Config.User,
  201. }
  202. processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
  203. processConfig.Env = env
  204. remappedRoot := &execdriver.User{}
  205. rootUID, rootGID := daemon.GetRemappedUIDGID()
  206. if rootUID != 0 {
  207. remappedRoot.UID = rootUID
  208. remappedRoot.GID = rootGID
  209. }
  210. uidMap, gidMap := daemon.GetUIDGIDMaps()
  211. defaultCgroupParent := "/docker"
  212. if daemon.configStore.CgroupParent != "" {
  213. defaultCgroupParent = daemon.configStore.CgroupParent
  214. } else {
  215. for _, option := range daemon.configStore.ExecOptions {
  216. key, val, err := parsers.ParseKeyValueOpt(option)
  217. if err != nil || !strings.EqualFold(key, "native.cgroupdriver") {
  218. continue
  219. }
  220. if val == "systemd" {
  221. defaultCgroupParent = "system.slice"
  222. }
  223. }
  224. }
  225. c.Command = &execdriver.Command{
  226. CommonCommand: execdriver.CommonCommand{
  227. ID: c.ID,
  228. InitPath: "/.dockerinit",
  229. MountLabel: c.GetMountLabel(),
  230. Network: en,
  231. ProcessConfig: processConfig,
  232. ProcessLabel: c.GetProcessLabel(),
  233. Rootfs: c.BaseFS,
  234. Resources: resources,
  235. WorkingDir: c.Config.WorkingDir,
  236. },
  237. AllowedDevices: allowedDevices,
  238. AppArmorProfile: c.AppArmorProfile,
  239. AutoCreatedDevices: autoCreatedDevices,
  240. CapAdd: c.HostConfig.CapAdd.Slice(),
  241. CapDrop: c.HostConfig.CapDrop.Slice(),
  242. CgroupParent: defaultCgroupParent,
  243. GIDMapping: gidMap,
  244. GroupAdd: c.HostConfig.GroupAdd,
  245. Ipc: ipc,
  246. OomScoreAdj: c.HostConfig.OomScoreAdj,
  247. Pid: pid,
  248. ReadonlyRootfs: c.HostConfig.ReadonlyRootfs,
  249. RemappedRoot: remappedRoot,
  250. SeccompProfile: c.SeccompProfile,
  251. UIDMapping: uidMap,
  252. UTS: uts,
  253. }
  254. if c.HostConfig.CgroupParent != "" {
  255. c.Command.CgroupParent = c.HostConfig.CgroupParent
  256. }
  257. return nil
  258. }
  259. // getSize returns the real size & virtual size of the container.
  260. func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
  261. var (
  262. sizeRw, sizeRootfs int64
  263. err error
  264. )
  265. if err := daemon.Mount(container); err != nil {
  266. logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
  267. return sizeRw, sizeRootfs
  268. }
  269. defer daemon.Unmount(container)
  270. sizeRw, err = container.RWLayer.Size()
  271. if err != nil {
  272. logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
  273. daemon.GraphDriverName(), container.ID, err)
  274. // FIXME: GetSize should return an error. Not changing it now in case
  275. // there is a side-effect.
  276. sizeRw = -1
  277. }
  278. if parent := container.RWLayer.Parent(); parent != nil {
  279. sizeRootfs, err = parent.Size()
  280. if err != nil {
  281. sizeRootfs = -1
  282. } else if sizeRw != -1 {
  283. sizeRootfs += sizeRw
  284. }
  285. }
  286. return sizeRw, sizeRootfs
  287. }
  288. func (daemon *Daemon) buildSandboxOptions(container *container.Container, n libnetwork.Network) ([]libnetwork.SandboxOption, error) {
  289. var (
  290. sboxOptions []libnetwork.SandboxOption
  291. err error
  292. dns []string
  293. dnsSearch []string
  294. dnsOptions []string
  295. )
  296. sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname),
  297. libnetwork.OptionDomainname(container.Config.Domainname))
  298. if container.HostConfig.NetworkMode.IsHost() {
  299. sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
  300. sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
  301. sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
  302. } else if daemon.execDriver.SupportsHooks() {
  303. // OptionUseExternalKey is mandatory for userns support.
  304. // But optional for non-userns support
  305. sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
  306. }
  307. container.HostsPath, err = container.GetRootResourcePath("hosts")
  308. if err != nil {
  309. return nil, err
  310. }
  311. sboxOptions = append(sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  312. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  313. if err != nil {
  314. return nil, err
  315. }
  316. sboxOptions = append(sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  317. if len(container.HostConfig.DNS) > 0 {
  318. dns = container.HostConfig.DNS
  319. } else if len(daemon.configStore.DNS) > 0 {
  320. dns = daemon.configStore.DNS
  321. }
  322. for _, d := range dns {
  323. sboxOptions = append(sboxOptions, libnetwork.OptionDNS(d))
  324. }
  325. if len(container.HostConfig.DNSSearch) > 0 {
  326. dnsSearch = container.HostConfig.DNSSearch
  327. } else if len(daemon.configStore.DNSSearch) > 0 {
  328. dnsSearch = daemon.configStore.DNSSearch
  329. }
  330. for _, ds := range dnsSearch {
  331. sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ds))
  332. }
  333. if len(container.HostConfig.DNSOptions) > 0 {
  334. dnsOptions = container.HostConfig.DNSOptions
  335. } else if len(daemon.configStore.DNSOptions) > 0 {
  336. dnsOptions = daemon.configStore.DNSOptions
  337. }
  338. for _, ds := range dnsOptions {
  339. sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ds))
  340. }
  341. if container.NetworkSettings.SecondaryIPAddresses != nil {
  342. name := container.Config.Hostname
  343. if container.Config.Domainname != "" {
  344. name = name + "." + container.Config.Domainname
  345. }
  346. for _, a := range container.NetworkSettings.SecondaryIPAddresses {
  347. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(name, a.Addr))
  348. }
  349. }
  350. for _, extraHost := range container.HostConfig.ExtraHosts {
  351. // allow IPv6 addresses in extra hosts; only split on first ":"
  352. parts := strings.SplitN(extraHost, ":", 2)
  353. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1]))
  354. }
  355. // Link feature is supported only for the default bridge network.
  356. // return if this call to build join options is not for default bridge network
  357. if n.Name() != "bridge" {
  358. return sboxOptions, nil
  359. }
  360. ep, _ := container.GetEndpointInNetwork(n)
  361. if ep == nil {
  362. return sboxOptions, nil
  363. }
  364. var childEndpoints, parentEndpoints []string
  365. children, err := daemon.children(container.Name)
  366. if err != nil {
  367. return nil, err
  368. }
  369. for linkAlias, child := range children {
  370. if !isLinkable(child) {
  371. return nil, fmt.Errorf("Cannot link to %s, as it does not belong to the default network", child.Name)
  372. }
  373. _, alias := path.Split(linkAlias)
  374. // allow access to the linked container via the alias, real name, and container hostname
  375. aliasList := alias + " " + child.Config.Hostname
  376. // only add the name if alias isn't equal to the name
  377. if alias != child.Name[1:] {
  378. aliasList = aliasList + " " + child.Name[1:]
  379. }
  380. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(aliasList, child.NetworkSettings.Networks["bridge"].IPAddress))
  381. cEndpoint, _ := child.GetEndpointInNetwork(n)
  382. if cEndpoint != nil && cEndpoint.ID() != "" {
  383. childEndpoints = append(childEndpoints, cEndpoint.ID())
  384. }
  385. }
  386. bridgeSettings := container.NetworkSettings.Networks["bridge"]
  387. refs := daemon.containerGraph().RefPaths(container.ID)
  388. for _, ref := range refs {
  389. if ref.ParentID == "0" {
  390. continue
  391. }
  392. c, err := daemon.GetContainer(ref.ParentID)
  393. if err != nil {
  394. logrus.Error(err)
  395. }
  396. if c != nil && !daemon.configStore.DisableBridge && container.HostConfig.NetworkMode.IsPrivate() {
  397. logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", c.ID, ref.Name, bridgeSettings.IPAddress)
  398. sboxOptions = append(sboxOptions, libnetwork.OptionParentUpdate(c.ID, ref.Name, bridgeSettings.IPAddress))
  399. if ep.ID() != "" {
  400. parentEndpoints = append(parentEndpoints, ep.ID())
  401. }
  402. }
  403. }
  404. linkOptions := options.Generic{
  405. netlabel.GenericData: options.Generic{
  406. "ParentEndpoints": parentEndpoints,
  407. "ChildEndpoints": childEndpoints,
  408. },
  409. }
  410. sboxOptions = append(sboxOptions, libnetwork.OptionGeneric(linkOptions))
  411. return sboxOptions, nil
  412. }
  413. func (daemon *Daemon) updateNetworkSettings(container *container.Container, n libnetwork.Network) error {
  414. if container.NetworkSettings == nil {
  415. container.NetworkSettings = &network.Settings{Networks: make(map[string]*networktypes.EndpointSettings)}
  416. }
  417. if !container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  418. return runconfig.ErrConflictHostNetwork
  419. }
  420. for s := range container.NetworkSettings.Networks {
  421. sn, err := daemon.FindNetwork(s)
  422. if err != nil {
  423. continue
  424. }
  425. if sn.Name() == n.Name() {
  426. // Avoid duplicate config
  427. return nil
  428. }
  429. if !containertypes.NetworkMode(sn.Type()).IsPrivate() ||
  430. !containertypes.NetworkMode(n.Type()).IsPrivate() {
  431. return runconfig.ErrConflictSharedNetwork
  432. }
  433. if containertypes.NetworkMode(sn.Name()).IsNone() ||
  434. containertypes.NetworkMode(n.Name()).IsNone() {
  435. return runconfig.ErrConflictNoNetwork
  436. }
  437. }
  438. if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok {
  439. container.NetworkSettings.Networks[n.Name()] = new(networktypes.EndpointSettings)
  440. }
  441. return nil
  442. }
  443. func (daemon *Daemon) updateEndpointNetworkSettings(container *container.Container, n libnetwork.Network, ep libnetwork.Endpoint) error {
  444. if err := container.BuildEndpointInfo(n, ep); err != nil {
  445. return err
  446. }
  447. if container.HostConfig.NetworkMode == containertypes.NetworkMode("bridge") {
  448. container.NetworkSettings.Bridge = daemon.configStore.Bridge.Iface
  449. }
  450. return nil
  451. }
  452. // UpdateNetwork is used to update the container's network (e.g. when linked containers
  453. // get removed/unlinked).
  454. func (daemon *Daemon) updateNetwork(container *container.Container) error {
  455. ctrl := daemon.netController
  456. sid := container.NetworkSettings.SandboxID
  457. sb, err := ctrl.SandboxByID(sid)
  458. if err != nil {
  459. return derr.ErrorCodeNoSandbox.WithArgs(sid, err)
  460. }
  461. // Find if container is connected to the default bridge network
  462. var n libnetwork.Network
  463. for name := range container.NetworkSettings.Networks {
  464. sn, err := daemon.FindNetwork(name)
  465. if err != nil {
  466. continue
  467. }
  468. if sn.Name() == "bridge" {
  469. n = sn
  470. break
  471. }
  472. }
  473. if n == nil {
  474. // Not connected to the default bridge network; Nothing to do
  475. return nil
  476. }
  477. options, err := daemon.buildSandboxOptions(container, n)
  478. if err != nil {
  479. return derr.ErrorCodeNetworkUpdate.WithArgs(err)
  480. }
  481. if err := sb.Refresh(options...); err != nil {
  482. return derr.ErrorCodeNetworkRefresh.WithArgs(sid, err)
  483. }
  484. return nil
  485. }
  486. // updateContainerNetworkSettings update the network settings
  487. func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) error {
  488. var (
  489. n libnetwork.Network
  490. err error
  491. )
  492. mode := container.HostConfig.NetworkMode
  493. if container.Config.NetworkDisabled || mode.IsContainer() {
  494. return nil
  495. }
  496. networkName := mode.NetworkName()
  497. if mode.IsDefault() {
  498. networkName = daemon.netController.Config().Daemon.DefaultNetwork
  499. }
  500. if mode.IsUserDefined() {
  501. n, err = daemon.FindNetwork(networkName)
  502. if err != nil {
  503. return err
  504. }
  505. networkName = n.Name()
  506. }
  507. if container.NetworkSettings == nil {
  508. container.NetworkSettings = &network.Settings{}
  509. }
  510. if endpointsConfig != nil {
  511. container.NetworkSettings.Networks = endpointsConfig
  512. }
  513. if container.NetworkSettings.Networks == nil {
  514. container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
  515. container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
  516. }
  517. if !mode.IsUserDefined() {
  518. return nil
  519. }
  520. // Make sure to internally store the per network endpoint config by network name
  521. if _, ok := container.NetworkSettings.Networks[networkName]; ok {
  522. return nil
  523. }
  524. if nwConfig, ok := container.NetworkSettings.Networks[n.ID()]; ok {
  525. container.NetworkSettings.Networks[networkName] = nwConfig
  526. delete(container.NetworkSettings.Networks, n.ID())
  527. return nil
  528. }
  529. return nil
  530. }
  531. func (daemon *Daemon) allocateNetwork(container *container.Container) error {
  532. controller := daemon.netController
  533. // Cleanup any stale sandbox left over due to ungraceful daemon shutdown
  534. if err := controller.SandboxDestroy(container.ID); err != nil {
  535. logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
  536. }
  537. updateSettings := false
  538. if len(container.NetworkSettings.Networks) == 0 {
  539. if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
  540. return nil
  541. }
  542. err := daemon.updateContainerNetworkSettings(container, nil)
  543. if err != nil {
  544. return err
  545. }
  546. updateSettings = true
  547. }
  548. for n, nConf := range container.NetworkSettings.Networks {
  549. if err := daemon.connectToNetwork(container, n, nConf, updateSettings); err != nil {
  550. return err
  551. }
  552. }
  553. return container.WriteHostConfig()
  554. }
  555. func (daemon *Daemon) getNetworkSandbox(container *container.Container) libnetwork.Sandbox {
  556. var sb libnetwork.Sandbox
  557. daemon.netController.WalkSandboxes(func(s libnetwork.Sandbox) bool {
  558. if s.ContainerID() == container.ID {
  559. sb = s
  560. return true
  561. }
  562. return false
  563. })
  564. return sb
  565. }
  566. // hasUserDefinedIPAddress returns whether the passed endpoint configuration contains IP address configuration
  567. func hasUserDefinedIPAddress(epConfig *networktypes.EndpointSettings) bool {
  568. return epConfig != nil && epConfig.IPAMConfig != nil && (len(epConfig.IPAMConfig.IPv4Address) > 0 || len(epConfig.IPAMConfig.IPv6Address) > 0)
  569. }
  570. // User specified ip address is acceptable only for networks with user specified subnets.
  571. func validateNetworkingConfig(n libnetwork.Network, epConfig *networktypes.EndpointSettings) error {
  572. if !hasUserDefinedIPAddress(epConfig) {
  573. return nil
  574. }
  575. _, _, nwIPv4Configs, nwIPv6Configs := n.Info().IpamConfig()
  576. for _, s := range []struct {
  577. ipConfigured bool
  578. subnetConfigs []*libnetwork.IpamConf
  579. }{
  580. {
  581. ipConfigured: len(epConfig.IPAMConfig.IPv4Address) > 0,
  582. subnetConfigs: nwIPv4Configs,
  583. },
  584. {
  585. ipConfigured: len(epConfig.IPAMConfig.IPv6Address) > 0,
  586. subnetConfigs: nwIPv6Configs,
  587. },
  588. } {
  589. if s.ipConfigured {
  590. foundSubnet := false
  591. for _, cfg := range s.subnetConfigs {
  592. if len(cfg.PreferredPool) > 0 {
  593. foundSubnet = true
  594. break
  595. }
  596. }
  597. if !foundSubnet {
  598. return runconfig.ErrUnsupportedNetworkNoSubnetAndIP
  599. }
  600. }
  601. }
  602. return nil
  603. }
  604. // cleanOperationalData resets the operational data from the passed endpoint settings
  605. func cleanOperationalData(es *networktypes.EndpointSettings) {
  606. es.EndpointID = ""
  607. es.Gateway = ""
  608. es.IPAddress = ""
  609. es.IPPrefixLen = 0
  610. es.IPv6Gateway = ""
  611. es.GlobalIPv6Address = ""
  612. es.GlobalIPv6PrefixLen = 0
  613. es.MacAddress = ""
  614. }
  615. // ConnectToNetwork connects a container to a network
  616. func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  617. if !container.Running {
  618. return derr.ErrorCodeNotRunning.WithArgs(container.ID)
  619. }
  620. if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  621. return err
  622. }
  623. if err := container.ToDiskLocking(); err != nil {
  624. return fmt.Errorf("Error saving container to disk: %v", err)
  625. }
  626. return nil
  627. }
  628. func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
  629. if container.HostConfig.NetworkMode.IsContainer() {
  630. return runconfig.ErrConflictSharedNetwork
  631. }
  632. if !containertypes.NetworkMode(idOrName).IsUserDefined() && hasUserDefinedIPAddress(endpointConfig) {
  633. return runconfig.ErrUnsupportedNetworkAndIP
  634. }
  635. if containertypes.NetworkMode(idOrName).IsBridge() &&
  636. daemon.configStore.DisableBridge {
  637. container.Config.NetworkDisabled = true
  638. return nil
  639. }
  640. controller := daemon.netController
  641. n, err := daemon.FindNetwork(idOrName)
  642. if err != nil {
  643. return err
  644. }
  645. if err := validateNetworkingConfig(n, endpointConfig); err != nil {
  646. return err
  647. }
  648. if updateSettings {
  649. if err := daemon.updateNetworkSettings(container, n); err != nil {
  650. return err
  651. }
  652. }
  653. if endpointConfig != nil {
  654. container.NetworkSettings.Networks[n.Name()] = endpointConfig
  655. }
  656. createOptions, err := container.BuildCreateEndpointOptions(n)
  657. if err != nil {
  658. return err
  659. }
  660. endpointName := strings.TrimPrefix(container.Name, "/")
  661. ep, err := n.CreateEndpoint(endpointName, createOptions...)
  662. if err != nil {
  663. return err
  664. }
  665. defer func() {
  666. if err != nil {
  667. if e := ep.Delete(); e != nil {
  668. logrus.Warnf("Could not rollback container connection to network %s", idOrName)
  669. }
  670. }
  671. }()
  672. if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
  673. return err
  674. }
  675. sb := daemon.getNetworkSandbox(container)
  676. if sb == nil {
  677. options, err := daemon.buildSandboxOptions(container, n)
  678. if err != nil {
  679. return err
  680. }
  681. sb, err = controller.NewSandbox(container.ID, options...)
  682. if err != nil {
  683. return err
  684. }
  685. container.UpdateSandboxNetworkSettings(sb)
  686. }
  687. if err := ep.Join(sb); err != nil {
  688. return err
  689. }
  690. if err := container.UpdateJoinInfo(n, ep); err != nil {
  691. return derr.ErrorCodeJoinInfo.WithArgs(err)
  692. }
  693. daemon.LogNetworkEventWithAttributes(n, "connect", map[string]string{"container": container.ID})
  694. return nil
  695. }
  696. // DisconnectFromNetwork disconnects container from network n.
  697. func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network) error {
  698. if !container.Running {
  699. return derr.ErrorCodeNotRunning.WithArgs(container.ID)
  700. }
  701. if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  702. return runconfig.ErrConflictHostNetwork
  703. }
  704. if err := disconnectFromNetwork(container, n); err != nil {
  705. return err
  706. }
  707. if err := container.ToDiskLocking(); err != nil {
  708. return fmt.Errorf("Error saving container to disk: %v", err)
  709. }
  710. attributes := map[string]string{
  711. "container": container.ID,
  712. }
  713. daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
  714. return nil
  715. }
  716. func disconnectFromNetwork(container *container.Container, n libnetwork.Network) error {
  717. var (
  718. ep libnetwork.Endpoint
  719. sbox libnetwork.Sandbox
  720. )
  721. s := func(current libnetwork.Endpoint) bool {
  722. epInfo := current.Info()
  723. if epInfo == nil {
  724. return false
  725. }
  726. if sb := epInfo.Sandbox(); sb != nil {
  727. if sb.ContainerID() == container.ID {
  728. ep = current
  729. sbox = sb
  730. return true
  731. }
  732. }
  733. return false
  734. }
  735. n.WalkEndpoints(s)
  736. if ep == nil {
  737. return fmt.Errorf("container %s is not connected to the network", container.ID)
  738. }
  739. if err := ep.Leave(sbox); err != nil {
  740. return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
  741. }
  742. if err := ep.Delete(); err != nil {
  743. return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
  744. }
  745. delete(container.NetworkSettings.Networks, n.Name())
  746. return nil
  747. }
  748. func (daemon *Daemon) initializeNetworking(container *container.Container) error {
  749. var err error
  750. if container.HostConfig.NetworkMode.IsContainer() {
  751. // we need to get the hosts files from the container to join
  752. nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
  753. if err != nil {
  754. return err
  755. }
  756. container.HostnamePath = nc.HostnamePath
  757. container.HostsPath = nc.HostsPath
  758. container.ResolvConfPath = nc.ResolvConfPath
  759. container.Config.Hostname = nc.Config.Hostname
  760. container.Config.Domainname = nc.Config.Domainname
  761. return nil
  762. }
  763. if container.HostConfig.NetworkMode.IsHost() {
  764. container.Config.Hostname, err = os.Hostname()
  765. if err != nil {
  766. return err
  767. }
  768. parts := strings.SplitN(container.Config.Hostname, ".", 2)
  769. if len(parts) > 1 {
  770. container.Config.Hostname = parts[0]
  771. container.Config.Domainname = parts[1]
  772. }
  773. }
  774. if err := daemon.allocateNetwork(container); err != nil {
  775. return err
  776. }
  777. return container.BuildHostnameFile()
  778. }
  779. // called from the libcontainer pre-start hook to set the network
  780. // namespace configuration linkage to the libnetwork "sandbox" entity
  781. func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
  782. path := fmt.Sprintf("/proc/%d/ns/net", pid)
  783. var sandbox libnetwork.Sandbox
  784. search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
  785. daemon.netController.WalkSandboxes(search)
  786. if sandbox == nil {
  787. return derr.ErrorCodeNoSandbox.WithArgs(containerID, "no sandbox found")
  788. }
  789. return sandbox.SetKey(path)
  790. }
  791. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  792. containerID := container.HostConfig.IpcMode.Container()
  793. c, err := daemon.GetContainer(containerID)
  794. if err != nil {
  795. return nil, err
  796. }
  797. if !c.IsRunning() {
  798. return nil, derr.ErrorCodeIPCRunning.WithArgs(containerID)
  799. }
  800. return c, nil
  801. }
  802. func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
  803. nc, err := daemon.GetContainer(connectedContainerID)
  804. if err != nil {
  805. return nil, err
  806. }
  807. if containerID == nc.ID {
  808. return nil, derr.ErrorCodeJoinSelf
  809. }
  810. if !nc.IsRunning() {
  811. return nil, derr.ErrorCodeJoinRunning.WithArgs(connectedContainerID)
  812. }
  813. return nc, nil
  814. }
  815. func (daemon *Daemon) releaseNetwork(container *container.Container) {
  816. if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
  817. return
  818. }
  819. sid := container.NetworkSettings.SandboxID
  820. settings := container.NetworkSettings.Networks
  821. if sid == "" || len(settings) == 0 {
  822. return
  823. }
  824. var networks []libnetwork.Network
  825. for n, epSettings := range settings {
  826. if nw, err := daemon.FindNetwork(n); err == nil {
  827. networks = append(networks, nw)
  828. }
  829. cleanOperationalData(epSettings)
  830. }
  831. sb, err := daemon.netController.SandboxByID(sid)
  832. if err != nil {
  833. logrus.Errorf("error locating sandbox id %s: %v", sid, err)
  834. return
  835. }
  836. if err := sb.Delete(); err != nil {
  837. logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
  838. }
  839. attributes := map[string]string{
  840. "container": container.ID,
  841. }
  842. for _, nw := range networks {
  843. daemon.LogNetworkEventWithAttributes(nw, "disconnect", attributes)
  844. }
  845. }
  846. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  847. rootUID, rootGID := daemon.GetRemappedUIDGID()
  848. if !c.HasMountFor("/dev/shm") {
  849. shmPath, err := c.ShmResourcePath()
  850. if err != nil {
  851. return err
  852. }
  853. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  854. return err
  855. }
  856. shmSize := container.DefaultSHMSize
  857. if c.HostConfig.ShmSize != 0 {
  858. shmSize = c.HostConfig.ShmSize
  859. }
  860. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  861. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  862. return fmt.Errorf("mounting shm tmpfs: %s", err)
  863. }
  864. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  865. return err
  866. }
  867. }
  868. if !c.HasMountFor("/dev/mqueue") {
  869. mqueuePath, err := c.MqueueResourcePath()
  870. if err != nil {
  871. return err
  872. }
  873. if err := idtools.MkdirAllAs(mqueuePath, 0700, rootUID, rootGID); err != nil {
  874. return err
  875. }
  876. if err := syscall.Mount("mqueue", mqueuePath, "mqueue", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), ""); err != nil {
  877. return fmt.Errorf("mounting mqueue mqueue : %s", err)
  878. }
  879. }
  880. return nil
  881. }
  882. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  883. mounts, err := daemon.setupMounts(container)
  884. if err != nil {
  885. return err
  886. }
  887. for _, m := range mounts {
  888. dest, err := container.GetResourcePath(m.Destination)
  889. if err != nil {
  890. return err
  891. }
  892. var stat os.FileInfo
  893. stat, err = os.Stat(m.Source)
  894. if err != nil {
  895. return err
  896. }
  897. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  898. return err
  899. }
  900. opts := "rbind,ro"
  901. if m.Writable {
  902. opts = "rbind,rw"
  903. }
  904. if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
  905. return err
  906. }
  907. }
  908. return nil
  909. }
  910. func killProcessDirectly(container *container.Container) error {
  911. if _, err := container.WaitStop(10 * time.Second); err != nil {
  912. // Ensure that we don't kill ourselves
  913. if pid := container.GetPID(); pid != 0 {
  914. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  915. if err := syscall.Kill(pid, 9); err != nil {
  916. if err != syscall.ESRCH {
  917. return err
  918. }
  919. logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
  920. }
  921. }
  922. }
  923. return nil
  924. }
  925. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
  926. device, err := devices.DeviceFromPath(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
  927. // if there was no error, return the device
  928. if err == nil {
  929. device.Path = deviceMapping.PathInContainer
  930. return append(devs, device), nil
  931. }
  932. // if the device is not a device node
  933. // try to see if it's a directory holding many devices
  934. if err == devices.ErrNotADevice {
  935. // check if it is a directory
  936. if src, e := os.Stat(deviceMapping.PathOnHost); e == nil && src.IsDir() {
  937. // mount the internal devices recursively
  938. filepath.Walk(deviceMapping.PathOnHost, func(dpath string, f os.FileInfo, e error) error {
  939. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  940. if e != nil {
  941. // ignore the device
  942. return nil
  943. }
  944. // add the device to userSpecified devices
  945. childDevice.Path = strings.Replace(dpath, deviceMapping.PathOnHost, deviceMapping.PathInContainer, 1)
  946. devs = append(devs, childDevice)
  947. return nil
  948. })
  949. }
  950. }
  951. if len(devs) > 0 {
  952. return devs, nil
  953. }
  954. return devs, derr.ErrorCodeDeviceInfo.WithArgs(deviceMapping.PathOnHost, err)
  955. }
  956. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  957. if len(userDevices) == 0 {
  958. return defaultDevices
  959. }
  960. paths := map[string]*configs.Device{}
  961. for _, d := range userDevices {
  962. paths[d.Path] = d
  963. }
  964. var devs []*configs.Device
  965. for _, d := range defaultDevices {
  966. if _, defined := paths[d.Path]; !defined {
  967. devs = append(devs, d)
  968. }
  969. }
  970. return append(devs, userDevices...)
  971. }
  972. func detachMounted(path string) error {
  973. return syscall.Unmount(path, syscall.MNT_DETACH)
  974. }
  975. func isLinkable(child *container.Container) bool {
  976. // A container is linkable only if it belongs to the default network
  977. _, ok := child.NetworkSettings.Networks["bridge"]
  978. return ok
  979. }