Skip to main content

btrfs_cli/inspect/
min_dev_size.rs

1use crate::{
2    Format, Runnable,
3    util::{human_bytes, open_path},
4};
5use anyhow::{Context, Result};
6use clap::Parser;
7use std::{os::unix::io::AsFd, path::PathBuf};
8
9/// Print the minimum size a device can be shrunk to.
10///
11/// Returns the minimum size in bytes that the specified device can be
12/// resized to without losing data. The device id 1 is used by default.
13/// Requires CAP_SYS_ADMIN.
14#[derive(Parser, Debug)]
15#[allow(clippy::doc_markdown)]
16pub struct MinDevSizeCommand {
17    /// Specify the device id to query
18    #[arg(long = "id", default_value = "1")]
19    devid: u64,
20
21    /// Path to a file or directory on the btrfs filesystem
22    path: PathBuf,
23}
24
25impl Runnable for MinDevSizeCommand {
26    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
27        let file = open_path(&self.path)?;
28
29        let size = btrfs_uapi::device::device_min_size(
30            file.as_fd(),
31            self.devid,
32        )
33        .with_context(|| {
34            format!(
35                "failed to determine min device size for devid {} on '{}'",
36                self.devid,
37                self.path.display()
38            )
39        })?;
40
41        println!("{} bytes ({})", size, human_bytes(size));
42        Ok(())
43    }
44}