2015-04-27 12:25:38 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-07-16 17:14:58 -04:00
|
|
|
"io/ioutil"
|
2015-04-27 12:25:38 -04:00
|
|
|
"os"
|
2015-05-19 16:05:25 -04:00
|
|
|
"sort"
|
2015-04-27 12:25:38 -04:00
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-09-09 22:23:06 -04:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-04-27 12:25:38 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-06-03 12:26:41 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2015-06-12 09:25:32 -04:00
|
|
|
volumedrivers "github.com/docker/docker/volume/drivers"
|
2015-06-03 12:26:41 -04:00
|
|
|
"github.com/docker/docker/volume/local"
|
2015-04-27 12:25:38 -04:00
|
|
|
)
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// copyExistingContents copies from the source to the destination and
|
|
|
|
// ensures the ownership is appropriately set.
|
|
|
|
func copyExistingContents(source, destination string) error {
|
|
|
|
volList, err := ioutil.ReadDir(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(volList) > 0 {
|
|
|
|
srcList, err := ioutil.ReadDir(destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(srcList) == 0 {
|
|
|
|
// If the source volume is empty copy files from the root into the volume
|
|
|
|
if err := chrootarchive.CopyWithTar(source, destination); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copyOwnership(source, destination)
|
|
|
|
}
|
|
|
|
|
2015-04-27 12:25:38 -04:00
|
|
|
// copyOwnership copies the permissions and uid:gid of the source file
|
2015-07-30 19:10:56 -04:00
|
|
|
// to the destination file
|
2015-04-27 12:25:38 -04:00
|
|
|
func copyOwnership(source, destination string) error {
|
|
|
|
stat, err := system.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-12 10:58:33 -04:00
|
|
|
if err := os.Chown(destination, int(stat.UID()), int(stat.GID())); err != nil {
|
2015-04-27 12:25:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Chmod(destination, os.FileMode(stat.Mode()))
|
|
|
|
}
|
2015-04-29 18:53:35 -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.
|
2015-11-03 14:25:22 -05:00
|
|
|
func (daemon *Daemon) setupMounts(container *Container) ([]execdriver.Mount, error) {
|
2015-05-19 16:05:25 -04:00
|
|
|
var mounts []execdriver.Mount
|
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
path, err := m.Setup()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-13 20:54:00 -04:00
|
|
|
if !container.trySetNetworkMount(m.Destination, path) {
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
|
|
|
})
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
2015-10-08 11:51:41 -04:00
|
|
|
netMounts := container.networkMounts()
|
|
|
|
// 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.
|
2015-05-19 16:05:25 -04:00
|
|
|
func sortMounts(m []execdriver.Mount) []execdriver.Mount {
|
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-06-03 12:26:41 -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 {
|
2015-06-12 09:25:32 -04:00
|
|
|
l, err := volumedrivers.Lookup(volume.DefaultDriverName)
|
2015-06-03 12:26:41 -04:00
|
|
|
if err != nil {
|
2015-06-08 16:45:28 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:26:41 -04:00
|
|
|
newDataPath := l.(*local.Root).DataPath(id)
|
|
|
|
fi, err := os.Stat(newDataPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2015-06-08 16:45:28 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 12:26:41 -04: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 16:45:28 -04:00
|
|
|
}
|
|
|
|
|
2015-06-03 12:26:41 -04:00
|
|
|
return true
|
2015-06-08 16:45:28 -04:00
|
|
|
}
|
2015-07-16 17:14:58 -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
|
|
|
}
|