Skip to content

Commit 4b29fed

Browse files
authored
Merge pull request #159 from HileQAQ/main
Add zstd compress options for image convert
2 parents 1efe320 + e739fc9 commit 4b29fed

File tree

4 files changed

+97
-25
lines changed

4 files changed

+97
-25
lines changed

cmd/ctr/overlaybd_conv.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ var convertCommand = cli.Command{
5858
Usage: "data base config string used for layer deduplication",
5959
Value: "",
6060
},
61+
cli.StringFlag{
62+
Name: "algorithm",
63+
Usage: "compress algorithm uses in zfile, [lz4|zstd]",
64+
Value: "",
65+
},
66+
cli.IntFlag{
67+
Name: "bs",
68+
Usage: "The size of a compressed data block in KB. Must be a power of two between 4K~64K [4/8/16/32/64])",
69+
Value: 0,
70+
},
6171
),
6272
Action: func(context *cli.Context) error {
6373
var (
@@ -97,6 +107,10 @@ var convertCommand = cli.Command{
97107
obdOpts = append(obdOpts, obdconv.WithDbstr(dbstr))
98108
fmt.Printf("database config string: %s\n", dbstr)
99109
}
110+
algorithm := context.String("algorithm")
111+
obdOpts = append(obdOpts, obdconv.WithAlgorithm(algorithm))
112+
blockSize := context.Int("bs")
113+
obdOpts = append(obdOpts, obdconv.WithBlockSize(blockSize))
100114

101115
resolver, err := commands.GetResolver(ctx, context)
102116
if err != nil {

pkg/convertor/convertor.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const (
5858
labelOverlayBDBlobWritable = "containerd.io/snapshot/overlaybd.writable"
5959
labelKeyAccelerationLayer = "containerd.io/snapshot/overlaybd/acceleration-layer"
6060
labelBuildLayerFrom = "containerd.io/snapshot/overlaybd/build.layer-from"
61+
labelKeyZFileConfig = "containerd.io/snapshot/overlaybd/zfile-config"
6162
labelDistributionSource = "containerd.io/distribution.source"
6263
)
6364

@@ -70,6 +71,11 @@ var (
7071
convContentNameFormat = convSnapshotNameFormat
7172
)
7273

74+
type ZFileConfig struct {
75+
Algorithm string `json:"algorithm"`
76+
BlockSize int `json:"blockSize"`
77+
}
78+
7379
type ImageConvertor interface {
7480
Convert(ctx context.Context, srcManifest ocispec.Manifest, fsType string) (ocispec.Descriptor, error)
7581
}
@@ -187,21 +193,23 @@ func (loader *contentLoader) Load(ctx context.Context, cs content.Store) (l laye
187193

188194
type overlaybdConvertor struct {
189195
ImageConvertor
190-
cs content.Store
191-
sn snapshots.Snapshotter
192-
remote bool
193-
fetcher remotes.Fetcher
194-
pusher remotes.Pusher
195-
db *sql.DB
196-
host string
197-
repo string
196+
cs content.Store
197+
sn snapshots.Snapshotter
198+
remote bool
199+
fetcher remotes.Fetcher
200+
pusher remotes.Pusher
201+
db *sql.DB
202+
host string
203+
repo string
204+
zfileCfg ZFileConfig
198205
}
199206

200-
func NewOverlaybdConvertor(ctx context.Context, cs content.Store, sn snapshots.Snapshotter, resolver remotes.Resolver, ref string, dbstr string) (ImageConvertor, error) {
207+
func NewOverlaybdConvertor(ctx context.Context, cs content.Store, sn snapshots.Snapshotter, resolver remotes.Resolver, ref string, dbstr string, zfileCfg ZFileConfig) (ImageConvertor, error) {
201208
c := &overlaybdConvertor{
202-
cs: cs,
203-
sn: sn,
204-
remote: false,
209+
cs: cs,
210+
sn: sn,
211+
remote: false,
212+
zfileCfg: zfileCfg,
205213
}
206214
var err error
207215
if dbstr != "" {
@@ -505,6 +513,13 @@ func (c *overlaybdConvertor) convertLayers(ctx context.Context, srcDescs []ocisp
505513
labelOverlayBDBlobFsType: fsType,
506514
}),
507515
}
516+
cfgStr, err := json.Marshal(c.zfileCfg)
517+
if err != nil {
518+
return nil, err
519+
}
520+
opts = append(opts, snapshots.WithLabels(map[string]string{
521+
labelKeyZFileConfig: string(cfgStr),
522+
}))
508523
lastParentID, err = c.applyOCIV1LayerInZfile(ctx, lastParentID, desc, opts, nil)
509524
if err != nil {
510525
return nil, err
@@ -606,7 +621,7 @@ func (c *overlaybdConvertor) applyOCIV1LayerInZfile(
606621
}
607622

608623
commitID := fmt.Sprintf(convSnapshotNameFormat, digester.Digest())
609-
if err = c.sn.Commit(ctx, commitID, key); err != nil {
624+
if err = c.sn.Commit(ctx, commitID, key, snOpts...); err != nil {
610625
if !errdefs.IsAlreadyExists(err) {
611626
return emptyString, err
612627
}
@@ -638,11 +653,13 @@ func (wc *writeCountWrapper) Write(p []byte) (n int, err error) {
638653

639654
// NOTE: based on https://github.com/containerd/containerd/blob/v1.6.8/images/converter/converter.go#L29-L71
640655
type options struct {
641-
fsType string
642-
dbstr string
643-
imgRef string
644-
resolver remotes.Resolver
645-
client *containerd.Client
656+
fsType string
657+
dbstr string
658+
imgRef string
659+
algorithm string
660+
blockSize int
661+
resolver remotes.Resolver
662+
client *containerd.Client
646663
}
647664

648665
type Option func(o *options) error
@@ -668,6 +685,20 @@ func WithImageRef(imgRef string) Option {
668685
}
669686
}
670687

688+
func WithAlgorithm(algorithm string) Option {
689+
return func(o *options) error {
690+
o.algorithm = algorithm
691+
return nil
692+
}
693+
}
694+
695+
func WithBlockSize(blockSize int) Option {
696+
return func(o *options) error {
697+
o.blockSize = blockSize
698+
return nil
699+
}
700+
}
701+
671702
func WithResolver(resolver remotes.Resolver) Option {
672703
return func(o *options) error {
673704
o.resolver = resolver
@@ -703,8 +734,11 @@ func IndexConvertFunc(opts ...Option) converter.ConvertFunc {
703734
if err != nil {
704735
return nil, errors.Wrapf(err, "failed to read manifest")
705736
}
706-
707-
c, err := NewOverlaybdConvertor(ctx, cs, sn, copts.resolver, imgRef, copts.dbstr)
737+
zfileCfg := ZFileConfig{
738+
Algorithm: copts.algorithm,
739+
BlockSize: copts.blockSize,
740+
}
741+
c, err := NewOverlaybdConvertor(ctx, cs, sn, copts.resolver, imgRef, copts.dbstr, zfileCfg)
708742
if err != nil {
709743
return nil, err
710744
}

pkg/snapshot/overlay.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package snapshot
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"os"
2324
"path/filepath"
@@ -107,6 +108,9 @@ const (
107108
// labelKeyRecordTracePath is the the file path to record trace
108109
labelKeyRecordTracePath = "containerd.io/snapshot/overlaybd/record-trace-path"
109110

111+
// labelKeyZFileConfig is the config of ZFile
112+
labelKeyZFileConfig = "containerd.io/snapshot/overlaybd/zfile-config"
113+
110114
// labelKeyCriImageRef is thr image-ref from cri
111115
labelKeyCriImageRef = "containerd.io/snapshot/cri.image-ref"
112116

@@ -160,6 +164,11 @@ func DefaultBootConfig() *BootConfig {
160164
}
161165
}
162166

167+
type ZFileConfig struct {
168+
Algorithm string `json:"algorithm"`
169+
BlockSize int `json:"blockSize"`
170+
}
171+
163172
// SnapshotterConfig is used to configure the snapshotter instance
164173
type SnapshotterConfig struct {
165174
// OverlayBDUtilBinDir contains overlaybd-create/overlaybd-commit tools
@@ -668,7 +677,14 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
668677
return errors.Wrapf(err, "failed to destroy target device for snapshot %s", key)
669678
}
670679

671-
if err := o.commitWritableOverlaybd(ctx, id); err != nil {
680+
var zfileCfg ZFileConfig
681+
if cfgStr, ok := oinfo.Labels[labelKeyZFileConfig]; ok {
682+
if err := json.Unmarshal([]byte(cfgStr), &zfileCfg); err != nil {
683+
return err
684+
}
685+
}
686+
687+
if err := o.commitWritableOverlaybd(ctx, id, zfileCfg); err != nil {
672688
return err
673689
}
674690

pkg/snapshot/storage.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,21 @@ func (o *snapshotter) prepareWritableOverlaybd(ctx context.Context, snID string)
649649
}
650650

651651
// commitWritableOverlaybd
652-
func (o *snapshotter) commitWritableOverlaybd(ctx context.Context, snID string) (retErr error) {
652+
func (o *snapshotter) commitWritableOverlaybd(ctx context.Context, snID string, cfg ZFileConfig) (retErr error) {
653653
binpath := filepath.Join(o.config.OverlayBDUtilBinDir, "overlaybd-commit")
654-
655-
out, err := exec.CommandContext(ctx, binpath, "-z",
654+
opts := []string{
655+
"-z",
656656
o.overlaybdWritableDataPath(snID),
657657
o.overlaybdWritableIndexPath(snID),
658-
o.magicFilePath(snID)).CombinedOutput()
658+
o.magicFilePath(snID),
659+
}
660+
if cfg.BlockSize != 0 {
661+
opts = append(opts, "--bs", strconv.Itoa(cfg.BlockSize))
662+
}
663+
if cfg.Algorithm != "" {
664+
opts = append(opts, "--algorithm", cfg.Algorithm)
665+
}
666+
out, err := exec.CommandContext(ctx, binpath, opts...).CombinedOutput()
659667
if err != nil {
660668
return errors.Wrapf(err, "failed to commit writable overlaybd: %s", out)
661669
}

0 commit comments

Comments
 (0)