From 66851d962fae7340a1c9937ff92b63128034226e Mon Sep 17 00:00:00 2001 From: Jason Solomon Date: Thu, 2 May 2024 15:05:18 +1000 Subject: [PATCH] Add support for other overlay type snapshotters --- explorers/containerd/containerd.go | 41 ++++++++++++++++++++++-------- explorers/containerd/snapshot.go | 15 ++++++++++- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/explorers/containerd/containerd.go b/explorers/containerd/containerd.go index 18edf1a..763c207 100644 --- a/explorers/containerd/containerd.go +++ b/explorers/containerd/containerd.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "fmt" + "io/fs" "os" "os/exec" "path/filepath" @@ -74,9 +75,21 @@ func NewExplorer(imageroot string, root string, manifest string, snapshot string // /var/lib/containerd/io.containerd.snapshotter.v1.overlayfs func (e *explorer) SnapshotRoot(snapshotter string) string { dirs, _ := filepath.Glob(filepath.Join(e.root, "*")) + snapshotRoot := "" for _, dir := range dirs { if strings.Contains(strings.ToLower(dir), strings.ToLower(snapshotter)) { - return dir + filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if strings.Contains(path, "metadata.db") { + snapshotRoot, _ = filepath.Split(path) + log.WithFields(log.Fields{ + "path": path, + "snapshotRoot": snapshotRoot, + }).Debug("snapshot root") + return fs.SkipAll + } + return nil + }) + return snapshotRoot } } return "unknown" @@ -469,7 +482,22 @@ func (e *explorer) MountContainer(ctx context.Context, containerid string, mount // snapshot store ssstore := NewSnaptshotStore(e.root, e.mdb, ssdb) var mountArgs []string - if container.Snapshotter == "overlayfs" { + hasWorkDir := false + snapshotRoot, _ := filepath.Split(e.snapshot) + matches, _ := filepath.Glob(filepath.Join(snapshotRoot, "snapshots/*/work")) + if len(matches) > 0 { + hasWorkDir = true + } + if container.Snapshotter == "native" { + upperdir, err := ssstore.NativePath(ctx, container) + log.WithFields(log.Fields{ + "upperdir": upperdir, + }).Debug("native directories") + if err != nil { + return fmt.Errorf("failed to get native path %v", err) + } + mountArgs = []string{"-t", "bind", upperdir, mountpoint, "-o", "rbind,ro"} + } else if hasWorkDir { lowerdir, upperdir, workdir, err := ssstore.OverlayPath(ctx, container) log.WithFields(log.Fields{ "lowerdir": lowerdir, @@ -488,15 +516,6 @@ func (e *explorer) MountContainer(ctx context.Context, containerid string, mount // a container mountopts := fmt.Sprintf("ro,lowerdir=%s:%s", upperdir, lowerdir) mountArgs = []string{"-t", "overlay", "overlay", "-o", mountopts, mountpoint} - } else if container.Snapshotter == "native" { - upperdir, err := ssstore.NativePath(ctx, container) - log.WithFields(log.Fields{ - "upperdir": upperdir, - }).Debug("native directories") - if err != nil { - return fmt.Errorf("failed to get native path %v", err) - } - mountArgs = []string{"-t", "bind", upperdir, mountpoint, "-o", "rbind,ro"} } else { log.Error("Unsupported snapshotter ", container.Snapshotter) } diff --git a/explorers/containerd/snapshot.go b/explorers/containerd/snapshot.go index e976de6..2e00169 100644 --- a/explorers/containerd/snapshot.go +++ b/explorers/containerd/snapshot.go @@ -20,6 +20,7 @@ import ( "context" "encoding/binary" "fmt" + "io/fs" "path/filepath" "strings" @@ -344,9 +345,21 @@ func getSnapshotID(tx *bolt.Tx, snapshotkey string) (uint64, error) { // /var/lib/containerd/io.containerd.snapshotter.v1.overlayfs func snapshotRootDir(root string, snapshotter string) string { dirs, _ := filepath.Glob(filepath.Join(root, "*")) + snapshotRoot := "" for _, dir := range dirs { if strings.Contains(strings.ToLower(dir), strings.ToLower(snapshotter)) { - return dir + filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if strings.Contains(path, "metadata.db") { + snapshotRoot, _ = filepath.Split(path) + log.WithFields(log.Fields{ + "path": path, + "snapshotRoot": snapshotRoot, + }).Debug("snapshot root") + return fs.SkipAll + } + return nil + }) + return snapshotRoot } } return ""