Skip to main content

btrfs_cli/rescue/
create_control_device.rs

1use crate::{Format, Runnable};
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::ffi::CString;
5
6/// Create /dev/btrfs-control
7///
8/// The btrfs control device is a character device (major 10, minor 234) used
9/// by the btrfs kernel module for management operations. This command creates
10/// it if it is missing, for example after a minimal system installation or
11/// when udev is not running.
12#[derive(Parser, Debug)]
13pub struct RescueCreateControlDeviceCommand {}
14
15impl Runnable for RescueCreateControlDeviceCommand {
16    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
17        const PATH: &str = "/dev/btrfs-control";
18        let dev = libc::makedev(10, 234);
19        let path = CString::new(PATH).unwrap();
20        // SAFETY: path is a valid NUL-terminated string from CString.
21        let ret =
22            unsafe { libc::mknod(path.as_ptr(), libc::S_IFCHR | 0o600, dev) };
23        if ret != 0 {
24            return Err(std::io::Error::last_os_error())
25                .with_context(|| format!("could not create {PATH}"));
26        }
27        Ok(())
28    }
29}