Skip to main content

btrfs_cli/
balance.rs

1use crate::{Format, Runnable};
2use anyhow::Result;
3use clap::Parser;
4use std::{fs::File, path::PathBuf};
5
6mod cancel;
7mod filters;
8mod pause;
9mod resume;
10mod start;
11mod status;
12
13pub use self::{cancel::*, pause::*, resume::*, start::*, status::*};
14
15/// Balance data across devices, or change block groups using filters.
16///
17/// Rebalance data and metadata across devices to improve performance or
18/// recover space. Balance is typically a long-running operation. You can
19/// pause, resume, or cancel a balance in progress. Progress and status can
20/// be queried at any time. Requires CAP_SYS_ADMIN.
21#[derive(Parser, Debug)]
22pub struct BalanceCommand {
23    #[clap(subcommand)]
24    pub subcommand: BalanceSubcommand,
25}
26
27impl Runnable for BalanceCommand {
28    fn run(&self, format: Format, dry_run: bool) -> Result<()> {
29        match &self.subcommand {
30            BalanceSubcommand::Start(cmd) => cmd.run(format, dry_run),
31            BalanceSubcommand::Pause(cmd) => cmd.run(format, dry_run),
32            BalanceSubcommand::Cancel(cmd) => cmd.run(format, dry_run),
33            BalanceSubcommand::Resume(cmd) => cmd.run(format, dry_run),
34            BalanceSubcommand::Status(cmd) => cmd.run(format, dry_run),
35        }
36    }
37}
38
39#[derive(Parser, Debug)]
40pub enum BalanceSubcommand {
41    Start(BalanceStartCommand),
42    Pause(BalancePauseCommand),
43    Cancel(BalanceCancelCommand),
44    Resume(BalanceResumeCommand),
45    Status(BalanceStatusCommand),
46}
47
48/// Open a path as a read-only file descriptor, suitable for passing to ioctls.
49fn open_path(path: &PathBuf) -> Result<File> {
50    use anyhow::Context;
51    File::open(path)
52        .with_context(|| format!("failed to open '{}'", path.display()))
53}