krata/hypha/bin/controller.rs

67 lines
1.5 KiB
Rust
Raw Normal View History

2024-01-17 22:29:05 +00:00
use clap::Parser;
use hypha::ctl::Controller;
2024-01-18 08:02:21 +00:00
use hypha::error::{HyphaError, Result};
2024-01-18 14:15:42 +00:00
use std::path::PathBuf;
2024-01-17 22:29:05 +00:00
#[derive(Parser, Debug)]
#[command(version, about)]
struct ControllerArgs {
#[arg(short, long)]
kernel: String,
#[arg(short = 'r', long)]
initrd: String,
#[arg(short, long)]
image: String,
#[arg(short, long, default_value_t = 1)]
cpus: u32,
#[arg(short, long, default_value_t = 512)]
mem: u64,
2024-01-18 08:02:21 +00:00
2024-01-18 14:15:42 +00:00
#[arg(short, long, default_value = "auto")]
store: String,
2024-01-17 22:29:05 +00:00
}
fn main() -> Result<()> {
env_logger::init();
let args = ControllerArgs::parse();
2024-01-18 14:15:42 +00:00
let store_path = if args.store == "auto" {
default_store_path()
.ok_or_else(|| HyphaError::new("unable to determine default store path"))
2024-01-18 08:02:21 +00:00
} else {
2024-01-18 14:15:42 +00:00
Ok(PathBuf::from(args.store))
2024-01-18 08:02:21 +00:00
}?;
2024-01-18 14:15:42 +00:00
let store_path = store_path
.to_str()
.map(|x| x.to_string())
.ok_or_else(|| HyphaError::new("unable to convert store path to string"))?;
2024-01-18 08:02:21 +00:00
let mut controller = Controller::new(
2024-01-18 14:15:42 +00:00
store_path,
2024-01-18 08:02:21 +00:00
args.kernel,
args.initrd,
args.image,
args.cpus,
args.mem,
)?;
2024-01-17 22:29:05 +00:00
let domid = controller.launch()?;
println!("launched domain: {}", domid);
Ok(())
}
2024-01-18 08:02:21 +00:00
2024-01-18 14:15:42 +00:00
fn default_store_path() -> Option<PathBuf> {
2024-01-18 08:02:21 +00:00
let user_dirs = directories::UserDirs::new()?;
let mut path = user_dirs.home_dir().to_path_buf();
2024-01-18 14:15:42 +00:00
if path == PathBuf::from("/root") {
path.push("/var/lib/hypha")
} else {
path.push(".hypha");
}
Some(path)
2024-01-18 08:02:21 +00:00
}