Skip to content

Commit

Permalink
snapshot: extend the ExtraOption struct
Browse files Browse the repository at this point in the history
Move ExtraOption to dedicated file and extend it for more usage cases.

Signed-off-by: Jiang Liu <[email protected]>
  • Loading branch information
jiangliu committed Aug 31, 2023
1 parent 7745703 commit 2f2f196
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 80 deletions.
99 changes: 99 additions & 0 deletions snapshot/mount_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2023. Nydus Developers. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package snapshot

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"

"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/nydus-snapshotter/config/daemonconfig"
"github.com/containerd/nydus-snapshotter/pkg/daemon"
"github.com/containerd/nydus-snapshotter/pkg/layout"
"github.com/pkg/errors"
)

type ExtraOption struct {
Source string `json:"source"`
Config string `json:"config"`
Snapshotdir string `json:"snapshotdir"`
Version string `json:"fs_version"`
}

func (o *snapshotter) remoteMountWithExtraOptions(ctx context.Context, s storage.Snapshot, id string, overlayOptions []string) ([]mount.Mount, error) {
source, err := o.fs.BootstrapFile(id)
if err != nil {
return nil, err
}

instance := daemon.RafsSet.Get(id)
daemon, err := o.fs.GetDaemonByID(instance.DaemonID)
if err != nil {
return nil, errors.Wrapf(err, "get daemon with ID %s", instance.DaemonID)
}

var c daemonconfig.DaemonConfig
if daemon.IsSharedDaemon() {
c, err = daemonconfig.NewDaemonConfig(daemon.States.FsDriver, daemon.ConfigFile(instance.SnapshotID))
if err != nil {
return nil, errors.Wrapf(err, "Failed to load instance configuration %s",
daemon.ConfigFile(instance.SnapshotID))
}
} else {
c = daemon.Config
}
configContent, err := c.DumpString()
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to marshal config")
}

// get version from bootstrap
f, err := os.Open(source)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: check bootstrap version: failed to open bootstrap")
}
defer f.Close()
header := make([]byte, 4096)
sz, err := f.Read(header)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: check bootstrap version: failed to read bootstrap")
}
version, err := layout.DetectFsVersion(header[0:sz])
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to detect filesystem version")
}

// when enable nydus-overlayfs, return unified mount slice for runc and kata
extraOption := &ExtraOption{
Source: source,
Config: configContent,
Snapshotdir: o.snapshotDir(s.ID),
Version: version,
}
no, err := json.Marshal(extraOption)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to marshal NydusOption")
}
// XXX: Log options without extraoptions as it might contain secrets.
log.G(ctx).Debugf("fuse.nydus-overlayfs mount options %v", overlayOptions)
// base64 to filter easily in `nydus-overlayfs`
opt := fmt.Sprintf("extraoption=%s", base64.StdEncoding.EncodeToString(no))
overlayOptions = append(overlayOptions, opt)

return []mount.Mount{
{
Type: "fuse.nydus-overlayfs",
Source: "overlay",
Options: overlayOptions,
},
}, nil
}
80 changes: 0 additions & 80 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ package snapshot

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -31,9 +29,7 @@ import (
"github.com/containerd/nydus-snapshotter/pkg/cache"
"github.com/containerd/nydus-snapshotter/pkg/cgroup"
v2 "github.com/containerd/nydus-snapshotter/pkg/cgroup/v2"
"github.com/containerd/nydus-snapshotter/pkg/daemon"
"github.com/containerd/nydus-snapshotter/pkg/errdefs"
"github.com/containerd/nydus-snapshotter/pkg/layout"
mgr "github.com/containerd/nydus-snapshotter/pkg/manager"
"github.com/containerd/nydus-snapshotter/pkg/metrics"
"github.com/containerd/nydus-snapshotter/pkg/metrics/collector"
Expand Down Expand Up @@ -858,82 +854,6 @@ func (o *snapshotter) remoteMounts(ctx context.Context, labels map[string]string
return overlayMount(overlayOptions), nil
}

type ExtraOption struct {
Source string `json:"source"`
Config string `json:"config"`
Snapshotdir string `json:"snapshotdir"`
Version string `json:"fs_version"`
}

func (o *snapshotter) remoteMountWithExtraOptions(ctx context.Context, s storage.Snapshot, id string, overlayOptions []string) ([]mount.Mount, error) {
source, err := o.fs.BootstrapFile(id)
if err != nil {
return nil, err
}

instance := daemon.RafsSet.Get(id)
daemon, err := o.fs.GetDaemonByID(instance.DaemonID)
if err != nil {
return nil, errors.Wrapf(err, "get daemon with ID %s", instance.DaemonID)
}

var c daemonconfig.DaemonConfig
if daemon.IsSharedDaemon() {
c, err = daemonconfig.NewDaemonConfig(daemon.States.FsDriver, daemon.ConfigFile(instance.SnapshotID))
if err != nil {
return nil, errors.Wrapf(err, "Failed to load instance configuration %s",
daemon.ConfigFile(instance.SnapshotID))
}
} else {
c = daemon.Config
}
configContent, err := c.DumpString()
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to marshal config")
}

// get version from bootstrap
f, err := os.Open(source)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: check bootstrap version: failed to open bootstrap")
}
defer f.Close()
header := make([]byte, 4096)
sz, err := f.Read(header)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: check bootstrap version: failed to read bootstrap")
}
version, err := layout.DetectFsVersion(header[0:sz])
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to detect filesystem version")
}

// when enable nydus-overlayfs, return unified mount slice for runc and kata
extraOption := &ExtraOption{
Source: source,
Config: configContent,
Snapshotdir: o.snapshotDir(s.ID),
Version: version,
}
no, err := json.Marshal(extraOption)
if err != nil {
return nil, errors.Wrapf(err, "remoteMounts: failed to marshal NydusOption")
}
// XXX: Log options without extraoptions as it might contain secrets.
log.G(ctx).Debugf("fuse.nydus-overlayfs mount options %v", overlayOptions)
// base64 to filter easily in `nydus-overlayfs`
opt := fmt.Sprintf("extraoption=%s", base64.StdEncoding.EncodeToString(no))
overlayOptions = append(overlayOptions, opt)

return []mount.Mount{
{
Type: "fuse.nydus-overlayfs",
Source: "overlay",
Options: overlayOptions,
},
}, nil
}

func (o *snapshotter) mounts(ctx context.Context, labels map[string]string, s storage.Snapshot) ([]mount.Mount, error) {
if len(s.ParentIDs) == 0 {
// if we only have one layer/no parents then just return a bind mount as overlay will not work
Expand Down

0 comments on commit 2f2f196

Please sign in to comment.