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"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2014-05-05 17:45:14 -04:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/archive"
|
|
|
|
"github.com/dotcloud/docker/daemon/execdriver"
|
2014-05-13 13:54:08 -04:00
|
|
|
"github.com/dotcloud/docker/pkg/symlink"
|
2014-02-14 20:15:40 -05: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 12:18:27 -05:00
|
|
|
if err := applyVolumesFrom(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := createVolumes(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-14 02:40:49 -04:00
|
|
|
func setupMountsForContainer(container *Container) error {
|
2014-03-03 10:15:29 -05:00
|
|
|
mounts := []execdriver.Mount{
|
2014-04-17 17:43:01 -04:00
|
|
|
{container.daemon.sysInitPath, "/.dockerinit", false, true},
|
2014-03-03 10:15:29 -05:00
|
|
|
{container.ResolvConfPath, "/etc/resolv.conf", false, true},
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:01:25 -04:00
|
|
|
if container.HostnamePath != "" {
|
2014-03-03 10:15:29 -05:00
|
|
|
mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true})
|
2014-05-16 18:01:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostsPath != "" {
|
2014-03-03 10:15:29 -05:00
|
|
|
mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true})
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount user specified volumes
|
2014-03-03 10:15:29 -05: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-14 20:15:40 -05:00
|
|
|
for r, v := range container.Volumes {
|
2014-03-03 10:15:29 -05:00
|
|
|
mounts = append(mounts, execdriver.Mount{v, r, container.VolumesRW[r], false})
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-03-03 10:15:29 -05:00
|
|
|
container.command.Mounts = mounts
|
2014-02-14 20:15:40 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyVolumesFrom(container *Container) error {
|
2014-04-08 05:26:09 -04:00
|
|
|
volumesFrom := container.hostConfig.VolumesFrom
|
2014-04-08 06:02:17 -04:00
|
|
|
if len(volumesFrom) > 0 {
|
|
|
|
for _, containerSpec := range volumesFrom {
|
2014-02-14 20:15:40 -05:00
|
|
|
var (
|
|
|
|
mountRW = true
|
|
|
|
specParts = strings.SplitN(containerSpec, ":", 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
switch len(specParts) {
|
|
|
|
case 0:
|
2014-04-08 06:02:17 -04:00
|
|
|
return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
|
2014-02-14 20:15:40 -05: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 17:43:01 -04:00
|
|
|
c := container.daemon.Get(specParts[0])
|
2014-02-14 20:15:40 -05:00
|
|
|
if c == nil {
|
2014-04-03 10:27:07 -04:00
|
|
|
return fmt.Errorf("Container %s not found. Impossible to mount its volumes", specParts[0])
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-04-03 10:27:07 -04: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-14 20:15:40 -05:00
|
|
|
for volPath, id := range c.Volumes {
|
|
|
|
if _, exists := container.Volumes[volPath]; exists {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-11 16:49:46 -04:00
|
|
|
stat, err := os.Stat(c.getResourcePath(volPath))
|
2014-03-18 18:28:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-11 16:49:46 -04:00
|
|
|
if err := createIfNotExists(container.getResourcePath(volPath), stat.IsDir()); err != nil {
|
2014-02-14 20:15:40 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-02-14 20:15:40 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the requested volumes if they don't exist
|
|
|
|
for volPath := range container.Config.Volumes {
|
2014-05-19 18:04:51 -04:00
|
|
|
if err := initializeVolume(container, volPath, binds); err != nil {
|
|
|
|
return err
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
|
|
|
|
for volPath := range binds {
|
|
|
|
if err := initializeVolume(container, volPath, binds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
func createIfNotExists(destination string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(destination); err != nil && os.IsNotExist(err) {
|
|
|
|
if isDir {
|
|
|
|
if err := os.MkdirAll(destination, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(destination, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
f.Close()
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeVolume(container *Container, volPath string, binds map[string]BindMap) error {
|
|
|
|
volumesDriver := container.daemon.volumes.Driver()
|
|
|
|
volPath = filepath.Clean(volPath)
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
// Skip existing volumes
|
|
|
|
if _, exists := container.Volumes[volPath]; exists {
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
var (
|
2014-06-02 17:16:30 -04:00
|
|
|
destination string
|
2014-05-19 18:04:51 -04:00
|
|
|
isBindMount bool
|
|
|
|
volIsDir = true
|
|
|
|
|
|
|
|
srcRW = false
|
|
|
|
)
|
|
|
|
|
|
|
|
// If an external bind is defined for this volume, use that as a source
|
|
|
|
if bindMap, exists := binds[volPath]; exists {
|
|
|
|
isBindMount = true
|
2014-06-02 17:16:30 -04:00
|
|
|
destination = bindMap.SrcPath
|
|
|
|
|
|
|
|
if !filepath.IsAbs(destination) {
|
|
|
|
return fmt.Errorf("%s must be an absolute path", destination)
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
if strings.ToLower(bindMap.Mode) == "rw" {
|
|
|
|
srcRW = true
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
if stat, err := os.Stat(bindMap.SrcPath); err != nil {
|
2014-02-14 20:15:40 -05:00
|
|
|
return err
|
|
|
|
} else {
|
2014-05-19 18:04:51 -04:00
|
|
|
volIsDir = stat.IsDir()
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
} 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.
|
|
|
|
c, err := container.daemon.volumes.Create(nil, "", "", "", "", nil, nil)
|
2014-04-24 21:22:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
|
|
|
destination, err = volumesDriver.Get(c.ID, "")
|
2014-02-14 20:15:40 -05:00
|
|
|
if err != nil {
|
2014-05-19 18:04:51 -04:00
|
|
|
return fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
|
|
|
srcRW = true
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-04-24 21:22:22 -04:00
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
if p, err := filepath.EvalSymlinks(destination); err != nil {
|
2014-05-19 18:04:51 -04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-06-02 17:16:30 -04:00
|
|
|
destination = p
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-04-24 21:22:22 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
// Create the mountpoint
|
2014-06-02 17:16:30 -04:00
|
|
|
source, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, volPath), container.basefs)
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
newVolPath, err := filepath.Rel(container.basefs, source)
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newVolPath = "/" + newVolPath
|
|
|
|
|
|
|
|
if volPath != newVolPath {
|
|
|
|
delete(container.Volumes, volPath)
|
|
|
|
delete(container.VolumesRW, volPath)
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
container.Volumes[newVolPath] = destination
|
2014-05-19 18:04:51 -04:00
|
|
|
container.VolumesRW[newVolPath] = srcRW
|
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
if err := createIfNotExists(source, volIsDir); err != nil {
|
2014-05-19 18:04:51 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-04-24 21:22:22 -04:00
|
|
|
|
2014-05-19 18:04:51 -04:00
|
|
|
// Do not copy or change permissions if we are mounting from the host
|
|
|
|
if srcRW && !isBindMount {
|
2014-06-02 17:16:30 -04:00
|
|
|
if err := copyExistingContents(source, destination); err != nil {
|
2014-05-19 18:18:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-06-02 17:16:30 -04:00
|
|
|
if err := archive.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyOwnership copies the permissions and uid:gid of the source file
|
|
|
|
// into the destination file
|
|
|
|
func copyOwnership(source, destination string) error {
|
|
|
|
var stat syscall.Stat_t
|
2014-05-19 18:18:37 -04:00
|
|
|
|
2014-06-02 17:16:30 -04:00
|
|
|
if err := syscall.Stat(source, &stat); err != nil {
|
2014-05-19 18:18:37 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
|
|
|
if err := os.Chown(destination, int(stat.Uid), int(stat.Gid)); err != nil {
|
2014-05-19 18:18:37 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
|
|
|
return os.Chmod(destination, os.FileMode(stat.Mode))
|
2014-03-18 18:28:40 -04:00
|
|
|
}
|