Skip to content

Commit 47d4311

Browse files
authored
Merge pull request #560 from ChengyuZhu6/layer_block
Support sharing image on the host for layer block mode
2 parents 0397a1f + 4473328 commit 47d4311

File tree

5 files changed

+100
-47
lines changed

5 files changed

+100
-47
lines changed

pkg/filesystem/tarfs_adaptor.go

+7
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ func (fs *Filesystem) GetTarfsImageDiskFilePath(id string) (string, error) {
8282
}
8383
return fs.tarfsMgr.ImageDiskFilePath(id), nil
8484
}
85+
86+
func (fs *Filesystem) GetTarfsLayerDiskFilePath(id string) (string, error) {
87+
if fs.tarfsMgr == nil {
88+
return "", errors.New("tarfs mode is not enabled")
89+
}
90+
return fs.tarfsMgr.LayerDiskFilePath(id), nil
91+
}

pkg/tarfs/tarfs.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,17 @@ func (t *Manager) ExportBlockData(s storage.Snapshot, perLayer bool, labels map[
466466
updateFields := []string{}
467467

468468
wholeImage, exportDisk, withVerity := config.GetTarfsExportFlags()
469+
470+
log.L.Debugf("ExportBlockData wholeImage = %v, exportDisk = %v, withVerity = %v, perLayer = %v", wholeImage, exportDisk, withVerity, perLayer)
469471
// Nothing to do for this case, all needed datum are ready.
470472
if !exportDisk && !withVerity {
471473
return updateFields, nil
472474
} else if !wholeImage != perLayer {
475+
// Special handling for `layer_block` mode
476+
if exportDisk && !withVerity && !perLayer {
477+
labels[label.NydusLayerBlockInfo] = ""
478+
updateFields = append(updateFields, "labels."+label.NydusLayerBlockInfo)
479+
}
473480
return updateFields, nil
474481
}
475482

@@ -505,7 +512,7 @@ func (t *Manager) ExportBlockData(s storage.Snapshot, perLayer bool, labels map[
505512
diskFileName = t.ImageDiskFilePath(blobID)
506513
} else {
507514
metaFileName = t.layerMetaFilePath(storageLocater(snapshotID))
508-
diskFileName = t.layerDiskFilePath(blobID)
515+
diskFileName = t.LayerDiskFilePath(blobID)
509516
}
510517

511518
// Do not regenerate if the disk image already exists.
@@ -821,7 +828,7 @@ func (t *Manager) layerTarFilePath(blobID string) string {
821828
return filepath.Join(t.cacheDirPath, blobID)
822829
}
823830

824-
func (t *Manager) layerDiskFilePath(blobID string) string {
831+
func (t *Manager) LayerDiskFilePath(blobID string) string {
825832
return filepath.Join(t.cacheDirPath, blobID+"."+TarfsLayerDiskName)
826833
}
827834

snapshot/mount_option.go

+79-40
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import (
1717

1818
"github.com/containerd/containerd/log"
1919
"github.com/containerd/containerd/mount"
20+
"github.com/containerd/containerd/snapshots"
2021
"github.com/containerd/containerd/snapshots/storage"
2122
"github.com/containerd/nydus-snapshotter/config/daemonconfig"
2223
"github.com/containerd/nydus-snapshotter/pkg/label"
2324
"github.com/containerd/nydus-snapshotter/pkg/layout"
2425
"github.com/containerd/nydus-snapshotter/pkg/rafs"
26+
"github.com/containerd/nydus-snapshotter/pkg/snapshot"
2527
"github.com/pkg/errors"
2628
)
2729

@@ -101,7 +103,7 @@ func (o *snapshotter) remoteMountWithExtraOptions(ctx context.Context, s storage
101103
}, nil
102104
}
103105

104-
func (o *snapshotter) mountWithKataVolume(ctx context.Context, id string, overlayOptions []string) ([]mount.Mount, error) {
106+
func (o *snapshotter) mountWithKataVolume(ctx context.Context, id string, overlayOptions []string, key string) ([]mount.Mount, error) {
105107
hasVolume := false
106108
rafs := rafs.RafsGlobalCache.Get(id)
107109
if rafs == nil {
@@ -122,7 +124,7 @@ func (o *snapshotter) mountWithKataVolume(ctx context.Context, id string, overla
122124

123125
// Insert Kata volume for tarfs
124126
if blobID, ok := rafs.Annotations[label.NydusTarfsLayer]; ok {
125-
options, err := o.mountWithTarfsVolume(*rafs, blobID)
127+
options, err := o.mountWithTarfsVolume(ctx, *rafs, blobID, key)
126128
if err != nil {
127129
return []mount.Mount{}, errors.Wrapf(err, "create kata volume for tarfs")
128130
}
@@ -149,66 +151,103 @@ func (o *snapshotter) mountWithKataVolume(ctx context.Context, id string, overla
149151

150152
func (o *snapshotter) mountWithProxyVolume(rafs rafs.Rafs) ([]string, error) {
151153
options := []string{}
154+
source := rafs.Annotations[label.CRIImageRef]
152155
for k, v := range rafs.Annotations {
153156
options = append(options, fmt.Sprintf("%s=%s", k, v))
154157
}
155-
156-
volume := &KataVirtualVolume{
157-
VolumeType: KataVirtualVolumeImageGuestPullType,
158-
Source: "",
159-
FSType: "",
160-
Options: options,
161-
ImagePull: &ImagePullVolume{Metadata: rafs.Annotations},
162-
}
163-
if !volume.Validate() {
164-
return []string{}, errors.Errorf("got invalid kata volume, %v", volume)
165-
}
166-
167-
info, err := EncodeKataVirtualVolumeToBase64(*volume)
158+
opt, err := o.prepareKataVirtualVolume(label.NydusProxyMode, source, KataVirtualVolumeImageGuestPullType, "", options, rafs.Annotations)
168159
if err != nil {
169-
return []string{}, errors.Errorf("failed to encoding Kata Volume info %v", volume)
160+
return options, errors.Wrapf(err, "failed to prepare KataVirtualVolume")
170161
}
171-
opt := fmt.Sprintf("%s=%s", KataVirtualVolumeOptionName, info)
172-
173162
return []string{opt}, nil
174163
}
175164

176-
func (o *snapshotter) mountWithTarfsVolume(rafs rafs.Rafs, blobID string) ([]string, error) {
177-
var volume *KataVirtualVolume
178-
165+
func (o *snapshotter) mountWithTarfsVolume(ctx context.Context, rafs rafs.Rafs, blobID, key string) ([]string, error) {
166+
options := []string{}
179167
if info, ok := rafs.Annotations[label.NydusImageBlockInfo]; ok {
180168
path, err := o.fs.GetTarfsImageDiskFilePath(blobID)
181169
if err != nil {
182170
return []string{}, errors.Wrapf(err, "get tarfs image disk file path")
183171
}
184-
volume = &KataVirtualVolume{
185-
VolumeType: KataVirtualVolumeImageRawBlockType,
186-
Source: path,
187-
FSType: "erofs",
188-
Options: []string{"ro"},
172+
log.L.Debugf("mountWithTarfsVolume info %v", info)
173+
opt, err := o.prepareKataVirtualVolume(label.NydusImageBlockInfo, path, KataVirtualVolumeImageRawBlockType, "erofs", []string{"ro"}, rafs.Annotations)
174+
if err != nil {
175+
return options, errors.Wrapf(err, "failed to prepare KataVirtualVolume for image_raw_block")
189176
}
190-
if len(info) > 0 {
191-
dmverity, err := parseTarfsDmVerityInfo(info)
177+
178+
options = append(options, opt)
179+
log.L.Debugf("mountWithTarfsVolume type=%v, options %v", KataVirtualVolumeImageRawBlockType, options)
180+
return options, nil
181+
}
182+
183+
if _, ok := rafs.Annotations[label.NydusLayerBlockInfo]; ok {
184+
for {
185+
pID, pInfo, _, pErr := snapshot.GetSnapshotInfo(ctx, o.ms, key)
186+
log.G(ctx).Debugf("mountWithKataVolume pID= %v, pInfo = %v", pID, pInfo)
187+
188+
if pErr != nil {
189+
return options, errors.Wrapf(pErr, "failed to get snapshot info")
190+
}
191+
if pInfo.Kind == snapshots.KindActive {
192+
key = pInfo.Parent
193+
continue
194+
}
195+
196+
blobID = pInfo.Labels[label.NydusTarfsLayer]
197+
path, err := o.fs.GetTarfsLayerDiskFilePath(blobID)
192198
if err != nil {
193-
return []string{}, err
199+
return options, errors.Wrapf(err, "get tarfs image disk file path")
194200
}
195-
volume.DmVerity = &dmverity
201+
202+
opt, err := o.prepareKataVirtualVolume(label.NydusLayerBlockInfo, path, KataVirtualVolumeLayerRawBlockType, "erofs", []string{"ro"}, pInfo.Labels)
203+
if err != nil {
204+
return options, errors.Wrapf(err, "failed to prepare KataVirtualVolume for layer_raw_block")
205+
}
206+
207+
options = append(options, opt)
208+
209+
if pInfo.Parent == "" {
210+
break
211+
}
212+
key = pInfo.Parent
196213
}
214+
log.L.Debugf("mountWithTarfsVolume type=%v, options %v", KataVirtualVolumeLayerRawBlockType, options)
215+
return options, nil
197216
}
198217

199-
if volume != nil {
200-
if !volume.Validate() {
201-
return []string{}, errors.Errorf("got invalid kata volume, %v", volume)
202-
}
203-
info, err := EncodeKataVirtualVolumeToBase64(*volume)
204-
if err != nil {
205-
return []string{}, errors.Errorf("failed to encoding Kata Volume info %v", volume)
218+
// If Neither image_raw_block or layer_raw_block, return empty strings
219+
return options, nil
220+
}
221+
222+
func (o *snapshotter) prepareKataVirtualVolume(blockType, source, volumeType, fsType string, options []string, labels map[string]string) (string, error) {
223+
volume := &KataVirtualVolume{
224+
VolumeType: volumeType,
225+
Source: source,
226+
FSType: fsType,
227+
Options: options,
228+
}
229+
if blockType == label.NydusImageBlockInfo || blockType == label.NydusLayerBlockInfo {
230+
dmverityInfo := labels[blockType]
231+
if len(dmverityInfo) > 0 {
232+
dmverity, err := parseTarfsDmVerityInfo(dmverityInfo)
233+
if err != nil {
234+
return "", err
235+
}
236+
volume.DmVerity = &dmverity
206237
}
207-
opt := fmt.Sprintf("%s=%s", KataVirtualVolumeOptionName, info)
208-
return []string{opt}, nil
238+
} else if blockType == label.NydusProxyMode {
239+
volume.ImagePull = &ImagePullVolume{Metadata: labels}
209240
}
210241

211-
return []string{}, nil
242+
if !volume.Validate() {
243+
return "", errors.Errorf("got invalid kata volume, %v", volume)
244+
}
245+
info, err := EncodeKataVirtualVolumeToBase64(*volume)
246+
if err != nil {
247+
return "", errors.Errorf("failed to encoding Kata Volume info %v", volume)
248+
}
249+
opt := fmt.Sprintf("%s=%s", KataVirtualVolumeOptionName, info)
250+
return opt, nil
212251
}
213252

214253
func parseTarfsDmVerityInfo(info string) (DmVerityInfo, error) {

snapshot/process.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func chooseProcessor(ctx context.Context, logger *logrus.Entry,
5252
}
5353

5454
logger.Infof("Nydus remote snapshot %s is ready", id)
55-
mounts, err := sn.mountRemote(ctx, labels, s, id)
55+
mounts, err := sn.mountRemote(ctx, labels, s, id, key)
5656
return false, mounts, err
5757
}
5858
}

snapshot/snapshot.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func (o *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, er
433433
}
434434

435435
if needRemoteMounts {
436-
return o.mountRemote(ctx, info.Labels, *snap, metaSnapshotID)
436+
return o.mountRemote(ctx, info.Labels, *snap, metaSnapshotID, key)
437437
}
438438

439439
return o.mountNative(ctx, info.Labels, *snap)
@@ -531,7 +531,7 @@ func (o *snapshotter) View(ctx context.Context, key, parent string, opts ...snap
531531
}
532532

533533
if needRemoteMounts {
534-
return o.mountRemote(ctx, base.Labels, s, metaSnapshotID)
534+
return o.mountRemote(ctx, base.Labels, s, metaSnapshotID, key)
535535
}
536536
return o.mountNative(ctx, base.Labels, s)
537537
}
@@ -840,7 +840,7 @@ func overlayMount(options []string) []mount.Mount {
840840

841841
// `s` is the upmost snapshot and `id` refers to the nydus meta snapshot
842842
// `s` and `id` can represent a different layer, it's useful when View an image
843-
func (o *snapshotter) mountRemote(ctx context.Context, labels map[string]string, s storage.Snapshot, id string) ([]mount.Mount, error) {
843+
func (o *snapshotter) mountRemote(ctx context.Context, labels map[string]string, s storage.Snapshot, id, key string) ([]mount.Mount, error) {
844844
var overlayOptions []string
845845
if _, ok := labels[label.OverlayfsVolatileOpt]; ok {
846846
overlayOptions = append(overlayOptions, "volatile")
@@ -871,7 +871,7 @@ func (o *snapshotter) mountRemote(ctx context.Context, labels map[string]string,
871871
log.G(ctx).Infof("remote mount options %v", overlayOptions)
872872

873873
if o.enableKataVolume {
874-
return o.mountWithKataVolume(ctx, id, overlayOptions)
874+
return o.mountWithKataVolume(ctx, id, overlayOptions, key)
875875
}
876876
// Add `extraoption` if NydusOverlayFS is enable or daemonMode is `None`
877877
if o.enableNydusOverlayFS || config.GetDaemonMode() == config.DaemonModeNone {

0 commit comments

Comments
 (0)