volumes_unix.go 5.2 KB

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