From 42061f6c392bab370eb4c235720a9c07ecceb8f9 Mon Sep 17 00:00:00 2001 From: Jose Carlos Venegas Munoz Date: Tue, 3 Dec 2019 22:11:43 +0000 Subject: [PATCH] clh: cleanup VM dir remove dirtory created for VM. This should be refactored in all hypervisors Signed-off-by: Jose Carlos Venegas Munoz --- virtcontainers/clh.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/virtcontainers/clh.go b/virtcontainers/clh.go index b24588719a..fc5a17a9ca 100644 --- a/virtcontainers/clh.go +++ b/virtcontainers/clh.go @@ -500,6 +500,8 @@ func (clh *cloudHypervisor) terminate() (err error) { } } } + + clh.cleanupVM() }() pid := clh.state.PID @@ -1079,3 +1081,41 @@ func (clh *cloudHypervisor) addVolume(volume types.Volume) error { clh.Logger().Debug("Adding share volume to hypervisor: ", volume.MountTag) return nil } + +// cleanupVM will remove generated files and directories related with the virtual machine +func (clh *cloudHypervisor) cleanupVM() error { + + // cleanup vm path + dir := filepath.Join(store.RunVMStoragePath(), clh.id) + + // If it's a symlink, remove both dir and the target. + // This can happen when vm template links a sandbox to a vm. + link, err := filepath.EvalSymlinks(dir) + if err != nil { + // Well, it's just cleanup failure. Let's ignore it. + clh.Logger().WithError(err).WithField("dir", dir).Warn("failed to resolve vm path") + } + clh.Logger().WithField("link", link).WithField("dir", dir).Infof("cleanup vm path") + + if err := os.RemoveAll(dir); err != nil { + clh.Logger().WithError(err).Warnf("failed to remove vm path %s", dir) + } + if link != dir && link != "" { + if err := os.RemoveAll(link); err != nil { + clh.Logger().WithError(err).WithField("link", link).Warn("failed to remove resolved vm path") + } + } + + if clh.config.VMid != "" { + dir = store.SandboxConfigurationRootPath(clh.config.VMid) + if err := os.RemoveAll(dir); err != nil { + clh.Logger().WithError(err).WithField("path", dir).Warnf("failed to remove vm path") + } + dir = store.SandboxRuntimeRootPath(clh.config.VMid) + if err := os.RemoveAll(dir); err != nil { + clh.Logger().WithError(err).WithField("path", dir).Warnf("failed to remove vm path") + } + } + + return nil +}