Merge pull request #1139 from bergwolf/delete

clean up container dir
This commit is contained in:
Xu Wang 2019-01-22 10:16:34 +08:00 committed by GitHub
commit 3b0b0147bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 3 deletions

View File

@ -254,4 +254,7 @@ type agent interface {
// copyFile copies file from host to container's rootfs
copyFile(src, dst string) error
// cleanup removes all on disk information generated by the agent
cleanup(id string)
}

View File

@ -1012,3 +1012,10 @@ func (h *hyper) copyFile(src, dst string) error {
// hyperstart-agent does not support copyFile
return nil
}
func (h *hyper) cleanup(id string) {
path := h.getSharePath(id)
if err := os.RemoveAll(path); err != nil {
h.Logger().WithError(err).Errorf("failed to cleanup vm share path %s", path)
}
}

View File

@ -1835,3 +1835,11 @@ func (k *kataAgent) copyFile(src, dst string) error {
return nil
}
func (k *kataAgent) cleanup(id string) {
path := k.getSharePath(id)
k.Logger().WithField("path", path).Infof("cleanup agent")
if err := os.RemoveAll(path); err != nil {
k.Logger().WithError(err).Errorf("failed to cleanup vm share path %s", path)
}
}

View File

@ -306,7 +306,7 @@ func bindUnmountContainerRootfs(ctx context.Context, sharedDir, sandboxID, cID s
defer span.Finish()
rootfsDest := filepath.Join(sharedDir, sandboxID, cID, rootfsDir)
syscall.Unmount(rootfsDest, 0)
syscall.Unmount(rootfsDest, syscall.MNT_DETACH)
return nil
}

View File

@ -215,3 +215,6 @@ func (n *noopAgent) setGuestDateTime(time.Time) error {
func (n *noopAgent) copyFile(src, dst string) error {
return nil
}
func (n *noopAgent) cleanup(id string) {
}

View File

@ -654,9 +654,25 @@ func (q *qemu) stopSandbox() error {
return err
}
err = os.RemoveAll(filepath.Join(RunVMStoragePath, q.id))
// cleanup vm path
dir := filepath.Join(RunVMStoragePath, q.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 {
q.Logger().WithError(err).Error("Fail to clean up vm directory")
// Well, it's just cleanup failure. Let's ignore it.
q.Logger().WithError(err).WithField("dir", dir).Warn("failed to resolve vm path")
}
q.Logger().WithField("link", link).WithField("dir", dir).Infof("cleanup vm path")
if err := os.RemoveAll(dir); err != nil {
q.Logger().WithError(err).Warnf("failed to remove vm path %s", dir)
}
if link != dir && link != "" {
if err := os.RemoveAll(link); err != nil {
q.Logger().WithError(err).WithField("link", link).Warn("failed to remove resolved vm path")
}
}
return nil

View File

@ -744,6 +744,8 @@ func (s *Sandbox) Delete() error {
s.Logger().WithError(err).Error("failed to cleanup hypervisor")
}
s.agent.cleanup(s.id)
return s.storage.deleteSandboxResources(s.id, nil)
}