volumes.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. dereferenceIfExists := func(destination string) {
  77. if v, ok := mountPoints[destination]; ok {
  78. logrus.Debugf("Duplicate mount point '%s'", destination)
  79. if v.Volume != nil {
  80. daemon.volumes.Dereference(v.Volume, container.ID)
  81. }
  82. }
  83. }
  84. // 1. Read already configured mount points.
  85. for destination, point := range container.MountPoints {
  86. mountPoints[destination] = point
  87. }
  88. // 2. Read volumes from other containers.
  89. for _, v := range hostConfig.VolumesFrom {
  90. containerID, mode, err := volume.ParseVolumesFrom(v)
  91. if err != nil {
  92. return err
  93. }
  94. c, err := daemon.GetContainer(containerID)
  95. if err != nil {
  96. return err
  97. }
  98. for _, m := range c.MountPoints {
  99. cp := &volume.MountPoint{
  100. Name: m.Name,
  101. Source: m.Source,
  102. RW: m.RW && volume.ReadWrite(mode),
  103. Driver: m.Driver,
  104. Destination: m.Destination,
  105. Propagation: m.Propagation,
  106. Spec: m.Spec,
  107. CopyData: false,
  108. }
  109. if len(cp.Source) == 0 {
  110. v, err := daemon.volumes.GetWithRef(cp.Name, cp.Driver, container.ID)
  111. if err != nil {
  112. return err
  113. }
  114. cp.Volume = v
  115. }
  116. dereferenceIfExists(cp.Destination)
  117. mountPoints[cp.Destination] = cp
  118. }
  119. }
  120. // 3. Read bind mounts
  121. for _, b := range hostConfig.Binds {
  122. bind, err := volume.ParseMountRaw(b, hostConfig.VolumeDriver)
  123. if err != nil {
  124. return err
  125. }
  126. // #10618
  127. _, tmpfsExists := hostConfig.Tmpfs[bind.Destination]
  128. if binds[bind.Destination] || tmpfsExists {
  129. return fmt.Errorf("Duplicate mount point '%s'", bind.Destination)
  130. }
  131. if bind.Type == mounttypes.TypeVolume {
  132. // create the volume
  133. v, err := daemon.volumes.CreateWithRef(bind.Name, bind.Driver, container.ID, nil, nil)
  134. if err != nil {
  135. return err
  136. }
  137. bind.Volume = v
  138. bind.Source = v.Path()
  139. // bind.Name is an already existing volume, we need to use that here
  140. bind.Driver = v.DriverName()
  141. if bind.Driver == volume.DefaultDriverName {
  142. setBindModeIfNull(bind)
  143. }
  144. }
  145. binds[bind.Destination] = true
  146. dereferenceIfExists(bind.Destination)
  147. mountPoints[bind.Destination] = bind
  148. }
  149. for _, cfg := range hostConfig.Mounts {
  150. mp, err := volume.ParseMountSpec(cfg)
  151. if err != nil {
  152. return dockererrors.NewBadRequestError(err)
  153. }
  154. if binds[mp.Destination] {
  155. return fmt.Errorf("Duplicate mount point '%s'", cfg.Target)
  156. }
  157. if mp.Type == mounttypes.TypeVolume {
  158. var v volume.Volume
  159. if cfg.VolumeOptions != nil {
  160. var driverOpts map[string]string
  161. if cfg.VolumeOptions.DriverConfig != nil {
  162. driverOpts = cfg.VolumeOptions.DriverConfig.Options
  163. }
  164. v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, driverOpts, cfg.VolumeOptions.Labels)
  165. } else {
  166. v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, nil, nil)
  167. }
  168. if err != nil {
  169. return err
  170. }
  171. if err := label.Relabel(mp.Source, container.MountLabel, false); err != nil {
  172. return err
  173. }
  174. mp.Volume = v
  175. mp.Name = v.Name()
  176. mp.Driver = v.DriverName()
  177. // only use the cached path here since getting the path is not necessary right now and calling `Path()` may be slow
  178. if cv, ok := v.(interface {
  179. CachedPath() string
  180. }); ok {
  181. mp.Source = cv.CachedPath()
  182. }
  183. }
  184. binds[mp.Destination] = true
  185. dereferenceIfExists(mp.Destination)
  186. mountPoints[mp.Destination] = mp
  187. }
  188. container.Lock()
  189. // 4. Cleanup old volumes that are about to be reassigned.
  190. for _, m := range mountPoints {
  191. if m.BackwardsCompatible() {
  192. if mp, exists := container.MountPoints[m.Destination]; exists && mp.Volume != nil {
  193. daemon.volumes.Dereference(mp.Volume, container.ID)
  194. }
  195. }
  196. }
  197. container.MountPoints = mountPoints
  198. container.Unlock()
  199. return nil
  200. }
  201. // lazyInitializeVolume initializes a mountpoint's volume if needed.
  202. // This happens after a daemon restart.
  203. func (daemon *Daemon) lazyInitializeVolume(containerID string, m *volume.MountPoint) error {
  204. if len(m.Driver) > 0 && m.Volume == nil {
  205. v, err := daemon.volumes.GetWithRef(m.Name, m.Driver, containerID)
  206. if err != nil {
  207. return err
  208. }
  209. m.Volume = v
  210. }
  211. return nil
  212. }
  213. func backportMountSpec(container *container.Container) error {
  214. for target, m := range container.MountPoints {
  215. if m.Spec.Type != "" {
  216. // if type is set on even one mount, no need to migrate
  217. return nil
  218. }
  219. if m.Name != "" {
  220. m.Type = mounttypes.TypeVolume
  221. m.Spec.Type = mounttypes.TypeVolume
  222. // make sure this is not an anyonmous volume before setting the spec source
  223. if _, exists := container.Config.Volumes[target]; !exists {
  224. m.Spec.Source = m.Name
  225. }
  226. if container.HostConfig.VolumeDriver != "" {
  227. m.Spec.VolumeOptions = &mounttypes.VolumeOptions{
  228. DriverConfig: &mounttypes.Driver{Name: container.HostConfig.VolumeDriver},
  229. }
  230. }
  231. if strings.Contains(m.Mode, "nocopy") {
  232. if m.Spec.VolumeOptions == nil {
  233. m.Spec.VolumeOptions = &mounttypes.VolumeOptions{}
  234. }
  235. m.Spec.VolumeOptions.NoCopy = true
  236. }
  237. } else {
  238. m.Type = mounttypes.TypeBind
  239. m.Spec.Type = mounttypes.TypeBind
  240. m.Spec.Source = m.Source
  241. if m.Propagation != "" {
  242. m.Spec.BindOptions = &mounttypes.BindOptions{
  243. Propagation: m.Propagation,
  244. }
  245. }
  246. }
  247. m.Spec.Target = m.Destination
  248. if !m.RW {
  249. m.Spec.ReadOnly = true
  250. }
  251. }
  252. return container.ToDiskLocking()
  253. }
  254. func (daemon *Daemon) traverseLocalVolumes(fn func(volume.Volume) error) error {
  255. localVolumeDriver, err := volumedrivers.GetDriver(volume.DefaultDriverName)
  256. if err != nil {
  257. return fmt.Errorf("can't retrieve local volume driver: %v", err)
  258. }
  259. vols, err := localVolumeDriver.List()
  260. if err != nil {
  261. return fmt.Errorf("can't retrieve local volumes: %v", err)
  262. }
  263. for _, v := range vols {
  264. name := v.Name()
  265. _, err := daemon.volumes.Get(name)
  266. if err != nil {
  267. logrus.Warnf("failed to retrieve volume %s from store: %v", name, err)
  268. }
  269. err = fn(v)
  270. if err != nil {
  271. return err
  272. }
  273. }
  274. return nil
  275. }