mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 13:11:31 +00:00
controller: move guest launch to module
This commit is contained in:
@ -1,11 +1,12 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use kratactrl::ctl::{
|
use kratactrl::{
|
||||||
console::ControllerConsole,
|
ctl::{
|
||||||
destroy::ControllerDestroy,
|
console::ControllerConsole, destroy::ControllerDestroy, launch::ControllerLaunch,
|
||||||
launch::{ControllerLaunch, ControllerLaunchRequest},
|
|
||||||
ControllerContext,
|
ControllerContext,
|
||||||
|
},
|
||||||
|
launch::GuestLaunchRequest,
|
||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@ -86,7 +87,7 @@ async fn main() -> Result<()> {
|
|||||||
let kernel = map_kernel_path(&store_path, kernel);
|
let kernel = map_kernel_path(&store_path, kernel);
|
||||||
let initrd = map_initrd_path(&store_path, initrd);
|
let initrd = map_initrd_path(&store_path, initrd);
|
||||||
let mut launch = ControllerLaunch::new(&mut context);
|
let mut launch = ControllerLaunch::new(&mut context);
|
||||||
let request = ControllerLaunchRequest {
|
let request = GuestLaunchRequest {
|
||||||
kernel_path: &kernel,
|
kernel_path: &kernel,
|
||||||
initrd_path: &initrd,
|
initrd_path: &initrd,
|
||||||
image: &image,
|
image: &image,
|
||||||
@ -96,11 +97,11 @@ async fn main() -> Result<()> {
|
|||||||
run: if run.is_empty() { None } else { Some(run) },
|
run: if run.is_empty() { None } else { Some(run) },
|
||||||
debug,
|
debug,
|
||||||
};
|
};
|
||||||
let (uuid, _domid) = launch.perform(request).await?;
|
let info = launch.perform(request).await?;
|
||||||
println!("launched container: {}", uuid);
|
println!("launched guest: {}", info.uuid);
|
||||||
if attach {
|
if attach {
|
||||||
let mut console = ControllerConsole::new(&mut context);
|
let mut console = ControllerConsole::new(&mut context);
|
||||||
console.perform(&uuid.to_string()).await?;
|
console.perform(&info.uuid.to_string()).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +131,7 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if table.num_records() == 1 {
|
if table.num_records() == 1 {
|
||||||
println!("no containers have been launched");
|
println!("no guests have been launched");
|
||||||
} else {
|
} else {
|
||||||
println!("{}", table.to_string());
|
println!("{}", table.to_string());
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ impl ConfigBlock<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(&self, launch_config: &LaunchInfo) -> Result<()> {
|
pub fn build(&self, launch_config: &LaunchInfo) -> Result<()> {
|
||||||
trace!("ConfigBlock build launch_config={:?}", launch_config);
|
trace!("build launch_config={:?}", launch_config);
|
||||||
let manifest = self.image_info.config.to_string()?;
|
let manifest = self.image_info.config.to_string()?;
|
||||||
let launch = serde_json::to_string(launch_config)?;
|
let launch = serde_json::to_string(launch_config)?;
|
||||||
let mut writer = FilesystemWriter::default();
|
let mut writer = FilesystemWriter::default();
|
||||||
@ -63,9 +63,9 @@ impl ConfigBlock<'_> {
|
|||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
let mut file = File::create(&self.file)?;
|
let mut file = File::create(&self.file)?;
|
||||||
trace!("ConfigBlock build write sqaushfs");
|
trace!("build write sqaushfs");
|
||||||
writer.write(&mut file)?;
|
writer.write(&mut file)?;
|
||||||
trace!("ConfigBlock build complete");
|
trace!("build complete");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ impl ControllerConsole<'_> {
|
|||||||
.context
|
.context
|
||||||
.resolve(id)
|
.resolve(id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
.ok_or_else(|| anyhow!("unable to resolve guest: {}", id))?;
|
||||||
let domid = info.domid;
|
let domid = info.domid;
|
||||||
let tty = self.context.xen.get_console_path(domid).await?;
|
let tty = self.context.xen.get_console_path(domid).await?;
|
||||||
let console = XenConsole::new(&tty).await?;
|
let console = XenConsole::new(&tty).await?;
|
||||||
|
@ -20,7 +20,7 @@ impl ControllerDestroy<'_> {
|
|||||||
.context
|
.context
|
||||||
.resolve(id)
|
.resolve(id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
.ok_or_else(|| anyhow!("unable to resolve guest: {}", id))?;
|
||||||
let domid = info.domid;
|
let domid = info.domid;
|
||||||
let mut store = XsdClient::open().await?;
|
let mut store = XsdClient::open().await?;
|
||||||
let dom_path = store.get_domain_path(domid).await?;
|
let dom_path = store.get_domain_path(domid).await?;
|
||||||
|
@ -1,29 +1,6 @@
|
|||||||
use std::{fs, net::Ipv4Addr, str::FromStr};
|
use super::{ControllerContext, GuestInfo};
|
||||||
|
use crate::launch::{GuestLaunchRequest, GuestLauncher};
|
||||||
use advmac::MacAddr6;
|
use anyhow::Result;
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
use ipnetwork::Ipv4Network;
|
|
||||||
use krata::{
|
|
||||||
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
|
||||||
};
|
|
||||||
use uuid::Uuid;
|
|
||||||
use xenclient::{DomainConfig, DomainDisk, DomainNetworkInterface};
|
|
||||||
use xenstore::client::XsdInterface;
|
|
||||||
|
|
||||||
use crate::image::{name::ImageName, ImageCompiler, ImageInfo};
|
|
||||||
|
|
||||||
use super::{cfgblk::ConfigBlock, ControllerContext};
|
|
||||||
|
|
||||||
pub struct ControllerLaunchRequest<'a> {
|
|
||||||
pub kernel_path: &'a str,
|
|
||||||
pub initrd_path: &'a str,
|
|
||||||
pub image: &'a str,
|
|
||||||
pub vcpus: u32,
|
|
||||||
pub mem: u64,
|
|
||||||
pub env: Option<Vec<String>>,
|
|
||||||
pub run: Option<Vec<String>>,
|
|
||||||
pub debug: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ControllerLaunch<'a> {
|
pub struct ControllerLaunch<'a> {
|
||||||
context: &'a mut ControllerContext,
|
context: &'a mut ControllerContext,
|
||||||
@ -34,195 +11,11 @@ impl ControllerLaunch<'_> {
|
|||||||
ControllerLaunch { context }
|
ControllerLaunch { context }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn perform(&mut self, request: ControllerLaunchRequest<'_>) -> Result<(Uuid, u32)> {
|
pub async fn perform<'c, 'r>(
|
||||||
let uuid = Uuid::new_v4();
|
&'c mut self,
|
||||||
let name = format!("krata-{uuid}");
|
request: GuestLaunchRequest<'r>,
|
||||||
let image_info = self.compile(request.image).await?;
|
) -> Result<GuestInfo> {
|
||||||
|
let mut launcher = GuestLauncher::new()?;
|
||||||
let mut gateway_mac = MacAddr6::random();
|
launcher.launch(self.context, request).await
|
||||||
gateway_mac.set_local(true);
|
|
||||||
gateway_mac.set_multicast(false);
|
|
||||||
let mut container_mac = MacAddr6::random();
|
|
||||||
container_mac.set_local(true);
|
|
||||||
container_mac.set_multicast(false);
|
|
||||||
|
|
||||||
let guest_ipv4 = self.allocate_ipv4().await?;
|
|
||||||
let guest_ipv6 = container_mac.to_link_local_ipv6();
|
|
||||||
let gateway_ipv4 = "192.168.42.1";
|
|
||||||
let gateway_ipv6 = "fe80::1";
|
|
||||||
let ipv4_network_mask: u32 = 24;
|
|
||||||
let ipv6_network_mask: u32 = 10;
|
|
||||||
|
|
||||||
let launch_config = LaunchInfo {
|
|
||||||
network: Some(LaunchNetwork {
|
|
||||||
link: "eth0".to_string(),
|
|
||||||
ipv4: LaunchNetworkIpv4 {
|
|
||||||
address: format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
|
||||||
gateway: gateway_ipv4.to_string(),
|
|
||||||
},
|
|
||||||
ipv6: LaunchNetworkIpv6 {
|
|
||||||
address: format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
|
||||||
gateway: gateway_ipv6.to_string(),
|
|
||||||
},
|
|
||||||
resolver: LaunchNetworkResolver {
|
|
||||||
nameservers: vec![
|
|
||||||
"1.1.1.1".to_string(),
|
|
||||||
"1.0.0.1".to_string(),
|
|
||||||
"2606:4700:4700::1111".to_string(),
|
|
||||||
"2606:4700:4700::1001".to_string(),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
env: request.env,
|
|
||||||
run: request.run,
|
|
||||||
};
|
|
||||||
|
|
||||||
let cfgblk = ConfigBlock::new(&uuid, &image_info)?;
|
|
||||||
cfgblk.build(&launch_config)?;
|
|
||||||
|
|
||||||
let image_squashfs_path = image_info
|
|
||||||
.image_squashfs
|
|
||||||
.to_str()
|
|
||||||
.ok_or_else(|| anyhow!("failed to convert image squashfs path to string"))?;
|
|
||||||
|
|
||||||
let cfgblk_dir_path = cfgblk
|
|
||||||
.dir
|
|
||||||
.to_str()
|
|
||||||
.ok_or_else(|| anyhow!("failed to convert cfgblk directory path to string"))?;
|
|
||||||
let cfgblk_squashfs_path = cfgblk
|
|
||||||
.file
|
|
||||||
.to_str()
|
|
||||||
.ok_or_else(|| anyhow!("failed to convert cfgblk squashfs path to string"))?;
|
|
||||||
|
|
||||||
let image_squashfs_loop = self.context.autoloop.loopify(image_squashfs_path)?;
|
|
||||||
let cfgblk_squashfs_loop = self.context.autoloop.loopify(cfgblk_squashfs_path)?;
|
|
||||||
|
|
||||||
let cmdline_options = [
|
|
||||||
if request.debug { "debug" } else { "quiet" },
|
|
||||||
"elevator=noop",
|
|
||||||
];
|
|
||||||
let cmdline = cmdline_options.join(" ");
|
|
||||||
|
|
||||||
let container_mac_string = container_mac.to_string().replace('-', ":");
|
|
||||||
let gateway_mac_string = gateway_mac.to_string().replace('-', ":");
|
|
||||||
let config = DomainConfig {
|
|
||||||
backend_domid: 0,
|
|
||||||
name: &name,
|
|
||||||
max_vcpus: request.vcpus,
|
|
||||||
mem_mb: request.mem,
|
|
||||||
kernel_path: request.kernel_path,
|
|
||||||
initrd_path: request.initrd_path,
|
|
||||||
cmdline: &cmdline,
|
|
||||||
disks: vec![
|
|
||||||
DomainDisk {
|
|
||||||
vdev: "xvda",
|
|
||||||
block: &image_squashfs_loop,
|
|
||||||
writable: false,
|
|
||||||
},
|
|
||||||
DomainDisk {
|
|
||||||
vdev: "xvdb",
|
|
||||||
block: &cfgblk_squashfs_loop,
|
|
||||||
writable: false,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
consoles: vec![],
|
|
||||||
vifs: vec![DomainNetworkInterface {
|
|
||||||
mac: &container_mac_string,
|
|
||||||
mtu: 1500,
|
|
||||||
bridge: None,
|
|
||||||
script: None,
|
|
||||||
}],
|
|
||||||
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_rw_paths: vec!["krata/guest".to_string()],
|
|
||||||
};
|
|
||||||
match self.context.xen.create(&config).await {
|
|
||||||
Ok(domid) => Ok((uuid, domid)),
|
|
||||||
Err(error) => {
|
|
||||||
let _ = self.context.autoloop.unloop(&image_squashfs_loop.path);
|
|
||||||
let _ = self.context.autoloop.unloop(&cfgblk_squashfs_loop.path);
|
|
||||||
let _ = fs::remove_dir(&cfgblk.dir);
|
|
||||||
Err(error.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn allocate_ipv4(&mut self) -> Result<Ipv4Addr> {
|
|
||||||
let network = Ipv4Network::new(Ipv4Addr::new(192, 168, 42, 0), 24)?;
|
|
||||||
let mut used: Vec<Ipv4Addr> = vec![
|
|
||||||
Ipv4Addr::new(192, 168, 42, 0),
|
|
||||||
Ipv4Addr::new(192, 168, 42, 1),
|
|
||||||
Ipv4Addr::new(192, 168, 42, 255),
|
|
||||||
];
|
|
||||||
for domid_candidate in self.context.xen.store.list("/local/domain").await? {
|
|
||||||
let dom_path = format!("/local/domain/{}", domid_candidate);
|
|
||||||
let ip_path = format!("{}/krata/network/guest/ipv4", dom_path);
|
|
||||||
let existing_ip = self.context.xen.store.read_string(&ip_path).await?;
|
|
||||||
if let Some(existing_ip) = existing_ip {
|
|
||||||
let ipv4_network = Ipv4Network::from_str(&existing_ip)?;
|
|
||||||
used.push(ipv4_network.ip());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut found: Option<Ipv4Addr> = None;
|
|
||||||
for ip in network.iter() {
|
|
||||||
if !used.contains(&ip) {
|
|
||||||
found = Some(ip);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if found.is_none() {
|
|
||||||
return Err(anyhow!(
|
|
||||||
"unable to find ipv4 to allocate to container, ipv4 addresses are exhausted"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(found.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn compile(&self, image: &str) -> Result<ImageInfo> {
|
|
||||||
let image = ImageName::parse(image)?;
|
|
||||||
let compiler = ImageCompiler::new(&self.context.image_cache)?;
|
|
||||||
compiler.compile(&image).await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ pub struct ContainerLoopInfo {
|
|||||||
pub delete: Option<String>,
|
pub delete: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ContainerInfo {
|
pub struct GuestInfo {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub domid: u32,
|
pub domid: u32,
|
||||||
pub image: String,
|
pub image: String,
|
||||||
@ -53,8 +53,8 @@ impl ControllerContext {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list(&mut self) -> Result<Vec<ContainerInfo>> {
|
pub async fn list(&mut self) -> Result<Vec<GuestInfo>> {
|
||||||
let mut containers: Vec<ContainerInfo> = Vec::new();
|
let mut containers: Vec<GuestInfo> = Vec::new();
|
||||||
for domid_candidate in self.xen.store.list("/local/domain").await? {
|
for domid_candidate in self.xen.store.list("/local/domain").await? {
|
||||||
let dom_path = format!("/local/domain/{}", domid_candidate);
|
let dom_path = format!("/local/domain/{}", domid_candidate);
|
||||||
let uuid_string = match self
|
let uuid_string = match self
|
||||||
@ -93,7 +93,7 @@ impl ControllerContext {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap_or("unknown".to_string());
|
.unwrap_or("unknown".to_string());
|
||||||
let loops = ControllerContext::parse_loop_set(&loops);
|
let loops = ControllerContext::parse_loop_set(&loops);
|
||||||
containers.push(ContainerInfo {
|
containers.push(GuestInfo {
|
||||||
uuid,
|
uuid,
|
||||||
domid,
|
domid,
|
||||||
image,
|
image,
|
||||||
@ -105,7 +105,7 @@ impl ControllerContext {
|
|||||||
Ok(containers)
|
Ok(containers)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn resolve(&mut self, id: &str) -> Result<Option<ContainerInfo>> {
|
pub async fn resolve(&mut self, id: &str) -> Result<Option<GuestInfo>> {
|
||||||
for container in self.list().await? {
|
for container in self.list().await? {
|
||||||
let uuid_string = container.uuid.to_string();
|
let uuid_string = container.uuid.to_string();
|
||||||
let domid_string = container.domid.to_string();
|
let domid_string = container.domid.to_string();
|
||||||
|
240
controller/src/launch/mod.rs
Normal file
240
controller/src/launch/mod.rs
Normal file
@ -0,0 +1,240 @@
|
|||||||
|
use std::{fs, net::Ipv4Addr, str::FromStr};
|
||||||
|
|
||||||
|
use advmac::MacAddr6;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use ipnetwork::Ipv4Network;
|
||||||
|
use krata::{
|
||||||
|
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
use xenclient::{DomainConfig, DomainDisk, DomainNetworkInterface};
|
||||||
|
use xenstore::client::XsdInterface;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
ctl::GuestInfo,
|
||||||
|
image::{cache::ImageCache, name::ImageName, ImageCompiler, ImageInfo},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::ctl::{cfgblk::ConfigBlock, ControllerContext};
|
||||||
|
|
||||||
|
pub struct GuestLaunchRequest<'a> {
|
||||||
|
pub kernel_path: &'a str,
|
||||||
|
pub initrd_path: &'a str,
|
||||||
|
pub image: &'a str,
|
||||||
|
pub vcpus: u32,
|
||||||
|
pub mem: u64,
|
||||||
|
pub env: Option<Vec<String>>,
|
||||||
|
pub run: Option<Vec<String>>,
|
||||||
|
pub debug: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GuestLauncher {}
|
||||||
|
|
||||||
|
impl GuestLauncher {
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
Ok(Self {})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn launch<'c, 'r>(
|
||||||
|
&mut self,
|
||||||
|
context: &'c mut ControllerContext,
|
||||||
|
request: GuestLaunchRequest<'r>,
|
||||||
|
) -> Result<GuestInfo> {
|
||||||
|
let uuid = Uuid::new_v4();
|
||||||
|
let name = format!("krata-{uuid}");
|
||||||
|
let image_info = self.compile(request.image, &context.image_cache).await?;
|
||||||
|
|
||||||
|
let mut gateway_mac = MacAddr6::random();
|
||||||
|
gateway_mac.set_local(true);
|
||||||
|
gateway_mac.set_multicast(false);
|
||||||
|
let mut container_mac = MacAddr6::random();
|
||||||
|
container_mac.set_local(true);
|
||||||
|
container_mac.set_multicast(false);
|
||||||
|
|
||||||
|
let guest_ipv4 = self.allocate_ipv4(context).await?;
|
||||||
|
let guest_ipv6 = container_mac.to_link_local_ipv6();
|
||||||
|
let gateway_ipv4 = "192.168.42.1";
|
||||||
|
let gateway_ipv6 = "fe80::1";
|
||||||
|
let ipv4_network_mask: u32 = 24;
|
||||||
|
let ipv6_network_mask: u32 = 10;
|
||||||
|
|
||||||
|
let launch_config = LaunchInfo {
|
||||||
|
network: Some(LaunchNetwork {
|
||||||
|
link: "eth0".to_string(),
|
||||||
|
ipv4: LaunchNetworkIpv4 {
|
||||||
|
address: format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
||||||
|
gateway: gateway_ipv4.to_string(),
|
||||||
|
},
|
||||||
|
ipv6: LaunchNetworkIpv6 {
|
||||||
|
address: format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
||||||
|
gateway: gateway_ipv6.to_string(),
|
||||||
|
},
|
||||||
|
resolver: LaunchNetworkResolver {
|
||||||
|
nameservers: vec![
|
||||||
|
"1.1.1.1".to_string(),
|
||||||
|
"1.0.0.1".to_string(),
|
||||||
|
"2606:4700:4700::1111".to_string(),
|
||||||
|
"2606:4700:4700::1001".to_string(),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
env: request.env,
|
||||||
|
run: request.run,
|
||||||
|
};
|
||||||
|
|
||||||
|
let cfgblk = ConfigBlock::new(&uuid, &image_info)?;
|
||||||
|
cfgblk.build(&launch_config)?;
|
||||||
|
|
||||||
|
let image_squashfs_path = image_info
|
||||||
|
.image_squashfs
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| anyhow!("failed to convert image squashfs path to string"))?;
|
||||||
|
|
||||||
|
let cfgblk_dir_path = cfgblk
|
||||||
|
.dir
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| anyhow!("failed to convert cfgblk directory path to string"))?;
|
||||||
|
let cfgblk_squashfs_path = cfgblk
|
||||||
|
.file
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| anyhow!("failed to convert cfgblk squashfs path to string"))?;
|
||||||
|
|
||||||
|
let image_squashfs_loop = context.autoloop.loopify(image_squashfs_path)?;
|
||||||
|
let cfgblk_squashfs_loop = context.autoloop.loopify(cfgblk_squashfs_path)?;
|
||||||
|
|
||||||
|
let cmdline_options = [
|
||||||
|
if request.debug { "debug" } else { "quiet" },
|
||||||
|
"elevator=noop",
|
||||||
|
];
|
||||||
|
let cmdline = cmdline_options.join(" ");
|
||||||
|
|
||||||
|
let container_mac_string = container_mac.to_string().replace('-', ":");
|
||||||
|
let gateway_mac_string = gateway_mac.to_string().replace('-', ":");
|
||||||
|
let config = DomainConfig {
|
||||||
|
backend_domid: 0,
|
||||||
|
name: &name,
|
||||||
|
max_vcpus: request.vcpus,
|
||||||
|
mem_mb: request.mem,
|
||||||
|
kernel_path: request.kernel_path,
|
||||||
|
initrd_path: request.initrd_path,
|
||||||
|
cmdline: &cmdline,
|
||||||
|
disks: vec![
|
||||||
|
DomainDisk {
|
||||||
|
vdev: "xvda",
|
||||||
|
block: &image_squashfs_loop,
|
||||||
|
writable: false,
|
||||||
|
},
|
||||||
|
DomainDisk {
|
||||||
|
vdev: "xvdb",
|
||||||
|
block: &cfgblk_squashfs_loop,
|
||||||
|
writable: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
consoles: vec![],
|
||||||
|
vifs: vec![DomainNetworkInterface {
|
||||||
|
mac: &container_mac_string,
|
||||||
|
mtu: 1500,
|
||||||
|
bridge: None,
|
||||||
|
script: None,
|
||||||
|
}],
|
||||||
|
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_rw_paths: vec!["krata/guest".to_string()],
|
||||||
|
};
|
||||||
|
match context.xen.create(&config).await {
|
||||||
|
Ok(domid) => Ok(GuestInfo {
|
||||||
|
uuid,
|
||||||
|
domid,
|
||||||
|
image: request.image.to_string(),
|
||||||
|
loops: vec![],
|
||||||
|
ipv4: format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
||||||
|
ipv6: format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
||||||
|
}),
|
||||||
|
Err(error) => {
|
||||||
|
let _ = context.autoloop.unloop(&image_squashfs_loop.path);
|
||||||
|
let _ = context.autoloop.unloop(&cfgblk_squashfs_loop.path);
|
||||||
|
let _ = fs::remove_dir(&cfgblk.dir);
|
||||||
|
Err(error.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn compile(&self, image: &str, image_cache: &ImageCache) -> Result<ImageInfo> {
|
||||||
|
let image = ImageName::parse(image)?;
|
||||||
|
let compiler = ImageCompiler::new(image_cache)?;
|
||||||
|
compiler.compile(&image).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn allocate_ipv4(&mut self, context: &mut ControllerContext) -> Result<Ipv4Addr> {
|
||||||
|
let network = Ipv4Network::new(Ipv4Addr::new(192, 168, 42, 0), 24)?;
|
||||||
|
let mut used: Vec<Ipv4Addr> = vec![
|
||||||
|
Ipv4Addr::new(192, 168, 42, 0),
|
||||||
|
Ipv4Addr::new(192, 168, 42, 1),
|
||||||
|
Ipv4Addr::new(192, 168, 42, 255),
|
||||||
|
];
|
||||||
|
for domid_candidate in context.xen.store.list("/local/domain").await? {
|
||||||
|
let dom_path = format!("/local/domain/{}", domid_candidate);
|
||||||
|
let ip_path = format!("{}/krata/network/guest/ipv4", dom_path);
|
||||||
|
let existing_ip = context.xen.store.read_string(&ip_path).await?;
|
||||||
|
if let Some(existing_ip) = existing_ip {
|
||||||
|
let ipv4_network = Ipv4Network::from_str(&existing_ip)?;
|
||||||
|
used.push(ipv4_network.ip());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut found: Option<Ipv4Addr> = None;
|
||||||
|
for ip in network.iter() {
|
||||||
|
if !used.contains(&ip) {
|
||||||
|
found = Some(ip);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found.is_none() {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"unable to find ipv4 to allocate to container, ipv4 addresses are exhausted"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(found.unwrap())
|
||||||
|
}
|
||||||
|
}
|
@ -2,3 +2,4 @@ pub mod autoloop;
|
|||||||
pub mod console;
|
pub mod console;
|
||||||
pub mod ctl;
|
pub mod ctl;
|
||||||
pub mod image;
|
pub mod image;
|
||||||
|
pub mod launch;
|
||||||
|
@ -96,9 +96,14 @@ impl XsdSocket {
|
|||||||
composed.extend_from_slice(buf);
|
composed.extend_from_slice(buf);
|
||||||
self.handle.xsd_write_all(&composed).await?;
|
self.handle.xsd_write_all(&composed).await?;
|
||||||
let mut result_buf = vec![0u8; size_of::<XsdMessageHeader>()];
|
let mut result_buf = vec![0u8; size_of::<XsdMessageHeader>()];
|
||||||
self.handle
|
match self.handle.xsd_read_exact(result_buf.as_mut_slice()).await {
|
||||||
.xsd_read_exact(result_buf.as_mut_slice())
|
Ok(_) => {}
|
||||||
.await?;
|
Err(error) => {
|
||||||
|
if result_buf.first().unwrap() == &0 {
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let result_header = bytemuck::from_bytes::<XsdMessageHeader>(&result_buf);
|
let result_header = bytemuck::from_bytes::<XsdMessageHeader>(&result_buf);
|
||||||
let mut payload = vec![0u8; result_header.len as usize];
|
let mut payload = vec![0u8; result_header.len as usize];
|
||||||
self.handle.xsd_read_exact(payload.as_mut_slice()).await?;
|
self.handle.xsd_read_exact(payload.as_mut_slice()).await?;
|
||||||
|
Reference in New Issue
Block a user