github.com/moby/docker@v26.1.3+incompatible/daemon/containerd/mount.go (about)

     1  package containerd
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/containerd/log"
     9  	"github.com/docker/docker/container"
    10  )
    11  
    12  // Mount mounts the container filesystem in a temporary location, use defer imageService.Unmount
    13  // to unmount the filesystem when calling this
    14  func (i *ImageService) Mount(ctx context.Context, container *container.Container) error {
    15  	snapshotter := i.client.SnapshotService(container.Driver)
    16  	mounts, err := snapshotter.Mounts(ctx, container.ID)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	var root string
    22  	if root, err = i.refCountMounter.Mount(mounts, container.ID); err != nil {
    23  		return fmt.Errorf("failed to mount %s: %w", root, err)
    24  	}
    25  
    26  	log.G(ctx).WithField("container", container.ID).Debugf("container mounted via snapshotter: %v", root)
    27  
    28  	container.BaseFS = root
    29  	return nil
    30  }
    31  
    32  // Unmount unmounts the container base filesystem
    33  func (i *ImageService) Unmount(ctx context.Context, container *container.Container) error {
    34  	baseFS := container.BaseFS
    35  	if baseFS == "" {
    36  		target, err := i.refCountMounter.Mounted(container.ID)
    37  		if err != nil {
    38  			log.G(ctx).WithField("containerID", container.ID).Warn("failed to determine if container is already mounted")
    39  		}
    40  		if target == "" {
    41  			return errors.New("BaseFS is empty")
    42  		}
    43  		baseFS = target
    44  	}
    45  
    46  	if err := i.refCountMounter.Unmount(baseFS); err != nil {
    47  		log.G(ctx).WithField("container", container.ID).WithError(err).Error("error unmounting container")
    48  		return fmt.Errorf("failed to unmount %s: %w", baseFS, err)
    49  	}
    50  
    51  	return nil
    52  }