mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 13:11:31 +00:00
krata: implement guest tab for automatic guest startup
This commit is contained in:
@ -22,6 +22,7 @@ use crate::RuntimeContext;
|
||||
use super::{GuestInfo, GuestState};
|
||||
|
||||
pub struct GuestLaunchRequest<'a> {
|
||||
pub name: Option<&'a str>,
|
||||
pub image: &'a str,
|
||||
pub vcpus: u32,
|
||||
pub mem: u64,
|
||||
@ -112,6 +113,51 @@ impl GuestLauncher {
|
||||
|
||||
let container_mac_string = container_mac.to_string().replace('-', ":");
|
||||
let gateway_mac_string = gateway_mac.to_string().replace('-', ":");
|
||||
|
||||
let mut extra_keys = vec![
|
||||
("krata/uuid".to_string(), uuid.to_string()),
|
||||
(
|
||||
"krata/loops".to_string(),
|
||||
format!(
|
||||
"{}:{}:none,{}:{}:{}",
|
||||
&image_squashfs_loop.path,
|
||||
image_squashfs_path,
|
||||
&cfgblk_squashfs_loop.path,
|
||||
cfgblk_squashfs_path,
|
||||
cfgblk_dir_path,
|
||||
),
|
||||
),
|
||||
("krata/image".to_string(), request.image.to_string()),
|
||||
(
|
||||
"krata/network/guest/ipv4".to_string(),
|
||||
format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/guest/ipv6".to_string(),
|
||||
format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/guest/mac".to_string(),
|
||||
container_mac_string.clone(),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/ipv4".to_string(),
|
||||
format!("{}/{}", gateway_ipv4, ipv4_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/ipv6".to_string(),
|
||||
format!("{}/{}", gateway_ipv6, ipv6_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/mac".to_string(),
|
||||
gateway_mac_string.clone(),
|
||||
),
|
||||
];
|
||||
|
||||
if let Some(name) = request.name {
|
||||
extra_keys.push(("krata/name".to_string(), name.to_string()));
|
||||
}
|
||||
|
||||
let config = DomainConfig {
|
||||
backend_domid: 0,
|
||||
name: &name,
|
||||
@ -141,49 +187,12 @@ impl GuestLauncher {
|
||||
}],
|
||||
filesystems: vec![],
|
||||
event_channels: vec![],
|
||||
extra_keys: vec![
|
||||
("krata/uuid".to_string(), uuid.to_string()),
|
||||
(
|
||||
"krata/loops".to_string(),
|
||||
format!(
|
||||
"{}:{}:none,{}:{}:{}",
|
||||
&image_squashfs_loop.path,
|
||||
image_squashfs_path,
|
||||
&cfgblk_squashfs_loop.path,
|
||||
cfgblk_squashfs_path,
|
||||
cfgblk_dir_path,
|
||||
),
|
||||
),
|
||||
("krata/image".to_string(), request.image.to_string()),
|
||||
(
|
||||
"krata/network/guest/ipv4".to_string(),
|
||||
format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/guest/ipv6".to_string(),
|
||||
format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/guest/mac".to_string(),
|
||||
container_mac_string.clone(),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/ipv4".to_string(),
|
||||
format!("{}/{}", gateway_ipv4, ipv4_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/ipv6".to_string(),
|
||||
format!("{}/{}", gateway_ipv6, ipv6_network_mask),
|
||||
),
|
||||
(
|
||||
"krata/network/gateway/mac".to_string(),
|
||||
gateway_mac_string.clone(),
|
||||
),
|
||||
],
|
||||
extra_keys,
|
||||
extra_rw_paths: vec!["krata/guest".to_string()],
|
||||
};
|
||||
match context.xen.create(&config).await {
|
||||
Ok(domid) => Ok(GuestInfo {
|
||||
name: request.name.map(|x| x.to_string()),
|
||||
uuid,
|
||||
domid,
|
||||
image: request.image.to_string(),
|
||||
|
@ -37,6 +37,7 @@ pub struct GuestState {
|
||||
}
|
||||
|
||||
pub struct GuestInfo {
|
||||
pub name: Option<String>,
|
||||
pub uuid: Uuid,
|
||||
pub domid: u32,
|
||||
pub image: String,
|
||||
@ -92,6 +93,9 @@ impl RuntimeContext {
|
||||
pub async fn list(&mut self) -> Result<Vec<GuestInfo>> {
|
||||
let mut guests: Vec<GuestInfo> = Vec::new();
|
||||
for domid_candidate in self.xen.store.list("/local/domain").await? {
|
||||
if domid_candidate == "0" {
|
||||
continue;
|
||||
}
|
||||
let dom_path = format!("/local/domain/{}", domid_candidate);
|
||||
let uuid_string = match self
|
||||
.xen
|
||||
@ -105,6 +109,13 @@ impl RuntimeContext {
|
||||
let domid =
|
||||
u32::from_str(&domid_candidate).map_err(|_| anyhow!("failed to parse domid"))?;
|
||||
let uuid = Uuid::from_str(&uuid_string)?;
|
||||
|
||||
let name = self
|
||||
.xen
|
||||
.store
|
||||
.read_string(&format!("{}/krata/name", &dom_path))
|
||||
.await?;
|
||||
|
||||
let image = self
|
||||
.xen
|
||||
.store
|
||||
@ -154,6 +165,7 @@ impl RuntimeContext {
|
||||
|
||||
let loops = RuntimeContext::parse_loop_set(&loops);
|
||||
guests.push(GuestInfo {
|
||||
name,
|
||||
uuid,
|
||||
domid,
|
||||
image,
|
||||
@ -170,6 +182,13 @@ impl RuntimeContext {
|
||||
for guest in self.list().await? {
|
||||
let uuid_string = guest.uuid.to_string();
|
||||
let domid_string = guest.domid.to_string();
|
||||
|
||||
if let Some(ref name) = guest.name {
|
||||
if name == id {
|
||||
return Ok(Some(guest));
|
||||
}
|
||||
}
|
||||
|
||||
if uuid_string == id || domid_string == id || id == format!("krata-{}", uuid_string) {
|
||||
return Ok(Some(guest));
|
||||
}
|
||||
|
Reference in New Issue
Block a user