Make runtime.mounts[].destination relative to container rootfs

This is similar to ae64ab6b82 from #2849 which
did the same for runtime.mkdir.

This makes it possible to specify both host (absolute) or container (relative)
paths.

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2018-01-11 11:39:58 +00:00
parent 57cf8df3b2
commit 794ea7ed72

View File

@ -144,11 +144,20 @@ func prepareFilesystem(path string, runtime Runtime) error {
// we execute Mounts before Mkdir so you can make a directory under a mount // we execute Mounts before Mkdir so you can make a directory under a mount
// but we do mkdir of the destination path in case missing // but we do mkdir of the destination path in case missing
rootfs := filepath.Join(path, "rootfs") rootfs := filepath.Join(path, "rootfs")
makeAbsolute := func(dir string) string {
if filepath.IsAbs(dir) {
return dir
}
// relative paths are relative to rootfs of container
return filepath.Join(rootfs, dir)
}
for _, mount := range runtime.Mounts { for _, mount := range runtime.Mounts {
const mode os.FileMode = 0755 const mode os.FileMode = 0755
err := os.MkdirAll(mount.Destination, mode) dir := makeAbsolute(mount.Destination)
err := os.MkdirAll(dir, mode)
if err != nil { if err != nil {
return fmt.Errorf("Cannot create directory for mount destination %s: %v", mount.Destination, err) return fmt.Errorf("Cannot create directory for mount destination %s: %v", dir, err)
} }
// also mkdir upper and work directories on overlay // also mkdir upper and work directories on overlay
for _, o := range mount.Options { for _, o := range mount.Options {
@ -161,17 +170,14 @@ func prepareFilesystem(path string, runtime Runtime) error {
} }
} }
opts, data := parseMountOptions(mount.Options) opts, data := parseMountOptions(mount.Options)
if err := unix.Mount(mount.Source, mount.Destination, mount.Type, uintptr(opts), data); err != nil { if err := unix.Mount(mount.Source, dir, mount.Type, uintptr(opts), data); err != nil {
return fmt.Errorf("Failed to mount %s: %v", mount.Source, err) return fmt.Errorf("Failed to mount %s: %v", mount.Source, err)
} }
} }
for _, dir := range runtime.Mkdir { for _, dir := range runtime.Mkdir {
// in future we may need to change the structure to set mode, ownership // in future we may need to change the structure to set mode, ownership
const mode os.FileMode = 0755 const mode os.FileMode = 0755
// relative paths are relative to rootfs of container dir = makeAbsolute(dir)
if !filepath.IsAbs(dir) {
dir = filepath.Join(rootfs, dir)
}
err := os.MkdirAll(dir, mode) err := os.MkdirAll(dir, mode)
if err != nil { if err != nil {
return fmt.Errorf("Cannot create directory %s: %v", dir, err) return fmt.Errorf("Cannot create directory %s: %v", dir, err)