From 92cf3764e032740f0c84a1b242c54b99f45a6bf0 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Tue, 15 Jan 2019 14:12:19 -0800 Subject: Create working directory if it doesn't yet exist PiperOrigin-RevId: 229438125 Change-Id: I58eb0d10178d1adfc709d7b859189d1acbcb2f22 --- runsc/container/container_test.go | 21 +++++++++++++++++++++ runsc/container/fs.go | 12 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index affb51fab..bd8655f3e 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -1699,6 +1699,27 @@ func TestDestroyStarting(t *testing.T) { } } +func TestCreateWorkingDir(t *testing.T) { + for _, conf := range configs(overlay) { + t.Logf("Running test with conf: %+v", conf) + + tmpDir, err := ioutil.TempDir(testutil.TmpDir(), "cwd-create") + if err != nil { + t.Fatalf("ioutil.TempDir() failed: %v", err) + } + dir := path.Join(tmpDir, "new/working/dir") + + // touch will fail if the directory doesn't exist. + spec := testutil.NewSpecWithArgs("/bin/touch", path.Join(dir, "file")) + spec.Process.Cwd = dir + spec.Root.Readonly = true + + if err := run(spec, conf); err != nil { + t.Fatalf("Error running container: %v", err) + } + } +} + // executeSync synchronously executes a new process. func (cont *Container) executeSync(args *control.ExecArgs) (syscall.WaitStatus, error) { pid, err := cont.Execute(args) diff --git a/runsc/container/fs.go b/runsc/container/fs.go index 41022686b..97195550f 100644 --- a/runsc/container/fs.go +++ b/runsc/container/fs.go @@ -87,7 +87,7 @@ func setupFS(spec *specs.Spec, conf *boot.Config, bundleDir string) ([]specs.Mou // container. dst, err := resolveSymlinks(spec.Root.Path, m.Destination) if err != nil { - return nil, fmt.Errorf("failed to resolve symlinks: %v", err) + return nil, fmt.Errorf("resolving symlinks to %q: %v", m.Destination, err) } flags := optionsToFlags(m.Options) @@ -113,6 +113,16 @@ func setupFS(spec *specs.Spec, conf *boot.Config, bundleDir string) ([]specs.Mou rv = append(rv, cpy) } + if spec.Process.Cwd != "" { + dst, err := resolveSymlinks(spec.Root.Path, spec.Process.Cwd) + if err != nil { + return nil, fmt.Errorf("resolving symlinks to %q: %v", spec.Process.Cwd, err) + } + if err := os.MkdirAll(dst, 0755); err != nil { + return nil, err + } + } + // If root is read only, check if it needs to be remounted as readonly. if spec.Root.Readonly { isMountPoint, readonly, err := mountInfo(spec.Root.Path) -- cgit v1.2.3