volumes_unix.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // +build !windows
  2. package daemon
  3. import (
  4. "encoding/json"
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/volume"
  12. "github.com/docker/docker/volume/drivers"
  13. "github.com/docker/docker/volume/local"
  14. "github.com/pkg/errors"
  15. )
  16. // setupMounts iterates through each of the mount points for a container and
  17. // calls Setup() on each. It also looks to see if is a network mount such as
  18. // /etc/resolv.conf, and if it is not, appends it to the array of mounts.
  19. func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
  20. var mounts []container.Mount
  21. // TODO: tmpfs mounts should be part of Mountpoints
  22. tmpfsMounts := make(map[string]bool)
  23. for _, m := range c.TmpfsMounts() {
  24. tmpfsMounts[m.Destination] = true
  25. }
  26. for _, m := range c.MountPoints {
  27. if tmpfsMounts[m.Destination] {
  28. continue
  29. }
  30. if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
  31. return nil, err
  32. }
  33. rootUID, rootGID := daemon.GetRemappedUIDGID()
  34. path, err := m.Setup(c.MountLabel, rootUID, rootGID)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if !c.TrySetNetworkMount(m.Destination, path) {
  39. mnt := container.Mount{
  40. Source: path,
  41. Destination: m.Destination,
  42. Writable: m.RW,
  43. Propagation: string(m.Propagation),
  44. }
  45. if m.Volume != nil {
  46. attributes := map[string]string{
  47. "driver": m.Volume.DriverName(),
  48. "container": c.ID,
  49. "destination": m.Destination,
  50. "read/write": strconv.FormatBool(m.RW),
  51. "propagation": string(m.Propagation),
  52. }
  53. daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
  54. }
  55. mounts = append(mounts, mnt)
  56. }
  57. }
  58. mounts = sortMounts(mounts)
  59. netMounts := c.NetworkMounts()
  60. // if we are going to mount any of the network files from container
  61. // metadata, the ownership must be set properly for potential container
  62. // remapped root (user namespaces)
  63. rootUID, rootGID := daemon.GetRemappedUIDGID()
  64. for _, mount := range netMounts {
  65. if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
  66. return nil, err
  67. }
  68. }
  69. return append(mounts, netMounts...), nil
  70. }
  71. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  72. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  73. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  74. func sortMounts(m []container.Mount) []container.Mount {
  75. sort.Sort(mounts(m))
  76. return m
  77. }
  78. // setBindModeIfNull is platform specific processing to ensure the
  79. // shared mode is set to 'z' if it is null. This is called in the case
  80. // of processing a named volume and not a typical bind.
  81. func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
  82. if bind.Mode == "" {
  83. bind.Mode = "z"
  84. }
  85. return bind
  86. }
  87. // migrateVolume links the contents of a volume created pre Docker 1.7
  88. // into the location expected by the local driver.
  89. // It creates a symlink from DOCKER_ROOT/vfs/dir/VOLUME_ID to DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
  90. // It preserves the volume json configuration generated pre Docker 1.7 to be able to
  91. // downgrade from Docker 1.7 to Docker 1.6 without losing volume compatibility.
  92. func migrateVolume(id, vfs string) error {
  93. l, err := volumedrivers.GetDriver(volume.DefaultDriverName)
  94. if err != nil {
  95. return err
  96. }
  97. newDataPath := l.(*local.Root).DataPath(id)
  98. fi, err := os.Stat(newDataPath)
  99. if err != nil && !os.IsNotExist(err) {
  100. return err
  101. }
  102. if fi != nil && fi.IsDir() {
  103. return nil
  104. }
  105. return os.Symlink(vfs, newDataPath)
  106. }
  107. // verifyVolumesInfo ports volumes configured for the containers pre docker 1.7.
  108. // It reads the container configuration and creates valid mount points for the old volumes.
  109. func (daemon *Daemon) verifyVolumesInfo(container *container.Container) error {
  110. // Inspect old structures only when we're upgrading from old versions
  111. // to versions >= 1.7 and the MountPoints has not been populated with volumes data.
  112. type volumes struct {
  113. Volumes map[string]string
  114. VolumesRW map[string]bool
  115. }
  116. cfgPath, err := container.ConfigPath()
  117. if err != nil {
  118. return err
  119. }
  120. f, err := os.Open(cfgPath)
  121. if err != nil {
  122. return errors.Wrap(err, "could not open container config")
  123. }
  124. var cv volumes
  125. if err := json.NewDecoder(f).Decode(&cv); err != nil {
  126. return errors.Wrap(err, "could not decode container config")
  127. }
  128. if len(container.MountPoints) == 0 && len(cv.Volumes) > 0 {
  129. for destination, hostPath := range cv.Volumes {
  130. vfsPath := filepath.Join(daemon.root, "vfs", "dir")
  131. rw := cv.VolumesRW != nil && cv.VolumesRW[destination]
  132. if strings.HasPrefix(hostPath, vfsPath) {
  133. id := filepath.Base(hostPath)
  134. v, err := daemon.volumes.CreateWithRef(id, volume.DefaultDriverName, container.ID, nil, nil)
  135. if err != nil {
  136. return err
  137. }
  138. if err := migrateVolume(id, hostPath); err != nil {
  139. return err
  140. }
  141. container.AddMountPointWithVolume(destination, v, true)
  142. } else { // Bind mount
  143. m := volume.MountPoint{Source: hostPath, Destination: destination, RW: rw}
  144. container.MountPoints[destination] = &m
  145. }
  146. }
  147. return container.ToDisk()
  148. }
  149. return nil
  150. }