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)]
15pub struct MinDevSizeCommand {
16    /// Specify the device id to query
17    #[arg(long = "id", default_value = "1")]
18    devid: u64,
19
20    /// Path to a file or directory on the btrfs filesystem
21    path: PathBuf,
22}
23
24impl Runnable for MinDevSizeCommand {
25    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
26        let file = open_path(&self.path)?;
27
28        let size = btrfs_uapi::device::device_min_size(
29            file.as_fd(),
30            self.devid,
31        )
32        .with_context(|| {
33            format!(
34                "failed to determine min device size for devid {} on '{}'",
35                self.devid,
36                self.path.display()
37            )
38        })?;
39
40        println!("{} bytes ({})", size, human_bytes(size));
41        Ok(())
42    }
43}