mount.go 1.5 KB

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