2024-03-07 09:04:05 +00:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
str::FromStr,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2024-03-05 11:35:25 +00:00
|
|
|
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use ipnetwork::IpNetwork;
|
|
|
|
use loopdev::LoopControl;
|
2024-04-02 00:56:18 +00:00
|
|
|
use tokio::sync::Semaphore;
|
2024-03-05 11:35:25 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use xenclient::XenClient;
|
2024-03-30 03:49:13 +00:00
|
|
|
use xenstore::{XsdClient, XsdInterface};
|
2024-03-05 11:35:25 +00:00
|
|
|
|
|
|
|
use self::{
|
|
|
|
autoloop::AutoLoop,
|
|
|
|
console::XenConsole,
|
|
|
|
launch::{GuestLaunchRequest, GuestLauncher},
|
|
|
|
};
|
2024-03-25 02:37:02 +00:00
|
|
|
use krataoci::cache::ImageCache;
|
2024-03-05 11:35:25 +00:00
|
|
|
|
|
|
|
pub mod autoloop;
|
|
|
|
pub mod cfgblk;
|
2024-03-28 07:36:48 +00:00
|
|
|
pub mod channel;
|
2024-03-05 11:35:25 +00:00
|
|
|
pub mod console;
|
|
|
|
pub mod launch;
|
|
|
|
|
2024-03-30 03:49:13 +00:00
|
|
|
pub struct GuestLoopInfo {
|
2024-03-05 11:35:25 +00:00
|
|
|
pub device: String,
|
|
|
|
pub file: String,
|
|
|
|
pub delete: Option<String>,
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:57:56 +00:00
|
|
|
pub struct GuestState {
|
|
|
|
pub exit_code: Option<i32>,
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
pub struct GuestInfo {
|
2024-03-13 13:05:17 +00:00
|
|
|
pub name: Option<String>,
|
2024-03-05 11:35:25 +00:00
|
|
|
pub uuid: Uuid,
|
|
|
|
pub domid: u32,
|
|
|
|
pub image: String,
|
2024-03-30 03:49:13 +00:00
|
|
|
pub loops: Vec<GuestLoopInfo>,
|
2024-03-27 02:54:39 +00:00
|
|
|
pub guest_ipv4: Option<IpNetwork>,
|
|
|
|
pub guest_ipv6: Option<IpNetwork>,
|
|
|
|
pub guest_mac: Option<String>,
|
|
|
|
pub gateway_ipv4: Option<IpNetwork>,
|
|
|
|
pub gateway_ipv6: Option<IpNetwork>,
|
|
|
|
pub gateway_mac: Option<String>,
|
2024-03-06 15:57:56 +00:00
|
|
|
pub state: GuestState,
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 00:56:18 +00:00
|
|
|
#[derive(Clone)]
|
2024-03-05 11:35:25 +00:00
|
|
|
pub struct RuntimeContext {
|
|
|
|
pub image_cache: ImageCache,
|
|
|
|
pub autoloop: AutoLoop,
|
|
|
|
pub xen: XenClient,
|
|
|
|
pub kernel: String,
|
|
|
|
pub initrd: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RuntimeContext {
|
|
|
|
pub async fn new(store: String) -> Result<Self> {
|
|
|
|
let mut image_cache_path = PathBuf::from(&store);
|
|
|
|
image_cache_path.push("cache");
|
|
|
|
fs::create_dir_all(&image_cache_path)?;
|
|
|
|
|
2024-03-10 00:22:24 +00:00
|
|
|
let xen = XenClient::open(0).await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
image_cache_path.push("image");
|
|
|
|
fs::create_dir_all(&image_cache_path)?;
|
|
|
|
let image_cache = ImageCache::new(&image_cache_path)?;
|
2024-03-07 09:04:05 +00:00
|
|
|
let kernel = RuntimeContext::detect_guest_file(&store, "kernel")?;
|
|
|
|
let initrd = RuntimeContext::detect_guest_file(&store, "initrd")?;
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
Ok(RuntimeContext {
|
|
|
|
image_cache,
|
|
|
|
autoloop: AutoLoop::new(LoopControl::open()?),
|
|
|
|
xen,
|
|
|
|
kernel,
|
|
|
|
initrd,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-07 09:04:05 +00:00
|
|
|
fn detect_guest_file(store: &str, name: &str) -> Result<String> {
|
2024-03-07 12:14:25 +00:00
|
|
|
let mut path = PathBuf::from(format!("{}/guest/{}", store, name));
|
2024-03-07 09:04:05 +00:00
|
|
|
if path.is_file() {
|
|
|
|
return path_as_string(&path);
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:14:25 +00:00
|
|
|
path = PathBuf::from(format!("/usr/share/krata/guest/{}", name));
|
|
|
|
if path.is_file() {
|
|
|
|
return path_as_string(&path);
|
|
|
|
}
|
|
|
|
Err(anyhow!("unable to find required guest file: {}", name))
|
2024-03-07 09:04:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 00:56:18 +00:00
|
|
|
pub async fn list(&self) -> Result<Vec<GuestInfo>> {
|
2024-03-05 11:35:25 +00:00
|
|
|
let mut guests: Vec<GuestInfo> = Vec::new();
|
|
|
|
for domid_candidate in self.xen.store.list("/local/domain").await? {
|
2024-03-13 13:05:17 +00:00
|
|
|
if domid_candidate == "0" {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-05 11:35:25 +00:00
|
|
|
let dom_path = format!("/local/domain/{}", domid_candidate);
|
|
|
|
let uuid_string = match self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/uuid", &dom_path))
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
None => continue,
|
|
|
|
Some(value) => value,
|
|
|
|
};
|
|
|
|
let domid =
|
|
|
|
u32::from_str(&domid_candidate).map_err(|_| anyhow!("failed to parse domid"))?;
|
|
|
|
let uuid = Uuid::from_str(&uuid_string)?;
|
2024-03-13 13:05:17 +00:00
|
|
|
|
|
|
|
let name = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/name", &dom_path))
|
|
|
|
.await?;
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
let image = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/image", &dom_path))
|
|
|
|
.await?
|
|
|
|
.unwrap_or("unknown".to_string());
|
|
|
|
let loops = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/loops", &dom_path))
|
|
|
|
.await?;
|
2024-03-27 02:54:39 +00:00
|
|
|
let guest_ipv4 = self
|
2024-03-05 11:35:25 +00:00
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/guest/ipv4", &dom_path))
|
|
|
|
.await?;
|
2024-03-27 02:54:39 +00:00
|
|
|
let guest_ipv6 = self
|
2024-03-05 11:35:25 +00:00
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/guest/ipv6", &dom_path))
|
|
|
|
.await?;
|
2024-03-27 02:54:39 +00:00
|
|
|
let guest_mac = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/guest/mac", &dom_path))
|
|
|
|
.await?;
|
|
|
|
let gateway_ipv4 = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/gateway/ipv4", &dom_path))
|
|
|
|
.await?;
|
|
|
|
let gateway_ipv6 = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/gateway/ipv6", &dom_path))
|
|
|
|
.await?;
|
|
|
|
let gateway_mac = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/network/gateway/mac", &dom_path))
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let guest_ipv4 = if let Some(guest_ipv4) = guest_ipv4 {
|
|
|
|
IpNetwork::from_str(&guest_ipv4).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let guest_ipv6 = if let Some(guest_ipv6) = guest_ipv6 {
|
|
|
|
IpNetwork::from_str(&guest_ipv6).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-03-05 11:35:25 +00:00
|
|
|
|
2024-03-27 02:54:39 +00:00
|
|
|
let gateway_ipv4 = if let Some(gateway_ipv4) = gateway_ipv4 {
|
|
|
|
IpNetwork::from_str(&gateway_ipv4).ok()
|
2024-03-05 11:35:25 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-03-27 02:54:39 +00:00
|
|
|
let gateway_ipv6 = if let Some(gateway_ipv6) = gateway_ipv6 {
|
|
|
|
IpNetwork::from_str(&gateway_ipv6).ok()
|
2024-03-05 11:35:25 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-03-06 15:57:56 +00:00
|
|
|
let exit_code = self
|
|
|
|
.xen
|
|
|
|
.store
|
|
|
|
.read_string(&format!("{}/krata/guest/exit-code", &dom_path))
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let exit_code: Option<i32> = match exit_code {
|
|
|
|
Some(code) => code.parse().ok(),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let state = GuestState { exit_code };
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
let loops = RuntimeContext::parse_loop_set(&loops);
|
|
|
|
guests.push(GuestInfo {
|
2024-03-13 13:05:17 +00:00
|
|
|
name,
|
2024-03-05 11:35:25 +00:00
|
|
|
uuid,
|
|
|
|
domid,
|
|
|
|
image,
|
|
|
|
loops,
|
2024-03-27 02:54:39 +00:00
|
|
|
guest_ipv4,
|
|
|
|
guest_ipv6,
|
|
|
|
guest_mac,
|
|
|
|
gateway_ipv4,
|
|
|
|
gateway_ipv6,
|
|
|
|
gateway_mac,
|
2024-03-06 15:57:56 +00:00
|
|
|
state,
|
2024-03-05 11:35:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(guests)
|
|
|
|
}
|
|
|
|
|
2024-04-02 00:56:18 +00:00
|
|
|
pub async fn resolve(&self, uuid: Uuid) -> Result<Option<GuestInfo>> {
|
2024-03-05 11:35:25 +00:00
|
|
|
for guest in self.list().await? {
|
2024-03-14 23:29:07 +00:00
|
|
|
if guest.uuid == uuid {
|
2024-03-05 11:35:25 +00:00
|
|
|
return Ok(Some(guest));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2024-03-30 03:49:13 +00:00
|
|
|
fn parse_loop_set(input: &Option<String>) -> Vec<GuestLoopInfo> {
|
2024-03-05 11:35:25 +00:00
|
|
|
let Some(input) = input else {
|
|
|
|
return Vec::new();
|
|
|
|
};
|
|
|
|
let sets = input
|
|
|
|
.split(',')
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.map(|x| x.split(':').map(|v| v.to_string()).collect::<Vec<String>>())
|
|
|
|
.map(|x| (x[0].clone(), x[1].clone(), x[2].clone()))
|
|
|
|
.collect::<Vec<(String, String, String)>>();
|
|
|
|
sets.iter()
|
2024-03-30 03:49:13 +00:00
|
|
|
.map(|(device, file, delete)| GuestLoopInfo {
|
2024-03-05 11:35:25 +00:00
|
|
|
device: device.clone(),
|
|
|
|
file: file.clone(),
|
|
|
|
delete: if delete == "none" {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(delete.clone())
|
|
|
|
},
|
|
|
|
})
|
2024-03-30 03:49:13 +00:00
|
|
|
.collect::<Vec<GuestLoopInfo>>()
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Runtime {
|
2024-03-06 15:57:56 +00:00
|
|
|
store: Arc<String>,
|
2024-04-02 00:56:18 +00:00
|
|
|
context: RuntimeContext,
|
|
|
|
launch_semaphore: Arc<Semaphore>,
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Runtime {
|
|
|
|
pub async fn new(store: String) -> Result<Self> {
|
2024-03-06 15:57:56 +00:00
|
|
|
let context = RuntimeContext::new(store.clone()).await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
Ok(Self {
|
2024-03-06 15:57:56 +00:00
|
|
|
store: Arc::new(store),
|
2024-04-02 00:56:18 +00:00
|
|
|
context,
|
|
|
|
launch_semaphore: Arc::new(Semaphore::new(1)),
|
2024-03-05 11:35:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn launch<'a>(&self, request: GuestLaunchRequest<'a>) -> Result<GuestInfo> {
|
2024-04-02 00:56:18 +00:00
|
|
|
let mut launcher = GuestLauncher::new(self.launch_semaphore.clone())?;
|
|
|
|
launcher.launch(&self.context, request).await
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 23:29:07 +00:00
|
|
|
pub async fn destroy(&self, uuid: Uuid) -> Result<Uuid> {
|
2024-04-02 00:56:18 +00:00
|
|
|
let info = self
|
|
|
|
.context
|
2024-03-14 23:29:07 +00:00
|
|
|
.resolve(uuid)
|
2024-03-05 11:35:25 +00:00
|
|
|
.await?
|
2024-03-14 23:29:07 +00:00
|
|
|
.ok_or_else(|| anyhow!("unable to resolve guest: {}", uuid))?;
|
2024-03-05 11:35:25 +00:00
|
|
|
let domid = info.domid;
|
2024-04-02 00:56:18 +00:00
|
|
|
let store = XsdClient::open().await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
let dom_path = store.get_domain_path(domid).await?;
|
|
|
|
let uuid = match store
|
|
|
|
.read_string(format!("{}/krata/uuid", dom_path).as_str())
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
None => {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"domain {} was not found or not created by krata",
|
|
|
|
domid
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Some(value) => value,
|
|
|
|
};
|
|
|
|
if uuid.is_empty() {
|
|
|
|
return Err(anyhow!("unable to find krata uuid based on the domain",));
|
|
|
|
}
|
|
|
|
let uuid = Uuid::parse_str(&uuid)?;
|
|
|
|
let loops = store
|
|
|
|
.read_string(format!("{}/krata/loops", dom_path).as_str())
|
|
|
|
.await?;
|
|
|
|
let loops = RuntimeContext::parse_loop_set(&loops);
|
2024-04-02 00:56:18 +00:00
|
|
|
self.context.xen.destroy(domid).await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
for info in &loops {
|
2024-04-02 00:56:18 +00:00
|
|
|
self.context.autoloop.unloop(&info.device).await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
match &info.delete {
|
|
|
|
None => {}
|
|
|
|
Some(delete) => {
|
|
|
|
let delete_path = PathBuf::from(delete);
|
|
|
|
if delete_path.is_file() || delete_path.is_symlink() {
|
|
|
|
fs::remove_file(&delete_path)?;
|
|
|
|
} else if delete_path.is_dir() {
|
|
|
|
fs::remove_dir_all(&delete_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(uuid)
|
|
|
|
}
|
|
|
|
|
2024-03-14 23:29:07 +00:00
|
|
|
pub async fn console(&self, uuid: Uuid) -> Result<XenConsole> {
|
2024-04-02 00:56:18 +00:00
|
|
|
let info = self
|
|
|
|
.context
|
2024-03-14 23:29:07 +00:00
|
|
|
.resolve(uuid)
|
2024-03-05 11:35:25 +00:00
|
|
|
.await?
|
2024-03-14 23:29:07 +00:00
|
|
|
.ok_or_else(|| anyhow!("unable to resolve guest: {}", uuid))?;
|
2024-03-05 11:35:25 +00:00
|
|
|
let domid = info.domid;
|
2024-04-02 00:56:18 +00:00
|
|
|
let tty = self.context.xen.get_console_path(domid).await?;
|
2024-03-05 11:35:25 +00:00
|
|
|
XenConsole::new(&tty).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list(&self) -> Result<Vec<GuestInfo>> {
|
2024-04-02 00:56:18 +00:00
|
|
|
self.context.list().await
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
2024-03-06 15:57:56 +00:00
|
|
|
|
|
|
|
pub async fn dupe(&self) -> Result<Runtime> {
|
|
|
|
Runtime::new((*self.store).clone()).await
|
|
|
|
}
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
2024-03-07 09:04:05 +00:00
|
|
|
|
|
|
|
fn path_as_string(path: &Path) -> Result<String> {
|
|
|
|
path.to_str()
|
|
|
|
.ok_or_else(|| anyhow!("unable to convert path to string"))
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
}
|