hypha: setup image as disk

This commit is contained in:
Alex Zenla
2024-01-18 06:15:42 -08:00
parent 80311db549
commit 649a0c303d
6 changed files with 324 additions and 87 deletions

View File

@ -1,6 +1,7 @@
use clap::Parser;
use hypha::ctl::Controller;
use hypha::error::{HyphaError, Result};
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(version, about)]
@ -20,23 +21,28 @@ struct ControllerArgs {
#[arg(short, long, default_value_t = 512)]
mem: u64,
#[arg(short = 'C', long, default_value = "auto")]
cache: String,
#[arg(short, long, default_value = "auto")]
store: String,
}
fn main() -> Result<()> {
env_logger::init();
let args = ControllerArgs::parse();
let cache_path = if args.cache == "auto" {
default_cache_path()
.ok_or_else(|| HyphaError::new("unable to determine default cache path"))
let store_path = if args.store == "auto" {
default_store_path()
.ok_or_else(|| HyphaError::new("unable to determine default store path"))
} else {
Ok(args.cache)
Ok(PathBuf::from(args.store))
}?;
let store_path = store_path
.to_str()
.map(|x| x.to_string())
.ok_or_else(|| HyphaError::new("unable to convert store path to string"))?;
let mut controller = Controller::new(
cache_path,
store_path,
args.kernel,
args.initrd,
args.image,
@ -48,9 +54,13 @@ fn main() -> Result<()> {
Ok(())
}
fn default_cache_path() -> Option<String> {
fn default_store_path() -> Option<PathBuf> {
let user_dirs = directories::UserDirs::new()?;
let mut path = user_dirs.home_dir().to_path_buf();
path.push(".hypha/cache");
Some(path.to_str()?.to_string())
if path == PathBuf::from("/root") {
path.push("/var/lib/hypha")
} else {
path.push(".hypha");
}
Some(path)
}

View File

@ -1,11 +1,11 @@
use crate::error::Result;
use crate::error::{HyphaError, Result};
use crate::image::cache::ImageCache;
use crate::image::{ImageCompiler, ImageInfo};
use ocipkg::ImageName;
use std::fs;
use std::path::PathBuf;
use uuid::Uuid;
use xenclient::{DomainConfig, XenClient};
use xenclient::{DomainConfig, DomainDisk, XenClient};
pub struct Controller {
image_cache: ImageCache,
@ -19,17 +19,18 @@ pub struct Controller {
impl Controller {
pub fn new(
cache_path: String,
store_path: String,
kernel_path: String,
initrd_path: String,
image: String,
vcpus: u32,
mem: u64,
) -> Result<Controller> {
fs::create_dir_all(&cache_path)?;
let mut image_cache_path = PathBuf::from(store_path);
image_cache_path.push("cache");
fs::create_dir_all(&image_cache_path)?;
let client = XenClient::open()?;
let mut image_cache_path = PathBuf::from(cache_path);
image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?;
@ -53,14 +54,24 @@ impl Controller {
pub fn launch(&mut self) -> Result<u32> {
let uuid = Uuid::new_v4();
let name = format!("hypha-{uuid}");
let _image_info = self.compile()?;
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"))?;
let config = DomainConfig {
backend_domid: 0,
name: &name,
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",
cmdline: "elevator=noop",
disks: vec![DomainDisk {
vdev: "xvda",
pdev: squashfs_path,
writable: false,
}],
};
Ok(self.client.create(&config)?)
}