volumes.go 8.4 KB

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