2014-04-17 17:43:01 -04:00
|
|
|
package daemon
|
2014-02-14 20:15:40 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-09-15 20:51:06 -04:00
|
|
|
"sort"
|
2014-02-14 20:15:40 -05:00
|
|
|
"strings"
|
2014-05-05 17:45:14 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2014-10-29 15:06:51 -04:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-02-24 17:17:13 -05:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2014-02-14 20:15:40 -05:00
|
|
|
)
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
type volumeMount struct {
|
|
|
|
containerPath string
|
|
|
|
hostPath string
|
|
|
|
writable bool
|
|
|
|
copyData bool
|
|
|
|
from string
|
2014-08-08 11:00:18 -04:00
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (container *Container) prepareVolumes() error {
|
2014-02-14 20:15:40 -05:00
|
|
|
if container.Volumes == nil || len(container.Volumes) == 0 {
|
|
|
|
container.Volumes = make(map[string]string)
|
|
|
|
container.VolumesRW = make(map[string]bool)
|
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
if len(container.hostConfig.VolumesFrom) > 0 && container.AppliedVolumesFrom == nil {
|
|
|
|
container.AppliedVolumesFrom = make(map[string]struct{})
|
|
|
|
}
|
2014-08-28 10:18:08 -04:00
|
|
|
return container.createVolumes()
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
func (container *Container) createVolumes() error {
|
|
|
|
mounts := make(map[string]*volumeMount)
|
|
|
|
|
|
|
|
// get the normal volumes
|
|
|
|
for path := range container.Config.Volumes {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
// skip if there is already a volume for this container path
|
|
|
|
if _, exists := container.Volumes[path]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
realPath, err := container.getResourcePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stat, err := os.Stat(realPath); err == nil {
|
|
|
|
if !stat.IsDir() {
|
|
|
|
return fmt.Errorf("can't mount to container path, file exists - %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mnt := &volumeMount{
|
|
|
|
containerPath: path,
|
|
|
|
writable: true,
|
|
|
|
copyData: true,
|
|
|
|
}
|
|
|
|
mounts[mnt.containerPath] = mnt
|
2014-09-15 20:51:06 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
// Get all the bind mounts
|
|
|
|
// track bind paths separately due to #10618
|
|
|
|
bindPaths := make(map[string]struct{})
|
|
|
|
for _, spec := range container.hostConfig.Binds {
|
|
|
|
mnt, err := parseBindMountSpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-15 20:51:06 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
// #10618
|
|
|
|
if _, exists := bindPaths[mnt.containerPath]; exists {
|
|
|
|
return fmt.Errorf("Duplicate volume mount %s", mnt.containerPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
bindPaths[mnt.containerPath] = struct{}{}
|
|
|
|
mounts[mnt.containerPath] = mnt
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
// Get volumes from
|
|
|
|
for _, from := range container.hostConfig.VolumesFrom {
|
|
|
|
cID, mode, err := parseVolumesFromSpec(from)
|
|
|
|
if err != nil {
|
2014-08-28 10:18:08 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-12-16 10:06:45 -05:00
|
|
|
if _, exists := container.AppliedVolumesFrom[cID]; exists {
|
|
|
|
// skip since it's already been applied
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := container.daemon.Get(cID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("container %s not found, impossible to mount its volumes", cID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mnt := range c.volumeMounts() {
|
|
|
|
mnt.writable = mnt.writable && (mode == "rw")
|
|
|
|
mnt.from = cID
|
|
|
|
mounts[mnt.containerPath] = mnt
|
|
|
|
}
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
for _, mnt := range mounts {
|
|
|
|
containerMntPath, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, mnt.containerPath), container.basefs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-28 10:18:08 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
// Create the actual volume
|
|
|
|
v, err := container.daemon.volumes.FindOrCreateVolume(mnt.hostPath, mnt.writable)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-12 11:01:05 -05:00
|
|
|
}
|
2014-12-16 10:06:45 -05:00
|
|
|
|
|
|
|
container.VolumesRW[mnt.containerPath] = mnt.writable
|
|
|
|
container.Volumes[mnt.containerPath] = v.Path
|
|
|
|
v.AddContainer(container.ID)
|
|
|
|
if mnt.from != "" {
|
|
|
|
container.AppliedVolumesFrom[mnt.from] = struct{}{}
|
2014-12-12 11:01:05 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
if mnt.writable && mnt.copyData {
|
|
|
|
// Copy whatever is in the container at the containerPath to the volume
|
|
|
|
copyExistingContents(containerMntPath, v.Path)
|
2015-02-10 13:45:50 -05:00
|
|
|
}
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortedVolumeMounts returns the list of container volume mount points sorted in lexicographic order
|
|
|
|
func (container *Container) sortedVolumeMounts() []string {
|
|
|
|
var mountPaths []string
|
|
|
|
for path := range container.Volumes {
|
|
|
|
mountPaths = append(mountPaths, path)
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
sort.Strings(mountPaths)
|
|
|
|
return mountPaths
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
2014-08-21 17:57:46 -04:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (container *Container) VolumePaths() map[string]struct{} {
|
|
|
|
var paths = make(map[string]struct{})
|
|
|
|
for _, path := range container.Volumes {
|
|
|
|
paths[path] = struct{}{}
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
2014-08-28 10:18:08 -04:00
|
|
|
return paths
|
2014-08-21 17:57:46 -04:00
|
|
|
}
|
|
|
|
|
2014-10-08 13:13:32 -04:00
|
|
|
func (container *Container) registerVolumes() {
|
2015-01-16 14:48:25 -05:00
|
|
|
for path := range container.VolumePaths() {
|
|
|
|
if v := container.daemon.volumes.Get(path); v != nil {
|
|
|
|
v.AddContainer(container.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if container was created with an old daemon, this volume may not be registered so we need to make sure it gets registered
|
|
|
|
writable := true
|
|
|
|
if rw, exists := container.VolumesRW[path]; exists {
|
|
|
|
writable = rw
|
|
|
|
}
|
|
|
|
v, err := container.daemon.volumes.FindOrCreateVolume(path, writable)
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("error registering volume %s: %v", path, err)
|
2015-01-16 14:48:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.AddContainer(container.ID)
|
2014-10-08 13:13:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (container *Container) derefVolumes() {
|
|
|
|
for path := range container.VolumePaths() {
|
|
|
|
vol := container.daemon.volumes.Get(path)
|
|
|
|
if vol == nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Volume %s was not found and could not be dereferenced", path)
|
2014-08-28 10:18:08 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
vol.RemoveContainer(container.ID)
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
func parseBindMountSpec(spec string) (*volumeMount, error) {
|
|
|
|
arr := strings.Split(spec, ":")
|
2014-08-06 15:27:58 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
mnt := &volumeMount{}
|
2014-08-06 15:27:58 -04:00
|
|
|
switch len(arr) {
|
|
|
|
case 2:
|
2014-12-16 10:06:45 -05:00
|
|
|
mnt.hostPath = arr[0]
|
|
|
|
mnt.containerPath = arr[1]
|
|
|
|
mnt.writable = true
|
2014-08-06 15:27:58 -04:00
|
|
|
case 3:
|
2014-12-16 10:06:45 -05:00
|
|
|
mnt.hostPath = arr[0]
|
|
|
|
mnt.containerPath = arr[1]
|
|
|
|
mnt.writable = validMountMode(arr[2]) && arr[2] == "rw"
|
2014-08-06 15:27:58 -04:00
|
|
|
default:
|
2014-12-16 10:06:45 -05:00
|
|
|
return nil, fmt.Errorf("Invalid volume specification: %s", spec)
|
2014-08-06 15:27:58 -04:00
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
if !filepath.IsAbs(mnt.hostPath) {
|
|
|
|
return nil, fmt.Errorf("cannot bind mount volume: %s volume paths must be absolute.", mnt.hostPath)
|
2014-08-08 11:00:18 -04:00
|
|
|
}
|
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
mnt.hostPath = filepath.Clean(mnt.hostPath)
|
|
|
|
mnt.containerPath = filepath.Clean(mnt.containerPath)
|
|
|
|
return mnt, nil
|
2014-08-06 15:27:58 -04:00
|
|
|
}
|
|
|
|
|
2014-12-18 09:57:36 -05:00
|
|
|
func parseVolumesFromSpec(spec string) (string, string, error) {
|
|
|
|
specParts := strings.SplitN(spec, ":", 2)
|
|
|
|
if len(specParts) == 0 {
|
|
|
|
return "", "", fmt.Errorf("malformed volumes-from specification: %s", spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
id = specParts[0]
|
|
|
|
mode = "rw"
|
|
|
|
)
|
|
|
|
if len(specParts) == 2 {
|
|
|
|
mode = specParts[1]
|
|
|
|
if !validMountMode(mode) {
|
|
|
|
return "", "", fmt.Errorf("invalid mode for volumes-from: %s", mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id, mode, nil
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func validMountMode(mode string) bool {
|
|
|
|
validModes := map[string]bool{
|
|
|
|
"rw": true,
|
|
|
|
"ro": true,
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
return validModes[mode]
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
|
|
|
|
2015-02-24 17:17:13 -05:00
|
|
|
func (container *Container) specialMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
|
|
|
if container.ResolvConfPath != "" {
|
|
|
|
mounts = append(mounts, execdriver.Mount{Source: container.ResolvConfPath, Destination: "/etc/resolv.conf", Writable: true, Private: true})
|
|
|
|
}
|
|
|
|
if container.HostnamePath != "" {
|
|
|
|
mounts = append(mounts, execdriver.Mount{Source: container.HostnamePath, Destination: "/etc/hostname", Writable: true, Private: true})
|
|
|
|
}
|
|
|
|
if container.HostsPath != "" {
|
|
|
|
mounts = append(mounts, execdriver.Mount{Source: container.HostsPath, Destination: "/etc/hosts", Writable: true, Private: true})
|
|
|
|
}
|
|
|
|
return mounts
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
func (container *Container) setupMounts() error {
|
2015-02-06 07:31:53 -05:00
|
|
|
mounts := []execdriver.Mount{}
|
|
|
|
|
|
|
|
// Mount user specified volumes
|
|
|
|
// Note, these are not private because you may want propagation of (un)mounts from host
|
|
|
|
// volumes. For instance if you use -v /usr:/usr and the host later mounts /usr/share you
|
|
|
|
// want this new mount in the container
|
|
|
|
// These mounts must be ordered based on the length of the path that it is being mounted to (lexicographic)
|
|
|
|
for _, path := range container.sortedVolumeMounts() {
|
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.Volumes[path],
|
|
|
|
Destination: path,
|
|
|
|
Writable: container.VolumesRW[path],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:17:13 -05:00
|
|
|
mounts = append(mounts, container.specialMounts()...)
|
2014-12-01 14:54:14 -05:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
container.command.Mounts = mounts
|
2014-08-08 11:00:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
func (container *Container) volumeMounts() map[string]*volumeMount {
|
|
|
|
mounts := make(map[string]*volumeMount)
|
2014-08-28 10:18:08 -04:00
|
|
|
|
2014-12-16 10:06:45 -05:00
|
|
|
for containerPath, path := range container.Volumes {
|
|
|
|
v := container.daemon.volumes.Get(path)
|
|
|
|
if v == nil {
|
|
|
|
// This should never happen
|
|
|
|
logrus.Debugf("reference by container %s to non-existent volume path %s", container.ID, path)
|
|
|
|
continue
|
2014-08-28 10:18:08 -04:00
|
|
|
}
|
2014-12-16 10:06:45 -05:00
|
|
|
mounts[containerPath] = &volumeMount{hostPath: path, containerPath: containerPath, writable: container.VolumesRW[containerPath]}
|
2014-05-19 18:18:37 -04:00
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
|
2014-08-28 10:18:08 -04:00
|
|
|
return mounts
|
2014-05-19 18:18:37 -04:00
|
|
|
}
|
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
func copyExistingContents(source, destination string) error {
|
|
|
|
volList, err := ioutil.ReadDir(source)
|
2014-05-19 18:18:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(volList) > 0 {
|
2014-06-02 17:16:30 -04:00
|
|
|
srcList, err := ioutil.ReadDir(destination)
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
2014-03-18 18:28:40 -04:00
|
|
|
return err
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
|
|
|
|
if len(srcList) == 0 {
|
|
|
|
// If the source volume is empty copy files from the root into the volume
|
2014-10-29 15:06:51 -04:00
|
|
|
if err := chrootarchive.CopyWithTar(source, destination); err != nil {
|
2014-02-14 20:15:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
}
|
2014-03-18 18:28:40 -04:00
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
return copyOwnership(source, destination)
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:17:13 -05:00
|
|
|
func (container *Container) mountVolumes() error {
|
|
|
|
for dest, source := range container.Volumes {
|
|
|
|
v := container.daemon.volumes.Get(source)
|
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("could not find volume for %s:%s, impossible to mount", source, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
destPath, err := container.getResourcePath(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(source, destPath, "bind", "rbind,rw"); err != nil {
|
|
|
|
return fmt.Errorf("error while mounting volume %s: %v", source, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mnt := range container.specialMounts() {
|
|
|
|
destPath, err := container.getResourcePath(mnt.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mount.Mount(mnt.Source, destPath, "bind", "bind,rw"); err != nil {
|
|
|
|
return fmt.Errorf("error while mounting volume %s: %v", mnt.Source, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) unmountVolumes() {
|
|
|
|
for dest := range container.Volumes {
|
|
|
|
destPath, err := container.getResourcePath(dest)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error while unmounting volumes %s: %v", destPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := mount.ForceUnmount(destPath); err != nil {
|
|
|
|
logrus.Errorf("error while unmounting volumes %s: %v", destPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mnt := range container.specialMounts() {
|
|
|
|
destPath, err := container.getResourcePath(mnt.Destination)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error while unmounting volumes %s: %v", destPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := mount.ForceUnmount(destPath); err != nil {
|
|
|
|
logrus.Errorf("error while unmounting volumes %s: %v", destPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|