Skip to content

Commit

Permalink
add verify command to check mounted mols
Browse files Browse the repository at this point in the history
uses the dm_status target to check each mounted device for corruption.
outputs status of each atom and returns nonzero if any are bad.

Signed-off-by: Michael McCracken <[email protected]>
  • Loading branch information
mikemccracken committed Sep 30, 2024
1 parent 820120d commit 154fa21
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/atomfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
app.Commands = []cli.Command{
mountCmd,
umountCmd,
verifyCmd,
}

app.Flags = []cli.Flag{
Expand Down
84 changes: 84 additions & 0 deletions cmd/atomfs/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"path/filepath"
"strings"

"github.com/urfave/cli"
"machinerun.io/atomfs/mount"
"machinerun.io/atomfs/squashfs"
)

var verifyCmd = cli.Command{
Name: "verify",
Usage: "check atomfs image for dm-verity errors",
ArgsUsage: "atomfs mountpoint",
Action: doVerify,
}

func verifyUsage(me string) error {
return fmt.Errorf("Usage: %s verify mountpoint", me)
}

func doVerify(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return verifyUsage(ctx.App.Name)
}

mountpoint := ctx.Args()[0]

var err error

if !filepath.IsAbs(mountpoint) {
mountpoint, err = filepath.Abs(mountpoint)
if err != nil {
return fmt.Errorf("Failed to find mountpoint: %w", err)
}
}

if !isMountpoint(mountpoint) {
return fmt.Errorf("%s is not a mountpoint", mountpoint)
}

// hidden by the final overlay mount, but visible in the mountinfo:
// $mountpoint/meta/mounts/* - the original squashfs mounts
mountsdir := filepath.Join(mountpoint, "meta", "mounts")

mounts, err := mount.ParseMounts("/proc/self/mountinfo")
if err != nil {
return err
}

// quick check that the top level mount is an overlayfs as expected:
for _, m := range mounts {
if m.Target == mountpoint && m.FSType != "overlay" {
return fmt.Errorf("%s is not an overlayfs, are you sure it is a mounted molecule? %+v", mountpoint, m)
}
}

allOK := true
for _, m := range mounts {

if m.FSType != "squashfs" {
continue
}

if !strings.HasPrefix(m.Target, mountsdir) {
continue
}

err = squashfs.ConfirmExistingVerityDeviceCurrentValidity(m.Source)
if err != nil {
fmt.Printf("%s: CORRUPTION FOUND\n", m.Source)
allOK = false
} else {
fmt.Printf("%s: OK\n", m.Source)
}
}

if allOK {
return nil
}
return fmt.Errorf("Found corrupt devices in molecule")
}

0 comments on commit 154fa21

Please sign in to comment.