daemon/volumes.go
359b7df5
 package daemon
bd54d40f
 
 import (
c32dde5b
 	"errors"
a793564b
 	"fmt"
bd54d40f
 	"os"
 	"path/filepath"
 	"strings"
4994b0fe
 
6bb0d181
 	"github.com/docker/docker/container"
81fa9feb
 	"github.com/docker/docker/volume"
907407d0
 	"github.com/docker/engine-api/types"
 	containertypes "github.com/docker/engine-api/types/container"
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
 
a7e686a7
 // volumeToAPIType converts a volume.Volume to the type used by the remote API
 func volumeToAPIType(v volume.Volume) *types.Volume {
fc214b44
 	tv := &types.Volume{
9e6b1852
 		Name:   v.Name(),
 		Driver: v.DriverName(),
7495fbc0
 	}
2f40b1b2
 	if v, ok := v.(volume.LabeledVolume); ok {
fc214b44
 		tv.Labels = v.Labels()
 	}
2f40b1b2
 
 	if v, ok := v.(volume.ScopedVolume); ok {
 		tv.Scope = v.Scope()
 	}
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.
 	for name, point := range container.MountPoints {
 		mountPoints[name] = 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,
dd7d1c8a
 				Named:       m.Named,
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 {
 		// #10618
 		bind, err := volume.ParseMountSpec(b, hostConfig.VolumeDriver)
e454be75
 		if err != nil {
a57900e3
 			return err
bd54d40f
 		}
a7e686a7
 
034d555d
 		_, tmpfsExists := hostConfig.Tmpfs[bind.Destination]
 		if binds[bind.Destination] || tmpfsExists {
a793564b
 			return fmt.Errorf("Duplicate mount point '%s'", bind.Destination)
a7e686a7
 		}
 
00ec6102
 		if len(bind.Name) > 0 {
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()
dd7d1c8a
 			bind.Named = true
843a119d
 			if bind.Driver == "local" {
 				bind = setBindModeIfNull(bind)
 			}
a7e686a7
 		}
b0ac69b6
 
a7e686a7
 		binds[bind.Destination] = true
 		mountPoints[bind.Destination] = bind
d535d981
 	}
b3b7eb27
 
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
 }