mount.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package containerd
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/containerd/containerd/log"
  6. "github.com/docker/docker/container"
  7. )
  8. // Mount mounts the container filesystem in a temporary location, use defer imageService.Unmount
  9. // to unmount the filesystem when calling this
  10. func (i *ImageService) Mount(ctx context.Context, container *container.Container) error {
  11. snapshotter := i.client.SnapshotService(container.Driver)
  12. mounts, err := snapshotter.Mounts(ctx, container.ID)
  13. if err != nil {
  14. return err
  15. }
  16. var root string
  17. if root, err = i.refCountMounter.Mount(mounts, container.ID); err != nil {
  18. return fmt.Errorf("failed to mount %s: %w", root, err)
  19. }
  20. log.G(ctx).WithField("container", container.ID).Debugf("container mounted via snapshotter: %v", root)
  21. container.BaseFS = root
  22. return nil
  23. }
  24. // Unmount unmounts the container base filesystem
  25. func (i *ImageService) Unmount(ctx context.Context, container *container.Container) error {
  26. root := container.BaseFS
  27. if err := i.refCountMounter.Unmount(root); err != nil {
  28. log.G(ctx).WithField("container", container.ID).WithError(err).Error("error unmounting container")
  29. return fmt.Errorf("failed to unmount %s: %w", root, err)
  30. }
  31. return nil
  32. }