container_operations_unix.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. if !daemon.seccompEnabled {
  207. if c.SeccompProfile != "" && c.SeccompProfile != "unconfined" {
  208. return fmt.Errorf("Seccomp is not enabled in your kernel, cannot run a custom seccomp profile.")
  209. }
  210. logrus.Warn("Seccomp is not enabled in your kernel, running container without default profile.")
  211. c.SeccompProfile = "unconfined"
  212. }
  213. defaultCgroupParent := "/docker"
  214. if daemon.configStore.CgroupParent != "" {
  215. defaultCgroupParent = daemon.configStore.CgroupParent
  216. } else {
  217. for _, option := range daemon.configStore.ExecOptions {
  218. key, val, err := parsers.ParseKeyValueOpt(option)
  219. if err != nil || !strings.EqualFold(key, "native.cgroupdriver") {
  220. continue
  221. }
  222. if val == "systemd" {
  223. defaultCgroupParent = "system.slice"
  224. }
  225. }
  226. }
  227. c.Command = &execdriver.Command{
  228. CommonCommand: execdriver.CommonCommand{
  229. ID: c.ID,
  230. InitPath: "/.dockerinit",
  231. MountLabel: c.GetMountLabel(),
  232. Network: en,
  233. ProcessConfig: processConfig,
  234. ProcessLabel: c.GetProcessLabel(),
  235. Rootfs: c.BaseFS,
  236. Resources: resources,
  237. WorkingDir: c.Config.WorkingDir,
  238. },
  239. AllowedDevices: allowedDevices,
  240. AppArmorProfile: c.AppArmorProfile,
  241. AutoCreatedDevices: autoCreatedDevices,
  242. CapAdd: c.HostConfig.CapAdd.Slice(),
  243. CapDrop: c.HostConfig.CapDrop.Slice(),
  244. CgroupParent: defaultCgroupParent,
  245. GIDMapping: gidMap,
  246. GroupAdd: c.HostConfig.GroupAdd,
  247. Ipc: ipc,
  248. OomScoreAdj: c.HostConfig.OomScoreAdj,
  249. Pid: pid,
  250. ReadonlyRootfs: c.HostConfig.ReadonlyRootfs,
  251. RemappedRoot: remappedRoot,
  252. SeccompProfile: c.SeccompProfile,
  253. UIDMapping: uidMap,
  254. UTS: uts,
  255. }
  256. if c.HostConfig.CgroupParent != "" {
  257. c.Command.CgroupParent = c.HostConfig.CgroupParent
  258. }
  259. return nil
  260. }
  261. // getSize returns the real size & virtual size of the container.
  262. func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
  263. var (
  264. sizeRw, sizeRootfs int64
  265. err error
  266. )
  267. if err := daemon.Mount(container); err != nil {
  268. logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
  269. return sizeRw, sizeRootfs
  270. }
  271. defer daemon.Unmount(container)
  272. sizeRw, err = container.RWLayer.Size()
  273. if err != nil {
  274. logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
  275. daemon.GraphDriverName(), container.ID, err)
  276. // FIXME: GetSize should return an error. Not changing it now in case
  277. // there is a side-effect.
  278. sizeRw = -1
  279. }
  280. if parent := container.RWLayer.Parent(); parent != nil {
  281. sizeRootfs, err = parent.Size()
  282. if err != nil {
  283. sizeRootfs = -1
  284. } else if sizeRw != -1 {
  285. sizeRootfs += sizeRw
  286. }
  287. }
  288. return sizeRw, sizeRootfs
  289. }
  290. func (daemon *Daemon) buildSandboxOptions(container *container.Container, n libnetwork.Network) ([]libnetwork.SandboxOption, error) {
  291. var (
  292. sboxOptions []libnetwork.SandboxOption
  293. err error
  294. dns []string
  295. dnsSearch []string
  296. dnsOptions []string
  297. )
  298. sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname),
  299. libnetwork.OptionDomainname(container.Config.Domainname))
  300. if container.HostConfig.NetworkMode.IsHost() {
  301. sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
  302. sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
  303. sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
  304. } else if daemon.execDriver.SupportsHooks() {
  305. // OptionUseExternalKey is mandatory for userns support.
  306. // But optional for non-userns support
  307. sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
  308. }
  309. container.HostsPath, err = container.GetRootResourcePath("hosts")
  310. if err != nil {
  311. return nil, err
  312. }
  313. sboxOptions = append(sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  314. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  315. if err != nil {
  316. return nil, err
  317. }
  318. sboxOptions = append(sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  319. if len(container.HostConfig.DNS) > 0 {
  320. dns = container.HostConfig.DNS
  321. } else if len(daemon.configStore.DNS) > 0 {
  322. dns = daemon.configStore.DNS
  323. }
  324. for _, d := range dns {
  325. sboxOptions = append(sboxOptions, libnetwork.OptionDNS(d))
  326. }
  327. if len(container.HostConfig.DNSSearch) > 0 {
  328. dnsSearch = container.HostConfig.DNSSearch
  329. } else if len(daemon.configStore.DNSSearch) > 0 {
  330. dnsSearch = daemon.configStore.DNSSearch
  331. }
  332. for _, ds := range dnsSearch {
  333. sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ds))
  334. }
  335. if len(container.HostConfig.DNSOptions) > 0 {
  336. dnsOptions = container.HostConfig.DNSOptions
  337. } else if len(daemon.configStore.DNSOptions) > 0 {
  338. dnsOptions = daemon.configStore.DNSOptions
  339. }
  340. for _, ds := range dnsOptions {
  341. sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ds))
  342. }
  343. if container.NetworkSettings.SecondaryIPAddresses != nil {
  344. name := container.Config.Hostname
  345. if container.Config.Domainname != "" {
  346. name = name + "." + container.Config.Domainname
  347. }
  348. for _, a := range container.NetworkSettings.SecondaryIPAddresses {
  349. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(name, a.Addr))
  350. }
  351. }
  352. for _, extraHost := range container.HostConfig.ExtraHosts {
  353. // allow IPv6 addresses in extra hosts; only split on first ":"
  354. parts := strings.SplitN(extraHost, ":", 2)
  355. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1]))
  356. }
  357. // Link feature is supported only for the default bridge network.
  358. // return if this call to build join options is not for default bridge network
  359. if n.Name() != "bridge" {
  360. return sboxOptions, nil
  361. }
  362. ep, _ := container.GetEndpointInNetwork(n)
  363. if ep == nil {
  364. return sboxOptions, nil
  365. }
  366. var childEndpoints, parentEndpoints []string
  367. children := daemon.children(container)
  368. for linkAlias, child := range children {
  369. if !isLinkable(child) {
  370. return nil, fmt.Errorf("Cannot link to %s, as it does not belong to the default network", child.Name)
  371. }
  372. _, alias := path.Split(linkAlias)
  373. // allow access to the linked container via the alias, real name, and container hostname
  374. aliasList := alias + " " + child.Config.Hostname
  375. // only add the name if alias isn't equal to the name
  376. if alias != child.Name[1:] {
  377. aliasList = aliasList + " " + child.Name[1:]
  378. }
  379. sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(aliasList, child.NetworkSettings.Networks["bridge"].IPAddress))
  380. cEndpoint, _ := child.GetEndpointInNetwork(n)
  381. if cEndpoint != nil && cEndpoint.ID() != "" {
  382. childEndpoints = append(childEndpoints, cEndpoint.ID())
  383. }
  384. }
  385. bridgeSettings := container.NetworkSettings.Networks["bridge"]
  386. for alias, parent := range daemon.parents(container) {
  387. if daemon.configStore.DisableBridge || !container.HostConfig.NetworkMode.IsPrivate() {
  388. continue
  389. }
  390. _, alias = path.Split(alias)
  391. logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", parent.ID, alias, bridgeSettings.IPAddress)
  392. sboxOptions = append(sboxOptions, libnetwork.OptionParentUpdate(
  393. parent.ID,
  394. alias,
  395. bridgeSettings.IPAddress,
  396. ))
  397. if ep.ID() != "" {
  398. parentEndpoints = append(parentEndpoints, ep.ID())
  399. }
  400. }
  401. linkOptions := options.Generic{
  402. netlabel.GenericData: options.Generic{
  403. "ParentEndpoints": parentEndpoints,
  404. "ChildEndpoints": childEndpoints,
  405. },
  406. }
  407. sboxOptions = append(sboxOptions, libnetwork.OptionGeneric(linkOptions))
  408. return sboxOptions, nil
  409. }
  410. func (daemon *Daemon) updateNetworkSettings(container *container.Container, n libnetwork.Network) error {
  411. if container.NetworkSettings == nil {
  412. container.NetworkSettings = &network.Settings{Networks: make(map[string]*networktypes.EndpointSettings)}
  413. }
  414. if !container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  415. return runconfig.ErrConflictHostNetwork
  416. }
  417. for s := range container.NetworkSettings.Networks {
  418. sn, err := daemon.FindNetwork(s)
  419. if err != nil {
  420. continue
  421. }
  422. if sn.Name() == n.Name() {
  423. // Avoid duplicate config
  424. return nil
  425. }
  426. if !containertypes.NetworkMode(sn.Type()).IsPrivate() ||
  427. !containertypes.NetworkMode(n.Type()).IsPrivate() {
  428. return runconfig.ErrConflictSharedNetwork
  429. }
  430. if containertypes.NetworkMode(sn.Name()).IsNone() ||
  431. containertypes.NetworkMode(n.Name()).IsNone() {
  432. return runconfig.ErrConflictNoNetwork
  433. }
  434. }
  435. if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok {
  436. container.NetworkSettings.Networks[n.Name()] = new(networktypes.EndpointSettings)
  437. }
  438. return nil
  439. }
  440. func (daemon *Daemon) updateEndpointNetworkSettings(container *container.Container, n libnetwork.Network, ep libnetwork.Endpoint) error {
  441. if err := container.BuildEndpointInfo(n, ep); err != nil {
  442. return err
  443. }
  444. if container.HostConfig.NetworkMode == containertypes.NetworkMode("bridge") {
  445. container.NetworkSettings.Bridge = daemon.configStore.Bridge.Iface
  446. }
  447. return nil
  448. }
  449. // UpdateNetwork is used to update the container's network (e.g. when linked containers
  450. // get removed/unlinked).
  451. func (daemon *Daemon) updateNetwork(container *container.Container) error {
  452. ctrl := daemon.netController
  453. sid := container.NetworkSettings.SandboxID
  454. sb, err := ctrl.SandboxByID(sid)
  455. if err != nil {
  456. return derr.ErrorCodeNoSandbox.WithArgs(sid, err)
  457. }
  458. // Find if container is connected to the default bridge network
  459. var n libnetwork.Network
  460. for name := range container.NetworkSettings.Networks {
  461. sn, err := daemon.FindNetwork(name)
  462. if err != nil {
  463. continue
  464. }
  465. if sn.Name() == "bridge" {
  466. n = sn
  467. break
  468. }
  469. }
  470. if n == nil {
  471. // Not connected to the default bridge network; Nothing to do
  472. return nil
  473. }
  474. options, err := daemon.buildSandboxOptions(container, n)
  475. if err != nil {
  476. return derr.ErrorCodeNetworkUpdate.WithArgs(err)
  477. }
  478. if err := sb.Refresh(options...); err != nil {
  479. return derr.ErrorCodeNetworkRefresh.WithArgs(sid, err)
  480. }
  481. return nil
  482. }
  483. // updateContainerNetworkSettings update the network settings
  484. func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) error {
  485. var (
  486. n libnetwork.Network
  487. err error
  488. )
  489. mode := container.HostConfig.NetworkMode
  490. if container.Config.NetworkDisabled || mode.IsContainer() {
  491. return nil
  492. }
  493. networkName := mode.NetworkName()
  494. if mode.IsDefault() {
  495. networkName = daemon.netController.Config().Daemon.DefaultNetwork
  496. }
  497. if mode.IsUserDefined() {
  498. n, err = daemon.FindNetwork(networkName)
  499. if err != nil {
  500. return err
  501. }
  502. networkName = n.Name()
  503. }
  504. if container.NetworkSettings == nil {
  505. container.NetworkSettings = &network.Settings{}
  506. }
  507. if len(endpointsConfig) > 0 {
  508. container.NetworkSettings.Networks = endpointsConfig
  509. }
  510. if container.NetworkSettings.Networks == nil {
  511. container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
  512. container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
  513. }
  514. if !mode.IsUserDefined() {
  515. return nil
  516. }
  517. // Make sure to internally store the per network endpoint config by network name
  518. if _, ok := container.NetworkSettings.Networks[networkName]; ok {
  519. return nil
  520. }
  521. if nwConfig, ok := container.NetworkSettings.Networks[n.ID()]; ok {
  522. container.NetworkSettings.Networks[networkName] = nwConfig
  523. delete(container.NetworkSettings.Networks, n.ID())
  524. return nil
  525. }
  526. return nil
  527. }
  528. func (daemon *Daemon) allocateNetwork(container *container.Container) error {
  529. controller := daemon.netController
  530. // Cleanup any stale sandbox left over due to ungraceful daemon shutdown
  531. if err := controller.SandboxDestroy(container.ID); err != nil {
  532. logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
  533. }
  534. updateSettings := false
  535. if len(container.NetworkSettings.Networks) == 0 {
  536. if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
  537. return nil
  538. }
  539. err := daemon.updateContainerNetworkSettings(container, nil)
  540. if err != nil {
  541. return err
  542. }
  543. updateSettings = true
  544. }
  545. for n, nConf := range container.NetworkSettings.Networks {
  546. if err := daemon.connectToNetwork(container, n, nConf, updateSettings); err != nil {
  547. return err
  548. }
  549. }
  550. return container.WriteHostConfig()
  551. }
  552. func (daemon *Daemon) getNetworkSandbox(container *container.Container) libnetwork.Sandbox {
  553. var sb libnetwork.Sandbox
  554. daemon.netController.WalkSandboxes(func(s libnetwork.Sandbox) bool {
  555. if s.ContainerID() == container.ID {
  556. sb = s
  557. return true
  558. }
  559. return false
  560. })
  561. return sb
  562. }
  563. // hasUserDefinedIPAddress returns whether the passed endpoint configuration contains IP address configuration
  564. func hasUserDefinedIPAddress(epConfig *networktypes.EndpointSettings) bool {
  565. return epConfig != nil && epConfig.IPAMConfig != nil && (len(epConfig.IPAMConfig.IPv4Address) > 0 || len(epConfig.IPAMConfig.IPv6Address) > 0)
  566. }
  567. // User specified ip address is acceptable only for networks with user specified subnets.
  568. func validateNetworkingConfig(n libnetwork.Network, epConfig *networktypes.EndpointSettings) error {
  569. if n == nil || epConfig == nil {
  570. return nil
  571. }
  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. func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (libnetwork.Network, error) {
  616. if container.HostConfig.NetworkMode.IsContainer() {
  617. return nil, runconfig.ErrConflictSharedNetwork
  618. }
  619. if containertypes.NetworkMode(idOrName).IsBridge() &&
  620. daemon.configStore.DisableBridge {
  621. container.Config.NetworkDisabled = true
  622. return nil, nil
  623. }
  624. if !containertypes.NetworkMode(idOrName).IsUserDefined() {
  625. if hasUserDefinedIPAddress(endpointConfig) {
  626. return nil, runconfig.ErrUnsupportedNetworkAndIP
  627. }
  628. if endpointConfig != nil && len(endpointConfig.Aliases) > 0 {
  629. return nil, runconfig.ErrUnsupportedNetworkAndAlias
  630. }
  631. }
  632. n, err := daemon.FindNetwork(idOrName)
  633. if err != nil {
  634. return nil, err
  635. }
  636. if err := validateNetworkingConfig(n, endpointConfig); err != nil {
  637. return nil, err
  638. }
  639. if updateSettings {
  640. if err := daemon.updateNetworkSettings(container, n); err != nil {
  641. return nil, err
  642. }
  643. }
  644. return n, nil
  645. }
  646. // ConnectToNetwork connects a container to a network
  647. func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  648. if !container.Running {
  649. if container.RemovalInProgress || container.Dead {
  650. return derr.ErrorCodeRemovalContainer.WithArgs(container.ID)
  651. }
  652. if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
  653. return err
  654. }
  655. } else {
  656. if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  657. return err
  658. }
  659. }
  660. if err := container.ToDiskLocking(); err != nil {
  661. return fmt.Errorf("Error saving container to disk: %v", err)
  662. }
  663. return nil
  664. }
  665. func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
  666. n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings)
  667. if err != nil {
  668. return err
  669. }
  670. if n == nil {
  671. return nil
  672. }
  673. controller := daemon.netController
  674. createOptions, err := container.BuildCreateEndpointOptions(n, endpointConfig)
  675. if err != nil {
  676. return err
  677. }
  678. endpointName := strings.TrimPrefix(container.Name, "/")
  679. ep, err := n.CreateEndpoint(endpointName, createOptions...)
  680. if err != nil {
  681. return err
  682. }
  683. defer func() {
  684. if err != nil {
  685. if e := ep.Delete(false); e != nil {
  686. logrus.Warnf("Could not rollback container connection to network %s", idOrName)
  687. }
  688. }
  689. }()
  690. if endpointConfig != nil {
  691. container.NetworkSettings.Networks[n.Name()] = endpointConfig
  692. }
  693. if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
  694. return err
  695. }
  696. sb := daemon.getNetworkSandbox(container)
  697. if sb == nil {
  698. options, err := daemon.buildSandboxOptions(container, n)
  699. if err != nil {
  700. return err
  701. }
  702. sb, err = controller.NewSandbox(container.ID, options...)
  703. if err != nil {
  704. return err
  705. }
  706. container.UpdateSandboxNetworkSettings(sb)
  707. }
  708. joinOptions, err := container.BuildJoinOptions(n)
  709. if err != nil {
  710. return err
  711. }
  712. if err := ep.Join(sb, joinOptions...); err != nil {
  713. return err
  714. }
  715. if err := container.UpdateJoinInfo(n, ep); err != nil {
  716. return derr.ErrorCodeJoinInfo.WithArgs(err)
  717. }
  718. daemon.LogNetworkEventWithAttributes(n, "connect", map[string]string{"container": container.ID})
  719. return nil
  720. }
  721. // ForceEndpointDelete deletes an endpoing from a network forcefully
  722. func (daemon *Daemon) ForceEndpointDelete(name string, n libnetwork.Network) error {
  723. ep, err := n.EndpointByName(name)
  724. if err != nil {
  725. return err
  726. }
  727. return ep.Delete(true)
  728. }
  729. // DisconnectFromNetwork disconnects container from network n.
  730. func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  731. if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  732. return runconfig.ErrConflictHostNetwork
  733. }
  734. if !container.Running {
  735. if container.RemovalInProgress || container.Dead {
  736. return derr.ErrorCodeRemovalContainer.WithArgs(container.ID)
  737. }
  738. if _, ok := container.NetworkSettings.Networks[n.Name()]; ok {
  739. delete(container.NetworkSettings.Networks, n.Name())
  740. } else {
  741. return fmt.Errorf("container %s is not connected to the network %s", container.ID, n.Name())
  742. }
  743. } else {
  744. if err := disconnectFromNetwork(container, n, false); err != nil {
  745. return err
  746. }
  747. }
  748. if err := container.ToDiskLocking(); err != nil {
  749. return fmt.Errorf("Error saving container to disk: %v", err)
  750. }
  751. attributes := map[string]string{
  752. "container": container.ID,
  753. }
  754. daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
  755. return nil
  756. }
  757. func disconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  758. var (
  759. ep libnetwork.Endpoint
  760. sbox libnetwork.Sandbox
  761. )
  762. s := func(current libnetwork.Endpoint) bool {
  763. epInfo := current.Info()
  764. if epInfo == nil {
  765. return false
  766. }
  767. if sb := epInfo.Sandbox(); sb != nil {
  768. if sb.ContainerID() == container.ID {
  769. ep = current
  770. sbox = sb
  771. return true
  772. }
  773. }
  774. return false
  775. }
  776. n.WalkEndpoints(s)
  777. if ep == nil && force {
  778. epName := strings.TrimPrefix(container.Name, "/")
  779. ep, err := n.EndpointByName(epName)
  780. if err != nil {
  781. return err
  782. }
  783. return ep.Delete(force)
  784. }
  785. if ep == nil {
  786. return fmt.Errorf("container %s is not connected to the network", container.ID)
  787. }
  788. if err := ep.Leave(sbox); err != nil {
  789. return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
  790. }
  791. if err := ep.Delete(false); err != nil {
  792. return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
  793. }
  794. delete(container.NetworkSettings.Networks, n.Name())
  795. return nil
  796. }
  797. func (daemon *Daemon) initializeNetworking(container *container.Container) error {
  798. var err error
  799. if container.HostConfig.NetworkMode.IsContainer() {
  800. // we need to get the hosts files from the container to join
  801. nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
  802. if err != nil {
  803. return err
  804. }
  805. container.HostnamePath = nc.HostnamePath
  806. container.HostsPath = nc.HostsPath
  807. container.ResolvConfPath = nc.ResolvConfPath
  808. container.Config.Hostname = nc.Config.Hostname
  809. container.Config.Domainname = nc.Config.Domainname
  810. return nil
  811. }
  812. if container.HostConfig.NetworkMode.IsHost() {
  813. container.Config.Hostname, err = os.Hostname()
  814. if err != nil {
  815. return err
  816. }
  817. parts := strings.SplitN(container.Config.Hostname, ".", 2)
  818. if len(parts) > 1 {
  819. container.Config.Hostname = parts[0]
  820. container.Config.Domainname = parts[1]
  821. }
  822. }
  823. if err := daemon.allocateNetwork(container); err != nil {
  824. return err
  825. }
  826. return container.BuildHostnameFile()
  827. }
  828. // called from the libcontainer pre-start hook to set the network
  829. // namespace configuration linkage to the libnetwork "sandbox" entity
  830. func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
  831. path := fmt.Sprintf("/proc/%d/ns/net", pid)
  832. var sandbox libnetwork.Sandbox
  833. search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
  834. daemon.netController.WalkSandboxes(search)
  835. if sandbox == nil {
  836. return derr.ErrorCodeNoSandbox.WithArgs(containerID, "no sandbox found")
  837. }
  838. return sandbox.SetKey(path)
  839. }
  840. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  841. containerID := container.HostConfig.IpcMode.Container()
  842. c, err := daemon.GetContainer(containerID)
  843. if err != nil {
  844. return nil, err
  845. }
  846. if !c.IsRunning() {
  847. return nil, derr.ErrorCodeIPCRunning.WithArgs(containerID)
  848. }
  849. return c, nil
  850. }
  851. func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
  852. nc, err := daemon.GetContainer(connectedContainerID)
  853. if err != nil {
  854. return nil, err
  855. }
  856. if containerID == nc.ID {
  857. return nil, derr.ErrorCodeJoinSelf
  858. }
  859. if !nc.IsRunning() {
  860. return nil, derr.ErrorCodeJoinRunning.WithArgs(connectedContainerID)
  861. }
  862. return nc, nil
  863. }
  864. func (daemon *Daemon) releaseNetwork(container *container.Container) {
  865. if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
  866. return
  867. }
  868. sid := container.NetworkSettings.SandboxID
  869. settings := container.NetworkSettings.Networks
  870. container.NetworkSettings.Ports = nil
  871. if sid == "" || len(settings) == 0 {
  872. return
  873. }
  874. var networks []libnetwork.Network
  875. for n, epSettings := range settings {
  876. if nw, err := daemon.FindNetwork(n); err == nil {
  877. networks = append(networks, nw)
  878. }
  879. cleanOperationalData(epSettings)
  880. }
  881. sb, err := daemon.netController.SandboxByID(sid)
  882. if err != nil {
  883. logrus.Errorf("error locating sandbox id %s: %v", sid, err)
  884. return
  885. }
  886. if err := sb.Delete(); err != nil {
  887. logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
  888. }
  889. attributes := map[string]string{
  890. "container": container.ID,
  891. }
  892. for _, nw := range networks {
  893. daemon.LogNetworkEventWithAttributes(nw, "disconnect", attributes)
  894. }
  895. }
  896. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  897. rootUID, rootGID := daemon.GetRemappedUIDGID()
  898. if !c.HasMountFor("/dev/shm") {
  899. shmPath, err := c.ShmResourcePath()
  900. if err != nil {
  901. return err
  902. }
  903. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  904. return err
  905. }
  906. shmSize := container.DefaultSHMSize
  907. if c.HostConfig.ShmSize != 0 {
  908. shmSize = c.HostConfig.ShmSize
  909. }
  910. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  911. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  912. return fmt.Errorf("mounting shm tmpfs: %s", err)
  913. }
  914. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  915. return err
  916. }
  917. }
  918. if !c.HasMountFor("/dev/mqueue") {
  919. mqueuePath, err := c.MqueueResourcePath()
  920. if err != nil {
  921. return err
  922. }
  923. if err := idtools.MkdirAllAs(mqueuePath, 0700, rootUID, rootGID); err != nil {
  924. return err
  925. }
  926. if err := syscall.Mount("mqueue", mqueuePath, "mqueue", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), ""); err != nil {
  927. return fmt.Errorf("mounting mqueue mqueue : %s", err)
  928. }
  929. }
  930. return nil
  931. }
  932. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  933. mounts, err := daemon.setupMounts(container)
  934. if err != nil {
  935. return err
  936. }
  937. for _, m := range mounts {
  938. dest, err := container.GetResourcePath(m.Destination)
  939. if err != nil {
  940. return err
  941. }
  942. var stat os.FileInfo
  943. stat, err = os.Stat(m.Source)
  944. if err != nil {
  945. return err
  946. }
  947. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  948. return err
  949. }
  950. opts := "rbind,ro"
  951. if m.Writable {
  952. opts = "rbind,rw"
  953. }
  954. if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
  955. return err
  956. }
  957. }
  958. return nil
  959. }
  960. func killProcessDirectly(container *container.Container) error {
  961. if _, err := container.WaitStop(10 * time.Second); err != nil {
  962. // Ensure that we don't kill ourselves
  963. if pid := container.GetPID(); pid != 0 {
  964. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  965. if err := syscall.Kill(pid, 9); err != nil {
  966. if err != syscall.ESRCH {
  967. return err
  968. }
  969. logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
  970. }
  971. }
  972. }
  973. return nil
  974. }
  975. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
  976. device, err := devices.DeviceFromPath(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
  977. // if there was no error, return the device
  978. if err == nil {
  979. device.Path = deviceMapping.PathInContainer
  980. return append(devs, device), nil
  981. }
  982. // if the device is not a device node
  983. // try to see if it's a directory holding many devices
  984. if err == devices.ErrNotADevice {
  985. // check if it is a directory
  986. if src, e := os.Stat(deviceMapping.PathOnHost); e == nil && src.IsDir() {
  987. // mount the internal devices recursively
  988. filepath.Walk(deviceMapping.PathOnHost, func(dpath string, f os.FileInfo, e error) error {
  989. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  990. if e != nil {
  991. // ignore the device
  992. return nil
  993. }
  994. // add the device to userSpecified devices
  995. childDevice.Path = strings.Replace(dpath, deviceMapping.PathOnHost, deviceMapping.PathInContainer, 1)
  996. devs = append(devs, childDevice)
  997. return nil
  998. })
  999. }
  1000. }
  1001. if len(devs) > 0 {
  1002. return devs, nil
  1003. }
  1004. return devs, derr.ErrorCodeDeviceInfo.WithArgs(deviceMapping.PathOnHost, err)
  1005. }
  1006. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  1007. if len(userDevices) == 0 {
  1008. return defaultDevices
  1009. }
  1010. paths := map[string]*configs.Device{}
  1011. for _, d := range userDevices {
  1012. paths[d.Path] = d
  1013. }
  1014. var devs []*configs.Device
  1015. for _, d := range defaultDevices {
  1016. if _, defined := paths[d.Path]; !defined {
  1017. devs = append(devs, d)
  1018. }
  1019. }
  1020. return append(devs, userDevices...)
  1021. }
  1022. func detachMounted(path string) error {
  1023. return syscall.Unmount(path, syscall.MNT_DETACH)
  1024. }
  1025. func isLinkable(child *container.Container) bool {
  1026. // A container is linkable only if it belongs to the default network
  1027. _, ok := child.NetworkSettings.Networks["bridge"]
  1028. return ok
  1029. }