Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
vc: Remove container share dir when stopping
Browse files Browse the repository at this point in the history
Remove the rootfs bind dest and finally remove the created share
directory when stopping the container.

Fixes #2516
Signed-off-by: Li Yuxuan <[email protected]>
  • Loading branch information
darfux committed Mar 9, 2020
1 parent 8cffbde commit ed43117
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,11 @@ func (c *Container) stop(force bool) error {
return err
}

shareDir := filepath.Join(kataHostSharedDir(), c.sandbox.id, c.id)
if err := syscall.Rmdir(shareDir); err != nil {
c.Logger().WithError(err).WithField("share-dir", shareDir).Warn("Could not remove container share dir")
}

// container was killed by force, container MUST change its state
// as soon as possible just in case one of below operations fail leaving
// the containers in a bad state.
Expand Down
4 changes: 4 additions & 0 deletions virtcontainers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func bindUnmountContainerRootfs(ctx context.Context, sharedDir, sandboxID, cID s
logrus.Warnf("%s: %s", err, rootfsDest)
return nil
}
if err := syscall.Rmdir(rootfsDest); err != nil {
logrus.WithError(err).WithField("rootfs-dir", rootfsDest).Warn("Could not remove container rootfs dir")
}

return err
}

Expand Down
26 changes: 26 additions & 0 deletions virtcontainers/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,29 @@ func TestBindUnmountContainerRootfsENOENTNotError(t *testing.T) {
err := bindUnmountContainerRootfs(context.Background(), testMnt, sID, cID)
assert.NoError(err)
}

func TestBindUnmountContainerRootfsRemoveRootfsDest(t *testing.T) {
assert := assert.New(t)
if tc.NotValid(ktu.NeedRoot()) {
t.Skip(ktu.TestDisabledNeedRoot)
}

sID := "sandIDTestRemoveRootfsDest"
cID := "contIDTestRemoveRootfsDest"

testPath := filepath.Join(testDir, sID, cID, rootfsDir)
syscall.Unmount(testPath, 0)
os.Remove(testPath)

err := os.MkdirAll(testPath, mountPerm)
assert.NoError(err)
defer os.RemoveAll(filepath.Join(testDir, sID))

bindUnmountContainerRootfs(context.Background(), testDir, sID, cID)

if _, err := os.Stat(testPath); err == nil {
t.Fatal("empty rootfs dest should be removed")
} else if !os.IsNotExist(err) {
t.Fatal(err)
}
}

0 comments on commit ed43117

Please sign in to comment.