From ac78a5b615e75d683b439d3d41239daeb94688b6 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 24 Aug 2018 10:14:57 -0400 Subject: [PATCH] Remove and create workdir if state dir does not exist This is the case where the work dir could still exist if a machine reboots, reseting the state dir. On container creation, we should just clear out the work dir. Signed-off-by: Michael Crosby --- runtime/v2/bundle.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/runtime/v2/bundle.go b/runtime/v2/bundle.go index ad2c894b4..85eeee444 100644 --- a/runtime/v2/bundle.go +++ b/runtime/v2/bundle.go @@ -58,16 +58,7 @@ func NewBundle(ctx context.Context, root, state, id string, spec []byte) (b *Bun Path: filepath.Join(state, ns, id), Namespace: ns, } - paths := []string{b.Path, work} - // create base directories - for _, d := range paths { - if err := os.MkdirAll(filepath.Dir(d), 0711); err != nil { - return nil, err - } - if err := os.Mkdir(d, 0711); err != nil { - return nil, err - } - } + var paths []string defer func() { if err != nil { for _, d := range paths { @@ -75,6 +66,28 @@ func NewBundle(ctx context.Context, root, state, id string, spec []byte) (b *Bun } } }() + // create state directory for the bundle + if err := os.MkdirAll(filepath.Dir(b.Path), 0711); err != nil { + return nil, err + } + if err := os.Mkdir(b.Path, 0711); err != nil { + return nil, err + } + paths = append(paths, b.Path) + // create working directory for the bundle + if err := os.MkdirAll(filepath.Dir(work), 0711); err != nil { + return nil, err + } + if err := os.Mkdir(work, 0711); err != nil { + if !os.IsExist(err) { + return nil, err + } + os.RemoveAll(work) + if err := os.Mkdir(work, 0711); err != nil { + return nil, err + } + } + paths = append(paths, work) // create rootfs dir if err := os.Mkdir(filepath.Join(b.Path, "rootfs"), 0711); err != nil { return nil, err