volumes_unix.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //go:build !windows
  2. // +build !windows
  3. package daemon // import "github.com/docker/docker/daemon"
  4. import (
  5. "fmt"
  6. "os"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. mounttypes "github.com/docker/docker/api/types/mount"
  11. "github.com/docker/docker/container"
  12. "github.com/docker/docker/pkg/fileutils"
  13. volumemounts "github.com/docker/docker/volume/mounts"
  14. "github.com/moby/sys/mount"
  15. )
  16. // setupMounts iterates through each of the mount points for a container and
  17. // calls Setup() on each. It also looks to see if is a network mount such as
  18. // /etc/resolv.conf, and if it is not, appends it to the array of mounts.
  19. func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
  20. var mounts []container.Mount
  21. // TODO: tmpfs mounts should be part of Mountpoints
  22. tmpfsMounts := make(map[string]bool)
  23. tmpfsMountInfo, err := c.TmpfsMounts()
  24. if err != nil {
  25. return nil, err
  26. }
  27. for _, m := range tmpfsMountInfo {
  28. tmpfsMounts[m.Destination] = true
  29. }
  30. for _, m := range c.MountPoints {
  31. if tmpfsMounts[m.Destination] {
  32. continue
  33. }
  34. if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
  35. return nil, err
  36. }
  37. // If the daemon is being shutdown, we should not let a container start if it is trying to
  38. // mount the socket the daemon is listening on. During daemon shutdown, the socket
  39. // (/var/run/docker.sock by default) doesn't exist anymore causing the call to m.Setup to
  40. // create at directory instead. This in turn will prevent the daemon to restart.
  41. checkfunc := func(m *volumemounts.MountPoint) error {
  42. if _, exist := daemon.hosts[m.Source]; exist && daemon.IsShuttingDown() {
  43. return fmt.Errorf("Could not mount %q to container while the daemon is shutting down", m.Source)
  44. }
  45. return nil
  46. }
  47. path, err := m.Setup(c.MountLabel, daemon.idMapping.RootPair(), checkfunc)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if !c.TrySetNetworkMount(m.Destination, path) {
  52. mnt := container.Mount{
  53. Source: path,
  54. Destination: m.Destination,
  55. Writable: m.RW,
  56. Propagation: string(m.Propagation),
  57. }
  58. if m.Spec.Type == mounttypes.TypeBind && m.Spec.BindOptions != nil {
  59. mnt.NonRecursive = m.Spec.BindOptions.NonRecursive
  60. }
  61. if m.Volume != nil {
  62. attributes := map[string]string{
  63. "driver": m.Volume.DriverName(),
  64. "container": c.ID,
  65. "destination": m.Destination,
  66. "read/write": strconv.FormatBool(m.RW),
  67. "propagation": string(m.Propagation),
  68. }
  69. daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
  70. }
  71. mounts = append(mounts, mnt)
  72. }
  73. }
  74. mounts = sortMounts(mounts)
  75. netMounts := c.NetworkMounts()
  76. // if we are going to mount any of the network files from container
  77. // metadata, the ownership must be set properly for potential container
  78. // remapped root (user namespaces)
  79. rootIDs := daemon.idMapping.RootPair()
  80. for _, mnt := range netMounts {
  81. // we should only modify ownership of network files within our own container
  82. // metadata repository. If the user specifies a mount path external, it is
  83. // up to the user to make sure the file has proper ownership for userns
  84. if strings.Index(mnt.Source, daemon.repository) == 0 {
  85. if err := os.Chown(mnt.Source, rootIDs.UID, rootIDs.GID); err != nil {
  86. return nil, err
  87. }
  88. }
  89. }
  90. return append(mounts, netMounts...), nil
  91. }
  92. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  93. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  94. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  95. func sortMounts(m []container.Mount) []container.Mount {
  96. sort.Sort(mounts(m))
  97. return m
  98. }
  99. // setBindModeIfNull is platform specific processing to ensure the
  100. // shared mode is set to 'z' if it is null. This is called in the case
  101. // of processing a named volume and not a typical bind.
  102. func setBindModeIfNull(bind *volumemounts.MountPoint) {
  103. if bind.Mode == "" {
  104. bind.Mode = "z"
  105. }
  106. }
  107. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  108. mounts, err := daemon.setupMounts(container)
  109. if err != nil {
  110. return err
  111. }
  112. for _, m := range mounts {
  113. dest, err := container.GetResourcePath(m.Destination)
  114. if err != nil {
  115. return err
  116. }
  117. var stat os.FileInfo
  118. stat, err = os.Stat(m.Source)
  119. if err != nil {
  120. return err
  121. }
  122. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  123. return err
  124. }
  125. bindMode := "rbind"
  126. if m.NonRecursive {
  127. bindMode = "bind"
  128. }
  129. writeMode := "ro"
  130. if m.Writable {
  131. writeMode = "rw"
  132. }
  133. // mountVolumes() seems to be called for temporary mounts
  134. // outside the container. Soon these will be unmounted with
  135. // lazy unmount option and given we have mounted the rbind,
  136. // all the submounts will propagate if these are shared. If
  137. // daemon is running in host namespace and has / as shared
  138. // then these unmounts will propagate and unmount original
  139. // mount as well. So make all these mounts rprivate.
  140. // Do not use propagation property of volume as that should
  141. // apply only when mounting happens inside the container.
  142. opts := strings.Join([]string{bindMode, writeMode, "rprivate"}, ",")
  143. if err := mount.Mount(m.Source, dest, "", opts); err != nil {
  144. return err
  145. }
  146. }
  147. return nil
  148. }