Files
krata/hypha/src/ctl/mod.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

2024-01-18 06:15:42 -08:00
use crate::error::{HyphaError, Result};
2024-01-18 00:02:21 -08:00
use crate::image::cache::ImageCache;
2024-01-18 10:16:59 -08:00
use crate::image::name::ImageName;
2024-01-18 00:02:21 -08:00
use crate::image::{ImageCompiler, ImageInfo};
use std::fs;
use std::path::PathBuf;
2024-01-18 01:38:56 -08:00
use uuid::Uuid;
2024-01-18 06:15:42 -08:00
use xenclient::{DomainConfig, DomainDisk, XenClient};
2024-01-17 08:18:12 -08:00
2024-01-17 14:29:05 -08:00
pub struct Controller {
2024-01-18 00:02:21 -08:00
image_cache: ImageCache,
image: String,
2024-01-17 08:18:12 -08:00
client: XenClient,
2024-01-17 12:36:13 -08:00
kernel_path: String,
initrd_path: String,
vcpus: u32,
mem: u64,
2024-01-17 08:18:12 -08:00
}
2024-01-17 14:29:05 -08:00
impl Controller {
pub fn new(
2024-01-18 06:15:42 -08:00
store_path: String,
2024-01-17 14:29:05 -08:00
kernel_path: String,
initrd_path: String,
image: String,
2024-01-17 14:29:05 -08:00
vcpus: u32,
mem: u64,
) -> Result<Controller> {
2024-01-18 06:15:42 -08:00
let mut image_cache_path = PathBuf::from(store_path);
image_cache_path.push("cache");
fs::create_dir_all(&image_cache_path)?;
2024-01-18 00:02:21 -08:00
2024-01-17 08:18:12 -08:00
let client = XenClient::open()?;
2024-01-18 00:02:21 -08:00
image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?;
2024-01-17 14:29:05 -08:00
Ok(Controller {
2024-01-18 00:02:21 -08:00
image_cache,
image,
2024-01-17 12:36:13 -08:00
client,
kernel_path,
initrd_path,
vcpus,
mem,
})
2024-01-17 08:18:12 -08:00
}
2024-01-18 00:02:21 -08:00
fn compile(&mut self) -> Result<ImageInfo> {
let image = ImageName::parse(&self.image)?;
2024-01-18 00:02:21 -08:00
let compiler = ImageCompiler::new(&self.image_cache)?;
compiler.compile(&image)
}
2024-01-17 08:18:12 -08:00
pub fn launch(&mut self) -> Result<u32> {
2024-01-18 01:38:56 -08:00
let uuid = Uuid::new_v4();
let name = format!("hypha-{uuid}");
2024-01-18 06:15:42 -08:00
let image_info = self.compile()?;
let squashfs_path = image_info
.squashfs
.to_str()
.ok_or_else(|| HyphaError::new("failed to convert squashfs path to string"))?;
2024-01-17 08:18:12 -08:00
let config = DomainConfig {
2024-01-18 06:15:42 -08:00
backend_domid: 0,
2024-01-18 01:38:56 -08:00
name: &name,
2024-01-17 12:36:13 -08:00
max_vcpus: self.vcpus,
mem_mb: self.mem,
kernel_path: self.kernel_path.as_str(),
initrd_path: self.initrd_path.as_str(),
2024-01-18 06:15:42 -08:00
cmdline: "elevator=noop",
disks: vec![DomainDisk {
vdev: "xvda",
pdev: squashfs_path,
writable: false,
}],
2024-01-17 08:18:12 -08:00
};
2024-01-17 12:36:13 -08:00
Ok(self.client.create(&config)?)
2024-01-17 08:18:12 -08:00
}
}