Skip to content

Commit

Permalink
Merge pull request #17 from dfjxs/mounting
Browse files Browse the repository at this point in the history
Add support for other overlay type snapshotters
  • Loading branch information
roshanmaskey committed May 2, 2024
2 parents e7b4a8e + 66851d9 commit 97ec86e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
41 changes: 30 additions & 11 deletions explorers/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
15 changes: 14 additions & 1 deletion explorers/containerd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/binary"
"fmt"
"io/fs"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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 ""
Expand Down

0 comments on commit 97ec86e

Please sign in to comment.