volumes_unix.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "github.com/docker/docker/api/types/events"
  10. mounttypes "github.com/docker/docker/api/types/mount"
  11. "github.com/docker/docker/container"
  12. volumemounts "github.com/docker/docker/volume/mounts"
  13. "github.com/pkg/errors"
  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. if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyNonRecursive {
  59. return nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyNonRecursive")
  60. }
  61. if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyForceRecursive {
  62. return nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyForceRecursive")
  63. }
  64. if m.Spec.BindOptions.ReadOnlyNonRecursive && m.Spec.BindOptions.ReadOnlyForceRecursive {
  65. return nil, errors.New("mount options conflict: ReadOnlyNonRecursive && BindOptions.ReadOnlyForceRecursive")
  66. }
  67. mnt.NonRecursive = m.Spec.BindOptions.NonRecursive
  68. mnt.ReadOnlyNonRecursive = m.Spec.BindOptions.ReadOnlyNonRecursive
  69. mnt.ReadOnlyForceRecursive = m.Spec.BindOptions.ReadOnlyForceRecursive
  70. }
  71. if m.Volume != nil {
  72. daemon.LogVolumeEvent(m.Volume.Name(), events.ActionMount, map[string]string{
  73. "driver": m.Volume.DriverName(),
  74. "container": c.ID,
  75. "destination": m.Destination,
  76. "read/write": strconv.FormatBool(m.RW),
  77. "propagation": string(m.Propagation),
  78. })
  79. }
  80. mounts = append(mounts, mnt)
  81. }
  82. }
  83. mounts = sortMounts(mounts)
  84. netMounts := c.NetworkMounts()
  85. // if we are going to mount any of the network files from container
  86. // metadata, the ownership must be set properly for potential container
  87. // remapped root (user namespaces)
  88. rootIDs := daemon.idMapping.RootPair()
  89. for _, mnt := range netMounts {
  90. // we should only modify ownership of network files within our own container
  91. // metadata repository. If the user specifies a mount path external, it is
  92. // up to the user to make sure the file has proper ownership for userns
  93. if strings.Index(mnt.Source, daemon.repository) == 0 {
  94. if err := os.Chown(mnt.Source, rootIDs.UID, rootIDs.GID); err != nil {
  95. return nil, err
  96. }
  97. }
  98. }
  99. return append(mounts, netMounts...), nil
  100. }
  101. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  102. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  103. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  104. func sortMounts(m []container.Mount) []container.Mount {
  105. sort.Sort(mounts(m))
  106. return m
  107. }
  108. // setBindModeIfNull is platform specific processing to ensure the
  109. // shared mode is set to 'z' if it is null. This is called in the case
  110. // of processing a named volume and not a typical bind.
  111. func setBindModeIfNull(bind *volumemounts.MountPoint) {
  112. if bind.Mode == "" {
  113. bind.Mode = "z"
  114. }
  115. }