volumes_unix.go 6.5 KB

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