|
@@ -9,6 +9,7 @@ import (
|
|
|
"path/filepath"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/containerd/containerd/images"
|
|
|
"github.com/docker/distribution"
|
|
|
"github.com/docker/distribution/reference"
|
|
|
"github.com/docker/docker/image"
|
|
@@ -18,12 +19,14 @@ import (
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
"github.com/moby/sys/sequential"
|
|
|
"github.com/opencontainers/go-digest"
|
|
|
+ "github.com/opencontainers/image-spec/specs-go"
|
|
|
+ ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
"github.com/pkg/errors"
|
|
|
)
|
|
|
|
|
|
type imageDescriptor struct {
|
|
|
refs []reference.NamedTagged
|
|
|
- layers []string
|
|
|
+ layers []digest.Digest
|
|
|
image *image.Image
|
|
|
layerRef layer.Layer
|
|
|
}
|
|
@@ -190,32 +193,96 @@ func (s *saveSession) save(outStream io.Writer) error {
|
|
|
var manifest []manifestItem
|
|
|
var parentLinks []parentLink
|
|
|
|
|
|
+ var manifestDescriptors []ocispec.Descriptor
|
|
|
+
|
|
|
for id, imageDescr := range s.images {
|
|
|
foreignSrcs, err := s.saveImage(id)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- var repoTags []string
|
|
|
- var layers []string
|
|
|
+ var (
|
|
|
+ repoTags []string
|
|
|
+ layers []string
|
|
|
+ foreign = make([]ocispec.Descriptor, 0, len(foreignSrcs))
|
|
|
+ )
|
|
|
+
|
|
|
+ for _, desc := range foreignSrcs {
|
|
|
+ foreign = append(foreign, ocispec.Descriptor{
|
|
|
+ MediaType: desc.MediaType,
|
|
|
+ Digest: desc.Digest,
|
|
|
+ Size: desc.Size,
|
|
|
+ URLs: desc.URLs,
|
|
|
+ Annotations: desc.Annotations,
|
|
|
+ Platform: desc.Platform,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ imgPlat := imageDescr.image.Platform()
|
|
|
+
|
|
|
+ m := ocispec.Manifest{
|
|
|
+ Versioned: specs.Versioned{
|
|
|
+ SchemaVersion: 2,
|
|
|
+ },
|
|
|
+ MediaType: ocispec.MediaTypeImageManifest,
|
|
|
+ Config: ocispec.Descriptor{
|
|
|
+ MediaType: ocispec.MediaTypeImageConfig,
|
|
|
+ Digest: digest.Digest(imageDescr.image.ID()),
|
|
|
+ Size: int64(len(imageDescr.image.RawJSON())),
|
|
|
+ Platform: &imgPlat,
|
|
|
+ },
|
|
|
+ Layers: foreign,
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := json.Marshal(m)
|
|
|
+ if err != nil {
|
|
|
+ return errors.Wrap(err, "error marshaling manifest")
|
|
|
+ }
|
|
|
+ dgst := digest.FromBytes(data)
|
|
|
+
|
|
|
+ mFile := filepath.Join(s.outDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
|
|
|
+ if err := os.MkdirAll(filepath.Dir(mFile), 0o755); err != nil {
|
|
|
+ return errors.Wrap(err, "error creating blob directory")
|
|
|
+ }
|
|
|
+ if err := system.Chtimes(filepath.Dir(mFile), time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
+ return errors.Wrap(err, "error setting blob directory timestamps")
|
|
|
+ }
|
|
|
+ if err := os.WriteFile(mFile, data, 0o644); err != nil {
|
|
|
+ return errors.Wrap(err, "error writing oci manifest file")
|
|
|
+ }
|
|
|
+ if err := system.Chtimes(mFile, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
+ return errors.Wrap(err, "error setting blob directory timestamps")
|
|
|
+ }
|
|
|
+ size := int64(len(data))
|
|
|
|
|
|
for _, ref := range imageDescr.refs {
|
|
|
familiarName := reference.FamiliarName(ref)
|
|
|
if _, ok := reposLegacy[familiarName]; !ok {
|
|
|
reposLegacy[familiarName] = make(map[string]string)
|
|
|
}
|
|
|
- reposLegacy[familiarName][ref.Tag()] = imageDescr.layers[len(imageDescr.layers)-1]
|
|
|
+ reposLegacy[familiarName][ref.Tag()] = imageDescr.layers[len(imageDescr.layers)-1].Encoded()
|
|
|
repoTags = append(repoTags, reference.FamiliarString(ref))
|
|
|
+
|
|
|
+ manifestDescriptors = append(manifestDescriptors, ocispec.Descriptor{
|
|
|
+ MediaType: ocispec.MediaTypeImageManifest,
|
|
|
+ Digest: dgst,
|
|
|
+ Size: size,
|
|
|
+ Platform: m.Config.Platform,
|
|
|
+ Annotations: map[string]string{
|
|
|
+ images.AnnotationImageName: ref.String(),
|
|
|
+ ocispec.AnnotationRefName: ref.Tag(),
|
|
|
+ },
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
for _, l := range imageDescr.layers {
|
|
|
// IMPORTANT: We use path, not filepath here to ensure the layers
|
|
|
// in the manifest use Unix-style forward-slashes.
|
|
|
- layers = append(layers, path.Join(l, legacyLayerFileName))
|
|
|
+ layers = append(layers, path.Join("blobs", l.Algorithm().String(), l.Encoded()))
|
|
|
}
|
|
|
|
|
|
manifest = append(manifest, manifestItem{
|
|
|
- Config: id.Digest().Encoded() + ".json",
|
|
|
+ Config: path.Join("blobs", id.Digest().Algorithm().String(), id.Digest().Encoded()),
|
|
|
RepoTags: repoTags,
|
|
|
Layers: layers,
|
|
|
LayerSources: foreignSrcs,
|
|
@@ -251,8 +318,8 @@ func (s *saveSession) save(outStream io.Writer) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- manifestFileName := filepath.Join(tempDir, manifestFileName)
|
|
|
- f, err := os.OpenFile(manifestFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
+ manifestPath := filepath.Join(tempDir, manifestFileName)
|
|
|
+ f, err := os.OpenFile(manifestPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -264,10 +331,34 @@ func (s *saveSession) save(outStream io.Writer) error {
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
- if err := system.Chtimes(manifestFileName, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
+ if err := system.Chtimes(manifestPath, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ layoutPath := filepath.Join(tempDir, ociLayoutFilename)
|
|
|
+ if err := os.WriteFile(layoutPath, []byte(ociLayoutContent), 0o644); err != nil {
|
|
|
+ return errors.Wrap(err, "error writing oci layout file")
|
|
|
+ }
|
|
|
+ if err := system.Chtimes(layoutPath, time.Unix(0, 0), time.Unix(0, 0)); err != nil {
|
|
|
+ return errors.Wrap(err, "error setting oci layout file timestamps")
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := json.Marshal(ocispec.Index{
|
|
|
+ Versioned: specs.Versioned{
|
|
|
+ SchemaVersion: 2,
|
|
|
+ },
|
|
|
+ MediaType: ocispec.MediaTypeImageIndex,
|
|
|
+ Manifests: manifestDescriptors,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return errors.Wrap(err, "error marshaling oci index")
|
|
|
+ }
|
|
|
+
|
|
|
+ idxFile := filepath.Join(s.outDir, ociIndexFileName)
|
|
|
+ if err := os.WriteFile(idxFile, data, 0o644); err != nil {
|
|
|
+ return errors.Wrap(err, "error writing oci index file")
|
|
|
+ }
|
|
|
+
|
|
|
fs, err := archive.Tar(tempDir, archive.Uncompressed)
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -285,9 +376,9 @@ func (s *saveSession) saveImage(id image.ID) (map[layer.DiffID]distribution.Desc
|
|
|
}
|
|
|
|
|
|
var parent digest.Digest
|
|
|
- var layers []string
|
|
|
+ var layers []digest.Digest
|
|
|
var foreignSrcs map[layer.DiffID]distribution.Descriptor
|
|
|
- for i := range img.RootFS.DiffIDs {
|
|
|
+ for i, diffID := range img.RootFS.DiffIDs {
|
|
|
v1Img := image.V1Image{
|
|
|
// This is for backward compatibility used for
|
|
|
// pre v1.9 docker.
|
|
@@ -313,7 +404,8 @@ func (s *saveSession) saveImage(id image.ID) (map[layer.DiffID]distribution.Desc
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- layers = append(layers, v1Img.ID)
|
|
|
+
|
|
|
+ layers = append(layers, digest.Digest(diffID))
|
|
|
parent = v1ID
|
|
|
if src.Digest != "" {
|
|
|
if foreignSrcs == nil {
|
|
@@ -323,7 +415,21 @@ func (s *saveSession) saveImage(id image.ID) (map[layer.DiffID]distribution.Desc
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- configFile := filepath.Join(s.outDir, id.Digest().Encoded()+".json")
|
|
|
+ data := img.RawJSON()
|
|
|
+ dgst := digest.FromBytes(data)
|
|
|
+
|
|
|
+ blobDir := filepath.Join(s.outDir, "blobs", dgst.Algorithm().String())
|
|
|
+ if err := os.MkdirAll(blobDir, 0o755); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if err := system.Chtimes(blobDir, img.Created, img.Created); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if err := system.Chtimes(filepath.Dir(blobDir), img.Created, img.Created); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ configFile := filepath.Join(blobDir, dgst.Encoded())
|
|
|
if err := os.WriteFile(configFile, img.RawJSON(), 0o644); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
@@ -340,47 +446,46 @@ func (s *saveSession) saveLayer(id layer.ChainID, legacyImg image.V1Image, creat
|
|
|
return distribution.Descriptor{}, nil
|
|
|
}
|
|
|
|
|
|
- outDir := filepath.Join(s.outDir, legacyImg.ID)
|
|
|
- if err := os.Mkdir(outDir, 0755); err != nil {
|
|
|
- return distribution.Descriptor{}, err
|
|
|
- }
|
|
|
-
|
|
|
- // todo: why is this version file here?
|
|
|
- if err := os.WriteFile(filepath.Join(outDir, legacyVersionFileName), []byte("1.0"), 0644); err != nil {
|
|
|
- return distribution.Descriptor{}, err
|
|
|
- }
|
|
|
+ outDir := filepath.Join(s.outDir, "blobs")
|
|
|
|
|
|
imageConfig, err := json.Marshal(legacyImg)
|
|
|
if err != nil {
|
|
|
return distribution.Descriptor{}, err
|
|
|
}
|
|
|
|
|
|
- if err := os.WriteFile(filepath.Join(outDir, legacyConfigFileName), imageConfig, 0644); err != nil {
|
|
|
+ cfgDgst := digest.FromBytes(imageConfig)
|
|
|
+ configPath := filepath.Join(outDir, cfgDgst.Algorithm().String(), cfgDgst.Encoded())
|
|
|
+ if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
|
|
+ return distribution.Descriptor{}, errors.Wrap(err, "could not create layer dir parent")
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := os.WriteFile(configPath, imageConfig, 0644); err != nil {
|
|
|
return distribution.Descriptor{}, err
|
|
|
}
|
|
|
|
|
|
// serialize filesystem
|
|
|
- layerPath := filepath.Join(outDir, legacyLayerFileName)
|
|
|
l, err := s.lss.Get(id)
|
|
|
if err != nil {
|
|
|
return distribution.Descriptor{}, err
|
|
|
}
|
|
|
+
|
|
|
+ lDgst := digest.Digest(l.DiffID())
|
|
|
+ layerPath := filepath.Join(outDir, lDgst.Algorithm().String(), lDgst.Encoded())
|
|
|
defer layer.ReleaseAndLog(s.lss, l)
|
|
|
|
|
|
- if oldPath, exists := s.diffIDPaths[l.DiffID()]; exists {
|
|
|
- relPath, err := filepath.Rel(outDir, oldPath)
|
|
|
- if err != nil {
|
|
|
+ if _, err = os.Stat(layerPath); err != nil {
|
|
|
+ if !os.IsNotExist(err) {
|
|
|
return distribution.Descriptor{}, err
|
|
|
}
|
|
|
- if err := os.Symlink(relPath, layerPath); err != nil {
|
|
|
- return distribution.Descriptor{}, errors.Wrap(err, "error creating symlink while saving layer")
|
|
|
- }
|
|
|
- } else {
|
|
|
+
|
|
|
// We use sequential file access to avoid depleting the standby list on
|
|
|
// Windows. On Linux, this equates to a regular os.Create.
|
|
|
+ if err := os.MkdirAll(filepath.Dir(layerPath), 0755); err != nil {
|
|
|
+ return distribution.Descriptor{}, errors.Wrap(err, "could not create layer dir parent")
|
|
|
+ }
|
|
|
tarFile, err := sequential.Create(layerPath)
|
|
|
if err != nil {
|
|
|
- return distribution.Descriptor{}, err
|
|
|
+ return distribution.Descriptor{}, errors.Wrap(err, "error creating layer file")
|
|
|
}
|
|
|
defer tarFile.Close()
|
|
|
|
|
@@ -394,16 +499,16 @@ func (s *saveSession) saveLayer(id layer.ChainID, legacyImg image.V1Image, creat
|
|
|
return distribution.Descriptor{}, err
|
|
|
}
|
|
|
|
|
|
- for _, fname := range []string{"", legacyVersionFileName, legacyConfigFileName, legacyLayerFileName} {
|
|
|
+ for _, fname := range []string{outDir, configPath, layerPath} {
|
|
|
// todo: maybe save layer created timestamp?
|
|
|
- if err := system.Chtimes(filepath.Join(outDir, fname), createdTime, createdTime); err != nil {
|
|
|
- return distribution.Descriptor{}, err
|
|
|
+ if err := system.Chtimes(fname, createdTime, createdTime); err != nil {
|
|
|
+ return distribution.Descriptor{}, errors.Wrap(err, "could not set layer timestamp")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
s.diffIDPaths[l.DiffID()] = layerPath
|
|
|
+ s.savedLayers[legacyImg.ID] = struct{}{}
|
|
|
}
|
|
|
- s.savedLayers[legacyImg.ID] = struct{}{}
|
|
|
|
|
|
var src distribution.Descriptor
|
|
|
if fs, ok := l.(distribution.Describable); ok {
|