Skip to content

Commit b205f2c

Browse files
committed
commit after sealed & refactored command execution
Signed-off-by: Haoqi Miao <miaohaoqi.mhq@alibaba-inc.com>
1 parent 115532c commit b205f2c

File tree

8 files changed

+143
-139
lines changed

8 files changed

+143
-139
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ jobs:
148148
echo "[ init env ]"
149149
sudo apt update && sudo apt install -y containerd.io libnl-3-200 libnl-genl-3-200
150150
sudo modprobe -a target_core_user tcm_loop
151-
wget https://github.com/containerd/overlaybd/releases/download/v0.6.1/overlaybd-0.6.1-1.x86_64.deb &&
152-
sudo dpkg -i overlaybd-0.6.1-1.x86_64.deb
151+
wget https://github.com/containerd/overlaybd/releases/download/v0.6.4/overlaybd-0.6.4-0ubuntu1.22.04.x86_64.deb &&
152+
sudo dpkg -i overlaybd-0.6.4-0ubuntu1.22.04.x86_64.deb
153153
sudo systemctl enable /opt/overlaybd/overlaybd-tcmu.service
154154
sudo systemctl start overlaybd-tcmu
155155
sudo systemctl status overlaybd-tcmu

cmd/convertor/builder/fastoci_builder.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/containerd/accelerated-container-image/pkg/label"
2828
"github.com/containerd/accelerated-container-image/pkg/snapshot"
29+
"github.com/containerd/accelerated-container-image/pkg/utils"
2930
"github.com/containerd/containerd/archive/compression"
3031
"github.com/containerd/containerd/errdefs"
3132
"github.com/containerd/containerd/images"
@@ -225,17 +226,7 @@ func (e *fastOCIBuilderEngine) createIdentifier(idx int) error {
225226
}
226227

227228
func (e *fastOCIBuilderEngine) create(ctx context.Context, dir string) error {
228-
binpath := filepath.Join("/opt/overlaybd/bin", "overlaybd-create")
229-
dataPath := path.Join(dir, "writable_data")
230-
indexPath := path.Join(dir, "writable_index")
231-
os.RemoveAll(dataPath)
232-
os.RemoveAll(indexPath)
233-
out, err := exec.CommandContext(ctx, binpath, "-s",
234-
dataPath, indexPath, "64", "--fastoci").CombinedOutput()
235-
if err != nil {
236-
return errors.Wrapf(err, "failed to overlaybd-create: %s", out)
237-
}
238-
return nil
229+
return utils.Create(ctx, dir, "-s", "64", "--fastoci")
239230
}
240231

