volumes_unix.go 4.5 KB

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