image_snapshot.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package containerd
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/containerd/containerd"
  6. cerrdefs "github.com/containerd/containerd/errdefs"
  7. containerdimages "github.com/containerd/containerd/images"
  8. "github.com/containerd/containerd/leases"
  9. "github.com/containerd/containerd/mount"
  10. "github.com/containerd/containerd/platforms"
  11. "github.com/containerd/containerd/snapshots"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/opencontainers/image-spec/identity"
  14. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  15. "github.com/pkg/errors"
  16. )
  17. const remapSuffix = "-remap"
  18. // PrepareSnapshot prepares a snapshot from a parent image for a container
  19. func (i *ImageService) PrepareSnapshot(ctx context.Context, id string, parentImage string, platform *ocispec.Platform, setupInit func(string) error) error {
  20. var parentSnapshot string
  21. if parentImage != "" {
  22. img, err := i.resolveImage(ctx, parentImage)
  23. if err != nil {
  24. return err
  25. }
  26. cs := i.client.ContentStore()
  27. matcher := platforms.Default()
  28. if platform != nil {
  29. matcher = platforms.Only(*platform)
  30. }
  31. platformImg := containerd.NewImageWithPlatform(i.client, img, matcher)
  32. unpacked, err := platformImg.IsUnpacked(ctx, i.snapshotter)
  33. if err != nil {
  34. return err
  35. }
  36. if !unpacked {
  37. if err := platformImg.Unpack(ctx, i.snapshotter); err != nil {
  38. return err
  39. }
  40. }
  41. desc, err := containerdimages.Config(ctx, cs, img.Target, matcher)
  42. if err != nil {
  43. return err
  44. }
  45. diffIDs, err := containerdimages.RootFS(ctx, cs, desc)
  46. if err != nil {
  47. return err
  48. }
  49. parentSnapshot = identity.ChainID(diffIDs).String()
  50. }
  51. ls := i.client.LeasesService()
  52. lease, err := ls.Create(ctx, leases.WithID(id))
  53. if err != nil {
  54. return err
  55. }
  56. ctx = leases.WithLease(ctx, lease.ID)
  57. snapshotter := i.client.SnapshotService(i.StorageDriver())
  58. if err := i.prepareInitLayer(ctx, id, parentSnapshot, setupInit); err != nil {
  59. return err
  60. }
  61. if !i.idMapping.Empty() {
  62. return i.remapSnapshot(ctx, snapshotter, id, id+"-init")
  63. }
  64. _, err = snapshotter.Prepare(ctx, id, id+"-init")
  65. return err
  66. }
  67. func (i *ImageService) prepareInitLayer(ctx context.Context, id string, parent string, setupInit func(string) error) error {
  68. snapshotter := i.client.SnapshotService(i.StorageDriver())
  69. mounts, err := snapshotter.Prepare(ctx, id+"-init-key", parent)
  70. if err != nil {
  71. return err
  72. }
  73. if err := mount.WithTempMount(ctx, mounts, func(root string) error {
  74. return setupInit(root)
  75. }); err != nil {
  76. return err
  77. }
  78. return snapshotter.Commit(ctx, id+"-init", id+"-init-key")
  79. }
  80. // calculateSnapshotParentUsage returns the usage of all ancestors of the
  81. // provided snapshot. It doesn't include the size of the snapshot itself.
  82. func calculateSnapshotParentUsage(ctx context.Context, snapshotter snapshots.Snapshotter, snapshotID string) (snapshots.Usage, error) {
  83. info, err := snapshotter.Stat(ctx, snapshotID)
  84. if err != nil {
  85. if cerrdefs.IsNotFound(err) {
  86. return snapshots.Usage{}, errdefs.NotFound(err)
  87. }
  88. return snapshots.Usage{}, errdefs.System(errors.Wrapf(err, "snapshotter.Stat failed for %s", snapshotID))
  89. }
  90. if info.Parent == "" {
  91. return snapshots.Usage{}, errdefs.NotFound(fmt.Errorf("snapshot %s has no parent", snapshotID))
  92. }
  93. return calculateSnapshotTotalUsage(ctx, snapshotter, info.Parent)
  94. }
  95. // calculateSnapshotTotalUsage returns the total usage of that snapshot
  96. // including all of its ancestors.
  97. func calculateSnapshotTotalUsage(ctx context.Context, snapshotter snapshots.Snapshotter, snapshotID string) (snapshots.Usage, error) {
  98. var total snapshots.Usage
  99. next := snapshotID
  100. for next != "" {
  101. usage, err := snapshotter.Usage(ctx, next)
  102. if err != nil {
  103. if cerrdefs.IsNotFound(err) {
  104. return total, errdefs.NotFound(errors.Wrapf(err, "non-existing ancestor of %s", snapshotID))
  105. }
  106. return total, errdefs.System(errors.Wrapf(err, "snapshotter.Usage failed for %s", next))
  107. }
  108. total.Size += usage.Size
  109. total.Inodes += usage.Inodes
  110. info, err := snapshotter.Stat(ctx, next)
  111. if err != nil {
  112. return total, errdefs.System(errors.Wrapf(err, "snapshotter.Stat failed for %s", next))
  113. }
  114. next = info.Parent
  115. }
  116. return total, nil
  117. }