|
@@ -17,7 +17,7 @@ import (
|
|
|
"github.com/containerd/containerd/images"
|
|
|
"github.com/containerd/containerd/leases"
|
|
|
"github.com/containerd/containerd/mount"
|
|
|
- "github.com/containerd/containerd/rootfs"
|
|
|
+ "github.com/containerd/containerd/pkg/cleanup"
|
|
|
"github.com/containerd/containerd/snapshots"
|
|
|
"github.com/containerd/log"
|
|
|
"github.com/docker/docker/api/types/backend"
|
|
@@ -29,6 +29,7 @@ import (
|
|
|
"github.com/opencontainers/image-spec/identity"
|
|
|
"github.com/opencontainers/image-spec/specs-go"
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
+ "github.com/pkg/errors"
|
|
|
)
|
|
|
|
|
|
/*
|
|
@@ -81,7 +82,7 @@ func (i *ImageService) CommitImage(ctx context.Context, cc backend.CommitConfig)
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- diffLayerDesc, diffID, err := createDiff(ctx, cc.ContainerID, sn, cs, differ)
|
|
|
+ diffLayerDesc, diffID, err := i.createDiff(ctx, cc.ContainerID, sn, cs, differ)
|
|
|
if err != nil {
|
|
|
return "", fmt.Errorf("failed to export layer: %w", err)
|
|
|
}
|
|
@@ -249,12 +250,82 @@ func writeContentsForImage(ctx context.Context, snName string, cs content.Store,
|
|
|
|
|
|
// createDiff creates a layer diff into containerd's content store.
|
|
|
// If the diff is empty it returns nil empty digest and no error.
|
|
|
-func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer) (*ocispec.Descriptor, digest.Digest, error) {
|
|
|
- newDesc, err := rootfs.CreateDiff(ctx, name, sn, comparer)
|
|
|
+func (i *ImageService) createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs content.Store, comparer diff.Comparer) (*ocispec.Descriptor, digest.Digest, error) {
|
|
|
+ info, err := sn.Stat(ctx, name)
|
|
|
if err != nil {
|
|
|
return nil, "", err
|
|
|
}
|
|
|
|
|
|
+ var upper []mount.Mount
|
|
|
+ if !i.idMapping.Empty() {
|
|
|
+ // The rootfs of the container is remapped if an id mapping exists, we
|
|
|
+ // need to "unremap" it before committing the snapshot
|
|
|
+ rootPair := i.idMapping.RootPair()
|
|
|
+ usernsID := fmt.Sprintf("%s-%d-%d-%s", name, rootPair.UID, rootPair.GID, uniquePart())
|
|
|
+ remappedID := usernsID + remapSuffix
|
|
|
+ baseName := name
|
|
|
+
|
|
|
+ if info.Kind == snapshots.KindActive {
|
|
|
+ source, err := sn.Mounts(ctx, name)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ // No need to use parent since the whole snapshot is copied.
|
|
|
+ // Using parent would require doing diff/apply while starting
|
|
|
+ // from empty can just copy the whole snapshot.
|
|
|
+ // TODO: Optimize this for overlay mounts, can use parent
|
|
|
+ // and just copy upper directories without mounting
|
|
|
+ upper, err = sn.Prepare(ctx, remappedID, "")
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := i.copyAndUnremapRootFS(ctx, upper, source); err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ upper, err = sn.Prepare(ctx, remappedID, baseName)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := i.unremapRootFS(ctx, upper); err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if info.Kind == snapshots.KindActive {
|
|
|
+ upper, err = sn.Mounts(ctx, name)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ upperKey := fmt.Sprintf("%s-view-%s", name, uniquePart())
|
|
|
+ upper, err = sn.View(ctx, upperKey, name)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ defer cleanup.Do(ctx, func(ctx context.Context) {
|
|
|
+ sn.Remove(ctx, upperKey)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ lowerKey := fmt.Sprintf("%s-parent-view-%s", info.Parent, uniquePart())
|
|
|
+ lower, err := sn.View(ctx, lowerKey, info.Parent)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", err
|
|
|
+ }
|
|
|
+ defer cleanup.Do(ctx, func(ctx context.Context) {
|
|
|
+ sn.Remove(ctx, lowerKey)
|
|
|
+ })
|
|
|
+
|
|
|
+ newDesc, err := comparer.Compare(ctx, lower, upper)
|
|
|
+ if err != nil {
|
|
|
+ return nil, "", errors.Wrap(err, "CreateDiff")
|
|
|
+ }
|
|
|
+
|
|
|
ra, err := cs.ReaderAt(ctx, newDesc)
|
|
|
if err != nil {
|
|
|
return nil, "", fmt.Errorf("failed to read diff archive: %w", err)
|
|
@@ -269,12 +340,12 @@ func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs c
|
|
|
return nil, "", nil
|
|
|
}
|
|
|
|
|
|
- info, err := cs.Info(ctx, newDesc.Digest)
|
|
|
+ cinfo, err := cs.Info(ctx, newDesc.Digest)
|
|
|
if err != nil {
|
|
|
return nil, "", fmt.Errorf("failed to get content info: %w", err)
|
|
|
}
|
|
|
|
|
|
- diffIDStr, ok := info.Labels["containerd.io/uncompressed"]
|
|
|
+ diffIDStr, ok := cinfo.Labels["containerd.io/uncompressed"]
|
|
|
if !ok {
|
|
|
return nil, "", fmt.Errorf("invalid differ response with no diffID")
|
|
|
}
|
|
@@ -287,7 +358,7 @@ func createDiff(ctx context.Context, name string, sn snapshots.Snapshotter, cs c
|
|
|
return &ocispec.Descriptor{
|
|
|
MediaType: ocispec.MediaTypeImageLayerGzip,
|
|
|
Digest: newDesc.Digest,
|
|
|
- Size: info.Size,
|
|
|
+ Size: cinfo.Size,
|
|
|
}, diffID, nil
|
|
|
}
|
|
|
|
|
@@ -311,7 +382,7 @@ func (i *ImageService) applyDiffLayer(ctx context.Context, name string, containe
|
|
|
|
|
|
defer func() {
|
|
|
if retErr != nil {
|
|
|
- // NOTE: the snapshotter should be hold by lease. Even
|
|
|
+ // NOTE: the snapshotter should be held by lease. Even
|
|
|
// if the cleanup fails, the containerd gc can delete it.
|
|
|
if err := sn.Remove(ctx, key); err != nil {
|
|
|
log.G(ctx).Warnf("failed to cleanup aborted apply %s: %s", key, err)
|
|
@@ -323,35 +394,6 @@ func (i *ImageService) applyDiffLayer(ctx context.Context, name string, containe
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if !i.idMapping.Empty() {
|
|
|
- // The rootfs of the container is remapped if an id mapping exists, we
|
|
|
- // need to "unremap" it before committing the snapshot
|
|
|
- rootPair := i.idMapping.RootPair()
|
|
|
- usernsID := fmt.Sprintf("%s-%d-%d", key, rootPair.UID, rootPair.GID)
|
|
|
- remappedID := usernsID + remapSuffix
|
|
|
-
|
|
|
- if err = sn.Commit(ctx, name+"-pre", key); err != nil {
|
|
|
- if cerrdefs.IsAlreadyExists(err) {
|
|
|
- return nil
|
|
|
- }
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- mounts, err = sn.Prepare(ctx, remappedID, name+"-pre")
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- if err := i.unremapRootFS(ctx, mounts); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- if err := sn.Commit(ctx, name, remappedID); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- key = remappedID
|
|
|
- }
|
|
|
-
|
|
|
if err = sn.Commit(ctx, name, key); err != nil {
|
|
|
if cerrdefs.IsAlreadyExists(err) {
|
|
|
return nil
|