2014-04-17 21:43:01 +00:00
|
|
|
package daemon
|
2014-02-15 01:15:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2014-05-05 21:45:14 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/archive"
|
|
|
|
"github.com/dotcloud/docker/daemon/execdriver"
|
2014-05-13 17:54:08 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/symlink"
|
2014-02-15 01:15:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BindMap struct {
|
|
|
|
SrcPath string
|
|
|
|
DstPath string
|
|
|
|
Mode string
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareVolumesForContainer(container *Container) error {
|
|
|
|
if container.Volumes == nil || len(container.Volumes) == 0 {
|
|
|
|
container.Volumes = make(map[string]string)
|
|
|
|
container.VolumesRW = make(map[string]bool)
|
2014-02-20 17:18:27 +00:00
|
|
|
if err := applyVolumesFrom(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := createVolumes(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-14 06:40:49 +00:00
|
|
|
func setupMountsForContainer(container *Container) error {
|
2014-03-03 15:15:29 +00:00
|
|
|
mounts := []execdriver.Mount{
|
2014-04-17 21:43:01 +00:00
|
|
|
{container.daemon.sysInitPath, "/.dockerinit", false, true},
|
2014-03-03 15:15:29 +00:00
|
|
|
{container.ResolvConfPath, "/etc/resolv.conf", false, true},
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostnamePath != "" && container.HostsPath != "" {
|
2014-03-03 15:15:29 +00:00
|
|
|
mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true})
|
|
|
|
mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true})
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount user specified volumes
|
2014-03-03 15:15:29 +00:00
|
|
|
// 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
|
2014-02-15 01:15:40 +00:00
|
|
|
for r, v := range container.Volumes {
|
2014-03-03 15:15:29 +00:00
|
|
|
mounts = append(mounts, execdriver.Mount{v, r, container.VolumesRW[r], false})
|
|
|
|
}
|
2014-02-15 01:15:40 +00:00
|
|
|
|
2014-03-03 15:15:29 +00:00
|
|
|
container.command.Mounts = mounts
|
2014-02-15 01:15:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyVolumesFrom(container *Container) error {
|
2014-04-08 09:26:09 +00:00
|
|
|
volumesFrom := container.hostConfig.VolumesFrom
|
2014-04-08 10:02:17 +00:00
|
|
|
if len(volumesFrom) > 0 {
|
|
|
|
for _, containerSpec := range volumesFrom {
|
2014-02-15 01:15:40 +00:00
|
|
|
var (
|
|
|
|
mountRW = true
|
|
|
|
specParts = strings.SplitN(containerSpec, ":", 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
switch len(specParts) {
|
|
|
|
case 0:
|
2014-04-08 10:02:17 +00:00
|
|
|
return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
|
2014-02-15 01:15:40 +00:00
|
|
|
case 2:
|
|
|
|
switch specParts[1] {
|
|
|
|
case "ro":
|
|
|
|
mountRW = false
|
|
|
|
case "rw": // mountRW is already true
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 21:43:01 +00:00
|
|
|
c := container.daemon.Get(specParts[0])
|
2014-02-15 01:15:40 +00:00
|
|
|
if c == nil {
|
2014-04-03 14:27:07 +00:00
|
|
|
return fmt.Errorf("Container %s not found. Impossible to mount its volumes", specParts[0])
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 14:27:07 +00:00
|
|
|
if err := c.Mount(); err != nil {
|
|
|
|
return fmt.Errorf("Container %s failed to mount. Impossible to mount its volumes", specParts[0])
|
|
|
|
}
|
|
|
|
defer c.Unmount()
|
|
|
|
|
2014-02-15 01:15:40 +00:00
|
|
|
for volPath, id := range c.Volumes {
|
|
|
|
if _, exists := container.Volumes[volPath]; exists {
|
|
|
|
continue
|
|
|
|
}
|
2014-05-11 20:49:46 +00:00
|
|
|
stat, err := os.Stat(c.getResourcePath(volPath))
|
2014-03-18 22:28:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-11 20:49:46 +00:00
|
|
|
if err := createIfNotExists(container.getResourcePath(volPath), stat.IsDir()); err != nil {
|
2014-02-15 01:15:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Volumes[volPath] = id
|
|
|
|
if isRW, exists := c.VolumesRW[volPath]; exists {
|
|
|
|
container.VolumesRW[volPath] = isRW && mountRW
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBindMap(container *Container) (map[string]BindMap, error) {
|
|
|
|
var (
|
|
|
|
// Create the requested bind mounts
|
|
|
|
binds = make(map[string]BindMap)
|
|
|
|
// Define illegal container destinations
|
|
|
|
illegalDsts = []string{"/", "."}
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, bind := range container.hostConfig.Binds {
|
|
|
|
// FIXME: factorize bind parsing in parseBind
|
|
|
|
var (
|
|
|
|
src, dst, mode string
|
|
|
|
arr = strings.Split(bind, ":")
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(arr) == 2 {
|
|
|
|
src = arr[0]
|
|
|
|
dst = arr[1]
|
|
|
|
mode = "rw"
|
|
|
|
} else if len(arr) == 3 {
|
|
|
|
src = arr[0]
|
|
|
|
dst = arr[1]
|
|
|
|
mode = arr[2]
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Invalid bind specification: %s", bind)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail if trying to mount to an illegal destination
|
|
|
|
for _, illegal := range illegalDsts {
|
|
|
|
if dst == illegal {
|
|
|
|
return nil, fmt.Errorf("Illegal bind destination: %s", dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bindMap := BindMap{
|
|
|
|
SrcPath: src,
|
|
|
|
DstPath: dst,
|
|
|
|
Mode: mode,
|
|
|
|
}
|
|
|
|
binds[filepath.Clean(dst)] = bindMap
|
|
|
|
}
|
|
|
|
return binds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createVolumes(container *Container) error {
|
|
|
|
binds, err := getBindMap(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-17 21:43:01 +00:00
|
|
|
volumesDriver := container.daemon.volumes.Driver()
|
2014-02-15 01:15:40 +00:00
|
|
|
// Create the requested volumes if they don't exist
|
|
|
|
for volPath := range container.Config.Volumes {
|
|
|
|
volPath = filepath.Clean(volPath)
|
|
|
|
volIsDir := true
|
|
|
|
// Skip existing volumes
|
|
|
|
if _, exists := container.Volumes[volPath]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var srcPath string
|
|
|
|
var isBindMount bool
|
|
|
|
srcRW := false
|
|
|
|
// If an external bind is defined for this volume, use that as a source
|
|
|
|
if bindMap, exists := binds[volPath]; exists {
|
|
|
|
isBindMount = true
|
|
|
|
srcPath = bindMap.SrcPath
|
2014-04-08 03:20:03 +00:00
|
|
|
if !filepath.IsAbs(srcPath) {
|
|
|
|
return fmt.Errorf("%s must be an absolute path", srcPath)
|
2014-03-31 19:10:19 +00:00
|
|
|
}
|
2014-02-15 01:15:40 +00:00
|
|
|
if strings.ToLower(bindMap.Mode) == "rw" {
|
|
|
|
srcRW = true
|
|
|
|
}
|
|
|
|
if stat, err := os.Stat(bindMap.SrcPath); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
volIsDir = stat.IsDir()
|
|
|
|
}
|
|
|
|
// Otherwise create an directory in $ROOT/volumes/ and use that
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Do not pass a container as the parameter for the volume creation.
|
|
|
|
// The graph driver using the container's information ( Image ) to
|
|
|
|
// create the parent.
|
2014-04-17 21:43:01 +00:00
|
|
|
c, err := container.daemon.volumes.Create(nil, "", "", "", "", nil, nil)
|
2014-02-15 01:15:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-17 23:47:27 +00:00
|
|
|
srcPath, err = volumesDriver.Get(c.ID, "")
|
2014-02-15 01:15:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
|
|
|
|
}
|
|
|
|
srcRW = true // RW by default
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, err := filepath.EvalSymlinks(srcPath); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
srcPath = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the mountpoint
|
2014-05-13 17:54:08 +00:00
|
|
|
rootVolPath, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, volPath), container.basefs)
|
2014-04-25 01:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newVolPath, err := filepath.Rel(container.basefs, rootVolPath)
|
2014-02-15 01:15:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-25 01:22:22 +00:00
|
|
|
newVolPath = "/" + newVolPath
|
|
|
|
|
|
|
|
if volPath != newVolPath {
|
|
|
|
delete(container.Volumes, volPath)
|
|
|
|
delete(container.VolumesRW, volPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Volumes[newVolPath] = srcPath
|
|
|
|
container.VolumesRW[newVolPath] = srcRW
|
|
|
|
|
2014-03-18 22:28:40 +00:00
|
|
|
if err := createIfNotExists(rootVolPath, volIsDir); err != nil {
|
|
|
|
return err
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not copy or change permissions if we are mounting from the host
|
|
|
|
if srcRW && !isBindMount {
|
|
|
|
volList, err := ioutil.ReadDir(rootVolPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(volList) > 0 {
|
|
|
|
srcList, err := ioutil.ReadDir(srcPath)
|
|
|
|
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 := archive.CopyWithTar(rootVolPath, srcPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-10 01:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 01:15:40 +00:00
|
|
|
|
2014-04-10 01:50:46 +00:00
|
|
|
var stat syscall.Stat_t
|
|
|
|
if err := syscall.Stat(rootVolPath, &stat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var srcStat syscall.Stat_t
|
|
|
|
if err := syscall.Stat(srcPath, &srcStat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Change the source volume's ownership if it differs from the root
|
|
|
|
// files that were just copied
|
|
|
|
if stat.Uid != srcStat.Uid || stat.Gid != srcStat.Gid {
|
|
|
|
if err := os.Chown(srcPath, int(stat.Uid), int(stat.Gid)); err != nil {
|
|
|
|
return err
|
2014-02-15 01:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-03-18 22:28:40 +00:00
|
|
|
|
|
|
|
func createIfNotExists(path string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if isDir {
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|