2024-03-24 05:25:48 +00:00
|
|
|
use std::collections::HashMap;
|
2024-05-14 11:29:12 -07:00
|
|
|
use std::fs;
|
2024-04-29 10:02:20 -07:00
|
|
|
use std::path::PathBuf;
|
2024-04-02 00:56:18 +00:00
|
|
|
use std::sync::Arc;
|
2024-02-29 12:52:44 +00:00
|
|
|
|
|
|
|
use advmac::MacAddr6;
|
|
|
|
use anyhow::{anyhow, Result};
|
2024-08-13 23:17:47 -07:00
|
|
|
use tokio::sync::Semaphore;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
use krata::launchcfg::{
|
2024-02-29 12:52:44 +00:00
|
|
|
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
2024-04-14 17:19:38 -07:00
|
|
|
LaunchPackedFormat, LaunchRoot,
|
2024-02-29 12:52:44 +00:00
|
|
|
};
|
2024-04-16 01:53:44 -07:00
|
|
|
use krataoci::packer::OciPackedImage;
|
2024-08-13 23:17:47 -07:00
|
|
|
pub use xenclient::{
|
|
|
|
pci::PciBdf, DomainPciDevice as PciDevice, DomainPciRdmReservePolicy as PciRdmReservePolicy,
|
|
|
|
};
|
2024-03-28 07:36:48 +00:00
|
|
|
use xenclient::{DomainChannel, DomainConfig, DomainDisk, DomainNetworkInterface};
|
2024-06-21 10:38:19 -07:00
|
|
|
use xenplatform::domain::BaseDomainConfig;
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-03-07 18:04:22 +00:00
|
|
|
use crate::cfgblk::ConfigBlock;
|
2024-03-25 02:37:02 +00:00
|
|
|
use crate::RuntimeContext;
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
use super::{ZoneInfo, ZoneState};
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub struct ZoneLaunchRequest {
|
2024-04-14 17:19:38 -07:00
|
|
|
pub format: LaunchPackedFormat,
|
2024-04-22 12:48:45 -07:00
|
|
|
pub kernel: Vec<u8>,
|
|
|
|
pub initrd: Vec<u8>,
|
2024-03-14 14:03:11 +00:00
|
|
|
pub uuid: Option<Uuid>,
|
2024-04-15 10:24:14 -07:00
|
|
|
pub name: Option<String>,
|
2024-08-15 00:58:44 -07:00
|
|
|
pub target_cpus: u32,
|
|
|
|
pub max_cpus: u32,
|
2024-08-14 01:14:49 -07:00
|
|
|
pub target_memory: u64,
|
|
|
|
pub max_memory: u64,
|
2024-03-24 05:25:48 +00:00
|
|
|
pub env: HashMap<String, String>,
|
2024-02-29 12:52:44 +00:00
|
|
|
pub run: Option<Vec<String>>,
|
2024-04-29 10:02:20 -07:00
|
|
|
pub pcis: Vec<PciDevice>,
|
2024-02-29 12:52:44 +00:00
|
|
|
pub debug: bool,
|
2024-04-16 01:53:44 -07:00
|
|
|
pub image: OciPackedImage,
|
2024-04-29 10:02:20 -07:00
|
|
|
pub addons_image: Option<PathBuf>,
|
2024-08-13 23:17:47 -07:00
|
|
|
pub network: ZoneLaunchNetwork,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ZoneLaunchNetwork {
|
|
|
|
pub ipv4: String,
|
|
|
|
pub ipv4_prefix: u8,
|
|
|
|
pub ipv6: String,
|
|
|
|
pub ipv6_prefix: u8,
|
|
|
|
pub gateway_ipv4: String,
|
|
|
|
pub gateway_ipv6: String,
|
|
|
|
pub zone_mac: MacAddr6,
|
|
|
|
pub nameservers: Vec<String>,
|
2024-02-29 12:52:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub struct ZoneLauncher {
|
2024-04-02 00:56:18 +00:00
|
|
|
pub launch_semaphore: Arc<Semaphore>,
|
|
|
|
}
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
impl ZoneLauncher {
|
2024-04-02 00:56:18 +00:00
|
|
|
pub fn new(launch_semaphore: Arc<Semaphore>) -> Result<Self> {
|
|
|
|
Ok(Self { launch_semaphore })
|
2024-02-29 12:52:44 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 10:24:14 -07:00
|
|
|
pub async fn launch(
|
2024-02-29 12:52:44 +00:00
|
|
|
&mut self,
|
2024-04-02 00:56:18 +00:00
|
|
|
context: &RuntimeContext,
|
2024-07-18 20:47:18 -07:00
|
|
|
request: ZoneLaunchRequest,
|
|
|
|
) -> Result<ZoneInfo> {
|
2024-03-14 14:03:11 +00:00
|
|
|
let uuid = request.uuid.unwrap_or_else(Uuid::new_v4);
|
2024-03-14 23:29:07 +00:00
|
|
|
let xen_name = format!("krata-{uuid}");
|
2024-04-02 00:56:18 +00:00
|
|
|
let _launch_permit = self.launch_semaphore.acquire().await?;
|
2024-02-29 12:52:44 +00:00
|
|
|
let launch_config = LaunchInfo {
|
2024-04-14 17:19:38 -07:00
|
|
|
root: LaunchRoot {
|
|
|
|
format: request.format.clone(),
|
|
|
|
},
|
2024-03-31 03:18:56 +00:00
|
|
|
hostname: Some(
|
|
|
|
request
|
|
|
|
.name
|
2024-04-15 10:24:14 -07:00
|
|
|
.as_ref()
|
2024-03-31 03:18:56 +00:00
|
|
|
.map(|x| x.to_string())
|
|
|
|
.unwrap_or_else(|| format!("krata-{}", uuid)),
|
|
|
|
),
|
2024-02-29 12:52:44 +00:00
|
|
|
network: Some(LaunchNetwork {
|
|
|
|
link: "eth0".to_string(),
|
|
|
|
ipv4: LaunchNetworkIpv4 {
|
2024-08-13 23:17:47 -07:00
|
|
|
address: format!("{}/{}", request.network.ipv4, request.network.ipv4_prefix),
|
|
|
|
gateway: request.network.gateway_ipv4,
|
2024-02-29 12:52:44 +00:00
|
|
|
},
|
|
|
|
ipv6: LaunchNetworkIpv6 {
|
2024-08-13 23:17:47 -07:00
|
|
|
address: format!("{}/{}", request.network.ipv6, request.network.ipv6_prefix),
|
|
|
|
gateway: request.network.gateway_ipv6.to_string(),
|
2024-02-29 12:52:44 +00:00
|
|
|
},
|
|
|
|
resolver: LaunchNetworkResolver {
|
2024-08-13 23:17:47 -07:00
|
|
|
nameservers: request.network.nameservers,
|
2024-02-29 12:52:44 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
env: request.env,
|
|
|
|
run: request.run,
|
|
|
|
};
|
|
|
|
|
2024-04-29 10:02:20 -07:00
|
|
|
let cfgblk = ConfigBlock::new(&uuid, request.image.clone())?;
|
|
|
|
let cfgblk_file = cfgblk.file.clone();
|
|
|
|
let cfgblk_dir = cfgblk.dir.clone();
|
|
|
|
tokio::task::spawn_blocking(move || cfgblk.build(&launch_config)).await??;
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-04-15 10:24:14 -07:00
|
|
|
let image_squashfs_path = request
|
2024-04-14 17:19:38 -07:00
|
|
|
.image
|
2024-04-15 10:24:14 -07:00
|
|
|
.path
|
2024-02-29 12:52:44 +00:00
|
|
|
.to_str()
|
2024-04-14 17:19:38 -07:00
|
|
|
.ok_or_else(|| anyhow!("failed to convert image path to string"))?;
|
2024-02-29 12:52:44 +00:00
|
|
|
|
2024-04-29 10:02:20 -07:00
|
|
|
let cfgblk_dir_path = cfgblk_dir
|
2024-02-29 12:52:44 +00:00
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| anyhow!("failed to convert cfgblk directory path to string"))?;
|
2024-04-29 10:02:20 -07:00
|
|
|
let cfgblk_squashfs_path = cfgblk_file
|
2024-02-29 12:52:44 +00:00
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| anyhow!("failed to convert cfgblk squashfs path to string"))?;
|
2024-04-29 10:02:20 -07:00
|
|
|
let addons_squashfs_path = request
|
|
|
|
.addons_image
|
|
|
|
.map(|x| x.to_str().map(|x| x.to_string()))
|
|
|
|
.map(|x| {
|
|
|
|
Some(x.ok_or_else(|| anyhow!("failed to convert addons squashfs path to string")))
|
|
|
|
})
|
|
|
|
.unwrap_or(None);
|
|
|
|
|
|
|
|
let addons_squashfs_path = if let Some(path) = addons_squashfs_path {
|
|
|
|
Some(path?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-02-29 12:52:44 +00:00
|
|
|
|
|
|
|
let image_squashfs_loop = context.autoloop.loopify(image_squashfs_path)?;
|
|
|
|
let cfgblk_squashfs_loop = context.autoloop.loopify(cfgblk_squashfs_path)?;
|
2024-04-29 10:02:20 -07:00
|
|
|
let addons_squashfs_loop = if let Some(ref addons_squashfs_path) = addons_squashfs_path {
|
|
|
|
Some(context.autoloop.loopify(addons_squashfs_path)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-06-20 19:42:45 -07:00
|
|
|
let mut cmdline_options = ["console=hvc0"].to_vec();
|
|
|
|
if !request.debug {
|
|
|
|
cmdline_options.push("quiet");
|
|
|
|
}
|
2024-02-29 12:52:44 +00:00
|
|
|
let cmdline = cmdline_options.join(" ");
|
|
|
|
|
2024-08-13 23:17:47 -07:00
|
|
|
let zone_mac_string = request.network.zone_mac.to_string().replace('-', ":");
|
2024-03-13 13:05:17 +00:00
|
|
|
|
2024-04-29 10:02:20 -07:00
|
|
|
let mut disks = vec![
|
|
|
|
DomainDisk {
|
|
|
|
vdev: "xvda".to_string(),
|
|
|
|
block: image_squashfs_loop.clone(),
|
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
DomainDisk {
|
|
|
|
vdev: "xvdb".to_string(),
|
|
|
|
block: cfgblk_squashfs_loop.clone(),
|
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if let Some(ref addons) = addons_squashfs_loop {
|
|
|
|
disks.push(DomainDisk {
|
|
|
|
vdev: "xvdc".to_string(),
|
|
|
|
block: addons.clone(),
|
|
|
|
writable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut loops = vec![
|
|
|
|
format!("{}:{}:none", image_squashfs_loop.path, image_squashfs_path),
|
|
|
|
format!(
|
|
|
|
"{}:{}:{}",
|
|
|
|
cfgblk_squashfs_loop.path, cfgblk_squashfs_path, cfgblk_dir_path
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
if let Some(ref addons) = addons_squashfs_loop {
|
|
|
|
loops.push(format!(
|
|
|
|
"{}:{}:none",
|
|
|
|
addons.path,
|
|
|
|
addons_squashfs_path
|
|
|
|
.clone()
|
|
|
|
.ok_or_else(|| anyhow!("addons squashfs path missing"))?
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:05:17 +00:00
|
|
|
let mut extra_keys = vec![
|
|
|
|
("krata/uuid".to_string(), uuid.to_string()),
|
2024-04-29 10:02:20 -07:00
|
|
|
("krata/loops".to_string(), loops.join(",")),
|
2024-03-13 13:05:17 +00:00
|
|
|
];
|
|
|
|
|
2024-04-15 10:24:14 -07:00
|
|
|
if let Some(name) = request.name.as_ref() {
|
|
|
|
extra_keys.push(("krata/name".to_string(), name.clone()));
|
2024-03-13 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 12:52:44 +00:00
|
|
|
let config = DomainConfig {
|
2024-06-21 10:38:19 -07:00
|
|
|
base: BaseDomainConfig {
|
2024-08-15 00:58:44 -07:00
|
|
|
max_vcpus: request.max_cpus,
|
|
|
|
target_vcpus: request.target_cpus,
|
2024-08-14 01:14:49 -07:00
|
|
|
max_mem_mb: request.max_memory,
|
|
|
|
target_mem_mb: request.target_memory,
|
2024-06-21 10:38:19 -07:00
|
|
|
kernel: request.kernel,
|
|
|
|
initrd: request.initrd,
|
|
|
|
cmdline,
|
|
|
|
uuid,
|
|
|
|
owner_domid: 0,
|
2024-07-09 21:02:53 -07:00
|
|
|
enable_iommu: !request.pcis.is_empty(),
|
2024-06-21 10:38:19 -07:00
|
|
|
},
|
2024-02-29 12:52:44 +00:00
|
|
|
backend_domid: 0,
|
2024-04-22 12:48:45 -07:00
|
|
|
name: xen_name,
|
2024-06-20 19:42:45 -07:00
|
|
|
swap_console_backend: Some("krata-console".to_string()),
|
2024-04-29 10:02:20 -07:00
|
|
|
disks,
|
2024-03-28 07:36:48 +00:00
|
|
|
channels: vec![DomainChannel {
|
|
|
|
typ: "krata-channel".to_string(),
|
|
|
|
initialized: false,
|
|
|
|
}],
|
2024-02-29 12:52:44 +00:00
|
|
|
vifs: vec![DomainNetworkInterface {
|
2024-07-18 20:47:18 -07:00
|
|
|
mac: zone_mac_string.clone(),
|
2024-02-29 12:52:44 +00:00
|
|
|
mtu: 1500,
|
|
|
|
bridge: None,
|
|
|
|
script: None,
|
|
|
|
}],
|
2024-04-29 10:02:20 -07:00
|
|
|
pcis: request.pcis.clone(),
|
2024-02-29 12:52:44 +00:00
|
|
|
filesystems: vec![],
|
2024-03-13 13:05:17 +00:00
|
|
|
extra_keys,
|
2024-07-18 20:47:18 -07:00
|
|
|
extra_rw_paths: vec!["krata/zone".to_string()],
|
2024-02-29 12:52:44 +00:00
|
|
|
};
|
|
|
|
match context.xen.create(&config).await {
|
2024-08-13 23:17:47 -07:00
|
|
|
Ok(created) => Ok(ZoneInfo {
|
|
|
|
name: request.name.as_ref().map(|x| x.to_string()),
|
|
|
|
uuid,
|
|
|
|
domid: created.domid,
|
|
|
|
image: request.image.digest,
|
|
|
|
loops: vec![],
|
|
|
|
state: ZoneState { exit_code: None },
|
|
|
|
}),
|
2024-02-29 12:52:44 +00:00
|
|
|
Err(error) => {
|
2024-04-02 00:56:18 +00:00
|
|
|
let _ = context.autoloop.unloop(&image_squashfs_loop.path).await;
|
|
|
|
let _ = context.autoloop.unloop(&cfgblk_squashfs_loop.path).await;
|
2024-04-29 10:02:20 -07:00
|
|
|
let _ = fs::remove_dir(&cfgblk_dir);
|
2024-02-29 12:52:44 +00:00
|
|
|
Err(error.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|