Skip to content

Commit

Permalink
virtcontainers : fix shared dir resource remaining
Browse files Browse the repository at this point in the history
Before this patch shared dir will reamin when sandox
has already removed, espacilly for kata-agent mod.

Do clean up shared dirs after all mounts are umounted.

Fixes: kata-containers#291

Signed-off-by: Haomin <caihaomin@huawei.com>
  • Loading branch information
jshachm committed Jun 19, 2018
1 parent 6f409b9 commit 8a6d383
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ type agent interface {
// stopSandbox will tell the agent to stop all containers related to the Sandbox.
stopSandbox(sandbox *Sandbox) error

// cleanup will clean the resources for sandbox
cleanupSandbox(sandbox *Sandbox) error

// createContainer will tell the agent to create a container related to a Sandbox.
createContainer(sandbox *Sandbox, c *Container) (*Process, error)

Expand Down
16 changes: 12 additions & 4 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,19 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
func (c *Container) unmountHostMounts() error {
for _, m := range c.mounts {
if m.HostPath != "" {
logger := c.Logger().WithField("host-path", m.HostPath)
if err := syscall.Unmount(m.HostPath, 0); err != nil {
c.Logger().WithFields(logrus.Fields{
"host-path": m.HostPath,
"error": err,
}).Warn("Could not umount")
// Unable to unmount paths could be a really big problem here
// we need to make sure cause 'less damage' if things are
// really broken. For further, we need to give admins more of
// a chance to diagnose the problem. As the rules of `fail fast`,
// here we return an error as soon as we get it.
logger.WithError(err).Warn("Could not umount")
return err
} else if err := os.RemoveAll(m.HostPath); err != nil {
// since the mounts related to the shared dir is umounted
// we need to remove the host path to avoid resource remaining
logger.WithError(err).Warn("Could not be removed")
return err
}
}
Expand Down
4 changes: 4 additions & 0 deletions virtcontainers/hyperstart_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,7 @@ func (h *hyper) resumeContainer(sandbox *Sandbox, c Container) error {
// hyperstart-agent does not support resume container
return nil
}

func (h *hyper) cleanupSandbox(sandbox *Sandbox) error {
return nil
}
13 changes: 12 additions & 1 deletion virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ func (k *kataAgent) stopSandbox(sandbox *Sandbox) error {
return k.proxy.stop(sandbox, k.state.ProxyPid)
}

func (k *kataAgent) cleanupSandbox(sandbox *Sandbox) error {
return os.RemoveAll(filepath.Join(kataHostSharedDir, sandbox.id))
}

func (k *kataAgent) replaceOCIMountSource(spec *specs.Spec, guestMounts []Mount) error {
ociMounts := spec.Mounts

Expand Down Expand Up @@ -951,7 +955,14 @@ func (k *kataAgent) stopContainer(sandbox *Sandbox, c Container) error {
return err
}

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

// since rootfs is umounted it's safe to remove the dir now
rootPathParent := filepath.Join(kataHostSharedDir, sandbox.id, c.id)

return os.RemoveAll(rootPathParent)
}

func (k *kataAgent) signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error {
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/noop_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (n *noopAgent) stopSandbox(sandbox *Sandbox) error {
return nil
}

// cleanup is the Noop agent clean up resource implementation. It does nothing.
func (n *noopAgent) cleanupSandbox(sandbox *Sandbox) error {
return nil
}

// createContainer is the Noop agent Container creation implementation. It does nothing.
func (n *noopAgent) createContainer(sandbox *Sandbox, c *Container) (*Process, error) {
return &Process{}, nil
Expand Down
7 changes: 7 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,13 @@ func (s *Sandbox) stop() error {
return err
}

// vm is stopped remove the sandbox shared dir
if err := s.agent.cleanupSandbox(s); err != nil {
// cleanup resource failed shouldn't block destroy sandbox
// just raise a warning
s.Logger().WithError(err).Warnf("cleanup sandbox failed")
}

return s.setSandboxState(StateStopped)
}

Expand Down

0 comments on commit 8a6d383

Please sign in to comment.