volumes_unix.go 3.8 KB

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