ctr: add EROFS image conversion support

Add EROFS conversion support to ctr convert command with configurable
options for tar-index mode and mkfs parameters.

Usage:
  ctr image convert --erofs src:tag dst:tag
  ctr image convert --erofs --erofs-compression='lz4hc,12' src:tag dst:tag

Signed-off-by: ChengyuZhu6 <hudson@cyzhu.com>
This commit is contained in:
ChengyuZhu6
2025-11-21 21:33:21 +08:00
committed by hudsonzhu
parent 997f813b5c
commit b320d3c855
4 changed files with 256 additions and 26 deletions

View File

@@ -22,7 +22,6 @@ import (
"io"
"os"
"path"
"runtime"
"strings"
"time"
@@ -93,15 +92,11 @@ func NewErofsDiffer(store content.Store, opts ...DifferOpt) differ {
}
// Add default block size on darwin if not already specified
d.mkfsExtraOpts = addDefaultMkfsOpts(d.mkfsExtraOpts)
d.mkfsExtraOpts = erofsutils.AddDefaultMkfsOpts(d.mkfsExtraOpts)
return d
}
func isErofsMediaType(mt string) bool {
return strings.HasSuffix(mt, ".erofs") || strings.HasPrefix(mt, "application/vnd.erofs.layer")
}
func (s erofsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount, opts ...diff.ApplyOpt) (d ocispec.Descriptor, err error) {
t1 := time.Now()
defer func() {
@@ -120,7 +115,7 @@ func (s erofsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []
fastcopy bool
)
diffLayerType := desc.MediaType
native := isErofsMediaType(diffLayerType)
native := erofsutils.IsErofsMediaType(diffLayerType)
if native {
base, ext, hasExt := strings.Cut(diffLayerType, "+")
// Mimic the OCI layer for EROFS blobs for diff.NewProcessorChain(), so
@@ -259,21 +254,3 @@ func (rc *readCounter) Read(p []byte) (n int, err error) {
rc.c += int64(n)
return
}
// addDefaultMkfsOpts adds default options for mkfs.erofs
func addDefaultMkfsOpts(mkfsExtraOpts []string) []string {
if runtime.GOOS != "darwin" {
return mkfsExtraOpts
}
// Check if -b argument is already present
for _, opt := range mkfsExtraOpts {
if strings.HasPrefix(opt, "-b") {
return mkfsExtraOpts
}
}
// Add -b4096 as the first option to prevent unusable block
// size from being used on macOS.
return append([]string{"-b4096"}, mkfsExtraOpts...)
}