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

Commit

Permalink
vc: move container mount cleanup to container.go
Browse files Browse the repository at this point in the history
For one thing, it is container specific resource so it should not
be cleaned up by the agent. For another thing, we can make container
stop to force cleanup these host mountpoints regardless of hypervisor
and agent liveness.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Jul 23, 2019
1 parent e02f6dc commit d5d7d82
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,5 @@ type agent interface {
markDead()

// cleanup removes all on disk information generated by the agent
cleanup(id string)
cleanup(s *Sandbox)
}
12 changes: 12 additions & 0 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ func TestStartSandboxFailing(t *testing.T) {
}

func TestStopSandboxNoopAgentSuccessful(t *testing.T) {
if tc.NotValid(ktu.NeedRoot()) {
t.Skip(testDisabledAsNonRoot)
}
defer cleanUp()
assert := assert.New(t)

Expand Down Expand Up @@ -910,6 +913,9 @@ func TestStartContainerFailingSandboxNotStarted(t *testing.T) {
}

func TestStopContainerNoopAgentSuccessful(t *testing.T) {
if tc.NotValid(ktu.NeedRoot()) {
t.Skip(testDisabledAsNonRoot)
}
defer cleanUp()
assert := assert.New(t)

Expand Down Expand Up @@ -942,6 +948,9 @@ func TestStopContainerNoopAgentSuccessful(t *testing.T) {
}

func TestStopContainerFailingNoSandbox(t *testing.T) {
if tc.NotValid(ktu.NeedRoot()) {
t.Skip(testDisabledAsNonRoot)
}
defer cleanUp()

contID := "100"
Expand All @@ -951,6 +960,9 @@ func TestStopContainerFailingNoSandbox(t *testing.T) {
}

func TestStopContainerFailingNoContainer(t *testing.T) {
if tc.NotValid(ktu.NeedRoot()) {
t.Skip(testDisabledAsNonRoot)
}
defer cleanUp()
assert := assert.New(t)

Expand Down
8 changes: 8 additions & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,14 @@ func (c *Container) stop(force bool) error {
return err
}

if err := c.unmountHostMounts(); err != nil && !force {
return err
}

if err := bindUnmountContainerRootfs(c.ctx, kataHostSharedDir, c.sandbox.id, c.id); err != nil && !force {
return err
}

if err := c.detachDevices(); err != nil && !force {
return err
}
Expand Down
22 changes: 7 additions & 15 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1419,19 +1419,8 @@ func (k *kataAgent) stopContainer(sandbox *Sandbox, c Container) error {
span, _ := k.trace("stopContainer")
defer span.Finish()

req := &grpc.RemoveContainerRequest{
ContainerId: c.id,
}

if _, err := k.sendReq(req); err != nil {
return err
}

if err := c.unmountHostMounts(); err != nil {
return err
}

return bindUnmountContainerRootfs(k.ctx, kataHostSharedDir, sandbox.id, c.id)
_, err := k.sendReq(&grpc.RemoveContainerRequest{ContainerId: c.id})
return err
}

func (k *kataAgent) signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error {
Expand Down Expand Up @@ -2075,9 +2064,12 @@ func (k *kataAgent) markDead() {
k.disconnect()
}

func (k *kataAgent) cleanup(id string) {
path := k.getSharePath(id)
func (k *kataAgent) cleanup(s *Sandbox) {
path := k.getSharePath(s.id)
k.Logger().WithField("path", path).Infof("cleanup agent")
if err := bindUnmountAllRootfs(k.ctx, path, s); err != nil {
k.Logger().WithError(err).Errorf("failed to unmount container share path %s", path)
}
if err := os.RemoveAll(path); err != nil {
k.Logger().WithError(err).Errorf("failed to cleanup vm share path %s", path)
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/kata_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ func TestKataCleanupSandbox(t *testing.T) {
err := os.MkdirAll(dir, 0777)
assert.Nil(err)

k := &kataAgent{}
k.cleanup(s.id)
k := &kataAgent{ctx: context.Background()}
k.cleanup(&s)

_, err = os.Stat(dir)
assert.False(os.IsExist(err))
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/noop_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,5 @@ func (n *noopAgent) copyFile(src, dst string) error {
func (n *noopAgent) markDead() {
}

func (n *noopAgent) cleanup(id string) {
func (n *noopAgent) cleanup(s *Sandbox) {
}
2 changes: 1 addition & 1 deletion virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func (s *Sandbox) Delete() error {
s.Logger().WithError(err).Error("failed to cleanup hypervisor")
}

s.agent.cleanup(s.id)
s.agent.cleanup(s)

return s.store.Delete()
}
Expand Down

0 comments on commit d5d7d82

Please sign in to comment.