1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dlog"
"github.com/datawire/ocibuild/pkg/cliutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
)
type logLevelFlag struct {
logrus.Level
}
func (lvl *logLevelFlag) Type() string { return "loglevel" }
func (lvl *logLevelFlag) Set(str string) error {
var err error
lvl.Level, err = logrus.ParseLevel(str)
return err
}
var _ pflag.Value = (*logLevelFlag)(nil)
type subcommand struct {
cobra.Command
RunE func(*btrfs.FS, *cobra.Command, []string) error
}
var inspectors, repairers []subcommand
func main() {
logLevelFlag := logLevelFlag{
Level: logrus.InfoLevel,
}
var pvsFlag []string
var mappingsFlag string
argparser := &cobra.Command{
Use: "btrfs-rec {[flags]|SUBCOMMAND}",
Short: "Recover (data from) a broken btrfs filesystem",
Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands),
RunE: cliutil.RunSubcommands,
SilenceErrors: true, // main() will handle this after .ExecuteContext() returns
SilenceUsage: true, // our FlagErrorFunc will handle it
CompletionOptions: cobra.CompletionOptions{ //nolint:exhaustivestruct
DisableDefaultCmd: true,
},
}
argparser.SetFlagErrorFunc(cliutil.FlagErrorFunc)
argparser.SetHelpTemplate(cliutil.HelpTemplate)
argparser.PersistentFlags().Var(&logLevelFlag, "verbosity", "set the verbosity")
argparser.PersistentFlags().StringArrayVar(&pvsFlag, "pv", nil, "open the file `physical_volume` as part of the filesystem")
if err := argparser.MarkPersistentFlagFilename("pv"); err != nil {
panic(err)
}
if err := argparser.MarkPersistentFlagRequired("pv"); err != nil {
panic(err)
}
argparser.PersistentFlags().StringVar(&mappingsFlag, "mappings", "", "load chunk/dev-extent/blockgroup data from external JSON file `mappings.json`")
if err := argparser.MarkPersistentFlagFilename("mappings"); err != nil {
panic(err)
}
var openFlag int = os.O_RDONLY
argparserInspect := &cobra.Command{
Use: "inspect {[flags]|SUBCOMMAND}",
Short: "Inspect (but don't modify) a broken btrfs filesystem",
Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands),
RunE: cliutil.RunSubcommands,
}
argparser.AddCommand(argparserInspect)
argparserRepair := &cobra.Command{
Use: "repair {[flags]|SUBCOMMAND}",
Short: "Repair a broken btrfs filesystem",
Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands),
RunE: cliutil.RunSubcommands,
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
openFlag = os.O_RDWR
return nil
},
}
argparser.AddCommand(argparserRepair)
for _, cmdgrp := range []struct {
parent *cobra.Command
children []subcommand
}{
{argparserInspect, inspectors},
{argparserRepair, repairers},
} {
for _, child := range cmdgrp.children {
cmd := child.Command
runE := child.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
logger := logrus.New()
logger.SetLevel(logLevelFlag.Level)
ctx = dlog.WithLogger(ctx, dlog.WrapLogrus(logger))
grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{
EnableSignalHandling: true,
})
grp.Go("main", func(ctx context.Context) (err error) {
maybeSetErr := func(_err error) {
if _err != nil && err == nil {
err = _err
}
}
fs, err := btrfsutil.Open(ctx, openFlag, pvsFlag...)
if err != nil {
return err
}
defer func() {
maybeSetErr(fs.Close())
}()
if mappingsFlag != "" {
bs, err := os.ReadFile(mappingsFlag)
if err != nil {
return err
}
var mappingsJSON []btrfsvol.Mapping
if err := json.Unmarshal(bs, &mappingsJSON); err != nil {
return err
}
for _, mapping := range mappingsJSON {
if err := fs.LV.AddMapping(mapping); err != nil {
return err
}
}
}
cmd.SetContext(ctx)
return runE(fs, cmd, args)
})
return grp.Wait()
}
cmdgrp.parent.AddCommand(&cmd)
}
}
if err := argparser.ExecuteContext(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "%v: error: %v\n", argparser.CommandPath(), err)
os.Exit(1)
}
}
|