hypha-agent: command line arguments

This commit is contained in:
Alex Zenla
2024-01-17 12:36:13 -08:00
parent 8af6e25edc
commit f69ce96054
6 changed files with 63 additions and 145 deletions

View File

@ -10,6 +10,10 @@ path = "../xenclient"
[dependencies]
log = "0.4.20"
[dependencies.clap]
version = "4.4.18"
features = ["derive"]
[lib]
path = "src/lib.rs"

View File

@ -1,8 +1,26 @@
use clap::Parser;
use hypha::agent::Agent;
use hypha::error::Result;
#[derive(Parser, Debug)]
#[command(version, about)]
struct AgentArgs {
#[arg(short, long)]
kernel: String,
#[arg(short, long)]
initrd: String,
#[arg(short, long, default_value_t = 1)]
cpus: u32,
#[arg(short, long, default_value_t = 512)]
mem: u64,
}
fn main() -> Result<()> {
let mut agent = Agent::new()?;
let args = AgentArgs::parse();
let mut agent = Agent::new(args.kernel, args.initrd, args.cpus, args.mem)?;
let domid = agent.launch()?;
println!("launched domain: {}", domid);
Ok(())

View File

@ -1,49 +1,34 @@
use crate::error::{HyphaError, Result};
use std::fs::{read_dir, DirEntry};
use xenclient::create::DomainConfig;
use xenclient::XenClient;
use crate::error::Result;
use xenclient::{DomainConfig, XenClient};
pub struct Agent {
client: XenClient,
kernel_path: String,
initrd_path: String,
vcpus: u32,
mem: u64,
}
impl Agent {
pub fn new() -> Result<Agent> {
pub fn new(kernel_path: String, initrd_path: String, vcpus: u32, mem: u64) -> Result<Agent> {
let client = XenClient::open()?;
Ok(Agent { client })
Ok(Agent {
client,
kernel_path,
initrd_path,
vcpus,
mem,
})
}
pub fn launch(&mut self) -> Result<u32> {
let kernel_path = self.find_boot_path("vmlinuz-")?;
let initrd_path = self.find_boot_path("initrd.img-")?;
let config = DomainConfig {
max_vcpus: 1,
mem_mb: 512,
kernel_path,
initrd_path,
cmdline: "debug elevator=noop".to_string(),
max_vcpus: self.vcpus,
mem_mb: self.mem,
kernel_path: self.kernel_path.as_str(),
initrd_path: self.initrd_path.as_str(),
cmdline: "debug elevator=noop",
};
Ok(self.client.create(config)?)
}
fn find_boot_path(&self, prefix: &str) -> Result<String> {
let vmlinuz = read_dir("/boot")?
.filter_map(|x| x.ok())
.filter(|x| {
x.file_name()
.to_str()
.ok_or(HyphaError::new("invalid direntry"))
.map(|x| x.starts_with(prefix))
.unwrap_or(false)
})
.collect::<Vec<DirEntry>>();
Ok(vmlinuz
.first()
.ok_or(HyphaError::new("unable to find suitable image"))?
.path()
.to_str()
.ok_or(HyphaError::new("invalid direntry"))?
.to_string())
Ok(self.client.create(&config)?)
}
}