summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-11 21:10:54 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-11 21:17:09 -0600
commit839dfa5d0aeadee9cb0f8581341922138f9595f0 (patch)
tree42130616613f57f0b9d40a7490a0f576aa94f35a
parent0f853e1ead10347be8c5715d6bb797dd5e643e2f (diff)
Move all of the small-ish tools to be part of btrfs-rec
-rw-r--r--.gitignore4
-rw-r--r--cmd/btrfs-clear-bad-nodes/main.go38
-rw-r--r--cmd/btrfs-dump-tree/main.go41
-rw-r--r--cmd/btrfs-ls-trees/main.go77
-rw-r--r--cmd/btrfs-mount/main.go54
-rw-r--r--cmd/btrfs-rec/inspect_dumptrees.go31
-rw-r--r--cmd/btrfs-rec/inspect_lsfiles.go (renamed from cmd/btrfs-ls-files/main.go)47
-rw-r--r--cmd/btrfs-rec/inspect_lstrees.go68
-rw-r--r--cmd/btrfs-rec/inspect_mount.go26
-rw-r--r--cmd/btrfs-rec/main.go2
-rw-r--r--cmd/btrfs-rec/repair_clearbadnodes.go28
-rwxr-xr-xscripts/diff-dump-tree.sh2
-rwxr-xr-xscripts/run-clear-nodes.sh2
13 files changed, 175 insertions, 245 deletions
diff --git a/.gitignore b/.gitignore
index d9cf8b3..5dff8c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,5 @@
*.html
bin/
vendor/
-btrfs-mount
-!/cmd/btrfs-mount/
+btrfs-rec
+!/cmd/btrfs-rec/
diff --git a/cmd/btrfs-clear-bad-nodes/main.go b/cmd/btrfs-clear-bad-nodes/main.go
deleted file mode 100644
index f44b1f7..0000000
--- a/cmd/btrfs-clear-bad-nodes/main.go
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package main
-
-import (
- "fmt"
- "os"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsrepair"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
-)
-
-func main() {
- if err := Main(os.Args[1:]...); err != nil {
- fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err)
- os.Exit(1)
- }
-}
-
-func Main(imgfilenames ...string) (err error) {
- maybeSetErr := func(_err error) {
- if _err != nil && err == nil {
- err = _err
- }
- }
-
- fs, err := btrfsutil.Open(os.O_RDWR, imgfilenames...)
- if err != nil {
- return err
- }
- defer func() {
- maybeSetErr(fs.Close())
- }()
-
- return btrfsrepair.ClearBadNodes(os.Stdout, os.Stderr, fs)
-}
diff --git a/cmd/btrfs-dump-tree/main.go b/cmd/btrfs-dump-tree/main.go
deleted file mode 100644
index 2770849..0000000
--- a/cmd/btrfs-dump-tree/main.go
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package main
-
-import (
- "fmt"
- "os"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
-)
-
-func main() {
- if err := Main(os.Args[1]); err != nil {
- fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err)
- os.Exit(1)
- }
-}
-
-const version = "5.18.1"
-
-func Main(imgfilename string) (err error) {
- maybeSetErr := func(_err error) {
- if _err != nil && err == nil {
- err = _err
- }
- }
-
- fs, err := btrfsutil.Open(os.O_RDONLY, imgfilename)
- if err != nil {
- return err
- }
- defer func() {
- maybeSetErr(fs.Close())
- }()
-
- fmt.Printf("btrfs-progs v%v\n", version)
- return btrfsinspect.DumpTrees(os.Stdout, os.Stderr, fs)
-}
diff --git a/cmd/btrfs-ls-trees/main.go b/cmd/btrfs-ls-trees/main.go
deleted file mode 100644
index a0ae460..0000000
--- a/cmd/btrfs-ls-trees/main.go
+++ /dev/null
@@ -1,77 +0,0 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package main
-
-import (
- "fmt"
- "os"
- "strconv"
- "text/tabwriter"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
- "git.lukeshu.com/btrfs-progs-ng/lib/util"
-)
-
-func main() {
- if err := Main(os.Args[1:]...); err != nil {
- fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err)
- os.Exit(1)
- }
-}
-
-func Main(imgfilenames ...string) (err error) {
- maybeSetErr := func(_err error) {
- if _err != nil && err == nil {
- err = _err
- }
- }
-
- fs, err := btrfsutil.Open(os.O_RDONLY, imgfilenames...)
- if err != nil {
- return err
- }
- defer func() {
- maybeSetErr(fs.Close())
- }()
-
- var treeErrCnt int
- var treeItemCnt map[btrfsitem.Type]int
- btrfsutil.WalkAllTrees(fs, btrfsutil.WalkAllTreesHandler{
- PreTree: func(name string, treeID btrfs.ObjID) {
- treeErrCnt = 0
- treeItemCnt = make(map[btrfsitem.Type]int)
- fmt.Printf("tree id=%v name=%q\n", treeID, name)
- },
- Err: func(_ error) {
- treeErrCnt++
- },
- TreeWalkHandler: btrfs.TreeWalkHandler{
- Item: func(_ btrfs.TreePath, item btrfs.Item) error {
- typ := item.Head.Key.ItemType
- treeItemCnt[typ] = treeItemCnt[typ] + 1
- return nil
- },
- },
- PostTree: func(_ string, _ btrfs.ObjID) {
- totalItems := 0
- for _, cnt := range treeItemCnt {
- totalItems += cnt
- }
- numWidth := len(strconv.Itoa(util.Max(treeErrCnt, totalItems)))
-
- table := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
- fmt.Fprintf(table, " errors\t% *s\n", numWidth, strconv.Itoa(treeErrCnt))
- for _, typ := range util.SortedMapKeys(treeItemCnt) {
- fmt.Fprintf(table, " %v items\t% *s\n", typ, numWidth, strconv.Itoa(treeItemCnt[typ]))
- }
- fmt.Fprintf(table, " total items\t% *s\n", numWidth, strconv.Itoa(totalItems))
- table.Flush()
- },
- })
-
- return nil
-}
diff --git a/cmd/btrfs-mount/main.go b/cmd/btrfs-mount/main.go
deleted file mode 100644
index 7debb3a..0000000
--- a/cmd/btrfs-mount/main.go
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package main
-
-import (
- "context"
- "fmt"
- "os"
-
- "github.com/datawire/dlib/dgroup"
- "github.com/datawire/dlib/dlog"
- "github.com/sirupsen/logrus"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
-)
-
-func main() {
- ctx := context.Background()
- logger := logrus.New()
- //logger.SetLevel(logrus.TraceLevel)
- ctx = dlog.WithLogger(ctx, dlog.WrapLogrus(logger))
-
- grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{
- EnableSignalHandling: true,
- })
- grp.Go("main", func(ctx context.Context) error {
- return Main(ctx, os.Args[1], os.Args[2:]...)
- })
- if err := grp.Wait(); err != nil {
- fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err)
- os.Exit(1)
- }
-}
-
-func Main(ctx context.Context, mountpoint string, imgfilenames ...string) (err error) {
- maybeSetErr := func(_err error) {
- if _err != nil && err == nil {
- err = _err
- }
- }
-
- fs, err := btrfsutil.Open(os.O_RDONLY, imgfilenames...)
- if err != nil {
- return err
- }
- defer func() {
- maybeSetErr(fs.Close())
- }()
-
- return btrfsinspect.MountRO(ctx, fs, mountpoint)
-}
diff --git a/cmd/btrfs-rec/inspect_dumptrees.go b/cmd/btrfs-rec/inspect_dumptrees.go
new file mode 100644
index 0000000..5ddff60
--- /dev/null
+++ b/cmd/btrfs-rec/inspect_dumptrees.go
@@ -0,0 +1,31 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/datawire/ocibuild/pkg/cliutil"
+ "github.com/spf13/cobra"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
+)
+
+func init() {
+ inspectors = append(inspectors, subcommand{
+ Command: cobra.Command{
+ Use: "dump-trees",
+ Short: "A clone of `btrfs inspect-internal dump-tree`",
+ Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
+ },
+ RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
+ const version = "5.18.1"
+ fmt.Printf("btrfs-progs v%v\n", version)
+ return btrfsinspect.DumpTrees(os.Stdout, os.Stderr, fs)
+ },
+ })
+}
diff --git a/cmd/btrfs-ls-files/main.go b/cmd/btrfs-rec/inspect_lsfiles.go
index 0f27def..2da31aa 100644
--- a/cmd/btrfs-ls-files/main.go
+++ b/cmd/btrfs-rec/inspect_lsfiles.go
@@ -6,47 +6,34 @@ package main
import (
"fmt"
- "os"
"reflect"
"strings"
"github.com/datawire/dlib/derror"
+ "github.com/datawire/ocibuild/pkg/cliutil"
+ "github.com/spf13/cobra"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
"git.lukeshu.com/btrfs-progs-ng/lib/util"
)
-func main() {
- if err := Main(os.Args[1:]...); err != nil {
- fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err)
- os.Exit(1)
- }
-}
-
-func Main(imgfilenames ...string) (err error) {
- maybeSetErr := func(_err error) {
- if _err != nil && err == nil {
- err = _err
- }
- }
-
- fs, err := btrfsutil.Open(os.O_RDONLY, imgfilenames...)
- if err != nil {
- return err
- }
- defer func() {
- maybeSetErr(fs.Close())
- }()
-
- printSubvol(fs, "", "", "/", btrfs.Key{
- ObjectID: btrfs.FS_TREE_OBJECTID,
- ItemType: btrfsitem.ROOT_ITEM_KEY,
- Offset: 0,
+func init() {
+ inspectors = append(inspectors, subcommand{
+ Command: cobra.Command{
+ Use: "ls-files",
+ Short: "A listing of all files in the filesystem",
+ Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
+ },
+ RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
+ printSubvol(fs, "", "", "/", btrfs.Key{
+ ObjectID: btrfs.FS_TREE_OBJECTID,
+ ItemType: btrfsitem.ROOT_ITEM_KEY,
+ Offset: 0,
+ })
+ return nil
+ },
})
-
- return nil
}
const (
diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go
new file mode 100644
index 0000000..976514f
--- /dev/null
+++ b/cmd/btrfs-rec/inspect_lstrees.go
@@ -0,0 +1,68 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+ "text/tabwriter"
+
+ "github.com/datawire/ocibuild/pkg/cliutil"
+ "github.com/spf13/cobra"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
+ "git.lukeshu.com/btrfs-progs-ng/lib/util"
+)
+
+func init() {
+ inspectors = append(inspectors, subcommand{
+ Command: cobra.Command{
+ Use: "ls-trees",
+ Short: "A brief view what types of items are in each tree",
+ Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
+ },
+ RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
+ var treeErrCnt int
+ var treeItemCnt map[btrfsitem.Type]int
+ btrfsutil.WalkAllTrees(fs, btrfsutil.WalkAllTreesHandler{
+ PreTree: func(name string, treeID btrfs.ObjID) {
+ treeErrCnt = 0
+ treeItemCnt = make(map[btrfsitem.Type]int)
+ fmt.Printf("tree id=%v name=%q\n", treeID, name)
+ },
+ Err: func(_ error) {
+ treeErrCnt++
+ },
+ TreeWalkHandler: btrfs.TreeWalkHandler{
+ Item: func(_ btrfs.TreePath, item btrfs.Item) error {
+ typ := item.Head.Key.ItemType
+ treeItemCnt[typ] = treeItemCnt[typ] + 1
+ return nil
+ },
+ },
+ PostTree: func(_ string, _ btrfs.ObjID) {
+ totalItems := 0
+ for _, cnt := range treeItemCnt {
+ totalItems += cnt
+ }
+ numWidth := len(strconv.Itoa(util.Max(treeErrCnt, totalItems)))
+
+ table := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
+ fmt.Fprintf(table, " errors\t% *s\n", numWidth, strconv.Itoa(treeErrCnt))
+ for _, typ := range util.SortedMapKeys(treeItemCnt) {
+ fmt.Fprintf(table, " %v items\t% *s\n", typ, numWidth, strconv.Itoa(treeItemCnt[typ]))
+ }
+ fmt.Fprintf(table, " total items\t% *s\n", numWidth, strconv.Itoa(totalItems))
+ table.Flush()
+ },
+ })
+
+ return nil
+ },
+ })
+}
diff --git a/cmd/btrfs-rec/inspect_mount.go b/cmd/btrfs-rec/inspect_mount.go
new file mode 100644
index 0000000..5aae54a
--- /dev/null
+++ b/cmd/btrfs-rec/inspect_mount.go
@@ -0,0 +1,26 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package main
+
+import (
+ "github.com/datawire/ocibuild/pkg/cliutil"
+ "github.com/spf13/cobra"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
+)
+
+func init() {
+ inspectors = append(inspectors, subcommand{
+ Command: cobra.Command{
+ Use: "mount MOUNTPOINT",
+ Short: "Mount the filesystem read-only",
+ Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)),
+ },
+ RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
+ return btrfsinspect.MountRO(cmd.Context(), fs, args[0])
+ },
+ })
+}
diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go
index 9b285b1..b68bdd8 100644
--- a/cmd/btrfs-rec/main.go
+++ b/cmd/btrfs-rec/main.go
@@ -74,7 +74,7 @@ func main() {
var openFlag int = os.O_RDONLY
argparserInspect := &cobra.Command{
- Use: "inpsect {[flags]|SUBCOMMAND}",
+ Use: "inspect {[flags]|SUBCOMMAND}",
Short: "Inspect (but don't modify) a broken btrfs filesystem",
Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands),
diff --git a/cmd/btrfs-rec/repair_clearbadnodes.go b/cmd/btrfs-rec/repair_clearbadnodes.go
new file mode 100644
index 0000000..58b8bd6
--- /dev/null
+++ b/cmd/btrfs-rec/repair_clearbadnodes.go
@@ -0,0 +1,28 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package main
+
+import (
+ "os"
+
+ "github.com/datawire/ocibuild/pkg/cliutil"
+ "github.com/spf13/cobra"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsrepair"
+)
+
+func init() {
+ repairers = append(repairers, subcommand{
+ Command: cobra.Command{
+ Use: "clear-bad-nodes",
+ Short: "Overwrite corrupt nodes with empty nodes",
+ Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
+ },
+ RunE: func(fs *btrfs.FS, _ *cobra.Command, _ []string) error {
+ return btrfsrepair.ClearBadNodes(os.Stdout, os.Stderr, fs)
+ },
+ })
+}
diff --git a/scripts/diff-dump-tree.sh b/scripts/diff-dump-tree.sh
index 597adb5..aa9c2a9 100755
--- a/scripts/diff-dump-tree.sh
+++ b/scripts/diff-dump-tree.sh
@@ -2,4 +2,4 @@
make -C ../btrfs-progs
diff -u \
<(../btrfs-progs/btrfs inspect dump-tree --noscan --csum-items ../scratch/new.img | sed -e 's/ UNKNOWN.0 / UNTYPED /g' -e 's/\b18446744073709551615\b/-1/g') \
- <(go run ./cmd/btrfs-dump-tree/ ../scratch/new.img)
+ <(go run ./cmd/btrfs-rec/ inspect dump-trees --pv=../scratch/new.img)
diff --git a/scripts/run-clear-nodes.sh b/scripts/run-clear-nodes.sh
index 2f390ea..48186f8 100755
--- a/scripts/run-clear-nodes.sh
+++ b/scripts/run-clear-nodes.sh
@@ -2,4 +2,4 @@
set -e
rm -f ../scratch/dump-clearnodes.img
cp --reflink=always ../scratch/dump-{scratch,clearnodes}.img
-time go run ./cmd/btrfs-clear-bad-nodes ../scratch/dump-clearnodes.img
+time go run ./cmd/btrfs-rec repair clear-bad-nodes --pv=../scratch/dump-clearnodes.img