2015-04-27 12:25:38 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2016-09-06 09:49:10 -04:00
|
|
|
"encoding/json"
|
2015-04-27 12:25:38 -04:00
|
|
|
"os"
|
2016-09-06 09:49:10 -04:00
|
|
|
"path/filepath"
|
2015-05-19 16:05:25 -04:00
|
|
|
"sort"
|
2015-12-21 19:45:31 -05:00
|
|
|
"strconv"
|
2016-09-06 09:49:10 -04:00
|
|
|
"strings"
|
2015-04-27 12:25:38 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-06-03 12:26:41 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2016-09-06 09:49:10 -04:00
|
|
|
"github.com/docker/docker/volume/drivers"
|
|
|
|
"github.com/docker/docker/volume/local"
|
|
|
|
"github.com/pkg/errors"
|
2015-04-27 12:25:38 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 19:10:56 -04: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.
|
2016-03-18 14:50:19 -04:00
|
|
|
func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
|
|
|
|
var mounts []container.Mount
|
2016-06-06 05:57:11 -04:00
|
|
|
// TODO: tmpfs mounts should be part of Mountpoints
|
|
|
|
tmpfsMounts := make(map[string]bool)
|
|
|
|
for _, m := range c.TmpfsMounts() {
|
|
|
|
tmpfsMounts[m.Destination] = true
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
for _, m := range c.MountPoints {
|
2016-06-06 05:57:11 -04:00
|
|
|
if tmpfsMounts[m.Destination] {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
|
2016-01-12 17:18:57 -05:00
|
|
|
return nil, err
|
2015-12-09 14:39:31 -05:00
|
|
|
}
|
2016-08-23 04:33:38 -04:00
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
|
|
|
path, err := m.Setup(c.MountLabel, rootUID, rootGID)
|
2015-05-19 16:05:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
if !c.TrySetNetworkMount(m.Destination, path) {
|
|
|
|
mnt := container.Mount{
|
2015-07-13 20:54:00 -04:00
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
2016-08-15 12:13:18 -04:00
|
|
|
Propagation: string(m.Propagation),
|
2015-10-23 16:57:57 -04:00
|
|
|
}
|
2015-12-21 19:45:31 -05:00
|
|
|
if m.Volume != nil {
|
|
|
|
attributes := map[string]string{
|
|
|
|
"driver": m.Volume.DriverName(),
|
2016-03-18 14:50:19 -04:00
|
|
|
"container": c.ID,
|
2015-12-21 19:45:31 -05:00
|
|
|
"destination": m.Destination,
|
|
|
|
"read/write": strconv.FormatBool(m.RW),
|
2016-08-15 12:13:18 -04:00
|
|
|
"propagation": string(m.Propagation),
|
2015-12-21 19:45:31 -05:00
|
|
|
}
|
|
|
|
daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
|
|
|
|
}
|
2015-10-23 16:57:57 -04:00
|
|
|
mounts = append(mounts, mnt)
|
2015-07-13 20:54:00 -04:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
2016-03-18 14:50:19 -04:00
|
|
|
netMounts := c.NetworkMounts()
|
2015-10-08 11:51:41 -04:00
|
|
|
// if we are going to mount any of the network files from container
|
|
|
|
// metadata, the ownership must be set properly for potential container
|
|
|
|
// remapped root (user namespaces)
|
2015-11-03 14:25:22 -05:00
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
2015-10-08 11:51:41 -04:00
|
|
|
for _, mount := range netMounts {
|
|
|
|
if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(mounts, netMounts...), nil
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 19:10:56 -04: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.
|
2016-03-18 14:50:19 -04:00
|
|
|
func sortMounts(m []container.Mount) []container.Mount {
|
2015-05-19 16:05:25 -04:00
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// setBindModeIfNull is platform specific processing to ensure the
|
|
|
|
// shared mode is set to 'z' if it is null. This is called in the case
|
|
|
|
// of processing a named volume and not a typical bind.
|
|
|
|
func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
|
|
|
|
if bind.Mode == "" {
|
|
|
|
bind.Mode = "z"
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
return bind
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
2016-09-06 09:49:10 -04: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 {
|
|
|
|
l, err := volumedrivers.GetDriver(volume.DefaultDriverName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newDataPath := l.(*local.Root).DataPath(id)
|
|
|
|
fi, err := os.Stat(newDataPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi != nil && fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Symlink(vfs, newDataPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.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.
|
|
|
|
type volumes struct {
|
|
|
|
Volumes map[string]string
|
|
|
|
VolumesRW map[string]bool
|
|
|
|
}
|
|
|
|
cfgPath, err := container.ConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Open(cfgPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not open container config")
|
|
|
|
}
|
|
|
|
var cv volumes
|
|
|
|
if err := json.NewDecoder(f).Decode(&cv); err != nil {
|
|
|
|
return errors.Wrap(err, "could not decode container config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(container.MountPoints) == 0 && len(cv.Volumes) > 0 {
|
|
|
|
for destination, hostPath := range cv.Volumes {
|
|
|
|
vfsPath := filepath.Join(daemon.root, "vfs", "dir")
|
|
|
|
rw := cv.VolumesRW != nil && cv.VolumesRW[destination]
|
|
|
|
|
|
|
|
if strings.HasPrefix(hostPath, vfsPath) {
|
|
|
|
id := filepath.Base(hostPath)
|
|
|
|
v, err := daemon.volumes.CreateWithRef(id, volume.DefaultDriverName, container.ID, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := migrateVolume(id, hostPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.AddMountPointWithVolume(destination, v, true)
|
|
|
|
} else { // Bind mount
|
|
|
|
m := volume.MountPoint{Source: hostPath, Destination: destination, RW: rw}
|
|
|
|
container.MountPoints[destination] = &m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return container.ToDisk()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|