Skip to content

Commit 0b0fd15

Browse files
authored
Merge pull request #263 from BigVan/conv2turbo
[Feat.] Converting ociv1 layer blob to turboOCI blob locally.
2 parents 6b72a04 + eeb5d09 commit 0b0fd15

File tree

9 files changed

+328
-71
lines changed

9 files changed

+328
-71
lines changed

cmd/convertor/builder/builder_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
"github.com/pkg/errors"
4141
"github.com/sirupsen/logrus"
4242

43-
"github.com/containerd/accelerated-container-image/pkg/snapshot"
43+
t "github.com/containerd/accelerated-container-image/pkg/types"
4444
)
4545

4646
func fetch(ctx context.Context, fetcher remotes.Fetcher, desc specs.Descriptor, target any) error {
@@ -144,7 +144,7 @@ func downloadLayer(ctx context.Context, fetcher remotes.Fetcher, targetFile stri
144144
}
145145

146146
// TODO maybe refactor this
147-
func writeConfig(dir string, configJSON *snapshot.OverlayBDBSConfig) error {
147+
func writeConfig(dir string, configJSON *t.OverlayBDBSConfig) error {
148148
data, err := json.Marshal(configJSON)
149149
if err != nil {
150150
return err

cmd/convertor/builder/builder_utils_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"testing"
2929

3030
testingresources "github.com/containerd/accelerated-container-image/cmd/convertor/testingresources"
31-
"github.com/containerd/accelerated-container-image/pkg/snapshot"
31+
sn "github.com/containerd/accelerated-container-image/pkg/types"
3232
"github.com/containerd/containerd/images"
3333
_ "github.com/containerd/containerd/pkg/testutil" // Handle custom root flag
3434
"github.com/containerd/containerd/remotes"
@@ -428,17 +428,17 @@ func Test_downloadLayer(t *testing.T) {
428428
func Test_writeConfig(t *testing.T) {
429429
ctx := context.Background()
430430
testingresources.RunTestWithTempDir(t, ctx, "writeConfigMinimal", func(t *testing.T, ctx context.Context, workdir string) {
431-
configSample := snapshot.OverlayBDBSConfig{
431+
configSample := sn.OverlayBDBSConfig{
432432
ResultFile: "",
433-
Lowers: []snapshot.OverlayBDBSConfigLower{
433+
Lowers: []sn.OverlayBDBSConfigLower{
434434
{
435435
File: overlaybdBaseLayer,
436436
},
437437
{
438438
File: path.Join(workdir, commitFile),
439439
},
440440
},
441-
Upper: snapshot.OverlayBDBSConfigUpper{
441+
Upper: sn.OverlayBDBSConfigUpper{
442442
Data: path.Join(workdir, "writable_data"),
443443
Index: path.Join(workdir, "writable_index"),
444444
},
@@ -455,7 +455,7 @@ func Test_writeConfig(t *testing.T) {
455455
}
456456
defer file.Close()
457457

458-
configRes := snapshot.OverlayBDBSConfig{}
458+
configRes := sn.OverlayBDBSConfig{}
459459

460460
err = json.NewDecoder(file).Decode(&configRes)
461461
if err != nil {

cmd/convertor/builder/overlaybd_builder.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"path"
2828

2929
"github.com/containerd/accelerated-container-image/pkg/label"
30-
"github.com/containerd/accelerated-container-image/pkg/snapshot"
30+
sn "github.com/containerd/accelerated-container-image/pkg/types"
3131
"github.com/containerd/accelerated-container-image/pkg/utils"
3232
"github.com/containerd/accelerated-container-image/pkg/version"
3333
"github.com/containerd/containerd/errdefs"
@@ -51,17 +51,17 @@ type overlaybdConvertResult struct {
5151

5252
type overlaybdBuilderEngine struct {
5353
*builderEngineBase
54-
overlaybdConfig *snapshot.OverlayBDBSConfig
54+
overlaybdConfig *sn.OverlayBDBSConfig
5555
overlaybdLayers []overlaybdConvertResult
5656
}
5757

5858
func NewOverlayBDBuilderEngine(base *builderEngineBase) builderEngine {
59-
config := &snapshot.OverlayBDBSConfig{
60-
Lowers: []snapshot.OverlayBDBSConfigLower{},
59+
config := &sn.OverlayBDBSConfig{
60+
Lowers: []sn.OverlayBDBSConfigLower{},
6161
ResultFile: "",
6262
}
6363
if !base.mkfs {
64-
config.Lowers = append(config.Lowers, snapshot.OverlayBDBSConfigLower{
64+
config.Lowers = append(config.Lowers, sn.OverlayBDBSConfigLower{
6565
File: overlaybdBaseLayer,
6666
})
6767
logrus.Infof("using default baselayer")
@@ -112,7 +112,7 @@ func (e *overlaybdBuilderEngine) BuildLayer(ctx context.Context, idx int) error
112112
if err := e.create(ctx, layerDir, mkfs, vsizeGB); err != nil {
113113
return err
114114
}
115-
e.overlaybdConfig.Upper = snapshot.OverlayBDBSConfigUpper{
115+
e.overlaybdConfig.Upper = sn.OverlayBDBSConfigUpper{
116116
Data: path.Join(layerDir, "writable_data"),
117117
Index: path.Join(layerDir, "writable_index"),
118118
}
@@ -131,7 +131,7 @@ func (e *overlaybdBuilderEngine) BuildLayer(ctx context.Context, idx int) error
131131
os.Remove(path.Join(layerDir, "writable_index"))
132132
}
133133
}
134-
e.overlaybdConfig.Lowers = append(e.overlaybdConfig.Lowers, snapshot.OverlayBDBSConfigLower{
134+
e.overlaybdConfig.Lowers = append(e.overlaybdConfig.Lowers, sn.OverlayBDBSConfigLower{
135135
File: path.Join(layerDir, commitFile),
136136
})
137137
return nil

cmd/convertor/builder/turboOCI_builder.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ import (
2323
"path"
2424

2525
"github.com/containerd/accelerated-container-image/pkg/label"
26-
"github.com/containerd/accelerated-container-image/pkg/snapshot"
26+
sn "github.com/containerd/accelerated-container-image/pkg/types"
2727
"github.com/containerd/accelerated-container-image/pkg/utils"
2828
"github.com/containerd/accelerated-container-image/pkg/version"
2929
"github.com/containerd/containerd/archive/compression"
3030
"github.com/containerd/containerd/errdefs"
3131
"github.com/containerd/containerd/images"
3232
"github.com/opencontainers/go-digest"
3333
specs "github.com/opencontainers/image-spec/specs-go/v1"
34+
3435
"github.com/pkg/errors"
3536
"github.com/sirupsen/logrus"
3637
)
@@ -51,18 +52,18 @@ const (
5152

5253
type turboOCIBuilderEngine struct {
5354
*builderEngineBase
54-
overlaybdConfig *snapshot.OverlayBDBSConfig
55+
overlaybdConfig *sn.OverlayBDBSConfig
5556
tociLayers []specs.Descriptor
5657
isGzip []bool
5758
}
5859

5960
func NewTurboOCIBuilderEngine(base *builderEngineBase) builderEngine {
60-
config := &snapshot.OverlayBDBSConfig{
61-
Lowers: []snapshot.OverlayBDBSConfigLower{},
61+
config := &sn.OverlayBDBSConfig{
62+
Lowers: []sn.OverlayBDBSConfigLower{},
6263
ResultFile: "",
6364
}
6465
if !base.mkfs {
65-
config.Lowers = append(config.Lowers, snapshot.OverlayBDBSConfigLower{
66+
config.Lowers = append(config.Lowers, sn.OverlayBDBSConfigLower{
6667
File: overlaybdBaseLayer,
6768
})
6869
logrus.Infof("using default baselayer")
@@ -91,7 +92,7 @@ func (e *turboOCIBuilderEngine) BuildLayer(ctx context.Context, idx int) error {
9192
if err := e.create(ctx, layerDir, e.mkfs && (idx == 0)); err != nil {
9293
return err
9394
}
94-
e.overlaybdConfig.Upper = snapshot.OverlayBDBSConfigUpper{
95+
e.overlaybdConfig.Upper = sn.OverlayBDBSConfigUpper{
9596
Data: path.Join(layerDir, "writable_data"),
9697
Index: path.Join(layerDir, "writable_index"),
9798
Target: path.Join(layerDir, "layer.tar"),
@@ -120,7 +121,7 @@ func (e *turboOCIBuilderEngine) BuildLayer(ctx context.Context, idx int) error {
120121
if err := buildArchiveFromFiles(ctx, path.Join(layerDir, tociLayerTar), compression.Gzip, files...); err != nil {
121122
return errors.Wrapf(err, "failed to create turboOCIv1 archive for layer %d", idx)
122123
}
123-
e.overlaybdConfig.Lowers = append(e.overlaybdConfig.Lowers, snapshot.OverlayBDBSConfigLower{
124+
e.overlaybdConfig.Lowers = append(e.overlaybdConfig.Lowers, sn.OverlayBDBSConfigLower{
124125
TargetFile: path.Join(layerDir, "layer.tar"),
125126
TargetDigest: string(e.manifest.Layers[idx].Digest), // TargetDigest should be set to work with gzip cache
126127
File: path.Join(layerDir, fsMetaFile),

pkg/label/label.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ const (
9292

9393
// OverlayBDVersion is the version number of overlaybd blob
9494
OverlayBDVersion = "containerd.io/snapshot/overlaybd/version"
95+
96+
// LayerToTurboOCI is used to convert local layer to turboOCI with tar index
97+
LayerToTurboOCI = "containerd.io/snapshot/overlaybd/convert2turbo-oci"
9598
)
9699

97100
// used in filterAnnotationsForSave (https://github.com/moby/buildkit/blob/v0.11/cache/refs.go#L882)

pkg/snapshot/overlay.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@ const (
5959
// storageTypeRemoteBlock means that there is no unpacked layer data.
6060
// But there are labels to mark data that will be pulling on demand.
6161
storageTypeRemoteBlock
62+
63+
// storageTypeLocalLayer means that the unpacked layer data is in
64+
// a tar file, which needs to generate overlaybd-turboOCI meta before
65+
// create an overlaybd device
66+
storageTypeLocalLayer
6267
)
6368

6469
const (
6570
RoDir = "overlayfs" // overlayfs as rootfs. upper + lower (overlaybd)
6671
RwDir = "dir" // mount overlaybd as rootfs
6772
RwDev = "dev" // use overlaybd directly
73+
74+
LayerBlob = "layer" // decompressed tgz layer (maybe compressed by ZFile)
6875
)
6976

7077
type Registry struct {
@@ -574,8 +581,20 @@ func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind,
574581
var m []mount.Mount
575582
switch stype {
576583
case storageTypeNormal:
577-
m = o.normalOverlayMount(s)
578-
log.G(ctx).Debugf("return mount point(R/W mode: %s): %v", writeType, m)
584+
if _, ok := info.Labels[label.LayerToTurboOCI]; ok {
585+
m = []mount.Mount{
586+
{
587+
Source: o.upperPath(s.ID),
588+
Type: "bind",
589+
Options: []string{
590+
"rw",
591+
"rbind",
592+
},
593+
},
594+
}
595+
} else {
596+
m = o.normalOverlayMount(s)
597+
}
579598
case storageTypeLocalBlock, storageTypeRemoteBlock:
580599
m, err = o.basedOnBlockDeviceMount(ctx, s, writeType)
581600
if err != nil {
@@ -584,6 +603,7 @@ func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind,
584603
default:
585604
panic("unreachable")
586605
}
606+
log.G(ctx).Debugf("return mount point(R/W mode: %s): %v", writeType, m)
587607
return m, nil
588608

589609
}
@@ -595,8 +615,15 @@ func (o *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...s
595615
defer func() {
596616
if retErr != nil {
597617
metrics.GRPCErrCount.WithLabelValues("Prepare").Inc()
618+
} else {
619+
log.G(ctx).WithFields(logrus.Fields{
620+
"d": time.Since(start),
621+
"key": key,
622+
"parent": parent,
623+
}).Infof("Prepare")
598624
}
599625
metrics.GRPCLatency.WithLabelValues("Prepare").Observe(time.Since(start).Seconds())
626+
600627
}()
601628
return o.createMountPoint(ctx, snapshots.KindActive, key, parent, opts...)
602629
}
@@ -626,6 +653,11 @@ func (o *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount,
626653
defer func() {
627654
if retErr != nil {
628655
metrics.GRPCErrCount.WithLabelValues("Mounts").Inc()
656+
} else {
657+
log.G(ctx).WithFields(logrus.Fields{
658+
"d": time.Since(start),
659+
"key": key,
660+
}).Infof("Mounts")
629661
}
630662
metrics.GRPCLatency.WithLabelValues("Mounts").Observe(time.Since(start).Seconds())
631663
}()
@@ -691,6 +723,12 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
691723
defer func() {
692724
if retErr != nil {
693725
metrics.GRPCErrCount.WithLabelValues("Commit").Inc()
726+
} else {
727+
log.G(ctx).WithFields(logrus.Fields{
728+
"d": time.Since(start),
729+
"name": name,
730+
"key": key,
731+
}).Infof("Commit")
694732
}
695733
metrics.GRPCLatency.WithLabelValues("Commit").Observe(time.Since(start).Seconds())
696734
}()
@@ -746,6 +784,15 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
746784
}
747785

748786
log.G(ctx).Debugf("Commit info (id: %s, info: %v, stype: %d)", id, info.Labels, stype)
787+
// Firstly, try to convert an OCIv1 tarball to a turboOCI layer.
788+
// then change stype to 'storageTypeLocalBlock' which can make it attach a overlaybd block
789+
if stype == storageTypeLocalLayer {
790+
log.G(ctx).Infof("convet a local blob to turboOCI layer (sn: %s)", id)
791+
if err := o.constructOverlayBDSpec(ctx, name, false); err != nil {
792+
return errors.Wrapf(err, "failed to construct overlaybd config")
793+
}
794+
stype = storageTypeLocalBlock
795+
}
749796

750797
if stype == storageTypeLocalBlock {
751798
if err := o.constructOverlayBDSpec(ctx, name, false); err != nil {
@@ -1158,6 +1205,13 @@ func (o *snapshotter) identifySnapshotStorageType(ctx context.Context, id string
11581205
if err == nil {
11591206
return st, nil
11601207
}
1208+
1209+
// check layer.tar if it should be converted to turboOCI
1210+
filePath = o.overlaybdOCILayerPath(id)
1211+
if _, err := os.Stat(filePath); err == nil {
1212+
log.G(ctx).Infof("uncompressed layer found in sn: %s", id)
1213+
return storageTypeLocalLayer, nil
1214+
}
11611215
if os.IsNotExist(err) {
11621216
// check config.v1.json
11631217
log.G(ctx).Debugf("failed to identify by writable_data(sn: %s), try to identify by config.v1.json", id)
@@ -1168,6 +1222,7 @@ func (o *snapshotter) identifySnapshotStorageType(ctx context.Context, id string
11681222
}
11691223
return storageTypeNormal, nil
11701224
}
1225+
11711226
log.G(ctx).Debugf("storageType(sn: %s): %d", id, st)
11721227

11731228
return st, err
@@ -1185,10 +1240,18 @@ func (o *snapshotter) workPath(id string) string {
11851240
return filepath.Join(o.root, "snapshots", id, "work")
11861241
}
11871242

1243+
func (o *snapshotter) convertTempdir(id string) string {
1244+
return filepath.Join(o.root, "snapshots", id, "temp")
1245+
}
1246+
11881247
func (o *snapshotter) blockPath(id string) string {
11891248
return filepath.Join(o.root, "snapshots", id, "block")
11901249
}
11911250

1251+
func (o *snapshotter) turboOCIFsMeta(id string) string {
1252+
return filepath.Join(o.root, "snapshots", id, "fs", "ext4.fs.meta")
1253+
}
1254+
11921255
func (o *snapshotter) magicFilePath(id string) string {
11931256
return filepath.Join(o.root, "snapshots", id, "fs", "overlaybd.commit")
11941257
}
@@ -1217,12 +1280,16 @@ func (o *snapshotter) overlaybdWritableDataPath(id string) string {
12171280
return filepath.Join(o.root, "snapshots", id, "block", "writable_data")
12181281
}
12191282

1220-
func (o *snapshotter) overlaybdBackstoreMarkFile(id string) string {
1221-
return filepath.Join(o.root, "snapshots", id, "block", "backstore_mark")
1283+
func (o *snapshotter) overlaybdOCILayerPath(id string) string {
1284+
return filepath.Join(o.root, "snapshots", id, "layer.tar")
12221285
}
12231286

1224-
func (o *snapshotter) turboOCIFsMeta(id string) string {
1225-
return filepath.Join(o.root, "snapshots", id, "fs", "ext4.fs.meta")
1287+
func (o *snapshotter) overlaybdOCILayerMeta(id string) string {
1288+
return filepath.Join(o.root, "snapshots", id, "layer.metadata")
1289+
}
1290+
1291+
func (o *snapshotter) overlaybdBackstoreMarkFile(id string) string {
1292+
return filepath.Join(o.root, "snapshots", id, "block", "backstore_mark")
12261293
}
12271294

12281295
func (o *snapshotter) turboOCIGzipIdx(id string) string {

0 commit comments

Comments
 (0)