volumes_unix.go 7.0 KB

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