Skip to content

Commit

Permalink
bugfix: delete upperDir and workDir when deleting container taken ove…
Browse files Browse the repository at this point in the history
…r by pouch

Signed-off-by: ziren.wzr <ziren.wzr@alibaba-inc.com>
  • Loading branch information
HusterWan committed Dec 5, 2018
1 parent be7e002 commit 30fd855
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
8 changes: 8 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 39 additions & 3 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 30fd855

Please sign in to comment.