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"
|
|
|
|
"syscall"
|
2014-05-05 17:45:14 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/archive"
|
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2014-02-14 20:15:40 -05:00
|
|
|
)
|
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
type Volume struct {
|
|
|
|
HostPath string
|
|
|
|
VolPath string
|
2014-08-21 17:57:46 -04:00
|
|
|
isReadWrite bool
|
2014-08-08 11:00:18 -04:00
|
|
|
isBindMount bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Volume) isDir() (bool, error) {
|
|
|
|
stat, err := os.Stat(v.HostPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat.IsDir(), nil
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
return createVolumes(container)
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
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-09-13 12:42:10 -04:00
|
|
|
{
|
|
|
|
Source: container.ResolvConfPath,
|
|
|
|
Destination: "/etc/resolv.conf",
|
|
|
|
Writable: true,
|
|
|
|
Slave: true,
|
|
|
|
},
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 18:01:25 -04:00
|
|
|
if container.HostnamePath != "" {
|
2014-09-13 12:42:10 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostnamePath,
|
|
|
|
Destination: "/etc/hostname",
|
|
|
|
Writable: true,
|
|
|
|
Private: true,
|
|
|
|
})
|
2014-05-16 18:01:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostsPath != "" {
|
2014-09-13 12:42:10 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostsPath,
|
|
|
|
Destination: "/etc/hosts",
|
|
|
|
Writable: true,
|
|
|
|
Slave: 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-09-15 20:51:06 -04:00
|
|
|
// These mounts must be ordered based on the length of the path that it is being mounted to (lexicographic)
|
|
|
|
for _, path := range container.sortedVolumeMounts() {
|
2014-09-13 12:42:10 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
2014-09-15 20:51:06 -04:00
|
|
|
Source: container.Volumes[path],
|
|
|
|
Destination: path,
|
|
|
|
Writable: container.VolumesRW[path],
|
2014-09-13 12:42:10 -04:00
|
|
|
})
|
2014-03-03 10:15:29 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-15 20:51:06 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(mountPaths)
|
|
|
|
return mountPaths
|
|
|
|
}
|
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
func parseVolumesFromSpec(container *Container, spec string) (map[string]*Volume, error) {
|
|
|
|
specParts := strings.SplitN(spec, ":", 2)
|
|
|
|
if len(specParts) == 0 {
|
|
|
|
return nil, fmt.Errorf("Malformed volumes-from specification: %s", spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := container.daemon.Get(specParts[0])
|
|
|
|
if c == nil {
|
|
|
|
return nil, fmt.Errorf("Container %s not found. Impossible to mount its volumes", specParts[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
volumes, err := c.GetVolumes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(specParts) == 2 {
|
|
|
|
mode := specParts[1]
|
|
|
|
if validVolumeMode(mode) {
|
|
|
|
return nil, fmt.Errorf("Invalid mode for volumes-from: %s", mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the mode for the inheritted volume
|
|
|
|
for _, v := range volumes {
|
|
|
|
v.isReadWrite = mode != "ro"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return volumes, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 20:15:40 -05:00
|
|
|
func applyVolumesFrom(container *Container) error {
|
2014-04-08 05:26:09 -04:00
|
|
|
volumesFrom := container.hostConfig.VolumesFrom
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
for _, spec := range volumesFrom {
|
|
|
|
volumes, err := parseVolumesFromSpec(container, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
for _, v := range volumes {
|
|
|
|
if err = v.initialize(container); err != nil {
|
|
|
|
return err
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
func validVolumeMode(mode string) bool {
|
|
|
|
validModes := map[string]bool{
|
|
|
|
"rw": true,
|
|
|
|
"ro": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return validModes[mode]
|
|
|
|
}
|
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
func parseBindVolumeSpec(spec string) (Volume, error) {
|
2014-08-06 15:27:58 -04:00
|
|
|
var (
|
2014-08-08 11:00:18 -04:00
|
|
|
arr = strings.Split(spec, ":")
|
|
|
|
vol Volume
|
2014-08-06 15:27:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
switch len(arr) {
|
|
|
|
case 1:
|
2014-08-08 11:00:18 -04:00
|
|
|
vol.VolPath = spec
|
2014-08-21 17:57:46 -04:00
|
|
|
vol.isReadWrite = true
|
2014-08-06 15:27:58 -04:00
|
|
|
case 2:
|
2014-08-08 11:00:18 -04:00
|
|
|
vol.HostPath = arr[0]
|
|
|
|
vol.VolPath = arr[1]
|
2014-08-21 17:57:46 -04:00
|
|
|
vol.isReadWrite = true
|
2014-08-06 15:27:58 -04:00
|
|
|
case 3:
|
2014-08-08 11:00:18 -04:00
|
|
|
vol.HostPath = arr[0]
|
|
|
|
vol.VolPath = arr[1]
|
2014-08-21 17:57:46 -04:00
|
|
|
vol.isReadWrite = validVolumeMode(arr[2]) && arr[2] == "rw"
|
2014-08-06 15:27:58 -04:00
|
|
|
default:
|
2014-08-08 11:00:18 -04:00
|
|
|
return vol, fmt.Errorf("Invalid volume specification: %s", spec)
|
2014-08-06 15:27:58 -04:00
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
|
|
|
|
if !filepath.IsAbs(vol.HostPath) {
|
|
|
|
return vol, fmt.Errorf("cannot bind mount volume: %s volume paths must be absolute.", vol.HostPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vol, nil
|
2014-08-06 15:27:58 -04:00
|
|
|
}
|
|
|
|
|
2014-02-14 20:15:40 -05:00
|
|
|
func createVolumes(container *Container) error {
|
2014-08-08 11:00:18 -04:00
|
|
|
// Get all the bindmounts
|
2014-08-21 17:57:46 -04:00
|
|
|
volumes, err := container.GetVolumes()
|
2014-02-14 20:15:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
// Get all the rest of the volumes
|
2014-02-14 20:15:40 -05:00
|
|
|
for volPath := range container.Config.Volumes {
|
2014-08-08 11:00:18 -04:00
|
|
|
// Make sure the the volume isn't already specified as a bindmount
|
|
|
|
if _, exists := volumes[volPath]; !exists {
|
2014-08-21 17:57:46 -04:00
|
|
|
volumes[volPath] = &Volume{
|
2014-08-08 11:00:18 -04:00
|
|
|
VolPath: volPath,
|
2014-08-21 17:57:46 -04:00
|
|
|
isReadWrite: true,
|
2014-08-08 11:00:18 -04:00
|
|
|
isBindMount: false,
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-05-19 18:18:37 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
for _, vol := range volumes {
|
|
|
|
if err = vol.initialize(container); err != nil {
|
2014-05-19 18:18:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
|
2014-08-21 17:57:46 -04:00
|
|
|
return nil
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
func createVolumeHostPath(container *Container) (string, error) {
|
|
|
|
volumesDriver := container.daemon.volumes.Driver()
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
// 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)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
hostPath, err := volumesDriver.Get(c.ID, "")
|
|
|
|
if err != nil {
|
|
|
|
return hostPath, fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
return hostPath, nil
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
func (v *Volume) initialize(container *Container) error {
|
|
|
|
var err error
|
|
|
|
v.VolPath = filepath.Clean(v.VolPath)
|
2014-06-02 17:16:30 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
// Do not initialize an existing volume
|
|
|
|
if _, exists := container.Volumes[v.VolPath]; exists {
|
2014-05-19 18:04:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-02-14 20:15:40 -05:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
// If it's not a bindmount we need to create the dir on the host
|
2014-08-21 17:57:46 -04:00
|
|
|
if !v.isBindMount && v.HostPath == "" {
|
2014-08-08 11:00:18 -04:00
|
|
|
v.HostPath, err = createVolumeHostPath(container)
|
2014-04-24 21:22:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-04-24 21:22:22 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
hostPath, err := filepath.EvalSymlinks(v.HostPath)
|
|
|
|
if 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
|
|
|
// Create the mountpoint
|
2014-08-08 11:00:18 -04:00
|
|
|
// This is the path to the volume within the container FS
|
|
|
|
// This differs from `hostPath` in that `hostPath` refers to the place where
|
|
|
|
// the volume data is actually stored on the host
|
|
|
|
fullVolPath, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, v.VolPath), container.basefs)
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
container.Volumes[v.VolPath] = hostPath
|
2014-08-21 17:57:46 -04:00
|
|
|
container.VolumesRW[v.VolPath] = v.isReadWrite
|
2014-08-08 11:00:18 -04:00
|
|
|
|
|
|
|
volIsDir, err := v.isDir()
|
2014-05-19 18:04:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
if err := createIfNotExists(fullVolPath, volIsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
// Do not copy or change permissions if we are mounting from the host
|
2014-08-21 17:57:46 -04:00
|
|
|
if v.isReadWrite && !v.isBindMount {
|
2014-08-08 11:00:18 -04:00
|
|
|
return copyExistingContents(fullVolPath, hostPath)
|
2014-05-19 18:04:51 -04:00
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
func createIfNotExists(destination string, isDir bool) error {
|
|
|
|
if _, err := os.Stat(destination); err == nil || !os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
if isDir {
|
|
|
|
return os.MkdirAll(destination, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
2014-05-19 18:04:51 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-04-24 21:22:22 -04:00
|
|
|
|
2014-08-08 11:00:18 -04:00
|
|
|
f, err := os.OpenFile(destination, os.O_CREATE, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-05-19 18:18:37 -04:00
|
|
|
}
|
2014-08-08 11:00:18 -04:00
|
|
|
f.Close()
|
|
|
|
|
2014-05-19 18:18:37 -04:00
|
|
|
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
|
|
|
}
|