2024-01-21 00:13:05 -08:00
|
|
|
pub mod cfgblk;
|
2024-01-18 14:52:35 -08:00
|
|
|
|
2024-01-21 00:13:05 -08:00
|
|
|
use crate::autoloop::AutoLoop;
|
2024-01-18 14:52:35 -08:00
|
|
|
use crate::ctl::cfgblk::ConfigBlock;
|
2024-01-21 01:58:07 -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};
|
2024-01-21 00:13:05 -08:00
|
|
|
use loopdev::LoopControl;
|
2024-01-21 04:49:31 -08:00
|
|
|
use std::io::{Read, Write};
|
2024-01-18 00:02:21 -08:00
|
|
|
use std::path::PathBuf;
|
2024-01-21 04:49:31 -08:00
|
|
|
use std::process::exit;
|
|
|
|
use std::{fs, io, thread};
|
|
|
|
use termion::raw::IntoRawMode;
|
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-21 01:58:07 -08:00
|
|
|
use xenstore::client::{XsdClient, XsdInterface};
|
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,
|
2024-01-21 00:13:05 -08:00
|
|
|
autoloop: AutoLoop,
|
2024-01-17 08:18:12 -08:00
|
|
|
client: XenClient,
|
|
|
|
}
|
|
|
|
|
2024-01-17 14:29:05 -08:00
|
|
|
impl Controller {
|
2024-01-21 04:49:31 -08:00
|
|
|
pub fn new(store_path: String) -> 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,
|
2024-01-21 00:13:05 -08:00
|
|
|
autoloop: AutoLoop::new(LoopControl::open()?),
|
2024-01-17 12:36:13 -08:00
|
|
|
client,
|
|
|
|
})
|
2024-01-17 08:18:12 -08:00
|
|
|
}
|
|
|
|
|
2024-01-21 01:58:07 -08:00
|
|
|
fn compile(&mut self, image: &str) -> Result<ImageInfo> {
|
|
|
|
let image = ImageName::parse(image)?;
|
2024-01-18 00:02:21 -08:00
|
|
|
let compiler = ImageCompiler::new(&self.image_cache)?;
|
|
|
|
compiler.compile(&image)
|
2024-01-17 14:33:20 -08:00
|
|
|
}
|
|
|
|
|
2024-01-21 04:49:31 -08:00
|
|
|
pub fn launch(
|
|
|
|
&mut self,
|
|
|
|
kernel_path: &str,
|
|
|
|
initrd_path: &str,
|
|
|
|
image: &str,
|
|
|
|
vcpus: u32,
|
|
|
|
mem: u64,
|
|
|
|
) -> Result<u32> {
|
2024-01-18 01:38:56 -08:00
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
let name = format!("hypha-{uuid}");
|
2024-01-21 01:58:07 -08:00
|
|
|
let image_info = self.compile(image)?;
|
2024-01-18 14:52:35 -08:00
|
|
|
let cfgblk = ConfigBlock::new(&uuid, &image_info)?;
|
|
|
|
cfgblk.build()?;
|
2024-01-21 00:13:05 -08:00
|
|
|
|
|
|
|
let image_squashfs_path = image_info.image_squashfs.clone();
|
|
|
|
let cfgblk_squashfs_path = cfgblk.file.clone();
|
|
|
|
|
|
|
|
let image_squashfs_loop = self.autoloop.loopify(&image_squashfs_path)?;
|
|
|
|
let cfgblk_squashfs_loop = self.autoloop.loopify(&cfgblk_squashfs_path)?;
|
|
|
|
|
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-21 01:58:07 -08:00
|
|
|
max_vcpus: vcpus,
|
|
|
|
mem_mb: mem,
|
2024-01-21 04:49:31 -08:00
|
|
|
kernel_path,
|
|
|
|
initrd_path,
|
2024-01-18 14:52:35 -08:00
|
|
|
cmdline: "quiet elevator=noop",
|
|
|
|
disks: vec![
|
|
|
|
DomainDisk {
|
|
|
|
vdev: "xvda",
|
2024-01-21 00:13:05 -08:00
|
|
|
block: &image_squashfs_loop,
|
2024-01-18 14:52:35 -08:00
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
DomainDisk {
|
|
|
|
vdev: "xvdb",
|
2024-01-21 00:13:05 -08:00
|
|
|
block: &cfgblk_squashfs_loop,
|
2024-01-18 14:52:35 -08:00
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
],
|
2024-01-21 01:58:07 -08:00
|
|
|
extra_keys: vec![
|
|
|
|
("hypha/uuid".to_string(), uuid.to_string()),
|
|
|
|
(
|
|
|
|
"hypha/loops".to_string(),
|
|
|
|
format!(
|
|
|
|
"{},{}",
|
|
|
|
&image_squashfs_loop.path, &cfgblk_squashfs_loop.path
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2024-01-17 08:18:12 -08:00
|
|
|
};
|
2024-01-21 00:13:05 -08:00
|
|
|
match self.client.create(&config) {
|
|
|
|
Ok(domid) => Ok(domid),
|
|
|
|
Err(error) => {
|
|
|
|
let _ = self.autoloop.unloop(&image_squashfs_loop.path);
|
|
|
|
let _ = self.autoloop.unloop(&cfgblk_squashfs_loop.path);
|
|
|
|
let _ = fs::remove_dir(&cfgblk.dir);
|
|
|
|
Err(error.into())
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 08:18:12 -08:00
|
|
|
}
|
2024-01-21 01:58:07 -08:00
|
|
|
|
|
|
|
pub fn destroy(&mut self, domid: u32) -> Result<Uuid> {
|
|
|
|
let mut store = XsdClient::open()?;
|
|
|
|
let dom_path = store.get_domain_path(domid)?;
|
|
|
|
let uuid = match store.read_string_optional(format!("{}/hypha/uuid", dom_path).as_str())? {
|
|
|
|
None => {
|
|
|
|
return Err(HyphaError::new(&format!(
|
|
|
|
"domain {} was not found or not created by hypha",
|
|
|
|
domid
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
Some(value) => value,
|
|
|
|
};
|
|
|
|
if uuid.is_empty() {
|
|
|
|
return Err(HyphaError::new(
|
|
|
|
"unable to find hypha uuid based on the domain",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let uuid = Uuid::parse_str(&uuid)?;
|
|
|
|
let loops = store.read_string(format!("{}/hypha/loops", dom_path).as_str())?;
|
|
|
|
let loops = loops
|
|
|
|
.split(',')
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
self.client.destroy(domid)?;
|
|
|
|
for lop in &loops {
|
|
|
|
self.autoloop.unloop(lop)?;
|
|
|
|
}
|
|
|
|
Ok(uuid)
|
|
|
|
}
|
2024-01-21 04:49:31 -08:00
|
|
|
|
|
|
|
pub fn console(&mut self, domid: u32) -> Result<()> {
|
|
|
|
let (mut read, mut write) = self.client.open_console(domid)?;
|
|
|
|
let mut stdin = io::stdin();
|
|
|
|
let is_tty = termion::is_tty(&stdin);
|
|
|
|
let mut stdout_for_exit = io::stdout().into_raw_mode()?;
|
|
|
|
thread::spawn(move || {
|
|
|
|
let mut buffer = vec![0u8; 60];
|
|
|
|
loop {
|
|
|
|
let size = stdin.read(&mut buffer).expect("failed to read stdin");
|
|
|
|
if is_tty && size == 1 && buffer[0] == 0x1d {
|
|
|
|
stdout_for_exit
|
|
|
|
.suspend_raw_mode()
|
|
|
|
.expect("failed to disable raw mode");
|
|
|
|
stdout_for_exit.flush().expect("failed to flush stdout");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
write
|
|
|
|
.write_all(&buffer[0..size])
|
|
|
|
.expect("failed to write to domain console");
|
|
|
|
write.flush().expect("failed to flush domain console");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut buffer = vec![0u8; 256];
|
|
|
|
if is_tty {
|
|
|
|
let mut stdout = io::stdout().into_raw_mode()?;
|
|
|
|
loop {
|
|
|
|
let size = read.read(&mut buffer)?;
|
|
|
|
stdout.write_all(&buffer[0..size])?;
|
|
|
|
stdout.flush()?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
loop {
|
|
|
|
let size = read.read(&mut buffer)?;
|
|
|
|
stdout.write_all(&buffer[0..size])?;
|
|
|
|
stdout.flush()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 08:18:12 -08:00
|
|
|
}
|