From 300f08312714f01104c0858cc26bb4fa70dec914 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 12 Jul 2017 15:36:00 +0100 Subject: [PATCH] Cleanup mounts if we fail to mount one element of rootfs Signed-off-by: Ian Campbell --- linux/shim/init.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/linux/shim/init.go b/linux/shim/init.go index 9e40daaac..8d6e1bfb1 100644 --- a/linux/shim/init.go +++ b/linux/shim/init.go @@ -72,6 +72,11 @@ func newInitProcess(context context.Context, path, namespace string, r *shimapi. // what was actually done rather than what should have been // done. nrRootMounts := 0 + cleanupMounts := func() { + if err2 := mount.UnmountN(rootfs, 0, nrRootMounts); err2 != nil { + log.G(context).WithError(err2).Warn("Failed to cleanup rootfs mount") + } + } for _, rm := range r.Rootfs { m := &mount.Mount{ Type: rm.Type, @@ -79,15 +84,11 @@ func newInitProcess(context context.Context, path, namespace string, r *shimapi. Options: rm.Options, } if err := m.Mount(rootfs); err != nil { + cleanupMounts() return nil, errors.Wrapf(err, "failed to mount rootfs component %v", m) } nrRootMounts++ } - cleanupMounts := func() { - if err2 := mount.UnmountN(rootfs, 0, nrRootMounts); err2 != nil { - log.G(context).WithError(err2).Warn("Failed to cleanup rootfs mount") - } - } runtime := &runc.Runc{ Command: r.Runtime, Log: filepath.Join(path, "log.json"),