container_operations_unix.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. if c.HostConfig.UsernsMode.IsPrivate() {
  189. rootUID, rootGID := daemon.GetRemappedUIDGID()
  190. if rootUID != 0 {
  191. remappedRoot.UID = rootUID
  192. remappedRoot.GID = rootGID
  193. }
  194. }
  195. uidMap, gidMap := daemon.GetUIDGIDMaps()
  196. if !daemon.seccompEnabled {
  197. if c.SeccompProfile != "" && c.SeccompProfile != "unconfined" {
  198. return fmt.Errorf("Seccomp is not enabled in your kernel, cannot run a custom seccomp profile.")
  199. }
  200. logrus.Warn("Seccomp is not enabled in your kernel, running container without default profile.")
  201. c.SeccompProfile = "unconfined"
  202. }
  203. defaultCgroupParent := "/docker"
  204. if daemon.configStore.CgroupParent != "" {
  205. defaultCgroupParent = daemon.configStore.CgroupParent
  206. } else if daemon.usingSystemd() {
  207. defaultCgroupParent = "system.slice"
  208. }
  209. c.Command = &execdriver.Command{
  210. CommonCommand: execdriver.CommonCommand{
  211. ID: c.ID,
  212. MountLabel: c.GetMountLabel(),
  213. Network: en,
  214. ProcessConfig: processConfig,
  215. ProcessLabel: c.GetProcessLabel(),
  216. Rootfs: c.BaseFS,
  217. Resources: resources,
  218. WorkingDir: c.Config.WorkingDir,
  219. },
  220. AllowedDevices: allowedDevices,
  221. AppArmorProfile: c.AppArmorProfile,
  222. AutoCreatedDevices: autoCreatedDevices,
  223. CapAdd: c.HostConfig.CapAdd,
  224. CapDrop: c.HostConfig.CapDrop,
  225. CgroupParent: defaultCgroupParent,
  226. GIDMapping: gidMap,
  227. GroupAdd: c.HostConfig.GroupAdd,
  228. Ipc: ipc,
  229. OomScoreAdj: c.HostConfig.OomScoreAdj,
  230. Pid: pid,
  231. ReadonlyRootfs: c.HostConfig.ReadonlyRootfs,
  232. RemappedRoot: remappedRoot,
  233. SeccompProfile: c.SeccompProfile,
  234. UIDMapping: uidMap,
  235. UTS: uts,
  236. NoNewPrivileges: c.NoNewPrivileges,
  237. }
  238. if c.HostConfig.CgroupParent != "" {
  239. c.Command.CgroupParent = c.HostConfig.CgroupParent
  240. }
  241. return nil
  242. }
  243. // getSize returns the real size & virtual size of the container.
  244. func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
  245. var (
  246. sizeRw, sizeRootfs int64
  247. err error
  248. )
  249. if err := daemon.Mount(container); err != nil {
  250. logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
  251. return sizeRw, sizeRootfs
  252. }
  253. defer daemon.Unmount(container)
  254. sizeRw, err = container.RWLayer.Size()
  255. if err != nil {
  256. logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
  257. daemon.GraphDriverName(), container.ID, err)
  258. // FIXME: GetSize should return an error. Not changing it now in case
  259. // there is a side-effect.
  260. sizeRw = -1
  261. }
  262. if parent := container.RWLayer.Parent(); parent != nil {
  263. sizeRootfs, err = parent.Size()
  264. if err != nil {
  265. sizeRootfs = -1
  266. } else if sizeRw != -1 {
  267. sizeRootfs += sizeRw
  268. }
  269. }
  270. return sizeRw, sizeRootfs
  271. }
  272. // ConnectToNetwork connects a container to a network
  273. func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
  274. if !container.Running {
  275. if container.RemovalInProgress || container.Dead {
  276. return errRemovalContainer(container.ID)
  277. }
  278. if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
  279. return err
  280. }
  281. if endpointConfig != nil {
  282. container.NetworkSettings.Networks[idOrName] = endpointConfig
  283. }
  284. } else {
  285. if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
  286. return err
  287. }
  288. }
  289. if err := container.ToDiskLocking(); err != nil {
  290. return fmt.Errorf("Error saving container to disk: %v", err)
  291. }
  292. return nil
  293. }
  294. // DisconnectFromNetwork disconnects container from network n.
  295. func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
  296. if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
  297. return runconfig.ErrConflictHostNetwork
  298. }
  299. if !container.Running {
  300. if container.RemovalInProgress || container.Dead {
  301. return errRemovalContainer(container.ID)
  302. }
  303. if _, ok := container.NetworkSettings.Networks[n.Name()]; ok {
  304. delete(container.NetworkSettings.Networks, n.Name())
  305. } else {
  306. return fmt.Errorf("container %s is not connected to the network %s", container.ID, n.Name())
  307. }
  308. } else {
  309. if err := disconnectFromNetwork(container, n, false); err != nil {
  310. return err
  311. }
  312. }
  313. if err := container.ToDiskLocking(); err != nil {
  314. return fmt.Errorf("Error saving container to disk: %v", err)
  315. }
  316. attributes := map[string]string{
  317. "container": container.ID,
  318. }
  319. daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
  320. return nil
  321. }
  322. // called from the libcontainer pre-start hook to set the network
  323. // namespace configuration linkage to the libnetwork "sandbox" entity
  324. func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
  325. path := fmt.Sprintf("/proc/%d/ns/net", pid)
  326. var sandbox libnetwork.Sandbox
  327. search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
  328. daemon.netController.WalkSandboxes(search)
  329. if sandbox == nil {
  330. return fmt.Errorf("error locating sandbox id %s: no sandbox found", containerID)
  331. }
  332. return sandbox.SetKey(path)
  333. }
  334. func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
  335. containerID := container.HostConfig.IpcMode.Container()
  336. c, err := daemon.GetContainer(containerID)
  337. if err != nil {
  338. return nil, err
  339. }
  340. if !c.IsRunning() {
  341. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  342. }
  343. if c.IsRestarting() {
  344. return nil, errContainerIsRestarting(container.ID)
  345. }
  346. return c, nil
  347. }
  348. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  349. rootUID, rootGID := daemon.GetRemappedUIDGID()
  350. if !c.HasMountFor("/dev/shm") {
  351. shmPath, err := c.ShmResourcePath()
  352. if err != nil {
  353. return err
  354. }
  355. if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
  356. return err
  357. }
  358. shmSize := container.DefaultSHMSize
  359. if c.HostConfig.ShmSize != 0 {
  360. shmSize = c.HostConfig.ShmSize
  361. }
  362. shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
  363. if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  364. return fmt.Errorf("mounting shm tmpfs: %s", err)
  365. }
  366. if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
  367. return err
  368. }
  369. }
  370. return nil
  371. }
  372. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  373. mounts, err := daemon.setupMounts(container)
  374. if err != nil {
  375. return err
  376. }
  377. for _, m := range mounts {
  378. dest, err := container.GetResourcePath(m.Destination)
  379. if err != nil {
  380. return err
  381. }
  382. var stat os.FileInfo
  383. stat, err = os.Stat(m.Source)
  384. if err != nil {
  385. return err
  386. }
  387. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  388. return err
  389. }
  390. opts := "rbind,ro"
  391. if m.Writable {
  392. opts = "rbind,rw"
  393. }
  394. if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
  395. return err
  396. }
  397. }
  398. return nil
  399. }
  400. func killProcessDirectly(container *container.Container) error {
  401. if _, err := container.WaitStop(10 * time.Second); err != nil {
  402. // Ensure that we don't kill ourselves
  403. if pid := container.GetPID(); pid != 0 {
  404. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  405. if err := syscall.Kill(pid, 9); err != nil {
  406. if err != syscall.ESRCH {
  407. return err
  408. }
  409. e := errNoSuchProcess{pid, 9}
  410. logrus.Debug(e)
  411. return e
  412. }
  413. }
  414. }
  415. return nil
  416. }
  417. func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
  418. resolvedPathOnHost := deviceMapping.PathOnHost
  419. // check if it is a symbolic link
  420. if src, e := os.Lstat(deviceMapping.PathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
  421. if linkedPathOnHost, e := os.Readlink(deviceMapping.PathOnHost); e == nil {
  422. resolvedPathOnHost = linkedPathOnHost
  423. }
  424. }
  425. device, err := devices.DeviceFromPath(resolvedPathOnHost, deviceMapping.CgroupPermissions)
  426. // if there was no error, return the device
  427. if err == nil {
  428. device.Path = deviceMapping.PathInContainer
  429. return append(devs, device), nil
  430. }
  431. // if the device is not a device node
  432. // try to see if it's a directory holding many devices
  433. if err == devices.ErrNotADevice {
  434. // check if it is a directory
  435. if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
  436. // mount the internal devices recursively
  437. filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
  438. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  439. if e != nil {
  440. // ignore the device
  441. return nil
  442. }
  443. // add the device to userSpecified devices
  444. childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, deviceMapping.PathInContainer, 1)
  445. devs = append(devs, childDevice)
  446. return nil
  447. })
  448. }
  449. }
  450. if len(devs) > 0 {
  451. return devs, nil
  452. }
  453. return devs, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
  454. }
  455. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  456. if len(userDevices) == 0 {
  457. return defaultDevices
  458. }
  459. paths := map[string]*configs.Device{}
  460. for _, d := range userDevices {
  461. paths[d.Path] = d
  462. }
  463. var devs []*configs.Device
  464. for _, d := range defaultDevices {
  465. if _, defined := paths[d.Path]; !defined {
  466. devs = append(devs, d)
  467. }
  468. }
  469. return append(devs, userDevices...)
  470. }
  471. func detachMounted(path string) error {
  472. return syscall.Unmount(path, syscall.MNT_DETACH)
  473. }
  474. func isLinkable(child *container.Container) bool {
  475. // A container is linkable only if it belongs to the default network
  476. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  477. return ok
  478. }
  479. func errRemovalContainer(containerID string) error {
  480. return fmt.Errorf("Container %s is marked for removal and cannot be connected or disconnected to the network", containerID)
  481. }