Skip to content

Commit

Permalink
clh: cleanup VM dir
Browse files Browse the repository at this point in the history
remove dirtory created for VM. This should be refactored in all
hypervisors

Signed-off-by: Jose Carlos Venegas Munoz <jose.carlos.venegas.munoz@intel.com>
  • Loading branch information
jcvenegas committed Dec 6, 2019
1 parent c688a15 commit 42061f6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions virtcontainers/clh.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ func (clh *cloudHypervisor) terminate() (err error) {
}
}
}

clh.cleanupVM()
}()

pid := clh.state.PID
Expand Down Expand Up @@ -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
}

0 comments on commit 42061f6

Please sign in to comment.