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