2015-07-16 17:14:58 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2015-08-11 22:27:33 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2015-08-11 22:27:33 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createContainerPlatformSpecificSettings performs platform specific container create functionality
|
2015-08-24 13:57:39 -04:00
|
|
|
func createContainerPlatformSpecificSettings(container *Container, config *runconfig.Config, hostConfig *runconfig.HostConfig, img *image.Image) error {
|
2015-07-16 17:14:58 -04:00
|
|
|
for spec := range config.Volumes {
|
|
|
|
var (
|
|
|
|
name, destination string
|
|
|
|
parts = strings.Split(spec, ":")
|
|
|
|
)
|
|
|
|
switch len(parts) {
|
|
|
|
case 2:
|
|
|
|
name, destination = parts[0], filepath.Clean(parts[1])
|
|
|
|
default:
|
2015-07-28 20:19:17 -04:00
|
|
|
name = stringid.GenerateNonCryptoID()
|
2015-07-16 17:14:58 -04:00
|
|
|
destination = filepath.Clean(parts[0])
|
|
|
|
}
|
|
|
|
// Skip volumes for which we already have something mounted on that
|
|
|
|
// destination because of a --volume-from.
|
|
|
|
if container.isDestinationMounted(destination) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path, err := container.GetResourcePath(destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err == nil && !stat.IsDir() {
|
|
|
|
return fmt.Errorf("cannot mount volume over existing file, file exists %s", path)
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:57:39 -04:00
|
|
|
volumeDriver := hostConfig.VolumeDriver
|
2015-08-11 22:27:33 -04:00
|
|
|
if destination != "" && img != nil {
|
|
|
|
if _, ok := img.ContainerConfig.Volumes[destination]; ok {
|
|
|
|
// check for whether bind is not specified and then set to local
|
|
|
|
if _, ok := container.MountPoints[destination]; !ok {
|
|
|
|
volumeDriver = volume.DefaultDriverName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
v, err := container.daemon.createVolume(name, volumeDriver, nil)
|
2015-07-16 17:14:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-09-11 16:17:59 -04:00
|
|
|
if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
|
2015-07-16 17:14:58 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:27:33 -04:00
|
|
|
// never attempt to copy existing content in a container FS to a shared volume
|
2015-09-01 20:13:38 -04:00
|
|
|
if v.DriverName() == volume.DefaultDriverName {
|
2015-08-11 22:27:33 -04:00
|
|
|
if err := container.copyImagePathContent(v, destination); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
container.addMountPointWithVolume(destination, v, true)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|