container_operations_unix.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "time"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/docker/container"
  13. "github.com/docker/docker/daemon/execdriver"
  14. "github.com/docker/docker/daemon/links"
  15. "github.com/docker/docker/pkg/fileutils"
  16. "github.com/docker/docker/pkg/idtools"
  17. "github.com/docker/docker/pkg/mount"
  18. "github.com/docker/docker/pkg/stringid"
  19. "github.com/docker/docker/runconfig"
  20. containertypes "github.com/docker/engine-api/types/container"
  21. networktypes "github.com/docker/engine-api/types/network"
  22. "github.com/docker/go-units"
  23. "github.com/docker/libnetwork"
  24. "github.com/opencontainers/runc/libcontainer/configs"
  25. "github.com/opencontainers/runc/libcontainer/devices"
  26. "github.com/opencontainers/runc/libcontainer/label"
  27. )
  28. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  29. var env []string
  30. children := daemon.children(container)
  31. bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  32. if bridgeSettings == nil {
  33. return nil, nil
  34. }
  35. for linkAlias, child := range children {
  36. if !child.IsRunning() {
  37. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  38. }
  39. childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  40. if childBridgeSettings == nil {
  41. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  42. }
  43. link := links.NewLink(
  44. bridgeSettings.IPAddress,
  45. childBridgeSettings.IPAddress,
  46. linkAlias,
  47. child.Config.Env,
  48. child.Config.ExposedPorts,
  49. )
  50. for _, envVar := range link.ToEnv() {
  51. env = append(env, envVar)
  52. }
  53. }
  54. return env, nil
  55. }
  56. func (daemon *Daemon) populateCommand(c *container.Container, env []string) error {
  57. var en *execdriver.Network
  58. if !c.Config.NetworkDisabled {
  59. en = &execdriver.Network{}
  60. if !daemon.execDriver.SupportsHooks() || c.HostConfig.NetworkMode.IsHost() {
  61. en.NamespacePath = c.NetworkSettings.SandboxKey
  62. }
  63. if c.HostConfig.NetworkMode.IsContainer() {
  64. nc, err := daemon.getNetworkedContainer(c.ID, c.HostConfig.NetworkMode.ConnectedContainer())
  65. if err != nil {
  66. return err
  67. }
  68. en.ContainerID = nc.ID
  69. }
  70. }
  71. ipc := &execdriver.Ipc{}
  72. var err error
  73. c.ShmPath, err = c.ShmResourcePath()
  74. if err != nil {
  75. return err
  76. }
  77. if c.HostConfig.IpcMode.IsContainer() {
  78. ic, err := daemon.getIpcContainer(c)
  79. if err != nil {
  80. return err
  81. }
  82. ipc.ContainerID = ic.ID
  83. c.ShmPath = ic.ShmPath
  84. } else {
  85. ipc.HostIpc = c.HostConfig.IpcMode.IsHost()
  86. if ipc.HostIpc {
  87. if _, err := os.Stat("/dev/shm"); err != nil {
  88. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  89. }
  90. c.ShmPath = "/dev/shm"
  91. }
  92. }
  93. pid := &execdriver.Pid{}
  94. pid.HostPid = c.HostConfig.PidMode.IsHost()
  95. uts := &execdriver.UTS{
  96. HostUTS: c.HostConfig.UTSMode.IsHost(),
  97. }
  98. // Build lists of devices allowed and created within the container.
  99. var userSpecifiedDevices []*configs.Device
  100. for _, deviceMapping := range c.HostConfig.Devices {
  101. devs, err := getDevicesFromPath(deviceMapping)
  102. if err != nil {
  103. return err
  104. }
  105. userSpecifiedDevices = append(userSpecifiedDevices, devs...)
  106. }
  107. allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
  108. autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
  109. var rlimits []*units.Rlimit
  110. ulimits := c.HostConfig.Ulimits
  111. // Merge ulimits with daemon defaults
  112. ulIdx := make(map[string]*units.Ulimit)
  113. for _, ul := range ulimits {
  114. ulIdx[ul.Name] = ul
  115. }
  116. for name, ul := range daemon.configStore.Ulimits {
  117. if _, exists := ulIdx[name]; !exists {
  118. ulimits = append(ulimits, ul)
  119. }
  120. }
  121. weightDevices, err := getBlkioWeightDevices(c.HostConfig)
  122. if err != nil {
  123. return err
  124. }
  125. readBpsDevice, err := getBlkioReadBpsDevices(c.HostConfig)
  126. if err != nil {
  127. return err
  128. }
  129. writeBpsDevice, err := getBlkioWriteBpsDevices(c.HostConfig)
  130. if err != nil {
  131. return err
  132. }
  133. readIOpsDevice, err := getBlkioReadIOpsDevices(c.HostConfig)
  134. if err != nil {
  135. return err
  136. }
  137. writeIOpsDevice, err := getBlkioWriteIOpsDevices(c.HostConfig)
  138. if err != nil {
  139. return err
  140. }
  141. for _, limit := range ulimits {
  142. rl, err := limit.GetRlimit()
  143. if err != nil {
  144. return err
  145. }
  146. rlimits = append(rlimits, rl)
  147. }
  148. resources := &execdriver.Resources{
  149. CommonResources: execdriver.CommonResources{
  150. Memory: c.HostConfig.Memory,
  151. MemoryReservation: c.HostConfig.MemoryReservation,
  152. CPUShares: c.HostConfig.CPUShares,
  153. BlkioWeight: c.HostConfig.BlkioWeight,
  154. },
  155. MemorySwap: c.HostConfig.MemorySwap,
  156. KernelMemory: c.HostConfig.KernelMemory,
  157. CpusetCpus: c.HostConfig.CpusetCpus,
  158. CpusetMems: c.HostConfig.CpusetMems,
  159. CPUPeriod: c.HostConfig.CPUPeriod,
  160. CPUQuota: c.HostConfig.CPUQuota,
  161. Rlimits: rlimits,
  162. BlkioWeightDevice: weightDevices,
  163. BlkioThrottleReadBpsDevice: readBpsDevice,
  164. BlkioThrottleWriteBpsDevice: writeBpsDevice,
  165. BlkioThrottleReadIOpsDevice: readIOpsDevice,
  166. BlkioThrottleWriteIOpsDevice: writeIOpsDevice,
  167. PidsLimit: c.HostConfig.PidsLimit,
  168. MemorySwappiness: -1,
  169. }
  170. if c.HostConfig.OomKillDisable != nil {
  171. resources.OomKillDisable = *c.HostConfig.OomKillDisable
  172. }
  173. if c.HostConfig.MemorySwappiness != nil {
  174. resources.MemorySwappiness = *c.HostConfig.MemorySwappiness
  175. }
  176. processConfig := execdriver.ProcessConfig{
  177. CommonProcessConfig: execdriver.CommonProcessConfig{
  178. Entrypoint: c.Path,
  179. Arguments: c.Args,
  180. Tty: c.Config.Tty,
  181. },
  182. Privileged: c.HostConfig.Privileged,
  183. User: c.Config.User,
  184. }
  185. processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
  186. processConfig.Env = env
  187. remappedRoot := &execdriver.User{}
  188. rootUID, rootGID := daemon.GetRemappedUIDGID()
  189. if rootUID != 0 {
  190. remappedRoot.UID = rootUID
  191. remappedRoot.GID = rootGID
  192. }
  193. uidMap, gidMap := daemon.GetUIDGIDMaps()
  194. if !daemon.seccompEnabled {
  195. if c.SeccompProfile != "" && c.SeccompProfile != "unconfined" {
  196. return fmt.Errorf("Seccomp is not enabled in your kernel, cannot run a custom seccomp profile.")
  197. }
  198. logrus.Warn("Seccomp is not enabled in your kernel, running container without default profile.")
  199. c.SeccompProfile = "unconfined"
  200. }
  201. defaultCgroupParent := "/docker"
  202. if daemon.configStore.CgroupParent != "" {
  203. defaultCgroupParent = daemon.configStore.CgroupParent
  204. } else if daemon.usingSystemd() {
  205. defaultCgroupParent = "system.slice"
  206. }
  207. c.Command = &execdriver.Command{
  208. CommonCommand: execdriver.CommonCommand{
  209. ID: c.ID,
  210. MountLabel: c.GetMountLabel(),
  211. Network: en,
  212. ProcessConfig: processConfig,
  213. ProcessLabel: c.GetProcessLabel(),
  214. Rootfs: c.BaseFS,
  215. Resources: resources,
  216. WorkingDir: c.Config.WorkingDir,
  217. },
  218. AllowedDevices: allowedDevices,
  219. AppArmorProfile: c.AppArmorProfile,
  220. AutoCreatedDevices: autoCreatedDevices,
  221. CapAdd: c.HostConfig.CapAdd,
  222. CapDrop: c.HostConfig.CapDrop,
  223. CgroupParent: defaultCgroupParent,
  224. GIDMapping: gidMap,
  225. GroupAdd: c.HostConfig.GroupAdd,
  226. Ipc: ipc,
  227. OomScoreAdj: c.HostConfig.OomScoreAdj,
  228. Pid: pid,
  229. ReadonlyRootfs: c.HostConfig.ReadonlyRootfs,
  230. RemappedRoot: remappedRoot,
  231. SeccompProfile: c.SeccompProfile,
  232. UIDMapping: uidMap,
  233. UTS: uts,
  234. NoNewPrivileges: c.NoNewPrivileges,
  235. }
  236. if c.HostConfig.CgroupParent != "" {
  237. c.Command.CgroupParent = c.HostConfig.CgroupParent
  238. }
  239. return nil
  240. }
  241. // getSize returns the real size & virtual size of the container.
  242. func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
  243. var (
  244. sizeRw, sizeRootfs int64
  245. err error
  246. )
  247. if err := daemon.Mount(container); err != nil {
  248. logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
  249. return sizeRw, sizeRootfs
  250. }
  251. defer daemon.Unmount(container)
  252. sizeRw, err = container.RWLayer.Size()
  253. if err != nil {
  254. logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
  255. daemon.GraphDriverName(), container.ID, err)
  256. // FIXME: GetSize should return an error. Not changing it now in case
  257. // there is a side-effect.
  258. sizeRw = -1
  259. }
  260. if parent := container.RWLayer.Parent(); parent != nil {
  261. sizeRootfs, err = parent.Size()
  262. if err != nil {
  263. sizeRootfs = -1
  264. } else if sizeRw != -1 {
  265. sizeRootfs += sizeRw
  266. }
  267. }
  268. return sizeRw, sizeRootfs
  269. }
  270. // ConnectToNetwork connects a container to a network
  271. func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  272. if !container.Running {
  273. if container.RemovalInProgress || container.Dead {
  274. return errRemovalContainer(container.ID)
  275. }
  276. if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
  277. return err
  278. }
  279. if endpointConfig != nil {
  280. container.NetworkSettings.Networks[idOrName] = endpointConfig
  281. }
  282. } else {
  283. if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  284. return err
  285. }
  286. }
  287. if err := container.ToDiskLocking(); err != nil {
  288. return fmt.Errorf("Error saving container to disk: %v", err)
  289. }
  290. return nil
  291. }
  292. // DisconnectFromNetwork disconnects container from network n.
  293. func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  294. if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  295. return runconfig.ErrConflictHostNetwork
  296. }
  297. if !container.Running {
  298. if container.RemovalInProgress || container.Dead {
  299. return errRemovalContainer(container.ID)
  300. }
  301. if _, ok := container.NetworkSettings.Networks[n.Name()]; ok {
  302. delete(container.NetworkSettings.Networks, n.Name())
  303. } else {
  304. return fmt.Errorf("container %s is not connected to the network %s", container.ID, n.Name())
  305. }
  306. } else {
  307. if err := disconnectFromNetwork(container, n, false); err != nil {
  308. return err
  309. }
  310. }
  311. if err := container.ToDiskLocking(); err != nil {
  312. return fmt.Errorf("Error saving container to disk: %v", err)
  313. }
  314. attributes := map[string]string{
  315. "container": container.ID,
  316. }
  317. daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
  318. return nil
  319. }
  320. // called from the libcontainer pre-start hook to set the network
  321. // namespace configuration linkage to the libnetwork "sandbox" entity
  322. func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
  323. path := fmt.Sprintf("/proc/%d/ns/net", pid)
  324. var sandbox libnetwork.Sandbox
  325. search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
  326. daemon.netController.WalkSandboxes(search)
  327. if sandbox == nil {
  328. return fmt.Errorf("error locating sandbox id %s: no sandbox found", containerID)
  329. }
  330. return sandbox.SetKey(path)
  331. }
  332. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  333. containerID := container.HostConfig.IpcMode.Container()
  334. c, err := daemon.GetContainer(containerID)
  335. if err != nil {
  336. return nil, err
  337. }
  338. if !c.IsRunning() {
  339. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  340. }
  341. if c.IsRestarting() {
  342. return nil, errContainerIsRestarting(container.ID)
  343. }
  344. return c, nil
  345. }
  346. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  347. rootUID, rootGID := daemon.GetRemappedUIDGID()
  348. if !c.HasMountFor("/dev/shm") {
  349. shmPath, err := c.ShmResourcePath()
  350. if err != nil {
  351. return err
  352. }
  353. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  354. return err
  355. }
  356. shmSize := container.DefaultSHMSize
  357. if c.HostConfig.ShmSize != 0 {
  358. shmSize = c.HostConfig.ShmSize
  359. }
  360. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  361. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  362. return fmt.Errorf("mounting shm tmpfs: %s", err)
  363. }
  364. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  365. return err
  366. }
  367. }
  368. return nil
  369. }
  370. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  371. mounts, err := daemon.setupMounts(container)
  372. if err != nil {
  373. return err
  374. }
  375. for _, m := range mounts {
  376. dest, err := container.GetResourcePath(m.Destination)
  377. if err != nil {
  378. return err
  379. }
  380. var stat os.FileInfo
  381. stat, err = os.Stat(m.Source)
  382. if err != nil {
  383. return err
  384. }
  385. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  386. return err
  387. }
  388. opts := "rbind,ro"
  389. if m.Writable {
  390. opts = "rbind,rw"
  391. }
  392. if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
  393. return err
  394. }
  395. }
  396. return nil
  397. }
  398. func killProcessDirectly(container *container.Container) error {
  399. if _, err := container.WaitStop(10 * time.Second); err != nil {
  400. // Ensure that we don't kill ourselves
  401. if pid := container.GetPID(); pid != 0 {
  402. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  403. if err := syscall.Kill(pid, 9); err != nil {
  404. if err != syscall.ESRCH {
  405. return err
  406. }
  407. e := errNoSuchProcess{pid, 9}
  408. logrus.Debug(e)
  409. return e
  410. }
  411. }
  412. }
  413. return nil
  414. }
  415. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
  416. resolvedPathOnHost := deviceMapping.PathOnHost
  417. // check if it is a symbolic link
  418. if src, e := os.Lstat(deviceMapping.PathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
  419. if linkedPathOnHost, e := os.Readlink(deviceMapping.PathOnHost); e == nil {
  420. resolvedPathOnHost = linkedPathOnHost
  421. }
  422. }
  423. device, err := devices.DeviceFromPath(resolvedPathOnHost, deviceMapping.CgroupPermissions)
  424. // if there was no error, return the device
  425. if err == nil {
  426. device.Path = deviceMapping.PathInContainer
  427. return append(devs, device), nil
  428. }
  429. // if the device is not a device node
  430. // try to see if it's a directory holding many devices
  431. if err == devices.ErrNotADevice {
  432. // check if it is a directory
  433. if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
  434. // mount the internal devices recursively
  435. filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
  436. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  437. if e != nil {
  438. // ignore the device
  439. return nil
  440. }
  441. // add the device to userSpecified devices
  442. childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, deviceMapping.PathInContainer, 1)
  443. devs = append(devs, childDevice)
  444. return nil
  445. })
  446. }
  447. }
  448. if len(devs) > 0 {
  449. return devs, nil
  450. }
  451. return devs, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
  452. }
  453. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  454. if len(userDevices) == 0 {
  455. return defaultDevices
  456. }
  457. paths := map[string]*configs.Device{}
  458. for _, d := range userDevices {
  459. paths[d.Path] = d
  460. }
  461. var devs []*configs.Device
  462. for _, d := range defaultDevices {
  463. if _, defined := paths[d.Path]; !defined {
  464. devs = append(devs, d)
  465. }
  466. }
  467. return append(devs, userDevices...)
  468. }
  469. func detachMounted(path string) error {
  470. return syscall.Unmount(path, syscall.MNT_DETACH)
  471. }
  472. func isLinkable(child *container.Container) bool {
  473. // A container is linkable only if it belongs to the default network
  474. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  475. return ok
  476. }
  477. func errRemovalContainer(containerID string) error {
  478. return fmt.Errorf("Container %s is marked for removal and cannot be connected or disconnected to the network", containerID)
  479. }