summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect_rebuildmappings.go
blob: 9c6b1ccbbc980d213e950c26871c9f6c8f513045 (plain)
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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package main

import (
	"os"

	"git.lukeshu.com/go/lowmemjson"
	"github.com/datawire/dlib/dlog"
	"github.com/datawire/ocibuild/pkg/cliutil"
	"github.com/spf13/cobra"

	"git.lukeshu.com/btrfs-progs-ng/cmd/btrfs-rec/inspect/rebuildmappings"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
)

func init() {
	cmd := &cobra.Command{
		Use:   "rebuild-mappings",
		Short: "Rebuild broken chunk/dev-extent/blockgroup trees",
		Long: "" +
			"The rebuilt information is printed as JSON on stdout, and can " +
			"be loaded by the --mappings flag.\n" +
			"\n" +
			"This is very similar to `btrfs rescue chunk-recover`, but (1) " +
			"does a better job, (2) is less buggy, and (3) doesn't actually " +
			"write the info back to the filesystem; instead writing it " +
			"out-of-band to stdout.\n" +
			"\n" +
			"The I/O and the CPU parts of this can be split up as:\n" +
			"\n" +
			"\tbtrfs-rec inspect rebuild-mappings scan > SCAN.json   # read\n" +
			"\tbtrfs-rec inspect rebuild-mappings process SCAN.json  # CPU\n",
		Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
		RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			scanResults, err := rebuildmappings.ScanDevices(ctx, fs)
			if err != nil {
				return err
			}

			if err := rebuildmappings.RebuildMappings(ctx, fs, scanResults); err != nil {
				return err
			}

			dlog.Infof(ctx, "Writing reconstructed mappings to stdout...")
			if err := writeJSONFile(os.Stdout, fs.LV.Mappings(), lowmemjson.ReEncoderConfig{
				Indent:                "\t",
				ForceTrailingNewlines: true,
				CompactIfUnder:        120, //nolint:gomnd // This is what looks nice.
			}); err != nil {
				return err
			}
			dlog.Info(ctx, "... done writing")

			return nil
		}),
	}

	cmd.AddCommand(&cobra.Command{
		Use:   "scan",
		Short: "Read from the filesystem all data nescessary to rebuild the mappings",
		Args:  cliutil.WrapPositionalArgs(cobra.NoArgs),
		RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) {
			ctx := cmd.Context()

			scanResults, err := rebuildmappings.ScanDevices(ctx, fs)
			if err != nil {
				return err
			}

			dlog.Info(ctx, "Writing scan results to stdout...")
			if err := writeJSONFile(os.Stdout, scanResults, lowmemjson.ReEncoderConfig{
				Indent:                "\t",
				ForceTrailingNewlines: true,
				CompactIfUnder:        16, //nolint:gomnd // This is what looks nice.
			}); err != nil {
				return err
			}
			dlog.Info(ctx, "... done writing")

			return nil
		}),
	})

	cmd.AddCommand(&cobra.Command{
		Use:   "process",
		Short: "Rebuild the mappings based on previously read data",
		Args:  cliutil.WrapPositionalArgs(cobra.ExactArgs(1)),
		RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			dlog.Infof(ctx, "Reading %q...", args[0])
			scanResults, err := readJSONFile[rebuildmappings.ScanDevicesResult](ctx, args[0])
			if err != nil {
				return err
			}
			dlog.Infof(ctx, "... done reading %q", args[0])

			if err := rebuildmappings.RebuildMappings(ctx, fs, scanResults); err != nil {
				return err
			}

			dlog.Infof(ctx, "Writing reconstructed mappings to stdout...")
			if err := writeJSONFile(os.Stdout, fs.LV.Mappings(), lowmemjson.ReEncoderConfig{
				Indent:                "\t",
				ForceTrailingNewlines: true,
				CompactIfUnder:        120, //nolint:gomnd // This is what looks nice.
			}); err != nil {
				return err
			}
			dlog.Info(ctx, "... done writing")

			return nil
		}),
	})

	inspectors.AddCommand(cmd)
}