daemon/volumes.go
359b7df5
 package daemon
bd54d40f
 
 import (
c32dde5b
 	"errors"
a793564b
 	"fmt"
bd54d40f
 	"os"
 	"path/filepath"
 	"strings"
4994b0fe
 
f2e11fb8
 	"github.com/Sirupsen/logrus"
c452e1bf
 	dockererrors "github.com/docker/docker/api/errors"
91e197d6
 	"github.com/docker/docker/api/types"
 	containertypes "github.com/docker/docker/api/types/container"
fc7b904d
 	mounttypes "github.com/docker/docker/api/types/mount"
6bb0d181
 	"github.com/docker/docker/container"
81fa9feb
 	"github.com/docker/docker/volume"
f2e11fb8
 	"github.com/docker/docker/volume/drivers"
fc7b904d
 	"github.com/opencontainers/runc/libcontainer/label"
bd54d40f
 )
 
b3b7eb27
 var (
 	// ErrVolumeReadonly is used to signal an error when trying to copy data into
 	// a volume mount that is not writable.
 	ErrVolumeReadonly = errors.New("mounted volume is marked read-only")
 )
c32dde5b
 
9c4570a9
 type mounts []container.Mount
f3680e74
 
520e601d
 // volumeToAPIType converts a volume.Volume to the type used by the Engine API
a7e686a7
 func volumeToAPIType(v volume.Volume) *types.Volume {
fc214b44
 	tv := &types.Volume{
d3695274
 		Name:   v.Name(),
 		Driver: v.DriverName(),
7495fbc0
 	}
9ce8aac5
 	if v, ok := v.(volume.DetailedVolume); ok {
fc214b44
 		tv.Labels = v.Labels()
9ce8aac5
 		tv.Options = v.Options()
2f40b1b2
 		tv.Scope = v.Scope()
 	}
9ce8aac5
 
fc214b44
 	return tv
a7e686a7
 }
7495fbc0
 
a7e686a7
 // Len returns the number of mounts. Used in sorting.
 func (m mounts) Len() int {
 	return len(m)
 }
45407cf0
 
a7e686a7
 // Less returns true if the number of parts (a/b/c would be 3 parts) in the
 // mount indexed by parameter 1 is less than that of the mount indexed by
 // parameter 2. Used in sorting.
 func (m mounts) Less(i, j int) bool {
 	return m.parts(i) < m.parts(j)
f3680e74
 }
 
a7e686a7
 // Swap swaps two items in an array of mounts. Used in sorting
 func (m mounts) Swap(i, j int) {
 	m[i], m[j] = m[j], m[i]
 }
c32dde5b
 
a7e686a7
 // parts returns the number of parts in the destination of a mount. Used in sorting.
 func (m mounts) parts(i int) int {
 	return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
c32dde5b
 }
 
a7e686a7
 // registerMountPoints initializes the container mount points with the configured volumes and bind mounts.
 // It follows the next sequence to decide what to mount in each final destination:
 //
 // 1. Select the previously configured mount points for the containers, if any.
 // 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
 // 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
927b334e
 // 4. Cleanup old volumes that are about to be reassigned.
5e5e1d7a
 func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) (retErr error) {
a7e686a7
 	binds := map[string]bool{}
 	mountPoints := map[string]*volume.MountPoint{}
5e5e1d7a
 	defer func() {
 		// clean up the container mountpoints once return with error
 		if retErr != nil {
 			for _, m := range mountPoints {
 				if m.Volume == nil {
 					continue
 				}
 				daemon.volumes.Dereference(m.Volume, container.ID)
 			}
 		}
 	}()
a7e686a7
 
 	// 1. Read already configured mount points.
57cec5a7
 	for destination, point := range container.MountPoints {
 		mountPoints[destination] = point
7495fbc0
 	}
 
a7e686a7
 	// 2. Read volumes from other containers.
 	for _, v := range hostConfig.VolumesFrom {
 		containerID, mode, err := volume.ParseVolumesFrom(v)
 		if err != nil {
 			return err
 		}
7495fbc0
 
d7d512bb
 		c, err := daemon.GetContainer(containerID)
a7e686a7
 		if err != nil {
 			return err
 		}
 
 		for _, m := range c.MountPoints {
 			cp := &volume.MountPoint{
 				Name:        m.Name,
 				Source:      m.Source,
 				RW:          m.RW && volume.ReadWrite(mode),
 				Driver:      m.Driver,
 				Destination: m.Destination,
a2dc4f79
 				Propagation: m.Propagation,
fc7b904d
 				Spec:        m.Spec,
 				CopyData:    false,
a7e686a7
 			}
 
 			if len(cp.Source) == 0 {
d3eca445
 				v, err := daemon.volumes.GetWithRef(cp.Name, cp.Driver, container.ID)
a7e686a7
 				if err != nil {
 					return err
 				}
 				cp.Volume = v
 			}
 
 			mountPoints[cp.Destination] = cp
 		}
d535d981
 	}
a7e686a7
 
 	// 3. Read bind mounts
 	for _, b := range hostConfig.Binds {
fc7b904d
 		bind, err := volume.ParseMountRaw(b, hostConfig.VolumeDriver)
e454be75
 		if err != nil {
a57900e3
 			return err
bd54d40f
 		}
a7e686a7
 
fc7b904d
 		// #10618
756f6cef
 		_, tmpfsExists := hostConfig.Tmpfs[bind.Destination]
 		if binds[bind.Destination] || tmpfsExists {
a793564b
 			return fmt.Errorf("Duplicate mount point '%s'", bind.Destination)
a7e686a7
 		}
 
fc7b904d
 		if bind.Type == mounttypes.TypeVolume {
a7e686a7
 			// create the volume
fc214b44
 			v, err := daemon.volumes.CreateWithRef(bind.Name, bind.Driver, container.ID, nil, nil)
a7e686a7
 			if err != nil {
bd54d40f
 				return err
 			}
a7e686a7
 			bind.Volume = v
 			bind.Source = v.Path()
 			// bind.Name is an already existing volume, we need to use that here
 			bind.Driver = v.DriverName()
fc7b904d
 			if bind.Driver == volume.DefaultDriverName {
 				setBindModeIfNull(bind)
843a119d
 			}
a7e686a7
 		}
b0ac69b6
 
a7e686a7
 		binds[bind.Destination] = true
 		mountPoints[bind.Destination] = bind
d535d981
 	}
b3b7eb27
 
fc7b904d
 	for _, cfg := range hostConfig.Mounts {
 		mp, err := volume.ParseMountSpec(cfg)
 		if err != nil {
 			return dockererrors.NewBadRequestError(err)
 		}
 
 		if binds[mp.Destination] {
 			return fmt.Errorf("Duplicate mount point '%s'", cfg.Target)
 		}
 
 		if mp.Type == mounttypes.TypeVolume {
 			var v volume.Volume
 			if cfg.VolumeOptions != nil {
 				var driverOpts map[string]string
 				if cfg.VolumeOptions.DriverConfig != nil {
 					driverOpts = cfg.VolumeOptions.DriverConfig.Options
 				}
 				v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, driverOpts, cfg.VolumeOptions.Labels)
 			} else {
 				v, err = daemon.volumes.CreateWithRef(mp.Name, mp.Driver, container.ID, nil, nil)
 			}
 			if err != nil {
 				return err
 			}
 
 			if err := label.Relabel(mp.Source, container.MountLabel, false); err != nil {
 				return err
 			}
 			mp.Volume = v
 			mp.Name = v.Name()
 			mp.Driver = v.DriverName()
 
e6866492
 			// only use the cached path here since getting the path is not necessary right now and calling `Path()` may be slow
fc7b904d
 			if cv, ok := v.(interface {
 				CachedPath() string
 			}); ok {
 				mp.Source = cv.CachedPath()
 			}
 		}
 
 		binds[mp.Destination] = true
 		mountPoints[mp.Destination] = mp
 	}
 
