Skip to content

Commit

Permalink
convertor: support setting virtual block device size
Browse files Browse the repository at this point in the history
Signed-off-by: yuchen.cc <[email protected]>
  • Loading branch information
yuchen0cc committed Jan 5, 2024
1 parent bf3aeab commit ffa02c3
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/convertor/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type BuilderOptions struct {
WorkDir string
OCI bool
Mkfs bool
Vsize int
DB database.ConversionDatabase
Engine BuilderEngineType
CertOption
Expand Down Expand Up @@ -101,6 +102,7 @@ func NewOverlayBDBuilder(ctx context.Context, opt BuilderOptions) (Builder, erro
engineBase.workDir = opt.WorkDir
engineBase.oci = opt.OCI
engineBase.mkfs = opt.Mkfs
engineBase.vsize = opt.Vsize
engineBase.db = opt.DB

refspec, err := reference.Parse(opt.Ref)
Expand Down
1 change: 1 addition & 0 deletions cmd/convertor/builder/builder_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type builderEngineBase struct {
workDir string
oci bool
mkfs bool
vsize int
db database.ConversionDatabase
host string
repository string
Expand Down
8 changes: 6 additions & 2 deletions cmd/convertor/builder/overlaybd_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func (e *overlaybdBuilderEngine) BuildLayer(ctx context.Context, idx int) error
mkfs := e.mkfs && (idx == 0)
vsizeGB := 0
if idx == 0 {
vsizeGB = 64
if mkfs {
vsizeGB = e.vsize
} else {
vsizeGB = 64 // in case that using default baselayer
}
}
if err := e.create(ctx, layerDir, mkfs, vsizeGB); err != nil {
return err
Expand Down Expand Up @@ -310,7 +314,7 @@ func (e *overlaybdBuilderEngine) create(ctx context.Context, dir string, mkfs bo
opts := []string{"-s", fmt.Sprintf("%d", vsizeGB)}
if mkfs {
opts = append(opts, "--mkfs")
logrus.Infof("mkfs for baselayer")
logrus.Infof("mkfs for baselayer, vsize: %d GB", vsizeGB)
}
return utils.Create(ctx, dir, opts...)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/convertor/builder/turboOCI_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,14 @@ func (e *turboOCIBuilderEngine) createIdentifier(idx int) error {
}

func (e *turboOCIBuilderEngine) create(ctx context.Context, dir string, mkfs bool) error {
opts := []string{"-s", "64", "--turboOCI"}
vsizeGB := 64
if mkfs {
vsizeGB = e.vsize
}
opts := []string{"-s", fmt.Sprintf("%d", vsizeGB), "--turboOCI"}
if mkfs {
opts = append(opts, "--mkfs")
logrus.Infof("mkfs for baselayer, vsize: %d GB", vsizeGB)
}
return utils.Create(ctx, dir, opts...)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/convertor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
oci bool
mkfs bool
verbose bool
vsize int
fastoci string
turboOCI string
overlaybd string
Expand Down Expand Up @@ -87,6 +88,7 @@ var (
WorkDir: dir,
OCI: oci,
Mkfs: mkfs,
Vsize: vsize,
CertOption: builder.CertOption{
CertDirs: certDirs,
RootCAs: rootCAs,
Expand Down Expand Up @@ -160,8 +162,8 @@ func init() {
rootCmd.Flags().StringVarP(&dir, "dir", "d", "tmp_conv", "directory used for temporary data")
rootCmd.Flags().BoolVarP(&oci, "oci", "", false, "export image with oci spec")
rootCmd.Flags().BoolVarP(&mkfs, "mkfs", "", true, "make ext4 fs in bottom layer")
rootCmd.Flags().IntVarP(&vsize, "vsize", "", 64, "virtual block device size (GB)")
rootCmd.Flags().StringVar(&fastoci, "fastoci", "", "build 'Overlaybd-Turbo OCIv1' format (old name of turboOCIv1. deprecated)")

rootCmd.Flags().StringVar(&turboOCI, "turboOCI", "", "build 'Overlaybd-Turbo OCIv1' format")
rootCmd.Flags().StringVar(&overlaybd, "overlaybd", "", "build overlaybd format")
rootCmd.Flags().StringVar(&dbstr, "db-str", "", "db str for overlaybd conversion")
Expand Down
10 changes: 9 additions & 1 deletion cmd/ctr/overlaybd_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ var convertCommand = cli.Command{
},
cli.IntFlag{
Name: "bs",
Usage: "The size of a compressed data block in KB. Must be a power of two between 4K~64K [4/8/16/32/64])",
Usage: "The size of a compressed data block in KB. Must be a power of two between 4K~64K [4/8/16/32/64]",
Value: 0,
},
cli.IntFlag{
Name: "vsize",
Usage: "virtual block device size (GB)",
Value: 64,
},
),
Action: func(context *cli.Context) error {
var (
Expand Down Expand Up @@ -98,6 +103,9 @@ var convertCommand = cli.Command{
obdOpts = append(obdOpts, obdconv.WithAlgorithm(algorithm))
blockSize := context.Int("bs")
obdOpts = append(obdOpts, obdconv.WithBlockSize(blockSize))
vsize := context.Int("vsize")
fmt.Printf("vsize: %d GB\n", vsize)
obdOpts = append(obdOpts, obdconv.WithVsize(vsize))

resolver, err := commands.GetResolver(ctx, context)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions docs/IMAGE_CONVERTOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,36 @@
We provide a ctr command tool to convert OCIv1 images into overlaybd format, which is stored in `bin` after `make` or downloading the release package.

# Basic Usage
```bash
# usage
$ bin/ctr obdconv --help

NAME:
ctr obdconv - convert image layer into overlaybd format type

USAGE:
ctr obdconv [command options] <src-image> <dst-image>

DESCRIPTION:
Export images to an OCI tar[.gz] into zfile format

OPTIONS:
--skip-verify, -k skip SSL certificate validation
--plain-http allow connections using plain HTTP
--user value, -u value user[:password] Registry user and password
--refresh value refresh token for authorization server
--hosts-dir value Custom hosts configuration directory
--tlscacert value path to TLS root CA
--tlscert value path to TLS client certificate
--tlskey value path to TLS client key
--http-dump dump all HTTP request/responses when interacting with container registry
--http-trace enable HTTP tracing for registry interactions
--fstype value filesystem type(required), used to mount block device, support specifying mount options and mkfs options, separate fs type and options by ';', separate mount options by ',', separate mkfs options by ' ' (default: "ext4")
--dbstr value data base config string used for layer deduplication
--algorithm value compress algorithm uses in zfile, [lz4|zstd]
--bs value The size of a compressed data block in KB. Must be a power of two between 4K~64K [4/8/16/32/64] (default: 0)
--vsize value virtual block device size (GB) (default: 64)
```
```bash
# pull the source image
sudo ctr i pull registry.hub.docker.com/library/redis:6.2.1
Expand Down
5 changes: 5 additions & 0 deletions docs/USERSPACE_CONVERTOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Flags:
-d, --dir string directory used for temporary data (default "tmp_conv")
--oci export image with oci spec
--mkfs make ext4 fs in bottom layer (default true)
--vsize int virtual block device size (GB) (default 64)
--fastoci string build 'Overlaybd-Turbo OCIv1' format (old name of turboOCIv1. deprecated)
--turboOCI string build 'Overlaybd-Turbo OCIv1' format
--overlaybd string build overlaybd format
Expand All @@ -52,11 +53,15 @@ Flags:
--root-ca stringArray root CA certificates
--client-cert stringArray client cert certificates, should form in ${cert-file}:${key-file}
--insecure don't verify the server's certificate chain and host name
--reserve reserve tmp data
--no-upload don't upload layer and manifest
--dump-manifest dump manifest
-h, --help help for convertor
# examples
$ bin/convertor -r docker.io/overlaybd/redis -u user:pass -i 6.2.6 -o 6.2.6_obd
$ bin/convertor -r docker.io/overlaybd/redis -u user:pass -i 6.2.6 --overlaybd 6.2.6_obd --fastoci 6.2.6_foci
$ bin/convertor -r docker.io/overlaybd/redis -u user:pass -i 6.2.6 -o 6.2.6_obd --vsize 256
```
Expand Down
15 changes: 13 additions & 2 deletions pkg/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,16 @@ type overlaybdConvertor struct {
host string
repo string
zfileCfg ZFileConfig
vsize int
}

func NewOverlaybdConvertor(ctx context.Context, cs content.Store, sn snapshots.Snapshotter, resolver remotes.Resolver, ref string, dbstr string, zfileCfg ZFileConfig) (ImageConvertor, error) {
func NewOverlaybdConvertor(ctx context.Context, cs content.Store, sn snapshots.Snapshotter, resolver remotes.Resolver, ref string, dbstr string, zfileCfg ZFileConfig, vsize int) (ImageConvertor, error) {
c := &overlaybdConvertor{
cs: cs,
sn: sn,
remote: false,
zfileCfg: zfileCfg,
vsize: vsize,
}
var err error
if dbstr != "" {
Expand Down Expand Up @@ -535,6 +537,7 @@ func (c *overlaybdConvertor) convertLayers(ctx context.Context, srcDescs []ocisp
snapshots.WithLabels(map[string]string{
label.SupportReadWriteMode: "dir",
label.OverlayBDBlobFsType: fsType,
label.OverlayBDVsize: fmt.Sprintf("%d", c.vsize),
}),
}
cfgStr, err := json.Marshal(c.zfileCfg)
Expand Down Expand Up @@ -682,6 +685,7 @@ type options struct {
imgRef string
algorithm string
blockSize int
vsize int
resolver remotes.Resolver
client *containerd.Client
}
Expand Down Expand Up @@ -723,6 +727,13 @@ func WithBlockSize(blockSize int) Option {
}
}

func WithVsize(vsize int) Option {
return func(o *options) error {
o.vsize = vsize
return nil
}
}

func WithResolver(resolver remotes.Resolver) Option {
return func(o *options) error {
o.resolver = resolver
Expand Down Expand Up @@ -762,7 +773,7 @@ func IndexConvertFunc(opts ...Option) converter.ConvertFunc {
Algorithm: copts.algorithm,
BlockSize: copts.blockSize,
}
c, err := NewOverlaybdConvertor(ctx, cs, sn, copts.resolver, imgRef, copts.dbstr, zfileCfg)
c, err := NewOverlaybdConvertor(ctx, cs, sn, copts.resolver, imgRef, copts.dbstr, zfileCfg, copts.vsize)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const (
// ZFileConfig is the config of ZFile
ZFileConfig = "containerd.io/snapshot/overlaybd/zfile-config"

// OverlayBD virtual block device size
OverlayBDVsize = "containerd.io/snapshot/overlaybd/vsize"

// CRIImageRef is the image-ref from cri
CRIImageRef = "containerd.io/snapshot/cri.image-ref"

Expand Down
11 changes: 9 additions & 2 deletions pkg/snapshot/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,18 @@ func (o *snapshotter) constructOverlayBDSpec(ctx context.Context, key string, wr
if !writable {
return errors.Errorf("unexpect storage %v of snapshot %v during construct overlaybd spec(writable=%v, parent=%s)", stype, key, writable, info.Parent)
}
log.G(ctx).Infof("prepare writable layer. (sn: %s)", id)
vsizeGB := 0
if info.Parent == "" {
vsizeGB = 64
if vsize, ok := info.Labels[label.OverlayBDVsize]; ok {
vsizeGB, err = strconv.Atoi(vsize)
if err != nil {
vsizeGB = 64
}
} else {
vsizeGB = 64
}
}
log.G(ctx).Infof("prepare writable layer. (sn: %s, vsize: %d GB)", id, vsizeGB)
if err := o.prepareWritableOverlaybd(ctx, id, vsizeGB); err != nil {
return err
}
Expand Down

0 comments on commit ffa02c3

Please sign in to comment.