Skip to content

Commit

Permalink
change DimgHeader format
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki MATSUMOTO <[email protected]>
  • Loading branch information
naoki9911 committed Mar 29, 2024
1 parent f2d5368 commit 293f56f
Show file tree
Hide file tree
Showing 22 changed files with 575 additions and 176 deletions.
19 changes: 11 additions & 8 deletions cmd/ctr-cli/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containerd/containerd/log"
"github.com/naoki9911/fuse-diff-containerd/pkg/image"
sns "github.com/naoki9911/fuse-diff-containerd/pkg/snapshotter"
"github.com/naoki9911/fuse-diff-containerd/pkg/utils"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/sirupsen/logrus"
Expand All @@ -27,13 +28,13 @@ var Flags = []cli.Flag{
Required: true,
},
&cli.StringFlag{
Name: "dimg",
Usage: "path to dimg to be loaded",
Name: "cdimg",
Usage: "path to cdimg to be loaded",
Required: true,
},
}

func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersion string, imageHeader *image.CdimgHeader) error {
func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersion string, imageHeader *image.CdimgHeader, dimgPath string) error {
cs := snClient.CtrClient.ContentStore()
err := cs.Delete(ctx, imageHeader.Head.ManifestDigest)
if err != nil {
Expand Down Expand Up @@ -93,7 +94,7 @@ func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersio
}

// now ready to create snapshot
err = sns.CreateSnapshot(ctx, snClient.SnClient, &imageHeader.Head.ManifestDigest, &imageHeader.DimgDigest, imageName+":"+imageVersion)
err = sns.CreateSnapshot(ctx, snClient.SnClient, &imageHeader.Head.ManifestDigest, &imageHeader.DimgDigest, imageName+":"+imageVersion, dimgPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,8 +130,8 @@ func Load(ctx context.Context, imgNameWithVersion, imgPath string) error {
log.G(ctx).Info("loaded image")

// extract dimg
imagePath := filepath.Join(snClient.SnImageStorePath, image.Header.DimgDigest.String()+".dimg")
dimgFile, err := os.Create(imagePath)
dimgPath := filepath.Join(os.TempDir(), utils.GetRandomId("d4c-snapshotter")+".dimg")
dimgFile, err := os.Create(dimgPath)
if err != nil {
return err
}
Expand All @@ -139,8 +140,10 @@ func Load(ctx context.Context, imgNameWithVersion, imgPath string) error {
if err != nil {
return err
}
// LaodImage use written dimg. so close here.
dimgFile.Close()

err = LoadImage(snClient, ctx, imgName, imgVersion, image.Header)
err = LoadImage(snClient, ctx, imgName, imgVersion, image.Header, dimgPath)
if err != nil {
return err
}
Expand All @@ -150,7 +153,7 @@ func Load(ctx context.Context, imgNameWithVersion, imgPath string) error {

func Action(c *cli.Context) error {
imgName := c.String("image")
imgPath := c.String("dimg")
imgPath := c.String("cdimg")
err := Load(context.TODO(), imgName, imgPath)
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion cmd/ctr-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/pack"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/patch"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/pull"
"github.com/naoki9911/fuse-diff-containerd/cmd/ctr-cli/show"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -38,7 +39,6 @@ func NewApp() *cli.App {
pack.Command(),
load.Command(),
pull.Command(),
merge.Command(),
}

return app
Expand All @@ -52,6 +52,8 @@ func dimgCommand() *cli.Command {
Subcommands: []*cli.Command{
patch.DimgCommand(),
diff.DimgCommand(),
merge.DimgCommand(),
show.DimgCommand(),
},
}
return &cmd
Expand All @@ -65,6 +67,8 @@ func cdimgCommand() *cli.Command {
Subcommands: []*cli.Command{
patch.CdimgCommand(),
diff.CdimgCommand(),
merge.CdimgCommand(),
show.CdimgCommand(),
},
}
return &cmd
Expand Down
140 changes: 112 additions & 28 deletions cmd/ctr-cli/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,42 @@ import (

var logger = log.G(context.TODO())

var (
Flags = []cli.Flag{
&cli.StringFlag{
Name: "lowerDimg",
Usage: "path to lower dimg",
Required: true,
},
&cli.StringFlag{
Name: "upperDimg",
Usage: "path to upper dimg",
Required: true,
},
&cli.StringFlag{
Name: "outDimg",
Usage: "path to merged dimg",
Required: true,
func DimgCommand() *cli.Command {
cmd := cli.Command{
Name: "merge",
Usage: "merge dimgs",
Action: func(context *cli.Context) error {
return dimgAction(context)
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lowerDimg",
Usage: "path to lower dimg",
Required: true,
},
&cli.StringFlag{
Name: "upperDimg",
Usage: "path to upper dimg",
Required: true,
},
&cli.StringFlag{
Name: "outDimg",
Usage: "path to merged dimg",
Required: true,
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
},
},
}
)

func Action(c *cli.Context) error {
return &cmd
}

func dimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
lowerDimg := c.String("lowerDimg")
upperDimg := c.String("upperDimg")
Expand Down Expand Up @@ -69,7 +78,7 @@ func Action(c *cli.Context) error {
panic(err)
}
defer mergeFile.Close()
err = image.MergeDimg(lowerDimg, upperDimg, mergeFile)
_, err = image.MergeDimg(lowerDimg, upperDimg, mergeFile)
if err != nil {
panic(err)
}
Expand All @@ -93,15 +102,90 @@ func Action(c *cli.Context) error {
return nil
}

func Command() *cli.Command {
func CdimgCommand() *cli.Command {
cmd := cli.Command{
Name: "merge",
Usage: "merge dimgs",
Usage: "merge cdimgs",
Action: func(context *cli.Context) error {
return Action(context)
return cdimgAction(context)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lowerCdimg",
Usage: "path to lower cdimg",
Required: true,
},
&cli.StringFlag{
Name: "upperCdimg",
Usage: "path to upper cdimg",
Required: true,
},
&cli.StringFlag{
Name: "outCdimg",
Usage: "path to merged cdimg",
Required: true,
},
&cli.BoolFlag{
Name: "benchmark",
Usage: "enable benchmark",
Value: false,
Required: false,
},
},
Flags: Flags,
}

return &cmd
}

func cdimgAction(c *cli.Context) error {
logger.Logger.SetLevel(logrus.WarnLevel)
lowerCdimg := c.String("lowerCdimg")
upperCdimg := c.String("upperCdimg")
outCdimg := c.String("outCdimg")
enableBench := c.Bool("benchmark")
logger.WithFields(logrus.Fields{
"lowerCdimg": lowerCdimg,
"upperCdimg": upperCdimg,
"outCdimg": outCdimg,
}).Info("starting to merge")

var b *benchmark.Benchmark = nil
var err error
if enableBench {
b, err = benchmark.NewBenchmark("./benchmark.log")
if err != nil {
return err
}
defer b.Close()
}

start := time.Now()

mergeFile, err := os.Create(outCdimg)
if err != nil {
panic(err)
}
defer mergeFile.Close()
err = image.MergeCdimg(lowerCdimg, upperCdimg, mergeFile)
if err != nil {
panic(err)
}

if b != nil {
metric := benchmark.Metric{
TaskName: "patch",
ElapsedMilli: int(time.Since(start).Milliseconds()),
Labels: []string{
"lowerCdimg:" + lowerCdimg,
"upperCdimg:" + upperCdimg,
"outCdimg:" + outCdimg,
},
}
err = b.AppendResult(metric)
if err != nil {
panic(err)
}
}
logger.Info("merge done")
return nil
}
4 changes: 2 additions & 2 deletions cmd/ctr-cli/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func dimgAction(c *cli.Context) error {
dimgHeader := dimgFile.Header()

start := time.Now()
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.BaseId == "")
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.ParentId == "")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func cdimgAction(c *cli.Context) error {
dimgHeader := dimgFile.Header()

start := time.Now()
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.BaseId == "")
err = image.ApplyPatch(baseDir, outDir, &dimgHeader.FileEntry, dimgFile, dimgHeader.ParentId == "")
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/ctr-cli/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/naoki9911/fuse-diff-containerd/pkg/image"
sns "github.com/naoki9911/fuse-diff-containerd/pkg/snapshotter"
"github.com/naoki9911/fuse-diff-containerd/pkg/update"
"github.com/naoki9911/fuse-diff-containerd/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -185,7 +186,7 @@ func pullImage(host string, imageNameWithVersion string, bench bool) error {
}
logger.WithField("dimgSize", header.Head.DimgSize).Debug("got image header")

dimgPath := filepath.Join(snClient.SnImageStorePath, header.DimgDigest.String()+".dimg")
dimgPath := filepath.Join(os.TempDir(), utils.GetRandomId("d4c-snapshotter")+".dimg")
dimgFile, err := os.Create(dimgPath)
if err != nil {
return fmt.Errorf("failed to create dimg at %s: %v", dimgPath, err)
Expand Down Expand Up @@ -215,7 +216,7 @@ func pullImage(host string, imageNameWithVersion string, bench bool) error {
}
}

err = load.LoadImage(snClient, context.TODO(), reqImgName, reqImgVersion, header)
err = load.LoadImage(snClient, context.TODO(), reqImgName, reqImgVersion, header, dimgPath)
if err != nil {
return fmt.Errorf("failed to load image: %v", err)
}
Expand Down
Loading

0 comments on commit 293f56f

Please sign in to comment.