From 794ea7ed72aff1bbd3bd88383acf58e221d02a07 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Thu, 11 Jan 2018 11:39:58 +0000 Subject: [PATCH] Make runtime.mounts[].destination relative to container rootfs This is similar to ae64ab6b825a6208b68eaf1bb3e79eafc178144f 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 --- pkg/init/cmd/service/prepare.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/init/cmd/service/prepare.go b/pkg/init/cmd/service/prepare.go index 1c852b817..ca2f82632 100644 --- a/pkg/init/cmd/service/prepare.go +++ b/pkg/init/cmd/service/prepare.go @@ -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 // but we do mkdir of the destination path in case missing 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 { const mode os.FileMode = 0755 - err := os.MkdirAll(mount.Destination, mode) + dir := makeAbsolute(mount.Destination) + err := os.MkdirAll(dir, mode) 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 for _, o := range mount.Options { @@ -161,17 +170,14 @@ func prepareFilesystem(path string, runtime Runtime) error { } } 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) } } for _, dir := range runtime.Mkdir { // in future we may need to change the structure to set mode, ownership const mode os.FileMode = 0755 - // relative paths are relative to rootfs of container - if !filepath.IsAbs(dir) { - dir = filepath.Join(rootfs, dir) - } + dir = makeAbsolute(dir) err := os.MkdirAll(dir, mode) if err != nil { return fmt.Errorf("Cannot create directory %s: %v", dir, err)