volumes_unix.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // +build !windows
  2. package daemon
  3. import (
  4. "encoding/json"
  5. "fmt"
  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. // If the daemon is being shutdown, we should not let a container start if it is trying to
  41. // mount the socket the daemon is listening on. During daemon shutdown, the socket
  42. // (/var/run/docker.sock by default) doesn't exist anymore causing the call to m.Setup to
  43. // create at directory instead. This in turn will prevent the daemon to restart.
  44. checkfunc := func(m *volume.MountPoint) error {
  45. if _, exist := daemon.hosts[m.Source]; exist && daemon.IsShuttingDown() {
  46. return fmt.Errorf("Could not mount %q to container while the daemon is shutting down", m.Source)
  47. }
  48. return nil
  49. }
  50. path, err := m.Setup(c.MountLabel, daemon.idMappings.RootPair(), checkfunc)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if !c.TrySetNetworkMount(m.Destination, path) {
  55. mnt := container.Mount{
  56. Source: path,
  57. Destination: m.Destination,
  58. Writable: m.RW,
  59. Propagation: string(m.Propagation),
  60. }
  61. if m.Volume != nil {
  62. attributes := map[string]string{
  63. "driver": m.Volume.DriverName(),
  64. "container": c.ID,
  65. "destination": m.Destination,
  66. "read/write": strconv.FormatBool(m.RW),
  67. "propagation": string(m.Propagation),
  68. }
  69. daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
  70. }
  71. mounts = append(mounts, mnt)
  72. }
  73. }
  74. mounts = sortMounts(mounts)
  75. netMounts := c.NetworkMounts()
  76. // if we are going to mount any of the network files from container
  77. // metadata, the ownership must be set properly for potential container
  78. // remapped root (user namespaces)
  79. rootIDs := daemon.idMappings.RootPair()
  80. for _, mount := range netMounts {
  81. // we should only modify ownership of network files within our own container
  82. // metadata repository. If the user specifies a mount path external, it is
  83. // up to the user to make sure the file has proper ownership for userns
  84. if strings.Index(mount.Source, daemon.repository) == 0 {
  85. if err := os.Chown(mount.Source, rootIDs.UID, rootIDs.GID); err != nil {
  86. return nil, err
  87. }
  88. }
  89. }
  90. return append(mounts, netMounts...), nil
  91. }
  92. // sortMounts sorts an array of mounts in lexicographic order. This ensure that
  93. // when mounting, the mounts don't shadow other mounts. For example, if mounting
  94. // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
  95. func sortMounts(m []container.Mount) []container.Mount {
  96. sort.Sort(mounts(m))
  97. return m
  98. }
  99. // setBindModeIfNull is platform specific processing to ensure the
  100. // shared mode is set to 'z' if it is null. This is called in the case
  101. // of processing a named volume and not a typical bind.
  102. func setBindModeIfNull(bind *volume.MountPoint) {
  103. if bind.Mode == "" {
  104. bind.Mode = "z"
  105. }
  106. }
  107. // migrateVolume links the contents of a volume created pre Docker 1.7
  108. // into the location expected by the local driver.
  109. // It creates a symlink from DOCKER_ROOT/vfs/dir/VOLUME_ID to DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
  110. // It preserves the volume json configuration generated pre Docker 1.7 to be able to
  111. // downgrade from Docker 1.7 to Docker 1.6 without losing volume compatibility.
  112. func migrateVolume(id, vfs string) error {
  113. l, err := volumedrivers.GetDriver(volume.DefaultDriverName)
  114. if err != nil {
  115. return err
  116. }
  117. newDataPath := l.(*local.Root).DataPath(id)
  118. fi, err := os.Stat(newDataPath)
  119. if err != nil && !os.IsNotExist(err) {
  120. return err
  121. }
  122. if fi != nil && fi.IsDir() {
  123. return nil
  124. }
  125. return os.Symlink(vfs, newDataPath)
  126. }
  127. // verifyVolumesInfo ports volumes configured for the containers pre docker 1.7.
  128. // It reads the container configuration and creates valid mount points for the old volumes.
  129. func (daemon *Daemon) verifyVolumesInfo(container *container.Container) error {
  130. container.Lock()
  131. defer container.Unlock()
  132. // Inspect old structures only when we're upgrading from old versions
  133. // to versions >= 1.7 and the MountPoints has not been populated with volumes data.
  134. type volumes struct {
  135. Volumes map[string]string
  136. VolumesRW map[string]bool
  137. }
  138. cfgPath, err := container.ConfigPath()
  139. if err != nil {
  140. return err
  141. }
  142. f, err := os.Open(cfgPath)
  143. if err != nil {
  144. return errors.Wrap(err, "could not open container config")
  145. }
  146. defer f.Close()
  147. var cv volumes
  148. if err := json.NewDecoder(f).Decode(&cv); err != nil {
  149. return errors.Wrap(err, "could not decode container config")
  150. }
  151. if len(container.MountPoints) == 0 && len(cv.Volumes) > 0 {
  152. for destination, hostPath := range cv.Volumes {
  153. vfsPath := filepath.Join(daemon.root, "vfs", "dir")
  154. rw := cv.VolumesRW != nil && cv.VolumesRW[destination]
  155. if strings.HasPrefix(hostPath, vfsPath) {
  156. id := filepath.Base(hostPath)
  157. v, err := daemon.volumes.CreateWithRef(id, volume.DefaultDriverName, container.ID, nil, nil)
  158. if err != nil {
  159. return err
  160. }
  161. if err := migrateVolume(id, hostPath); err != nil {
  162. return err
  163. }
  164. container.AddMountPointWithVolume(destination, v, true)
  165. } else { // Bind mount
  166. m := volume.MountPoint{Source: hostPath, Destination: destination, RW: rw}
  167. container.MountPoints[destination] = &m
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. func (daemon *Daemon) mountVolumes(container *container.Container) error {
  174. mounts, err := daemon.setupMounts(container)
  175. if err != nil {
  176. return err
  177. }
  178. for _, m := range mounts {
  179. dest, err := container.GetResourcePath(m.Destination)
  180. if err != nil {
  181. return err
  182. }
  183. var stat os.FileInfo
  184. stat, err = os.Stat(m.Source)
  185. if err != nil {
  186. return err
  187. }
  188. if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
  189. return err
  190. }
  191. opts := "rbind,ro"
  192. if m.Writable {
  193. opts = "rbind,rw"
  194. }
  195. if err := mount.Mount(m.Source, dest, bindMountType, opts); err != nil {
  196. return err
  197. }
  198. // mountVolumes() seems to be called for temporary mounts
  199. // outside the container. Soon these will be unmounted with
  200. // lazy unmount option and given we have mounted the rbind,
  201. // all the submounts will propagate if these are shared. If
  202. // daemon is running in host namespace and has / as shared
  203. // then these unmounts will propagate and unmount original
  204. // mount as well. So make all these mounts rprivate.
  205. // Do not use propagation property of volume as that should
  206. // apply only when mounting happen inside the container.
  207. if err := mount.MakeRPrivate(dest); err != nil {
  208. return err
  209. }
  210. }
  211. return nil
  212. }