volumes.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. "github.com/containerd/log"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/api/types/mount"
  11. mounttypes "github.com/docker/docker/api/types/mount"
  12. volumetypes "github.com/docker/docker/api/types/volume"
  13. "github.com/docker/docker/container"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/docker/docker/volume"
  16. volumemounts "github.com/docker/docker/volume/mounts"
  17. "github.com/docker/docker/volume/service"
  18. volumeopts "github.com/docker/docker/volume/service/opts"
  19. "github.com/pkg/errors"
  20. )
  21. var _ volume.LiveRestorer = (*volumeWrapper)(nil)
  22. type mounts []container.Mount
  23. // Len returns the number of mounts. Used in sorting.
  24. func (m mounts) Len() int {
  25. return len(m)
  26. }
  27. // Less returns true if the number of parts (a/b/c would be 3 parts) in the
  28. // mount indexed by parameter 1 is less than that of the mount indexed by
  29. // parameter 2. Used in sorting.
  30. func (m mounts) Less(i, j int) bool {
  31. return m.parts(i) < m.parts(j)
  32. }
  33. // Swap swaps two items in an array of mounts. Used in sorting
  34. func (m mounts) Swap(i, j int) {
  35. m[i], m[j] = m[j], m[i]
  36. }
  37. // parts returns the number of parts in the destination of a mount. Used in sorting.
  38. func (m mounts) parts(i int) int {
  39. return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
  40. }
  41. // registerMountPoints initializes the container mount points with the configured volumes and bind mounts.
  42. // It follows the next sequence to decide what to mount in each final destination:
  43. //
  44. // 1. Select the previously configured mount points for the containers, if any.
  45. // 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
  46. // 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
  47. // 4. Cleanup old volumes that are about to be reassigned.
  48. func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) (retErr error) {
  49. binds := map[string]bool{}
  50. mountPoints := map[string]*volumemounts.MountPoint{}
  51. parser := volumemounts.NewParser()
  52. ctx := context.TODO()
  53. defer func() {
  54. // clean up the container mountpoints once return with error
  55. if retErr != nil {
  56. for _, m := range mountPoints {
  57. if m.Volume == nil {
  58. continue
  59. }
  60. daemon.volumes.Release(ctx, m.Volume.Name(), container.ID)
  61. }
  62. }
  63. }()
  64. dereferenceIfExists := func(destination string) {
  65. if v, ok := mountPoints[destination]; ok {
  66. log.G(ctx).Debugf("Duplicate mount point '%s'", destination)
  67. if v.Volume != nil {
  68. daemon.volumes.Release(ctx, v.Volume.Name(), container.ID)
  69. }
  70. }
  71. }
  72. // 1. Read already configured mount points.
  73. for destination, point := range container.MountPoints {
  74. mountPoints[destination] = point
  75. }
  76. // 2. Read volumes from other containers.
  77. for _, v := range hostConfig.VolumesFrom {
  78. containerID, mode, err := parser.ParseVolumesFrom(v)
  79. if err != nil {
  80. return errdefs.InvalidParameter(err)
  81. }
  82. c, err := daemon.GetContainer(containerID)
  83. if err != nil {
  84. return errdefs.InvalidParameter(err)
  85. }
  86. for _, m := range c.MountPoints {
  87. cp := &volumemounts.MountPoint{
  88. Type: m.Type,
  89. Name: m.Name,
  90. Source: m.Source,
  91. RW: m.RW && parser.ReadWrite(mode),
  92. Driver: m.Driver,
  93. Destination: m.Destination,
  94. Propagation: m.Propagation,
  95. Spec: m.Spec,
  96. CopyData: false,
  97. }
  98. if len(cp.Source) == 0 {
  99. v, err := daemon.volumes.Get(ctx, cp.Name, volumeopts.WithGetDriver(cp.Driver), volumeopts.WithGetReference(container.ID))
  100. if err != nil {
  101. return err
  102. }
  103. cp.Volume = &volumeWrapper{v: v, s: daemon.volumes}
  104. }
  105. dereferenceIfExists(cp.Destination)
  106. mountPoints[cp.Destination] = cp
  107. }
  108. }
  109. // 3. Read bind mounts
  110. for _, b := range hostConfig.Binds {
  111. bind, err := parser.ParseMountRaw(b, hostConfig.VolumeDriver)
  112. if err != nil {
  113. return err
  114. }
  115. needsSlavePropagation, err := daemon.validateBindDaemonRoot(bind.Spec)
  116. if err != nil {
  117. return err
  118. }
  119. if needsSlavePropagation {
  120. bind.Propagation = mount.PropagationRSlave
  121. }
  122. // #10618
  123. _, tmpfsExists := hostConfig.Tmpfs[bind.Destination]
  124. if binds[bind.Destination] || tmpfsExists {
  125. return duplicateMountPointError(bind.Destination)
  126. }
  127. if bind.Type == mounttypes.TypeVolume {
  128. // create the volume
  129. v, err := daemon.volumes.Create(ctx, bind.Name, bind.Driver, volumeopts.WithCreateReference(container.ID))
  130. if err != nil {
  131. return err
  132. }
  133. bind.Volume = &volumeWrapper{v: v, s: daemon.volumes}
  134. bind.Source = v.Mountpoint
  135. // bind.Name is an already existing volume, we need to use that here
  136. bind.Driver = v.Driver
  137. if bind.Driver == volume.DefaultDriverName {
  138. setBindModeIfNull(bind)
  139. }
  140. }
  141. binds[bind.Destination] = true
  142. dereferenceIfExists(bind.Destination)
  143. mountPoints[bind.Destination] = bind
  144. }
  145. for _, cfg := range hostConfig.Mounts {
  146. mp, err := parser.ParseMountSpec(cfg)
  147. if err != nil {
  148. return errdefs.InvalidParameter(err)
  149. }
  150. needsSlavePropagation, err := daemon.validateBindDaemonRoot(mp.Spec)
  151. if err != nil {
  152. return err
  153. }
  154. if needsSlavePropagation {
  155. mp.Propagation = mount.PropagationRSlave
  156. }
  157. if binds[mp.Destination] {
  158. return duplicateMountPointError(cfg.Target)
  159. }
  160. if mp.Type == mounttypes.TypeVolume {
  161. var v *volumetypes.Volume
  162. if cfg.VolumeOptions != nil {
  163. var driverOpts map[string]string
  164. if cfg.VolumeOptions.DriverConfig != nil {
  165. driverOpts = cfg.VolumeOptions.DriverConfig.Options
  166. }
  167. v, err = daemon.volumes.Create(ctx,
  168. mp.Name,
  169. mp.Driver,
  170. volumeopts.WithCreateReference(container.ID),
  171. volumeopts.WithCreateOptions(driverOpts),
  172. volumeopts.WithCreateLabels(cfg.VolumeOptions.Labels),
  173. )
  174. } else {
  175. v, err = daemon.volumes.Create(ctx, mp.Name, mp.Driver, volumeopts.WithCreateReference(container.ID))
  176. }
  177. if err != nil {
  178. return err
  179. }
  180. mp.Volume = &volumeWrapper{v: v, s: daemon.volumes}
  181. mp.Name = v.Name
  182. mp.Driver = v.Driver
  183. // need to selinux-relabel local mounts
  184. mp.Source = v.Mountpoint
  185. if mp.Driver == volume.DefaultDriverName {
  186. setBindModeIfNull(mp)
  187. }
  188. }
  189. if mp.Type == mounttypes.TypeBind && (cfg.BindOptions == nil || !cfg.BindOptions.CreateMountpoint) {
  190. mp.SkipMountpointCreation = true
  191. }
  192. binds[mp.Destination] = true
  193. dereferenceIfExists(mp.Destination)
  194. mountPoints[mp.Destination] = mp
  195. }
  196. container.Lock()
  197. // 4. Cleanup old volumes that are about to be reassigned.
  198. for _, m := range mountPoints {
  199. if parser.IsBackwardCompatible(m) {
  200. if mp, exists := container.MountPoints[m.Destination]; exists && mp.Volume != nil {
  201. daemon.volumes.Release(ctx, mp.Volume.Name(), container.ID)
  202. }
  203. }
  204. }
  205. container.MountPoints = mountPoints
  206. container.Unlock()
  207. return nil
  208. }
  209. // lazyInitializeVolume initializes a mountpoint's volume if needed.
  210. // This happens after a daemon restart.
  211. func (daemon *Daemon) lazyInitializeVolume(containerID string, m *volumemounts.MountPoint) error {
  212. if len(m.Driver) > 0 && m.Volume == nil {
  213. v, err := daemon.volumes.Get(context.TODO(), m.Name, volumeopts.WithGetDriver(m.Driver), volumeopts.WithGetReference(containerID))
  214. if err != nil {
  215. return err
  216. }
  217. m.Volume = &volumeWrapper{v: v, s: daemon.volumes}
  218. }
  219. return nil
  220. }
  221. // VolumesService is used to perform volume operations
  222. func (daemon *Daemon) VolumesService() *service.VolumesService {
  223. return daemon.volumes
  224. }
  225. type volumeMounter interface {
  226. Mount(ctx context.Context, v *volumetypes.Volume, ref string) (string, error)
  227. Unmount(ctx context.Context, v *volumetypes.Volume, ref string) error
  228. LiveRestoreVolume(ctx context.Context, v *volumetypes.Volume, ref string) error
  229. }
  230. type volumeWrapper struct {
  231. v *volumetypes.Volume
  232. s volumeMounter
  233. }
  234. func (v *volumeWrapper) Name() string {
  235. return v.v.Name
  236. }
  237. func (v *volumeWrapper) DriverName() string {
  238. return v.v.Driver
  239. }
  240. func (v *volumeWrapper) Path() string {
  241. return v.v.Mountpoint
  242. }
  243. func (v *volumeWrapper) Mount(ref string) (string, error) {
  244. return v.s.Mount(context.TODO(), v.v, ref)
  245. }
  246. func (v *volumeWrapper) Unmount(ref string) error {
  247. return v.s.Unmount(context.TODO(), v.v, ref)
  248. }
  249. func (v *volumeWrapper) CreatedAt() (time.Time, error) {
  250. return time.Time{}, errors.New("not implemented")
  251. }
  252. func (v *volumeWrapper) Status() map[string]interface{} {
  253. return v.v.Status
  254. }
  255. func (v *volumeWrapper) LiveRestoreVolume(ctx context.Context, ref string) error {
  256. return v.s.LiveRestoreVolume(ctx, v.v, ref)
  257. }