From 30fd855b12f9068d32152e7594a539ec4dbea239 Mon Sep 17 00:00:00 2001 From: "ziren.wzr" Date: Sun, 2 Dec 2018 03:42:54 -0500 Subject: [PATCH] bugfix: delete upperDir and workDir when deleting container taken over by pouch Signed-off-by: ziren.wzr --- daemon/mgr/container.go | 8 +++++++ daemon/mgr/container_types.go | 42 ++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index fdc298202..f07494ccc 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -1165,6 +1165,14 @@ func (mgr *ContainerManager) Remove(ctx context.Context, name string, options *t if err := mount.Unmount(c.BaseFS, 0); err != nil { logrus.Errorf("failed to umount rootfs when remove the container %s: %v", c.ID, err) } + + // Note(ziren): when deleting a container whose rootfs was provided, we also should + // remove the upperDir and workDir of container. because the directories cost disk + // space and the disk space counted into the new container that using the same + // disk quota id. + if err := c.CleanRootfsSnapshotDirs(); err != nil { + logrus.Errorf("failed to clean rootfs: %v", err) + } } else if err := mgr.Client.RemoveSnapshot(ctx, c.SnapshotKey()); err != nil { // if the container is created by normal method, remove the // snapshot when delete it. diff --git a/daemon/mgr/container_types.go b/daemon/mgr/container_types.go index 409a31654..3cdef0ae5 100644 --- a/daemon/mgr/container_types.go +++ b/daemon/mgr/container_types.go @@ -439,10 +439,11 @@ func (c *Container) FormatStatus() (string, error) { } // UnsetMergedDir unsets Snapshot MergedDir. Stop a container will -// delete the containerd container, the merged dir -// will also be deleted, so we should unset the -// container's MergedDir. +// delete the containerd container, the merged dir will also be +// deleted, so we should unset the container's MergedDir. func (c *Container) UnsetMergedDir() { + c.Lock() + defer c.Unlock() if c.Snapshotter == nil || c.Snapshotter.Data == nil { return } @@ -491,6 +492,41 @@ func (c *Container) GetSpecificBasePath(path string) string { return "" } +// CleanRootfsSnapshotDirs deletes container's rootfs snapshot MergedDir, UpperDir and +// WorkDir. Since the snapshot of container created by containerd will be cleaned by +// containerd, so we only clean rootfs that is RootFSProvided. +func (c *Container) CleanRootfsSnapshotDirs() error { + // if RootFSProvided is not set or Snapshotter data empty , we no need clean the rootfs + if !c.RootFSProvided || c.Snapshotter == nil || c.Snapshotter.Data == nil { + return nil + } + + var ( + removeDirs []string + ) + + c.Lock() + for _, dir := range []string{"MergedDir", "UpperDir", "WorkDir"} { + if v, ok := c.Snapshotter.Data[dir]; ok { + removeDirs = append(removeDirs, v) + } + } + c.Unlock() + + var errMsgs []string + for _, dir := range removeDirs { + if err := os.RemoveAll(dir); err != nil { + errMsgs = append(errMsgs, err.Error()) + } + } + + if len(errMsgs) != 0 { + return fmt.Errorf(strings.Join(errMsgs, "\n")) + } + + return nil +} + // ContainerRestartPolicy represents the policy is used to manage container. type ContainerRestartPolicy types.RestartPolicy