volumes.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package daemon
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/Sirupsen/logrus"
  9. dockererrors "github.com/docker/docker/api/errors"
  10. "github.com/docker/docker/api/types"
  11. containertypes "github.com/docker/docker/api/types/container"
  12. mounttypes "github.com/docker/docker/api/types/mount"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/volume"
  15. "github.com/docker/docker/volume/drivers"
  16. "github.com/opencontainers/runc/libcontainer/label"
  17. )
  18. var (
  19. // ErrVolumeReadonly is used to signal an error when trying to copy data into
  20. // a volume mount that is not writable.
  21. ErrVolumeReadonly = errors.New("mounted volume is marked read-only")
  22. )
  23. type mounts []container.Mount
  24. // volumeToAPIType converts a volume.Volume to the type used by the Engine API
  25. func volumeToAPIType(v volume.Volume) *types.Volume {
  26. tv := &types.Volume{
  27. Name: v.Name(),
  28. Driver: v.DriverName(),
  29. }
  30. if v, ok := v.(volume.DetailedVolume); ok {
  31. tv.Labels = v.Labels()
  32. tv.Options = v.Options()
  33. tv.Scope = v.Scope()
  34. }
  35. return tv
  36. }
  37. // Len returns the number of mounts. Used in sorting.
  38. func (m mounts) Len() int {
  39. return len(m)
  40. }
  41. // Less returns true if the number of parts (a/b/c would be 3 parts) in the
  42. // mount indexed by parameter 1 is less than that of the mount indexed by
  43. // parameter 2. Used in sorting.
  44. func (m mounts) Less(i, j int) bool {
  45. return m.parts(i) < m.parts(j)
  46. }
  47. // Swap swaps two items in an array of mounts. Used in sorting
  48. func (m mounts) Swap(i, j int) {
  49. m[i], m[j] = m[j], m[i]
  50. }
  51. // parts returns the number of parts in the destination of a mount. Used in sorting.
  52. func (m mounts) parts(i int) int {
  53. return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
  54. }
  55. // registerMountPoints initializes the container mount points with the configured volumes and bind mounts.
  56. // It follows the next sequence to decide what to mount in each final destination:
  57. //
  58. // 1. Select the previously configured mount points for the containers, if any.
  59. // 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
  60. // 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
  61. // 4. Cleanup old volumes that are about to be reassigned.
  62. func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) (retErr error) {
  63. binds := map[string]bool{}
  64. mountPoints := map[string]*volume.MountPoint{}
  65. defer func() {
  66. // clean up the container mountpoints once return with error
  67. if retErr != nil {
  68. for _, m := range mountPoints {
  69. if m.Volume == nil {
  70. continue
  71. }
  72. daemon.volumes.Dereference(m.Volume, container.ID)
  73. }
  74. }
  75. }()
  76. // 1. Read already configured mount points.
  77. for destination, point := range container.MountPoints {
  78. mountPoints[destination] = point
  79. }
  80. // 2. Read volumes from other containers.
  81. for _, v := range hostConfig.VolumesFrom {
  82. containerID, mode, err := volume.ParseVolumesFrom(v)
  83. if err != nil {
  84. return err
  85. }
  86. c, err := daemon.GetContainer(containerID)
  87. if err != nil {
  88. return err
  89. }
  90. for _, m := range c.MountPoints {
  91. cp := &volume.MountPoint{
  92. Name: m.Name,
  93. Source: m.Source,
  94. RW: m.RW && volume.ReadWrite(mode),
  95. Driver: m.Driver,
  96. Destination: m.Destination,
  97. Propagation: m.Propagation,
  98. Spec: m.Spec,
  99. CopyData: false,
  100. }
  101. if len(cp.Source) == 0 {
  102. v, err := daemon.volumes.GetWithRef(cp.Name, cp.Driver, container.ID)
  103. if err != nil {
  104. return err
  105. }
  106. cp.Volume = v
  107. }
  108. mountPoints[cp.Destination] = cp
  109. }
  110. }
  111. // 3. Read bind mounts
  112. for _, b := range hostConfig.Binds {
  113. bind, err := volume.ParseMountRaw(b, hostConfig.VolumeDriver)
  114. if err != nil {
  115. return err
  116. }
  117. // #10618
  118. _, tmpfsExists := hostConfig.Tmpfs[bind.Destination]
  119. if binds[bind.Destination] || tmpfsExists {
  120. return fmt.Errorf("Duplicate mount point '%s'", bind.Destination)
  121. }
  122. if bind.Type == mounttypes.TypeVolume {
  123. // create the volume
  124. v, err := daemon.volumes.CreateWithRef(bind.Name, bind.Driver, container.ID, nil, nil)
  125. if err != nil {
  126. return err
  127. }
  128. bind.Volume = v
  129. bind.Source = v.Path()
  130. // bind.Name is an already existing volume, we need to use that here
  131. bind.Driver = v.DriverName()
  132. if bind.Driver == volume.DefaultDriverName {
  133. setBindModeIfNull(bind)
  134. }
  135. }
  136. binds[bind.Destination] = true
  137. mountPoints[bind.Destination] = bind
  138. }
  139. for _, cfg := range hostConfig.Mounts {
  140. mp, err := volume.ParseMountSpec(cfg)
  141. if err != nil {
  142. return dockererrors.NewBadRequestError(err)
  143. }
  144. if binds[mp.Destination] {
  145. return fmt.Errorf("Duplicate mount point '%s'", cfg.Target)
  146. }
  147. if mp.Type == mounttypes.TypeVolume {
  148. var v volume.Volume
  149. if cfg.VolumeOptions != nil {
  150. var driverOpts map[string]string
  151. if cfg.VolumeOptions.DriverConfig != nil {
  152. driverOpts = cfg.VolumeOptions.DriverConfig.Options
  153. }
  154. v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, driverOpts, cfg.VolumeOptions.Labels)
  155. } else {
  156. v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, nil, nil)
  157. }
  158. if err != nil {
  159. return err
  160. }
  161. if err := label.Relabel(mp.Source, container.MountLabel, false); err != nil {
  162. return err
  163. }
  164. mp.Volume = v
  165. mp.Name = v.Name()
  166. mp.Driver = v.DriverName()
  167. // only use the cached path here since getting the path is not necessary right now and calling `Path()` may be slow
  168. if cv, ok := v.(interface {
  169. CachedPath() string
  170. }); ok {
  171. mp.Source = cv.CachedPath()
  172. }
  173. }
  174. binds[mp.Destination] = true
  175. mountPoints[mp.Destination] = mp
  176. }
  177. container.Lock()
  178. // 4. Cleanup old volumes that are about to be reassigned.
  179. for _, m := range mountPoints {
  180. if m.BackwardsCompatible() {
  181. if mp, exists := container.MountPoints[m.Destination]; exists && mp.Volume != nil {
  182. daemon.volumes.Dereference(mp.Volume, container.ID)
  183. }
  184. }
  185. }
  186. container.MountPoints = mountPoints
  187. container.Unlock()
  188. return nil
  189. }
  190. // lazyInitializeVolume initializes a mountpoint's volume if needed.
  191. // This happens after a daemon restart.
  192. func (daemon *Daemon) lazyInitializeVolume(containerID string, m *volume.MountPoint) error {
  193. if len(m.Driver) > 0 && m.Volume == nil {
  194. v, err := daemon.volumes.GetWithRef(m.Name, m.Driver, containerID)
  195. if err != nil {
  196. return err
  197. }
  198. m.Volume = v
  199. }
  200. return nil
  201. }
  202. func backportMountSpec(container *container.Container) error {
  203. for target, m := range container.MountPoints {
  204. if m.Spec.Type != "" {
  205. // if type is set on even one mount, no need to migrate
  206. return nil
  207. }
  208. if m.Name != "" {
  209. m.Type = mounttypes.TypeVolume
  210. m.Spec.Type = mounttypes.TypeVolume
  211. // make sure this is not an anyonmous volume before setting the spec source
  212. if _, exists := container.Config.Volumes[target]; !exists {
  213. m.Spec.Source = m.Name
  214. }
  215. if container.HostConfig.VolumeDriver != "" {
  216. m.Spec.VolumeOptions = &mounttypes.VolumeOptions{
  217. DriverConfig: &mounttypes.Driver{Name: container.HostConfig.VolumeDriver},
  218. }
  219. }
  220. if strings.Contains(m.Mode, "nocopy") {
  221. if m.Spec.VolumeOptions == nil {
  222. m.Spec.VolumeOptions = &mounttypes.VolumeOptions{}
  223. }
  224. m.Spec.VolumeOptions.NoCopy = true
  225. }
  226. } else {
  227. m.Type = mounttypes.TypeBind
  228. m.Spec.Type = mounttypes.TypeBind
  229. m.Spec.Source = m.Source
  230. if m.Propagation != "" {
  231. m.Spec.BindOptions = &mounttypes.BindOptions{
  232. Propagation: m.Propagation,
  233. }
  234. }
  235. }
  236. m.Spec.Target = m.Destination
  237. if !m.RW {
  238. m.Spec.ReadOnly = true
  239. }
  240. }
  241. return container.ToDiskLocking()
  242. }
  243. func (daemon *Daemon) traverseLocalVolumes(fn func(volume.Volume) error) error {
  244. localVolumeDriver, err := volumedrivers.GetDriver(volume.DefaultDriverName)
  245. if err != nil {
  246. return fmt.Errorf("can't retrieve local volume driver: %v", err)
  247. }
  248. vols, err := localVolumeDriver.List()
  249. if err != nil {
  250. return fmt.Errorf("can't retrieve local volumes: %v", err)
  251. }
  252. for _, v := range vols {
  253. name := v.Name()
  254. _, err := daemon.volumes.Get(name)
  255. if err != nil {
  256. logrus.Warnf("failed to retrieve volume %s from store: %v", name, err)
  257. }
  258. err = fn(v)
  259. if err != nil {
  260. return err
  261. }
  262. }
  263. return nil
  264. }