volumes_unix.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "fmt"
  6. "os"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "github.com/containerd/log"
  11. "github.com/docker/docker/api/types/events"
  12. mounttypes "github.com/docker/docker/api/types/mount"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/internal/cleanups"
  15. "github.com/docker/docker/internal/compatcontext"
  16. volumemounts "github.com/docker/docker/volume/mounts"
  17. "github.com/pkg/errors"
  18. )
  19. // setupMounts iterates through each of the mount points for a container and
  20. // calls Setup() on each. It also looks to see if is a network mount such as
  21. // /etc/resolv.conf, and if it is not, appends it to the array of mounts.
  22. //
  23. // The cleanup function should be called as soon as the container has been
  24. // started.
  25. func (daemon *Daemon) setupMounts(ctx context.Context, c *container.Container) ([]container.Mount, func(context.Context) error, error) {
  26. var mounts []container.Mount
  27. // TODO: tmpfs mounts should be part of Mountpoints
  28. tmpfsMounts := make(map[string]bool)
  29. tmpfsMountInfo, err := c.TmpfsMounts()
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. for _, m := range tmpfsMountInfo {
  34. tmpfsMounts[m.Destination] = true
  35. }
  36. cleanups := cleanups.Composite{}
  37. defer func() {
  38. if err := cleanups.Call(compatcontext.WithoutCancel(ctx)); err != nil {
  39. log.G(ctx).WithError(err).Warn("failed to cleanup temporary mounts created by MountPoint.Setup")
  40. }
  41. }()
  42. for _, m := range c.MountPoints {
  43. if tmpfsMounts[m.Destination] {
  44. continue
  45. }
  46. if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
  47. return nil, nil, err
  48. }
  49. // If the daemon is being shutdown, we should not let a container start if it is trying to
  50. // mount the socket the daemon is listening on. During daemon shutdown, the socket
  51. // (/var/run/docker.sock by default) doesn't exist anymore causing the call to m.Setup to
  52. // create at directory instead. This in turn will prevent the daemon to restart.
  53. checkfunc := func(m *volumemounts.MountPoint) error {
  54. if _, exist := daemon.hosts[m.Source]; exist && daemon.IsShuttingDown() {
  55. return fmt.Errorf("Could not mount %q to container while the daemon is shutting down", m.Source)
  56. }
  57. return nil
  58. }
  59. path, clean, err := m.Setup(ctx, c.MountLabel, daemon.idMapping.RootPair(), checkfunc)
  60. if err != nil {
  61. return nil, nil, err
  62. }
  63. cleanups.Add(clean)
  64. if !c.TrySetNetworkMount(m.Destination, path) {
  65. mnt := container.Mount{
  66. Source: path,
  67. Destination: m.Destination,
  68. Writable: m.RW,
  69. Propagation: string(m.Propagation),
  70. }
  71. if m.Spec.Type == mounttypes.TypeBind && m.Spec.BindOptions != nil {
  72. if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyNonRecursive {
  73. return nil, nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyNonRecursive")
  74. }
  75. if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyForceRecursive {
  76. return nil, nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyForceRecursive")
  77. }
  78. if m.Spec.BindOptions.ReadOnlyNonRecursive && m.Spec.BindOptions.ReadOnlyForceRecursive {
  79. return nil, nil, errors.New("mount options conflict: ReadOnlyNonRecursive && BindOptions.ReadOnlyForceRecursive")
  80. }
  81. mnt.NonRecursive = m.Spec.BindOptions.NonRecursive
  82. mnt.ReadOnlyNonRecursive = m.Spec.BindOptions.ReadOnlyNonRecursive
  83. mnt.ReadOnlyForceRecursive = m.Spec.BindOptions.ReadOnlyForceRecursive
  84. }
  85. if m.Volume != nil {
  86. daemon.LogVolumeEvent(m.Volume.Name(), events.ActionMount, map[string]string{
  87. "driver": m.Volume.DriverName(),
  88. "container": c.ID,
  89. "destination": m.Destination,
  90. "read/write": strconv.FormatBool(m.RW),
  91. "propagation": string(m.Propagation),
  92. })
  93. }
  94. mounts = append(mounts, mnt)
  95. }
  96. }
  97. mounts = sortMounts(mounts)
  98. netMounts := c.NetworkMounts()
  99. // if we are going to mount any of the network files from container
  100. // metadata, the ownership must be set properly for potential container
  101. // remapped root (user namespaces)
  102. rootIDs := daemon.idMapping.RootPair()
  103. for _, mnt := range netMounts {
  104. // we should only modify ownership of network files within our own container
  105. // metadata repository. If the user specifies a mount path external, it is
  106. // up to the user to make sure the file has proper ownership for userns
  107. if strings.Index(mnt.Source, daemon.repository) == 0 {
  108. if err := os.Chown(mnt.Source, rootIDs.UID, rootIDs.GID); err != nil {
  109. return nil, nil, err
  110. }
  111. }
  112. }
  113. return append(mounts, netMounts...), cleanups.Release(), nil
  114. }
  115. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  116. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  117. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  118. func sortMounts(m []container.Mount) []container.Mount {
  119. sort.Sort(mounts(m))
  120. return m
  121. }
  122. // setBindModeIfNull is platform specific processing to ensure the
  123. // shared mode is set to 'z' if it is null. This is called in the case
  124. // of processing a named volume and not a typical bind.
  125. func setBindModeIfNull(bind *volumemounts.MountPoint) {
  126. if bind.Mode == "" {
  127. bind.Mode = "z"
  128. }
  129. }