create hypha

This commit is contained in:
Alex Zenla 2024-01-17 08:18:12 -08:00
parent 135182d847
commit 8689398032
No known key found for this signature in database
GPG Key ID: 067B238899B51269
8 changed files with 134 additions and 1 deletions

View File

@ -3,6 +3,7 @@ members = [
"xenstore", "xenstore",
"xenevtchn", "xenevtchn",
"xencall", "xencall",
"xenclient" "xenclient",
"hypha",
] ]
resolver = "2" resolver = "2"

22
hypha/Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
name = "hypha"
version = "0.0.1"
edition = "2021"
resolver = "2"
[dependencies.xenclient]
path = "../xenclient"
[dependencies]
log = "0.4.20"
[lib]
path = "src/lib.rs"
[[bin]]
name = "hypha-agent"
path = "bin/agent.rs"
[[bin]]
name = "hypha-container"
path = "bin/container.rs"

9
hypha/bin/agent.rs Normal file
View File

@ -0,0 +1,9 @@
use hypha::agent::Agent;
use hypha::error::Result;
fn main() -> Result<()> {
let mut agent = Agent::new()?;
let domid = agent.launch()?;
println!("launched domain: {}", domid);
Ok(())
}

5
hypha/bin/container.rs Normal file
View File

@ -0,0 +1,5 @@
use hypha::error::Result;
fn main() -> Result<()> {
Ok(())
}

50
hypha/src/agent/mod.rs Normal file
View File

@ -0,0 +1,50 @@
use crate::error::{HyphaError, Result};
use std::fs::{read_dir, DirEntry};
use xenclient::create::DomainConfig;
use xenclient::XenClient;
pub struct Agent {
client: XenClient,
}
impl Agent {
pub fn new() -> Result<Agent> {
let client = XenClient::open()?;
Ok(Agent { client })
}
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(),
};
Ok(self.client.create(config)?)
}
fn find_boot_path(&self, prefix: &str) -> Result<String> {
let vmlinuz = read_dir("/boot")?
.filter(|x| x.is_ok())
.map(|x| x.unwrap())
.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())
}
}

View File

@ -0,0 +1 @@

42
hypha/src/error.rs Normal file
View File

@ -0,0 +1,42 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use xenclient::XenClientError;
pub type Result<T> = std::result::Result<T, HyphaError>;
#[derive(Debug)]
pub struct HyphaError {
message: String,
}
impl HyphaError {
pub fn new(msg: &str) -> HyphaError {
HyphaError {
message: msg.to_string(),
}
}
}
impl Display for HyphaError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error for HyphaError {
fn description(&self) -> &str {
&self.message
}
}
impl From<std::io::Error> for HyphaError {
fn from(value: std::io::Error) -> Self {
HyphaError::new(value.to_string().as_str())
}
}
impl From<XenClientError> for HyphaError {
fn from(value: XenClientError) -> Self {
HyphaError::new(value.to_string().as_str())
}
}

3
hypha/src/lib.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod agent;
pub mod container;
pub mod error;