2015-04-27 16:25:38 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-07-16 21:14:58 +00:00
|
|
|
"io/ioutil"
|
2015-04-27 16:25:38 +00:00
|
|
|
"os"
|
2015-05-19 20:05:25 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-04-27 16:25:38 +00:00
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-29 22:53:35 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-17 18:54:14 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-04-27 16:25:38 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-06-03 16:26:41 +00:00
|
|
|
"github.com/docker/docker/volume"
|
2015-06-12 13:25:32 +00:00
|
|
|
volumedrivers "github.com/docker/docker/volume/drivers"
|
2015-06-03 16:26:41 +00:00
|
|
|
"github.com/docker/docker/volume/local"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2015-04-27 16:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// copyOwnership copies the permissions and uid:gid of the source file
|
2015-07-30 23:10:56 +00:00
|
|
|
// to the destination file
|
2015-04-27 16:25:38 +00:00
|
|
|
func copyOwnership(source, destination string) error {
|
|
|
|
stat, err := system.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:13:12 +00:00
|
|
|
if err := os.Chown(destination, int(stat.UID()), int(stat.Gid())); err != nil {
|
2015-04-27 16:25:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Chmod(destination, os.FileMode(stat.Mode()))
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// setupMounts iterates through each of the mount points for a container and
|
|
|
|
// calls Setup() on each. It also looks to see if is a network mount such as
|
|
|
|
// /etc/resolv.conf, and if it is not, appends it to the array of mounts.
|
2015-05-19 20:05:25 +00:00
|
|
|
func (container *Container) setupMounts() ([]execdriver.Mount, error) {
|
|
|
|
var mounts []execdriver.Mount
|
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
path, err := m.Setup()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-14 00:54:00 +00:00
|
|
|
if !container.trySetNetworkMount(m.Destination, path) {
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
|
|
|
})
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
|
|
|
return append(mounts, container.networkMounts()...), nil
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// parseBindMount validates the configuration of mount information in runconfig is valid.
|
2015-09-09 20:18:13 +00:00
|
|
|
func parseBindMount(spec, volumeDriver string) (*mountPoint, error) {
|
2015-07-16 21:14:58 +00:00
|
|
|
bind := &mountPoint{
|
|
|
|
RW: true,
|
|
|
|
}
|
|
|
|
arr := strings.Split(spec, ":")
|
|
|
|
|
|
|
|
switch len(arr) {
|
|
|
|
case 2:
|
|
|
|
bind.Destination = arr[1]
|
|
|
|
case 3:
|
|
|
|
bind.Destination = arr[1]
|
|
|
|
mode := arr[2]
|
2015-08-24 09:28:19 +00:00
|
|
|
if !volume.ValidMountMode(mode) {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeVolumeInvalidMode.WithArgs(mode)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
2015-08-24 09:28:19 +00:00
|
|
|
bind.RW = volume.ReadWrite(mode)
|
2015-07-16 21:14:58 +00:00
|
|
|
// Mode field is used by SELinux to decide whether to apply label
|
|
|
|
bind.Mode = mode
|
|
|
|
default:
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeVolumeInvalid.WithArgs(spec)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 02:48:34 +00:00
|
|
|
//validate the volumes destination path
|
|
|
|
if !filepath.IsAbs(bind.Destination) {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeVolumeAbs.WithArgs(bind.Destination)
|
2015-08-31 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
name, source, err := parseVolumeSource(arr[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(source) == 0 {
|
2015-08-24 17:57:39 +00:00
|
|
|
bind.Driver = volumeDriver
|
2015-07-16 21:14:58 +00:00
|
|
|
if len(bind.Driver) == 0 {
|
|
|
|
bind.Driver = volume.DefaultDriverName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bind.Source = filepath.Clean(source)
|
|
|
|
}
|
|
|
|
|
|
|
|
bind.Name = name
|
|
|
|
bind.Destination = filepath.Clean(bind.Destination)
|
|
|
|
return bind, nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// sortMounts sorts an array of mounts in lexicographic order. This ensure that
|
|
|
|
// when mounting, the mounts don't shadow other mounts. For example, if mounting
|
|
|
|
// /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
|
2015-05-19 20:05:25 +00:00
|
|
|
func sortMounts(m []execdriver.Mount) []execdriver.Mount {
|
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-05-19 20:05:25 +00:00
|
|
|
type mounts []execdriver.Mount
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// Len returns the number of mounts
|
2015-05-19 20:05:25 +00:00
|
|
|
func (m mounts) Len() int {
|
|
|
|
return len(m)
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// 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.
|
2015-05-19 20:05:25 +00:00
|
|
|
func (m mounts) Less(i, j int) bool {
|
|
|
|
return m.parts(i) < m.parts(j)
|
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// Swap swaps two items in an array of mounts.
|
2015-05-19 20:05:25 +00:00
|
|
|
func (m mounts) Swap(i, j int) {
|
|
|
|
m[i], m[j] = m[j], m[i]
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// parts returns the number of parts in the destination of a mount.
|
2015-05-19 20:05:25 +00:00
|
|
|
func (m mounts) parts(i int) int {
|
|
|
|
return len(strings.Split(filepath.Clean(m[i].Destination), string(os.PathSeparator)))
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-06-08 20:45:28 +00:00
|
|
|
|
2015-06-03 16:26:41 +00:00
|
|
|
// migrateVolume links the contents of a volume created pre Docker 1.7
|
|
|
|
// into the location expected by the local driver.
|
|
|
|
// It creates a symlink from DOCKER_ROOT/vfs/dir/VOLUME_ID to DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
|
|
|
|
// It preserves the volume json configuration generated pre Docker 1.7 to be able to
|
|
|
|
// downgrade from Docker 1.7 to Docker 1.6 without losing volume compatibility.
|
|
|
|
func migrateVolume(id, vfs string) error {
|
2015-06-12 13:25:32 +00:00
|
|
|
l, err := volumedrivers.Lookup(volume.DefaultDriverName)
|
2015-06-03 16:26:41 +00:00
|
|
|
if err != nil {
|
2015-06-08 20:45:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:26:41 +00:00
|
|
|
newDataPath := l.(*local.Root).DataPath(id)
|
|
|
|
fi, err := os.Stat(newDataPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2015-06-08 20:45:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:26:41 +00:00
|
|
|
if fi != nil && fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Symlink(vfs, newDataPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validVolumeLayout checks whether the volume directory layout
|
|
|
|
// is valid to work with Docker post 1.7 or not.
|
|
|
|
func validVolumeLayout(files []os.FileInfo) bool {
|
|
|
|
if len(files) == 1 && files[0].Name() == local.VolumeDataPathName && files[0].IsDir() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) != 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() == "config.json" ||
|
|
|
|
(f.Name() == local.VolumeDataPathName && f.Mode()&os.ModeSymlink == os.ModeSymlink) {
|
|
|
|
// Old volume configuration, we ignore it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return false
|
2015-06-08 20:45:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 16:26:41 +00:00
|
|
|
return true
|
2015-06-08 20:45:28 +00:00
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
|
|
|
|
// verifyVolumesInfo ports volumes configured for the containers pre docker 1.7.
|
|
|
|
// It reads the container configuration and creates valid mount points for the old volumes.
|
|
|
|
func (daemon *Daemon) verifyVolumesInfo(container *Container) error {
|
|
|
|
// Inspect old structures only when we're upgrading from old versions
|
|
|
|
// to versions >= 1.7 and the MountPoints has not been populated with volumes data.
|
|
|
|
if len(container.MountPoints) == 0 && len(container.Volumes) > 0 {
|
|
|
|
for destination, hostPath := range container.Volumes {
|
|
|
|
vfsPath := filepath.Join(daemon.root, "vfs", "dir")
|
|
|
|
rw := container.VolumesRW != nil && container.VolumesRW[destination]
|
|
|
|
|
|
|
|
if strings.HasPrefix(hostPath, vfsPath) {
|
|
|
|
id := filepath.Base(hostPath)
|
|
|
|
if err := migrateVolume(id, hostPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.addLocalMountPoint(id, destination, rw)
|
|
|
|
} else { // Bind mount
|
|
|
|
id, source, err := parseVolumeSource(hostPath)
|
|
|
|
// We should not find an error here coming
|
|
|
|
// from the old configuration, but who knows.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.addBindMountPoint(id, source, destination, rw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(container.MountPoints) > 0 {
|
|
|
|
// Volumes created with a Docker version >= 1.7. We verify integrity in case of data created
|
|
|
|
// with Docker 1.7 RC versions that put the information in
|
|
|
|
// DOCKER_ROOT/volumes/VOLUME_ID rather than DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
|
2015-06-12 13:25:32 +00:00
|
|
|
l, err := volumedrivers.Lookup(volume.DefaultDriverName)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
if m.Driver != volume.DefaultDriverName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataPath := l.(*local.Root).DataPath(m.Name)
|
|
|
|
volumePath := filepath.Dir(dataPath)
|
|
|
|
|
|
|
|
d, err := ioutil.ReadDir(volumePath)
|
|
|
|
if err != nil {
|
|
|
|
// If the volume directory doesn't exist yet it will be recreated,
|
|
|
|
// so we only return the error when there is a different issue.
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Do not check when the volume directory does not exist.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if validVolumeLayout(d) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(dataPath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move data inside the data directory
|
|
|
|
for _, f := range d {
|
|
|
|
oldp := filepath.Join(volumePath, f.Name())
|
|
|
|
newp := filepath.Join(dataPath, f.Name())
|
|
|
|
if err := os.Rename(oldp, newp); err != nil {
|
|
|
|
logrus.Errorf("Unable to move %s to %s\n", oldp, newp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.toDiskLocking()
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// parseVolumesFrom ensure that the supplied volumes-from is valid.
|
2015-07-16 21:14:58 +00:00
|
|
|
func parseVolumesFrom(spec string) (string, string, error) {
|
|
|
|
if len(spec) == 0 {
|
2015-09-16 18:56:26 +00:00
|
|
|
return "", "", derr.ErrorCodeVolumeFromBlank.WithArgs(spec)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
specParts := strings.SplitN(spec, ":", 2)
|
|
|
|
id := specParts[0]
|
|
|
|
mode := "rw"
|
|
|
|
|
|
|
|
if len(specParts) == 2 {
|
|
|
|
mode = specParts[1]
|
2015-08-24 09:28:19 +00:00
|
|
|
if !volume.ValidMountMode(mode) {
|
2015-09-16 18:56:26 +00:00
|
|
|
return "", "", derr.ErrorCodeVolumeMode.WithArgs(mode)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return id, mode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (daemon *Daemon) registerMountPoints(container *Container, hostConfig *runconfig.HostConfig) error {
|
2015-07-16 21:14:58 +00:00
|
|
|
binds := map[string]bool{}
|
|
|
|
mountPoints := map[string]*mountPoint{}
|
|
|
|
|
|
|
|
// 1. Read already configured mount points.
|
|
|
|
for name, point := range container.MountPoints {
|
|
|
|
mountPoints[name] = point
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Read volumes from other containers.
|
|
|
|
for _, v := range hostConfig.VolumesFrom {
|
|
|
|
containerID, mode, err := parseVolumesFrom(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
c, err := daemon.Get(containerID)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range c.MountPoints {
|
|
|
|
cp := &mountPoint{
|
|
|
|
Name: m.Name,
|
|
|
|
Source: m.Source,
|
|
|
|
RW: m.RW && volume.ReadWrite(mode),
|
|
|
|
Driver: m.Driver,
|
|
|
|
Destination: m.Destination,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cp.Source) == 0 {
|
2015-06-12 13:25:32 +00:00
|
|
|
v, err := daemon.createVolume(cp.Name, cp.Driver, nil)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cp.Volume = v
|
|
|
|
}
|
|
|
|
|
|
|
|
mountPoints[cp.Destination] = cp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Read bind mounts
|
|
|
|
for _, b := range hostConfig.Binds {
|
|
|
|
// #10618
|
2015-09-09 20:18:13 +00:00
|
|
|
bind, err := parseBindMount(b, hostConfig.VolumeDriver)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if binds[bind.Destination] {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeVolumeDup.WithArgs(bind.Destination)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(bind.Name) > 0 && len(bind.Driver) > 0 {
|
|
|
|
// create the volume
|
2015-06-12 13:25:32 +00:00
|
|
|
v, err := daemon.createVolume(bind.Name, bind.Driver, nil)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bind.Volume = v
|
|
|
|
bind.Source = v.Path()
|
2015-09-02 00:13:38 +00:00
|
|
|
// bind.Name is an already existing volume, we need to use that here
|
|
|
|
bind.Driver = v.DriverName()
|
2015-07-16 21:14:58 +00:00
|
|
|
// Since this is just a named volume and not a typical bind, set to shared mode `z`
|
|
|
|
if bind.Mode == "" {
|
|
|
|
bind.Mode = "z"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 20:17:59 +00:00
|
|
|
shared := label.IsShared(bind.Mode)
|
|
|
|
if err := label.Relabel(bind.Source, container.MountLabel, shared); err != nil {
|
2015-07-16 21:14:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
binds[bind.Destination] = true
|
|
|
|
mountPoints[bind.Destination] = bind
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep backwards compatible structures
|
|
|
|
bcVolumes := map[string]string{}
|
|
|
|
bcVolumesRW := map[string]bool{}
|
|
|
|
for _, m := range mountPoints {
|
|
|
|
if m.BackwardsCompatible() {
|
|
|
|
bcVolumes[m.Destination] = m.Path()
|
|
|
|
bcVolumesRW[m.Destination] = m.RW
|
2015-06-12 13:25:32 +00:00
|
|
|
|
|
|
|
// This mountpoint is replacing an existing one, so the count needs to be decremented
|
|
|
|
if mp, exists := container.MountPoints[m.Destination]; exists && mp.Volume != nil {
|
|
|
|
daemon.volumes.Decrement(mp.Volume)
|
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Lock()
|
|
|
|
container.MountPoints = mountPoints
|
|
|
|
container.Volumes = bcVolumes
|
|
|
|
container.VolumesRW = bcVolumesRW
|
|
|
|
container.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// createVolume creates a volume.
|
2015-06-12 13:25:32 +00:00
|
|
|
func (daemon *Daemon) createVolume(name, driverName string, opts map[string]string) (volume.Volume, error) {
|
|
|
|
v, err := daemon.volumes.Create(name, driverName, opts)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
daemon.volumes.Increment(v)
|
|
|
|
return v, nil
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// parseVolumeSource parses the origin sources that's mounted into the container.
|
2015-07-16 21:14:58 +00:00
|
|
|
func parseVolumeSource(spec string) (string, string, error) {
|
|
|
|
if !filepath.IsAbs(spec) {
|
|
|
|
return spec, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackwardsCompatible decides whether this mount point can be
|
|
|
|
// used in old versions of Docker or not.
|
|
|
|
// Only bind mounts and local volumes can be used in old versions of Docker.
|
|
|
|
func (m *mountPoint) BackwardsCompatible() bool {
|
|
|
|
return len(m.Source) > 0 || m.Driver == volume.DefaultDriverName
|
|
|
|
}
|