container_operations_unix.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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 !hasUserDefinedIPAddress(epConfig) {
  570. return nil
  571. }
  572. _, _, nwIPv4Configs, nwIPv6Configs := n.Info().IpamConfig()
  573. for _, s := range []struct {
  574. ipConfigured bool
  575. subnetConfigs []*libnetwork.IpamConf
  576. }{
  577. {
  578. ipConfigured: len(epConfig.IPAMConfig.IPv4Address) > 0,
  579. subnetConfigs: nwIPv4Configs,
  580. },
  581. {
  582. ipConfigured: len(epConfig.IPAMConfig.IPv6Address) > 0,
  583. subnetConfigs: nwIPv6Configs,
  584. },
  585. } {
  586. if s.ipConfigured {
  587. foundSubnet := false
  588. for _, cfg := range s.subnetConfigs {
  589. if len(cfg.PreferredPool) > 0 {
  590. foundSubnet = true
  591. break
  592. }
  593. }
  594. if !foundSubnet {
  595. return runconfig.ErrUnsupportedNetworkNoSubnetAndIP
  596. }
  597. }
  598. }
  599. return nil
  600. }
  601. // cleanOperationalData resets the operational data from the passed endpoint settings
  602. func cleanOperationalData(es *networktypes.EndpointSettings) {
  603. es.EndpointID = ""
  604. es.Gateway = ""
  605. es.IPAddress = ""
  606. es.IPPrefixLen = 0
  607. es.IPv6Gateway = ""
  608. es.GlobalIPv6Address = ""
  609. es.GlobalIPv6PrefixLen = 0
  610. es.MacAddress = ""
  611. }
  612. func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrName string, updateSettings bool) (libnetwork.Network, error) {
  613. if container.HostConfig.NetworkMode.IsContainer() {
  614. return nil, runconfig.ErrConflictSharedNetwork
  615. }
  616. if containertypes.NetworkMode(idOrName).IsBridge() &&
  617. daemon.configStore.DisableBridge {
  618. container.Config.NetworkDisabled = true
  619. return nil, nil
  620. }
  621. n, err := daemon.FindNetwork(idOrName)
  622. if err != nil {
  623. return nil, err
  624. }
  625. if updateSettings {
  626. if err := daemon.updateNetworkSettings(container, n); err != nil {
  627. return nil, err
  628. }
  629. }
  630. return n, nil
  631. }
  632. // ConnectToNetwork connects a container to a network
  633. func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  634. if !container.Running {
  635. if container.RemovalInProgress || container.Dead {
  636. return derr.ErrorCodeRemovalContainer.WithArgs(container.ID)
  637. }
  638. if _, err := daemon.updateNetworkConfig(container, idOrName, true); err != nil {
  639. return err
  640. }
  641. } else {
  642. if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  643. return err
  644. }
  645. }
  646. if err := container.ToDiskLocking(); err != nil {
  647. return fmt.Errorf("Error saving container to disk: %v", err)
  648. }
  649. return nil
  650. }
  651. func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
  652. n, err := daemon.updateNetworkConfig(container, idOrName, updateSettings)
  653. if err != nil {
  654. return err
  655. }
  656. if n == nil {
  657. return nil
  658. }
  659. if !containertypes.NetworkMode(idOrName).IsUserDefined() && hasUserDefinedIPAddress(endpointConfig) {
  660. return runconfig.ErrUnsupportedNetworkAndIP
  661. }
  662. if !containertypes.NetworkMode(idOrName).IsUserDefined() && len(endpointConfig.Aliases) > 0 {
  663. return runconfig.ErrUnsupportedNetworkAndAlias
  664. }
  665. controller := daemon.netController
  666. if err := validateNetworkingConfig(n, endpointConfig); err != nil {
  667. return err
  668. }
  669. if endpointConfig != nil {
  670. container.NetworkSettings.Networks[n.Name()] = endpointConfig
  671. }
  672. ep, err := container.GetEndpointInNetwork(n)
  673. if err == nil {
  674. return fmt.Errorf("Conflict. A container with name %q is already connected to network %s.", strings.TrimPrefix(container.Name, "/"), idOrName)
  675. }
  676. if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
  677. return err
  678. }
  679. createOptions, err := container.BuildCreateEndpointOptions(n)
  680. if err != nil {
  681. return err
  682. }
  683. endpointName := strings.TrimPrefix(container.Name, "/")
  684. ep, err = n.CreateEndpoint(endpointName, createOptions...)
  685. if err != nil {
  686. return err
  687. }
  688. defer func() {
  689. if err != nil {
  690. if e := ep.Delete(false); e != nil {
  691. logrus.Warnf("Could not rollback container connection to network %s", idOrName)
  692. }
  693. }
  694. }()
  695. if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
  696. return err
  697. }
  698. sb := daemon.getNetworkSandbox(container)
  699. if sb == nil {
  700. options, err := daemon.buildSandboxOptions(container, n)
  701. if err != nil {
  702. return err
  703. }
  704. sb, err = controller.NewSandbox(container.ID, options...)
  705. if err != nil {
  706. return err
  707. }
  708. container.UpdateSandboxNetworkSettings(sb)
  709. }
  710. joinOptions, err := container.BuildJoinOptions(n)
  711. if err != nil {
  712. return err
  713. }
  714. if err := ep.Join(sb, joinOptions...); err != nil {
  715. return err
  716. }
  717. if err := container.UpdateJoinInfo(n, ep); err != nil {
  718. return derr.ErrorCodeJoinInfo.WithArgs(err)
  719. }
  720. daemon.LogNetworkEventWithAttributes(n, "connect", map[string]string{"container": container.ID})
  721. return nil
  722. }
  723. // ForceEndpointDelete deletes an endpoing from a network forcefully
  724. func (daemon *Daemon) ForceEndpointDelete(name string, n libnetwork.Network) error {
  725. ep, err := n.EndpointByName(name)
  726. if err != nil {
  727. return err
  728. }
  729. return ep.Delete(true)
  730. }
  731. // DisconnectFromNetwork disconnects container from network n.
  732. func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  733. if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  734. return runconfig.ErrConflictHostNetwork
  735. }
  736. if !container.Running {
  737. if container.RemovalInProgress || container.Dead {
  738. return derr.ErrorCodeRemovalContainer.WithArgs(container.ID)
  739. }
  740. if _, ok := container.NetworkSettings.Networks[n.Name()]; ok {
  741. delete(container.NetworkSettings.Networks, n.Name())
  742. } else {
  743. return fmt.Errorf("container %s is not connected to the network %s", container.ID, n.Name())
  744. }
  745. } else {
  746. if err := disconnectFromNetwork(container, n, false); err != nil {
  747. return err
  748. }
  749. }
  750. if err := container.ToDiskLocking(); err != nil {
  751. return fmt.Errorf("Error saving container to disk: %v", err)
  752. }
  753. attributes := map[string]string{
  754. "container": container.ID,
  755. }
  756. daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
  757. return nil
  758. }
  759. func disconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  760. var (
  761. ep libnetwork.Endpoint
  762. sbox libnetwork.Sandbox
  763. )
  764. s := func(current libnetwork.Endpoint) bool {
  765. epInfo := current.Info()
  766. if epInfo == nil {
  767. return false
  768. }
  769. if sb := epInfo.Sandbox(); sb != nil {
  770. if sb.ContainerID() == container.ID {
  771. ep = current
  772. sbox = sb
  773. return true
  774. }
  775. }
  776. return false
  777. }
  778. n.WalkEndpoints(s)
  779. if ep == nil && force {
  780. epName := strings.TrimPrefix(container.Name, "/")
  781. ep, err := n.EndpointByName(epName)
  782. if err != nil {
  783. return err
  784. }
  785. return ep.Delete(force)
  786. }
  787. if ep == nil {
  788. return fmt.Errorf("container %s is not connected to the network", container.ID)
  789. }
  790. if err := ep.Leave(sbox); err != nil {
  791. return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
  792. }
  793. if err := ep.Delete(false); err != nil {
  794. return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
  795. }
  796. delete(container.NetworkSettings.Networks, n.Name())
  797. return nil
  798. }
  799. func (daemon *Daemon) initializeNetworking(container *container.Container) error {
  800. var err error
  801. if container.HostConfig.NetworkMode.IsContainer() {
  802. // we need to get the hosts files from the container to join
  803. nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
  804. if err != nil {
  805. return err
  806. }
  807. container.HostnamePath = nc.HostnamePath
  808. container.HostsPath = nc.HostsPath
  809. container.ResolvConfPath = nc.ResolvConfPath
  810. container.Config.Hostname = nc.Config.Hostname
  811. container.Config.Domainname = nc.Config.Domainname
  812. return nil
  813. }
  814. if container.HostConfig.NetworkMode.IsHost() {
  815. container.Config.Hostname, err = os.Hostname()
  816. if err != nil {
  817. return err
  818. }
  819. parts := strings.SplitN(container.Config.Hostname, ".", 2)
  820. if len(parts) > 1 {
  821. container.Config.Hostname = parts[0]
  822. container.Config.Domainname = parts[1]
  823. }
  824. }
  825. if err := daemon.allocateNetwork(container); err != nil {
  826. return err
  827. }
  828. return container.BuildHostnameFile()
  829. }
  830. // called from the libcontainer pre-start hook to set the network
  831. // namespace configuration linkage to the libnetwork "sandbox" entity
  832. func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
  833. path := fmt.Sprintf("/proc/%d/ns/net", pid)
  834. var sandbox libnetwork.Sandbox
  835. search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
  836. daemon.netController.WalkSandboxes(search)
  837. if sandbox == nil {
  838. return derr.ErrorCodeNoSandbox.WithArgs(containerID, "no sandbox found")
  839. }
  840. return sandbox.SetKey(path)
  841. }
  842. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  843. containerID := container.HostConfig.IpcMode.Container()
  844. c, err := daemon.GetContainer(containerID)
  845. if err != nil {
  846. return nil, err
  847. }
  848. if !c.IsRunning() {
  849. return nil, derr.ErrorCodeIPCRunning.WithArgs(containerID)
  850. }
  851. return c, nil
  852. }
  853. func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
  854. nc, err := daemon.GetContainer(connectedContainerID)
  855. if err != nil {
  856. return nil, err
  857. }
  858. if containerID == nc.ID {
  859. return nil, derr.ErrorCodeJoinSelf
  860. }
  861. if !nc.IsRunning() {
  862. return nil, derr.ErrorCodeJoinRunning.WithArgs(connectedContainerID)
  863. }
  864. return nc, nil
  865. }
  866. func (daemon *Daemon) releaseNetwork(container *container.Container) {
  867. if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
  868. return
  869. }
  870. sid := container.NetworkSettings.SandboxID
  871. settings := container.NetworkSettings.Networks
  872. if sid == "" || len(settings) == 0 {
  873. return
  874. }
  875. var networks []libnetwork.Network
  876. for n, epSettings := range settings {
  877. if nw, err := daemon.FindNetwork(n); err == nil {
  878. networks = append(networks, nw)
  879. }
  880. cleanOperationalData(epSettings)
  881. }
  882. sb, err := daemon.netController.SandboxByID(sid)
  883. if err != nil {
  884. logrus.Errorf("error locating sandbox id %s: %v", sid, err)
  885. return
  886. }
  887. if err := sb.Delete(); err != nil {
  888. logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
  889. }
  890. attributes := map[string]string{
  891. "container": container.ID,
  892. }
  893. for _, nw := range networks {
  894. daemon.LogNetworkEventWithAttributes(nw, "disconnect", attributes)
  895. }
  896. }
  897. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  898. rootUID, rootGID := daemon.GetRemappedUIDGID()
  899. if !c.HasMountFor("/dev/shm") {
  900. shmPath, err := c.ShmResourcePath()
  901. if err != nil {
  902. return err
  903. }
  904. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  905. return err
  906. }
  907. shmSize := container.DefaultSHMSize
  908. if c.HostConfig.ShmSize != 0 {
  909. shmSize = c.HostConfig.ShmSize
  910. }
  911. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  912. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  913. return fmt.Errorf("mounting shm tmpfs: %s", err)
  914. }
  915. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  916. return err
  917. }
  918. }
  919. if !c.HasMountFor("/dev/mqueue") {
  920. mqueuePath, err := c.MqueueResourcePath()
  921. if err != nil {
  922. return err
  923. }
  924. if err := idtools.MkdirAllAs(mqueuePath, 0700, rootUID, rootGID); err != nil {
  925. return err
  926. }
  927. if err := syscall.Mount("mqueue", mqueuePath, "mqueue", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), ""); err != nil {
  928. return fmt.Errorf("mounting mqueue mqueue : %s", err)
  929. }
  930. }
  931. return nil
  932. }
  933. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  934. mounts, err := daemon.setupMounts(container)
  935. if err != nil {
  936. return err
  937. }
  938. for _, m := range mounts {
  939. dest, err := container.GetResourcePath(m.Destination)
  940. if err != nil {
  941. return err
  942. }
  943. var stat os.FileInfo
  944. stat, err = os.Stat(m.Source)
  945. if err != nil {
  946. return err
  947. }
  948. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  949. return err
  950. }
  951. opts := "rbind,ro"
  952. if m.Writable {
  953. opts = "rbind,rw"
  954. }
  955. if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
  956. return err
  957. }
  958. }
  959. return nil
  960. }
  961. func killProcessDirectly(container *container.Container) error {
  962. if _, err := container.WaitStop(10 * time.Second); err != nil {
  963. // Ensure that we don't kill ourselves
  964. if pid := container.GetPID(); pid != 0 {
  965. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  966. if err := syscall.Kill(pid, 9); err != nil {
  967. if err != syscall.ESRCH {
  968. return err
  969. }
  970. logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
  971. }
  972. }
  973. }
  974. return nil
  975. }
  976. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
  977. device, err := devices.DeviceFromPath(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
  978. // if there was no error, return the device
  979. if err == nil {
  980. device.Path = deviceMapping.PathInContainer
  981. return append(devs, device), nil
  982. }
  983. // if the device is not a device node
  984. // try to see if it's a directory holding many devices
  985. if err == devices.ErrNotADevice {
  986. // check if it is a directory
  987. if src, e := os.Stat(deviceMapping.PathOnHost); e == nil && src.IsDir() {
  988. // mount the internal devices recursively
  989. filepath.Walk(deviceMapping.PathOnHost, func(dpath string, f os.FileInfo, e error) error {
  990. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  991. if e != nil {
  992. // ignore the device
  993. return nil
  994. }
  995. // add the device to userSpecified devices
  996. childDevice.Path = strings.Replace(dpath, deviceMapping.PathOnHost, deviceMapping.PathInContainer, 1)
  997. devs = append(devs, childDevice)
  998. return nil
  999. })
  1000. }
  1001. }
  1002. if len(devs) > 0 {
  1003. return devs, nil
  1004. }
  1005. return devs, derr.ErrorCodeDeviceInfo.WithArgs(deviceMapping.PathOnHost, err)
  1006. }
  1007. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  1008. if len(userDevices) == 0 {
  1009. return defaultDevices
  1010. }
  1011. paths := map[string]*configs.Device{}
  1012. for _, d := range userDevices {
  1013. paths[d.Path] = d
  1014. }
  1015. var devs []*configs.Device
  1016. for _, d := range defaultDevices {
  1017. if _, defined := paths[d.Path]; !defined {
  1018. devs = append(devs, d)
  1019. }
  1020. }
  1021. return append(devs, userDevices...)
  1022. }
  1023. func detachMounted(path string) error {
  1024. return syscall.Unmount(path, syscall.MNT_DETACH)
  1025. }
  1026. func isLinkable(child *container.Container) bool {
  1027. // A container is linkable only if it belongs to the default network
  1028. _, ok := child.NetworkSettings.Networks["bridge"]
  1029. return ok
  1030. }