volumes_unix.go 5.0 KB

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