Skip to main content

btrfs_cli/qgroup/
create.rs

1use crate::{
2    RunContext, Runnable,
3    util::{open_path, parse_qgroupid},
4};
5use anyhow::{Context, Result};
6use clap::Parser;
7use nix::errno::Errno;
8use std::{os::unix::io::AsFd, path::PathBuf};
9
10/// Create a subvolume quota group
11#[derive(Parser, Debug)]
12pub struct QgroupCreateCommand {
13    /// Qgroup id in the form LEVEL/ID
14    pub qgroupid: String,
15
16    /// Path to a mounted btrfs filesystem
17    pub path: PathBuf,
18}
19
20impl Runnable for QgroupCreateCommand {
21    fn run(&self, _ctx: &RunContext) -> Result<()> {
22        let qgroupid = parse_qgroupid(&self.qgroupid)?;
23
24        let file = open_path(&self.path)?;
25
26        match btrfs_uapi::quota::qgroup_create(file.as_fd(), qgroupid) {
27            Ok(()) => {
28                println!("qgroup {} created", self.qgroupid);
29                Ok(())
30            }
31            Err(Errno::ENOTCONN) => {
32                anyhow::bail!("quota not enabled on '{}'", self.path.display())
33            }
34            Err(e) => Err(e).with_context(|| {
35                format!(
36                    "failed to create qgroup '{}' on '{}'",
37                    self.qgroupid,
38                    self.path.display()
39                )
40            }),
41        }
42    }
43}