volumes_unix.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "sort"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/daemon/execdriver"
  8. "github.com/docker/docker/volume"
  9. volumedrivers "github.com/docker/docker/volume/drivers"
  10. "github.com/docker/docker/volume/local"
  11. )
  12. // setupMounts iterates through each of the mount points for a container and
  13. // calls Setup() on each. It also looks to see if is a network mount such as
  14. // /etc/resolv.conf, and if it is not, appends it to the array of mounts.
  15. func (daemon *Daemon) setupMounts(container *container.Container) ([]execdriver.Mount, error) {
  16. var mounts []execdriver.Mount
  17. for _, m := range container.MountPoints {
  18. path, err := m.Setup()
  19. if err != nil {
  20. return nil, err
  21. }
  22. if !container.TrySetNetworkMount(m.Destination, path) {
  23. mnt := execdriver.Mount{
  24. Source: path,
  25. Destination: m.Destination,
  26. Writable: m.RW,
  27. Propagation: m.Propagation,
  28. }
  29. mounts = append(mounts, mnt)
  30. }
  31. }
  32. mounts = sortMounts(mounts)
  33. netMounts := container.NetworkMounts()
  34. // if we are going to mount any of the network files from container
  35. // metadata, the ownership must be set properly for potential container
  36. // remapped root (user namespaces)
  37. rootUID, rootGID := daemon.GetRemappedUIDGID()
  38. for _, mount := range netMounts {
  39. if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
  40. return nil, err
  41. }
  42. }
  43. return append(mounts, netMounts...), nil
  44. }
  45. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  46. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  47. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  48. func sortMounts(m []execdriver.Mount) []execdriver.Mount {
  49. sort.Sort(mounts(m))
  50. return m
  51. }
  52. // migrateVolume links the contents of a volume created pre Docker 1.7
  53. // into the location expected by the local driver.
  54. // It creates a symlink from DOCKER_ROOT/vfs/dir/VOLUME_ID to DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
  55. // It preserves the volume json configuration generated pre Docker 1.7 to be able to
  56. // downgrade from Docker 1.7 to Docker 1.6 without losing volume compatibility.
  57. func migrateVolume(id, vfs string) error {
  58. l, err := volumedrivers.Lookup(volume.DefaultDriverName)
  59. if err != nil {
  60. return err
  61. }
  62. newDataPath := l.(*local.Root).DataPath(id)
  63. fi, err := os.Stat(newDataPath)
  64. if err != nil && !os.IsNotExist(err) {
  65. return err
  66. }
  67. if fi != nil && fi.IsDir() {
  68. return nil
  69. }
  70. return os.Symlink(vfs, newDataPath)
  71. }
  72. // validVolumeLayout checks whether the volume directory layout
  73. // is valid to work with Docker post 1.7 or not.
  74. func validVolumeLayout(files []os.FileInfo) bool {
  75. if len(files) == 1 && files[0].Name() == local.VolumeDataPathName && files[0].IsDir() {
  76. return true
  77. }
  78. if len(files) != 2 {
  79. return false
  80. }
  81. for _, f := range files {
  82. if f.Name() == "config.json" ||
  83. (f.Name() == local.VolumeDataPathName && f.Mode()&os.ModeSymlink == os.ModeSymlink) {
  84. // Old volume configuration, we ignore it
  85. continue
  86. }
  87. return false
  88. }
  89. return true
  90. }
  91. // setBindModeIfNull is platform specific processing to ensure the
  92. // shared mode is set to 'z' if it is null. This is called in the case
  93. // of processing a named volume and not a typical bind.
  94. func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
  95. if bind.Mode == "" {
  96. bind.Mode = "z"
  97. }
  98. return bind
  99. }