241232
func (e *fastOCIBuilderEngine) apply(ctx context.Context, dir string) error {
@@ -253,16 +244,5 @@ func (e *fastOCIBuilderEngine) apply(ctx context.Context, dir string) error {
253244
}
254245

255246
func (e *fastOCIBuilderEngine) commit(ctx context.Context, dir string) error {
256-
binpath := filepath.Join("/opt/overlaybd/bin", "overlaybd-commit")
257-
258-
out, err := exec.CommandContext(ctx, binpath, "-z",
259-
path.Join(dir, "writable_data"),
260-
path.Join(dir, "writable_index"),
261-
path.Join(dir, fsMetaFile),
262-
"--fastoci",
263-
).CombinedOutput()
264-
if err != nil {
265-
return errors.Wrapf(err, "failed to overlaybd-commit: %s", out)
266-
}
267-
return nil
247+
return utils.Commit(ctx, dir, dir, false, "-z", "--fastoci")
268248
}

cmd/convertor/builder/overlaybd_builder.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/containerd/accelerated-container-image/pkg/label"
3131
"github.com/containerd/accelerated-container-image/pkg/snapshot"
32+
"github.com/containerd/accelerated-container-image/pkg/utils"
3233
"github.com/containerd/containerd/errdefs"
3334
"github.com/opencontainers/go-digest"
3435
"github.com/opencontainers/image-spec/identity"
@@ -287,17 +288,7 @@ func (e *overlaybdBuilderEngine) getLayerDir(idx int) string {
287288
}
288289

289290
func (e *overlaybdBuilderEngine) create(ctx context.Context, dir string) error {
290-
binpath := filepath.Join("/opt/overlaybd/bin", "overlaybd-create")
291-
dataPath := path.Join(dir, "writable_data")
292-
indexPath := path.Join(dir, "writable_index")
293-
os.RemoveAll(dataPath)
294-
os.RemoveAll(indexPath)
295-
out, err := exec.CommandContext(ctx, binpath, "-s",
296-
dataPath, indexPath, "64").CombinedOutput()
297-
if err != nil {
298-
return errors.Wrapf(err, "failed to overlaybd-create: %s", out)
299-
}
300-
return nil
291+
return utils.Create(ctx, dir, "-s", "64")
301292
}
302293

303294
func (e *overlaybdBuilderEngine) apply(ctx context.Context, dir string) error {
@@ -314,7 +305,6 @@ func (e *overlaybdBuilderEngine) apply(ctx context.Context, dir string) error {
314305
}
315306

316307
func (e *overlaybdBuilderEngine) commit(ctx context.Context, dir string, idx int) error {
317-
binpath := filepath.Join("/opt/overlaybd/bin", "overlaybd-commit")
318308
var parentUUID string
319309
if idx > 0 {
320310
parentUUID = chainIDtoUUID(e.overlaybdLayers[idx-1].chainID)
@@ -323,20 +313,8 @@ func (e *overlaybdBuilderEngine) commit(ctx context.Context, dir string, idx int
323313
}
324314
curUUID := chainIDtoUUID(e.overlaybdLayers[idx].chainID)
325315

326-
opts := []string{
327-
"-z", "-t",
328-
path.Join(dir, "writable_data"),
329-
path.Join(dir, "writable_index"),
330-
path.Join(dir, commitFile),
331-
"--uuid", curUUID,
332-
}
333-
if parentUUID != "" {
334-
opts = append(opts, "--parent-uuid", parentUUID)
335-
}
336-
337-
out, err := exec.CommandContext(ctx, binpath, opts...).CombinedOutput()
338-
if err != nil {
339-
return errors.Wrapf(err, "failed to overlaybd-commit: %s", out)
316+
if err := utils.Commit(ctx, dir, dir, false, "-z", "-t", "--uuid", curUUID); err != nil {
317+
return err
340318
}
341319
logrus.Infof("layer %d committed, uuid: %s, parent uuid: %s", idx, curUUID, parentUUID)
342320
return nil

docs/QUICKSTART.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Users can compile the latest code to install or download the [release](https://g
3737
#### Compile from source
3838

3939
Install dependencies:
40-
- golang 1.16+
40+
- golang 1.19+
4141

4242
Run the following commands to build:
4343
```bash

pkg/convertor/convertor.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package convertor
1818

1919
import (
20-
"archive/tar"
2120
"bufio"
2221
"bytes"
2322
"context"
@@ -28,10 +27,13 @@ import (
2827
"fmt"
2928
"io"
3029
"os"
30+
"path/filepath"
3131
"strings"
3232
"time"
3333

3434
"github.com/containerd/accelerated-container-image/pkg/label"
35+
"github.com/containerd/accelerated-container-image/pkg/utils"
36+
"github.com/sirupsen/logrus"
3537

3638
"github.com/containerd/containerd"
3739
"github.com/containerd/containerd/archive"
@@ -95,8 +97,8 @@ func NewContentLoaderWithFsType(isAccelLayer bool, fsType string, files ...Conte
9597
}
9698

9799
type ContentFile struct {
98-
SrcFilePath string
99-
DstFileName string
100+
SrcFilePath string // .../{ID}/fs/overlaybd.sealed
101+
DstFileName string // overlaybd.commit
100102
}
101103

102104
type contentLoader struct {
@@ -116,7 +118,6 @@ func (loader *contentLoader) Load(ctx context.Context, cs content.Store) (l Laye
116118
srcPathList := make([]string, 0)
117119
digester := digest.Canonical.Digester()
118120
countWriter := &writeCountWrapper{w: io.MultiWriter(contentWriter, digester.Hash())}
119-
tarWriter := tar.NewWriter(countWriter)
120121

121122
openedSrcFile := make([]*os.File, 0)
122123
defer func() {
@@ -126,36 +127,26 @@ func (loader *contentLoader) Load(ctx context.Context, cs content.Store) (l Laye
126127
}()
127128

128129
for _, loader := range loader.files {
129-
srcPathList = append(srcPathList, loader.SrcFilePath)
130-
srcFile, err := os.Open(loader.SrcFilePath)
131-
if err != nil {
132-
return emptyLayer, errors.Wrapf(err, "failed to open src file of %s", loader.SrcFilePath)
133-
}
134-
openedSrcFile = append(openedSrcFile, srcFile)
130+
commitPath := filepath.Dir(loader.SrcFilePath)
131+
commitFile := filepath.Join(commitPath, "overlaybd.commit")
132+
srcPathList = append(srcPathList, commitFile)
135133

136-
fi, err := os.Stat(loader.SrcFilePath)
134+
err := utils.Commit(ctx, commitPath, commitPath, true, "-z", "-t")
137135
if err != nil {
138-
return emptyLayer, errors.Wrapf(err, "failed to get info of %s", loader.SrcFilePath)
136+
return emptyLayer, errors.Wrapf(err, "failed to overlaybd-commit for sealed file")
139137
}
140138

141-
if err := tarWriter.WriteHeader(&tar.Header{
142-
Name: loader.DstFileName,
143-
Mode: 0444,
144-
Size: fi.Size(),
145-
Typeflag: tar.TypeReg,
146-
}); err != nil {
147-
return emptyLayer, errors.Wrapf(err, "failed to write tar header")
139+
srcFile, err := os.Open(commitFile)
140+
if err != nil {
141+
return emptyLayer, errors.Wrapf(err, "failed to open src file of %s", loader.SrcFilePath)
148142
}
149-
150-
if _, err := io.Copy(tarWriter, bufio.NewReader(srcFile)); err != nil {
151-
return emptyLayer, errors.Wrapf(err, "failed to copy IO")
143+
openedSrcFile = append(openedSrcFile, srcFile)
144+
_, err = io.Copy(countWriter, bufio.NewReader(srcFile))
145+
if err != nil {
146+
logrus.Errorf("failed to do io.Copy(), error: %v", err)
147+
return emptyLayer, err
152148
}
153149
}
154-
155-
if err = tarWriter.Close(); err != nil {
156-
return emptyLayer, errors.Wrapf(err, "failed to close tar file")
157-
}
158-
159150
labels := map[string]string{
160151
labelBuildLayerFrom: strings.Join(srcPathList, ","),
161152
}
@@ -516,7 +507,7 @@ func (c *overlaybdConvertor) convertLayers(ctx context.Context, srcDescs []ocisp
516507
opts = append(opts, snapshots.WithLabels(map[string]string{
517508
label.ZFileConfig: string(cfgStr),
518509
}))
519-
lastParentID, err = c.applyOCIV1LayerInZfile(ctx, lastParentID, desc, opts, nil)
510+
lastParentID, err = c.applyOCIV1LayerInObd(ctx, lastParentID, desc, opts, nil)
520511
if err != nil {
521512
return nil, err
522513
}
@@ -548,8 +539,8 @@ func (c *overlaybdConvertor) convertLayers(ctx context.Context, srcDescs []ocisp
548539
return commitLayers, nil
549540
}
550541

551-
// applyOCIV1LayerInZfile applys the OCIv1 tarfile in zfile format and commit it.
552-
func (c *overlaybdConvertor) applyOCIV1LayerInZfile(
542+
// applyOCIV1LayerInObd applys the OCIv1 tarfile in overlaybd format and commit it.
543+
func (c *overlaybdConvertor) applyOCIV1LayerInObd(
553544
ctx context.Context,
554545
parentID string, // the ID of parent snapshot
555546
desc ocispec.Descriptor, // the descriptor of layer

pkg/snapshot/overlay.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package snapshot
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"os"
2423
"path/filepath"
@@ -507,7 +506,7 @@ func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind,
507506
}
508507
fsType, ok := obdInfo.Labels[label.OverlayBDBlobFsType]
509508
if !ok {
510-
log.G(ctx).Warnf("cannot get fs type from label, %v", obdInfo.Labels)
509+
log.G(ctx).Warnf("cannot get fs type from label, %v, using ext4", obdInfo.Labels)
511510
fsType = "ext4"
512511
}
513512
log.G(ctx).Debugf("attachAndMountBlockDevice (obdID: %s, writeType: %s, fsType %s, targetPath: %s)",
@@ -686,28 +685,11 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
686685
return errors.Wrapf(err, "failed to destroy target device for snapshot %s", key)
687686
}
688687

689-
var zfileCfg ZFileConfig
690-
if cfgStr, ok := oinfo.Labels[label.ZFileConfig]; ok {
691-
if err := json.Unmarshal([]byte(cfgStr), &zfileCfg); err != nil {
692-
return err
693-
}
694-
}
695-
696-
if err := o.commitWritableOverlaybd(ctx, id, zfileCfg); err != nil {
688+
if err := o.sealWritableOverlaybd(ctx, id); err != nil {
697689
return err
698690
}
699691

700-
defer func() {
701-
if retErr != nil {
702-
return
703-
}
704-
705-
// clean up the temporary data
706-
os.Remove(o.overlaybdWritableDataPath(id))
707-
os.Remove(o.overlaybdWritableIndexPath(id))
708-
}()
709-
710-
opts = append(opts, snapshots.WithLabels(map[string]string{label.LocalOverlayBDPath: o.magicFilePath(id)}))
692+
opts = append(opts, snapshots.WithLabels(map[string]string{label.LocalOverlayBDPath: o.overlaybdSealedFilePath(id)}))
711693
}
712694
}
713695

@@ -732,7 +714,7 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
732714
info.Labels = make(map[string]string)
733715
}
734716

735-
info.Labels[label.LocalOverlayBDPath] = o.magicFilePath(id)
717+
info.Labels[label.LocalOverlayBDPath] = o.overlaybdSealedFilePath(id)
736718
info, err = storage.UpdateInfo(ctx, info, fmt.Sprintf("labels.%s", label.LocalOverlayBDPath))
737719
if err != nil {
738720
return err
@@ -1118,6 +1100,13 @@ func (o *snapshotter) identifySnapshotStorageType(ctx context.Context, id string
11181100
}
11191101
log.G(ctx).Debugf("failed to identify by %s, error %v, try to identify by writable_data", filePath, err)
11201102

1103+
// check sealed data file
1104+
filePath = o.overlaybdSealedFilePath(id)
1105+
st, err = o.identifyLocalStorageType(filePath)
1106+
if err == nil {
1107+
return st, nil
1108+
}
1109+
11211110
// check writable data file
11221111
filePath = o.overlaybdWritableDataPath(id)
11231112
st, err = o.identifyLocalStorageType(filePath)
@@ -1151,10 +1140,18 @@ func (o *snapshotter) workPath(id string) string {
11511140
return filepath.Join(o.root, "snapshots", id, "work")
11521141
}
11531142

1143+
func (o *snapshotter) blockPath(id string) string {
1144+
return filepath.Join(o.root, "snapshots", id, "block")
1145+
}
1146+
11541147
func (o *snapshotter) magicFilePath(id string) string {
11551148
return filepath.Join(o.root, "snapshots", id, "fs", "overlaybd.commit")
11561149
}
11571150

1151+
func (o *snapshotter) overlaybdSealedFilePath(id string) string {
1152+
return filepath.Join(o.root, "snapshots", id, "fs", "overlaybd.sealed")
1153+
}
1154+
11581155
func (o *snapshotter) overlaybdMountpoint(id string) string {
11591156
return filepath.Join(o.root, "snapshots", id, "block", "mountpoint")
11601157
}

pkg/snapshot/storage.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"time"
3333

3434
"github.com/containerd/accelerated-container-image/pkg/label"
35+
"github.com/containerd/accelerated-container-image/pkg/utils"
3536
"github.com/containerd/containerd/images"
3637
"github.com/containerd/containerd/log"
3738
"github.com/containerd/containerd/mount"
@@ -653,47 +654,16 @@ func (o *snapshotter) atomicWriteOverlaybdTargetConfig(snID string, configJSON *
653654

654655
// prepareWritableOverlaybd
655656
func (o *snapshotter) prepareWritableOverlaybd(ctx context.Context, snID string) error {
656-
657-
binpath := filepath.Join(o.config.OverlayBDUtilBinDir, "overlaybd-create")
658-
659657
// TODO(fuweid): 256GB can be configurable?
660-
args := []string{
661-
o.overlaybdWritableDataPath(snID),
662-
o.overlaybdWritableIndexPath(snID),
663-
"64",
664-
}
658+
args := []string{"64"}
665659
if o.writableLayerType == "sparse" {
666660
args = append(args, "-s")
667661
}
668-
out, err := exec.CommandContext(ctx, binpath, args...).CombinedOutput()
669-
if err != nil {
670-
err := errors.Wrapf(err, "failed to prepare writable overlaybd: %s", out)
671-
log.G(ctx).Errorln(err)
672-
return err
673-
}
674-
return nil
662+
return utils.Create(ctx, o.blockPath(snID), args...)
675663
}
676664

677-
// commitWritableOverlaybd
678-
func (o *snapshotter) commitWritableOverlaybd(ctx context.Context, snID string, cfg ZFileConfig) (retErr error) {
679-
binpath := filepath.Join(o.config.OverlayBDUtilBinDir, "overlaybd-commit")
680-
opts := []string{
681-
"-z",
682-
o.overlaybdWritableDataPath(snID),
683-
o.overlaybdWritableIndexPath(snID),
684-
o.magicFilePath(snID),
685-
}
686-
if cfg.BlockSize != 0 {
687-
opts = append(opts, "--bs", strconv.Itoa(cfg.BlockSize))
688-
}
689-
if cfg.Algorithm != "" {
690-
opts = append(opts, "--algorithm", cfg.Algorithm)
691-
}
692-
out, err := exec.CommandContext(ctx, binpath, opts...).CombinedOutput()
693-
if err != nil {
694-
return errors.Wrapf(err, "failed to commit writable overlaybd: %s", out)
695-
}
696-
return nil
665+
func (o *snapshotter) sealWritableOverlaybd(ctx context.Context, snID string) (retErr error) {
666+
return utils.Seal(ctx, o.blockPath(snID), o.upperPath(snID))
697667
}
698668

699669
func (o *snapshotter) overlaybdTargetPath(id string) string {

0 commit comments

Comments
 (0)