Skip to main content

btrfs_cli/inspect/
dump_super.rs

1use crate::{Format, Runnable};
2use anyhow::{Context, Result, bail};
3use btrfs_disk::superblock::{self, SUPER_MIRROR_MAX};
4use clap::Parser;
5use std::{fs::File, path::PathBuf};
6
7/// Dump the btrfs superblock from a device or image file.
8///
9/// Reads and displays the superblock stored on a btrfs block device or
10/// filesystem image. By default only the primary superblock (mirror 0,
11/// at offset 64 KiB) is printed. Use -a to print all mirrors, or -s to
12/// select a specific one.
13#[derive(Parser, Debug)]
14pub struct DumpSuperCommand {
15    /// Path to a btrfs block device or image file
16    path: PathBuf,
17
18    /// Print full information including sys_chunk_array and backup roots
19    #[clap(short = 'f', long)]
20    full: bool,
21
22    /// Print all superblock mirrors (0, 1, 2)
23    #[clap(short = 'a', long)]
24    all: bool,
25
26    /// Print only this superblock mirror (0, 1, or 2)
27    #[clap(short = 's', long = "super")]
28    mirror: Option<u32>,
29
30    /// Attempt to print superblocks with bad magic
31    #[clap(short = 'F', long)]
32    force: bool,
33}
34
35impl Runnable for DumpSuperCommand {
36    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
37        let mut file = File::open(&self.path)
38            .with_context(|| format!("failed to open '{}'", self.path.display()))?;
39
40        let mirrors: Vec<u32> = if self.all {
41            (0..SUPER_MIRROR_MAX).collect()
42        } else if let Some(m) = self.mirror {
43            if m >= SUPER_MIRROR_MAX {
44                bail!("mirror index must be 0, 1, or 2 (got {m})");
45            }
46            vec![m]
47        } else {
48            vec![0]
49        };
50
51        for (i, &mirror) in mirrors.iter().enumerate() {
52            if i > 0 {
53                println!();
54            }
55
56            let offset = superblock::super_mirror_offset(mirror);
57            println!(
58                "superblock: bytenr={offset}, device={}",
59                self.path.display()
60            );
61
62            let sb = match superblock::read_superblock(&mut file, mirror) {
63                Ok(sb) => sb,
64                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
65                    println!("superblock mirror {mirror} beyond end of device, skipping");
66                    continue;
67                }
68                Err(e) => {
69                    return Err(e).with_context(|| {
70                        format!(
71                            "failed to read superblock mirror {mirror} from '{}'",
72                            self.path.display()
73                        )
74                    });
75                }
76            };
77
78            if !sb.magic_is_valid() && !self.force {
79                if self.all {
80                    println!(
81                        "superblock mirror {mirror} has bad magic, skipping (use -F to force)"
82                    );
83                    continue;
84                }
85                bail!(
86                    "bad magic on superblock mirror {mirror} of '{}' (use -F to force)",
87                    self.path.display()
88                );
89            }
90
91            println!("---------------------------------------------------------");
92            superblock::print_superblock(&sb, self.full);
93        }
94
95        Ok(())
96    }
97}