Skip to content

Commit

Permalink
Add a coldplug utility
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Dec 30, 2024
1 parent fa0df17 commit bd74b2d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/bin/mdev-coldplug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::path::PathBuf;

use clap::Parser;
use mdev::setup_log;
use tracing::{debug, error};
use walkdir::WalkDir;

#[derive(Parser)]
struct Opt {
/// Verbose mode, logs to stderr
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,

/// Path where the sysfs is mounted
#[arg(short, long, default_value = "/sys")]
sysfs_mount: PathBuf,
}

impl Opt {
fn setup_log(&self) -> anyhow::Result<()> {
setup_log(self.verbose)
}
}

fn main() -> anyhow::Result<()> {
let opt = Opt::parse();

opt.setup_log()?;

let classdir = WalkDir::new(opt.sysfs_mount.join("class"))
.follow_links(true)
.max_depth(4)
.into_iter();
let busdir = WalkDir::new(opt.sysfs_mount.join("bus"))
.follow_links(true)
.max_depth(3)
.into_iter();

for entry in classdir
.chain(busdir)
.filter_map(|e| e.ok().filter(|e| e.file_name().eq("uevent")))
{
debug!("{entry:?}");
let p = entry.path();
std::fs::write(p, "add").unwrap_or_else(|e| error!("cannot write to {}: {e}", p.display()));
}

Ok(())
}

0 comments on commit bd74b2d

Please sign in to comment.