container_operations_unix.go 32 KB

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