a7e686a7
 	container.Lock()
060f4ae6
 
927b334e
 	// 4. Cleanup old volumes that are about to be reassigned.
060f4ae6
 	for _, m := range mountPoints {
 		if m.BackwardsCompatible() {
 			if mp, exists := container.MountPoints[m.Destination]; exists && mp.Volume != nil {
d3eca445
 				daemon.volumes.Dereference(mp.Volume, container.ID)
060f4ae6
 			}
 		}
 	}
a7e686a7
 	container.MountPoints = mountPoints
 
 	container.Unlock()
 
 	return nil
b3b7eb27
 }
aab35963
 
 // lazyInitializeVolume initializes a mountpoint's volume if needed.
 // This happens after a daemon restart.
 func (daemon *Daemon) lazyInitializeVolume(containerID string, m *volume.MountPoint) error {
 	if len(m.Driver) > 0 && m.Volume == nil {
 		v, err := daemon.volumes.GetWithRef(m.Name, m.Driver, containerID)
 		if err != nil {
 			return err
 		}
 		m.Volume = v
 	}
 	return nil
 }
29b1c1da
 
 func backportMountSpec(container *container.Container) error {
 	for target, m := range container.MountPoints {
 		if m.Spec.Type != "" {
 			// if type is set on even one mount, no need to migrate
 			return nil
 		}
 		if m.Name != "" {
 			m.Type = mounttypes.TypeVolume
 			m.Spec.Type = mounttypes.TypeVolume
 
 			// make sure this is not an anyonmous volume before setting the spec source
 			if _, exists := container.Config.Volumes[target]; !exists {
 				m.Spec.Source = m.Name
 			}
 			if container.HostConfig.VolumeDriver != "" {
 				m.Spec.VolumeOptions = &mounttypes.VolumeOptions{
 					DriverConfig: &mounttypes.Driver{Name: container.HostConfig.VolumeDriver},
 				}
 			}
 			if strings.Contains(m.Mode, "nocopy") {
 				if m.Spec.VolumeOptions == nil {
 					m.Spec.VolumeOptions = &mounttypes.VolumeOptions{}
 				}
 				m.Spec.VolumeOptions.NoCopy = true
 			}
 		} else {
 			m.Type = mounttypes.TypeBind
 			m.Spec.Type = mounttypes.TypeBind
 			m.Spec.Source = m.Source
 			if m.Propagation != "" {
 				m.Spec.BindOptions = &mounttypes.BindOptions{
 					Propagation: m.Propagation,
 				}
 			}
 		}
 
 		m.Spec.Target = m.Destination
 		if !m.RW {
 			m.Spec.ReadOnly = true
 		}
 	}
 	return container.ToDiskLocking()
 }
f2e11fb8
 
 func (daemon *Daemon) traverseLocalVolumes(fn func(volume.Volume) error) error {
 	localVolumeDriver, err := volumedrivers.GetDriver(volume.DefaultDriverName)
 	if err != nil {
 		return fmt.Errorf("can't retrieve local volume driver: %v", err)
 	}
 	vols, err := localVolumeDriver.List()
 	if err != nil {
 		return fmt.Errorf("can't retrieve local volumes: %v", err)
 	}
 
 	for _, v := range vols {
 		name := v.Name()
 		_, err := daemon.volumes.Get(name)
 		if err != nil {
 			logrus.Warnf("failed to retrieve volume %s from store: %v", name, err)
 		}
 
 		err = fn(v)
 		if err != nil {
 			return err
 		}
 	}
 
 	return nil
 }