Skip to content

Commit

Permalink
Merge pull request moby#46770 from vvoland/c8d-unmount-empty-basefs
Browse files Browse the repository at this point in the history
daemon/c8d: Unmount container fs after unclean shutdown
  • Loading branch information
vvoland authored Nov 27, 2023
2 parents ce1ee98 + 203bac0 commit 4cd2654
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
17 changes: 14 additions & 3 deletions daemon/containerd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containerd

import (
"context"
"errors"
"fmt"

"github.com/containerd/log"
Expand Down Expand Up @@ -30,11 +31,21 @@ func (i *ImageService) Mount(ctx context.Context, container *container.Container

// Unmount unmounts the container base filesystem
func (i *ImageService) Unmount(ctx context.Context, container *container.Container) error {
root := container.BaseFS
baseFS := container.BaseFS
if baseFS == "" {
target, err := i.refCountMounter.Mounted(container.ID)
if err != nil {
log.G(ctx).WithField("containerID", container.ID).Warn("failed to determine if container is already mounted")
}
if target == "" {
return errors.New("BaseFS is empty")
}
baseFS = target
}

if err := i.refCountMounter.Unmount(root); err != nil {
if err := i.refCountMounter.Unmount(baseFS); err != nil {
log.G(ctx).WithField("container", container.ID).WithError(err).Error("error unmounting container")
return fmt.Errorf("failed to unmount %s: %w", root, err)
return fmt.Errorf("failed to unmount %s: %w", baseFS, err)
}

return nil
Expand Down
30 changes: 30 additions & 0 deletions daemon/snapshotter/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/locker"
"github.com/moby/sys/mountinfo"
)

// List of known filesystems that can't be re-mounted or have shared layers
Expand All @@ -22,6 +23,8 @@ type Mounter interface {
Mount(mounts []mount.Mount, containerID string) (string, error)
// Unmount unmounts the container rootfs
Unmount(target string) error
// Mounted returns a target mountpoint if it's already mounted
Mounted(containerID string) (string, error)
}

// inSlice tests whether a string is contained in a slice of strings or not.
Expand Down Expand Up @@ -110,6 +113,23 @@ func (m *refCountMounter) Unmount(target string) error {
return nil
}

func (m *refCountMounter) Mounted(containerID string) (string, error) {
mounted, err := m.base.Mounted(containerID)
if err != nil || mounted == "" {
return mounted, err
}

target := m.base.target(containerID)

// Check if the refcount is non-zero.
m.rc.Increment(target)
if m.rc.Decrement(target) > 0 {
return mounted, nil
}

return "", nil
}

type mounter struct {
home string
snapshotter string
Expand Down Expand Up @@ -137,6 +157,16 @@ func (m mounter) Unmount(target string) error {
return unmount(target)
}

func (m mounter) Mounted(containerID string) (string, error) {
target := m.target(containerID)

mounted, err := mountinfo.Mounted(target)
if err != nil || !mounted {
return "", err
}
return target, nil
}

func (m mounter) target(containerID string) string {
return filepath.Join(m.home, "rootfs", m.snapshotter, containerID)
}

0 comments on commit 4cd2654

Please sign in to comment.