mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 21:21:32 +00:00
controller: split out commands into their own files
This commit is contained in:
@ -1,7 +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 hyphactrl::ctl::Controller;
|
use hyphactrl::ctl::{
|
||||||
|
console::ControllerConsole,
|
||||||
|
destroy::ControllerDestroy,
|
||||||
|
launch::{ControllerLaunch, ControllerLaunchRequest},
|
||||||
|
ControllerContext,
|
||||||
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@ -27,8 +32,6 @@ enum Commands {
|
|||||||
cpus: u32,
|
cpus: u32,
|
||||||
#[arg(short, long, default_value_t = 512)]
|
#[arg(short, long, default_value_t = 512)]
|
||||||
mem: u64,
|
mem: u64,
|
||||||
#[arg(long)]
|
|
||||||
config_bundle: Option<String>,
|
|
||||||
#[arg[short, long]]
|
#[arg[short, long]]
|
||||||
env: Option<Vec<String>>,
|
env: Option<Vec<String>>,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
@ -65,7 +68,7 @@ fn main() -> Result<()> {
|
|||||||
.map(|x| x.to_string())
|
.map(|x| x.to_string())
|
||||||
.ok_or_else(|| anyhow!("unable to convert store path to string"))?;
|
.ok_or_else(|| anyhow!("unable to convert store path to string"))?;
|
||||||
|
|
||||||
let mut controller = Controller::new(store_path.clone())?;
|
let mut context = ControllerContext::new(store_path.clone())?;
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
Commands::Launch {
|
Commands::Launch {
|
||||||
@ -74,7 +77,6 @@ fn main() -> Result<()> {
|
|||||||
image,
|
image,
|
||||||
cpus,
|
cpus,
|
||||||
mem,
|
mem,
|
||||||
config_bundle,
|
|
||||||
attach,
|
attach,
|
||||||
env,
|
env,
|
||||||
run,
|
run,
|
||||||
@ -82,33 +84,37 @@ 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 (uuid, _domid) = controller.launch(
|
let mut launch = ControllerLaunch::new(&mut context);
|
||||||
&kernel,
|
let request = ControllerLaunchRequest {
|
||||||
&initrd,
|
kernel_path: &kernel,
|
||||||
config_bundle.as_deref(),
|
initrd_path: &initrd,
|
||||||
&image,
|
image: &image,
|
||||||
cpus,
|
vcpus: cpus,
|
||||||
mem,
|
mem,
|
||||||
env,
|
env,
|
||||||
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)?;
|
||||||
println!("launched container: {}", uuid);
|
println!("launched container: {}", uuid);
|
||||||
if attach {
|
if attach {
|
||||||
controller.console(&uuid.to_string())?;
|
let mut console = ControllerConsole::new(&mut context);
|
||||||
|
console.perform(&uuid.to_string())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Destroy { container } => {
|
Commands::Destroy { container } => {
|
||||||
controller.destroy(&container)?;
|
let mut destroy = ControllerDestroy::new(&mut context);
|
||||||
|
destroy.perform(&container)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Console { container } => {
|
Commands::Console { container } => {
|
||||||
controller.console(&container)?;
|
let mut console = ControllerConsole::new(&mut context);
|
||||||
|
console.perform(&container)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::List { .. } => {
|
Commands::List { .. } => {
|
||||||
let containers = controller.list()?;
|
let containers = context.list()?;
|
||||||
let mut table = cli_tables::Table::new();
|
let mut table = cli_tables::Table::new();
|
||||||
let header = vec!["uuid", "ipv4", "ipv6", "image"];
|
let header = vec!["uuid", "ipv4", "ipv6", "image"];
|
||||||
table.push_row(&header)?;
|
table.push_row(&header)?;
|
||||||
|
@ -7,7 +7,7 @@ pub struct AutoLoop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AutoLoop {
|
impl AutoLoop {
|
||||||
pub(crate) fn new(control: LoopControl) -> AutoLoop {
|
pub fn new(control: LoopControl) -> AutoLoop {
|
||||||
AutoLoop { control }
|
AutoLoop { control }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,17 +10,12 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
pub struct ConfigBlock<'a> {
|
pub struct ConfigBlock<'a> {
|
||||||
pub image_info: &'a ImageInfo,
|
pub image_info: &'a ImageInfo,
|
||||||
pub config_bundle: Option<&'a str>,
|
|
||||||
pub file: PathBuf,
|
pub file: PathBuf,
|
||||||
pub dir: PathBuf,
|
pub dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigBlock<'_> {
|
impl ConfigBlock<'_> {
|
||||||
pub fn new<'a>(
|
pub fn new<'a>(uuid: &Uuid, image_info: &'a ImageInfo) -> Result<ConfigBlock<'a>> {
|
||||||
uuid: &Uuid,
|
|
||||||
image_info: &'a ImageInfo,
|
|
||||||
config_bundle: Option<&'a str>,
|
|
||||||
) -> Result<ConfigBlock<'a>> {
|
|
||||||
let mut dir = std::env::temp_dir().clone();
|
let mut dir = std::env::temp_dir().clone();
|
||||||
dir.push(format!("hypha-cfg-{}", uuid));
|
dir.push(format!("hypha-cfg-{}", uuid));
|
||||||
fs::create_dir_all(&dir)?;
|
fs::create_dir_all(&dir)?;
|
||||||
@ -28,7 +23,6 @@ impl ConfigBlock<'_> {
|
|||||||
file.push("config.squashfs");
|
file.push("config.squashfs");
|
||||||
Ok(ConfigBlock {
|
Ok(ConfigBlock {
|
||||||
image_info,
|
image_info,
|
||||||
config_bundle,
|
|
||||||
file,
|
file,
|
||||||
dir,
|
dir,
|
||||||
})
|
})
|
||||||
@ -36,10 +30,6 @@ 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!("ConfigBlock build launch_config={:?}", launch_config);
|
||||||
let config_bundle_content = match self.config_bundle {
|
|
||||||
None => None,
|
|
||||||
Some(path) => Some(fs::read(path)?),
|
|
||||||
};
|
|
||||||
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();
|
||||||
@ -72,18 +62,6 @@ impl ConfigBlock<'_> {
|
|||||||
mtime: 0,
|
mtime: 0,
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
if let Some(config_bundle_content) = config_bundle_content.as_ref() {
|
|
||||||
writer.push_file(
|
|
||||||
config_bundle_content.as_slice(),
|
|
||||||
"/bundle",
|
|
||||||
NodeHeader {
|
|
||||||
permissions: 384,
|
|
||||||
uid: 0,
|
|
||||||
gid: 0,
|
|
||||||
mtime: 0,
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
let mut file = File::create(&self.file)?;
|
let mut file = File::create(&self.file)?;
|
||||||
trace!("ConfigBlock build write sqaushfs");
|
trace!("ConfigBlock build write sqaushfs");
|
||||||
writer.write(&mut file)?;
|
writer.write(&mut file)?;
|
||||||
|
66
controller/src/ctl/console.rs
Normal file
66
controller/src/ctl/console.rs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
use std::{
|
||||||
|
io::{self, Read, Write},
|
||||||
|
process::exit,
|
||||||
|
thread,
|
||||||
|
};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use termion::raw::IntoRawMode;
|
||||||
|
|
||||||
|
use super::ControllerContext;
|
||||||
|
|
||||||
|
pub struct ControllerConsole<'a> {
|
||||||
|
context: &'a mut ControllerContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerConsole<'_> {
|
||||||
|
pub fn new(context: &mut ControllerContext) -> ControllerConsole<'_> {
|
||||||
|
ControllerConsole { context }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perform(&mut self, id: &str) -> Result<()> {
|
||||||
|
let info = self
|
||||||
|
.context
|
||||||
|
.resolve(id)?
|
||||||
|
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
||||||
|
let domid = info.domid;
|
||||||
|
let (mut read, mut write) = self.context.xen.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()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
controller/src/ctl/destroy.rs
Normal file
58
controller/src/ctl/destroy.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use uuid::Uuid;
|
||||||
|
use xenstore::client::{XsdClient, XsdInterface};
|
||||||
|
|
||||||
|
use super::ControllerContext;
|
||||||
|
|
||||||
|
pub struct ControllerDestroy<'a> {
|
||||||
|
context: &'a mut ControllerContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerDestroy<'_> {
|
||||||
|
pub fn new(context: &mut ControllerContext) -> ControllerDestroy<'_> {
|
||||||
|
ControllerDestroy { context }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perform(&mut self, id: &str) -> Result<Uuid> {
|
||||||
|
let info = self
|
||||||
|
.context
|
||||||
|
.resolve(id)?
|
||||||
|
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
||||||
|
let domid = info.domid;
|
||||||
|
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(anyhow!(
|
||||||
|
"domain {} was not found or not created by hypha",
|
||||||
|
domid
|
||||||
|
))
|
||||||
|
}
|
||||||
|
Some(value) => value,
|
||||||
|
};
|
||||||
|
if uuid.is_empty() {
|
||||||
|
return Err(anyhow!("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 = ControllerContext::parse_loop_set(&loops);
|
||||||
|
self.context.xen.destroy(domid)?;
|
||||||
|
for info in &loops {
|
||||||
|
self.context.autoloop.unloop(&info.device)?;
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
226
controller/src/ctl/launch.rs
Normal file
226
controller/src/ctl/launch.rs
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
use std::{fs, net::Ipv4Addr, str::FromStr};
|
||||||
|
|
||||||
|
use advmac::MacAddr6;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use hypha::{
|
||||||
|
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
||||||
|
};
|
||||||
|
use ipnetwork::Ipv4Network;
|
||||||
|
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> {
|
||||||
|
context: &'a mut ControllerContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerLaunch<'_> {
|
||||||
|
pub fn new(context: &mut ControllerContext) -> ControllerLaunch<'_> {
|
||||||
|
ControllerLaunch { context }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn perform(&mut self, request: ControllerLaunchRequest) -> Result<(Uuid, u32)> {
|
||||||
|
let uuid = Uuid::new_v4();
|
||||||
|
let name = format!("hypha-{uuid}");
|
||||||
|
let image_info = self.compile(request.image)?;
|
||||||
|
|
||||||
|
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()?;
|
||||||
|
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![],
|
||||||
|
extra_keys: vec![
|
||||||
|
("hypha/uuid".to_string(), uuid.to_string()),
|
||||||
|
(
|
||||||
|
"hypha/loops".to_string(),
|
||||||
|
format!(
|
||||||
|
"{}:{}:none,{}:{}:{}",
|
||||||
|
&image_squashfs_loop.path,
|
||||||
|
image_squashfs_path,
|
||||||
|
&cfgblk_squashfs_loop.path,
|
||||||
|
cfgblk_squashfs_path,
|
||||||
|
cfgblk_dir_path,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("hypha/image".to_string(), request.image.to_string()),
|
||||||
|
(
|
||||||
|
"hypha/network/guest/ipv4".to_string(),
|
||||||
|
format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"hypha/network/guest/ipv6".to_string(),
|
||||||
|
format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"hypha/network/guest/mac".to_string(),
|
||||||
|
container_mac_string.clone(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"hypha/network/gateway/ipv4".to_string(),
|
||||||
|
format!("{}/{}", gateway_ipv4, ipv4_network_mask),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"hypha/network/gateway/ipv6".to_string(),
|
||||||
|
format!("{}/{}", gateway_ipv6, ipv6_network_mask),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"hypha/network/gateway/mac".to_string(),
|
||||||
|
gateway_mac_string.clone(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
match self.context.xen.create(&config) {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_any("/local/domain")? {
|
||||||
|
let dom_path = format!("/local/domain/{}", domid_candidate);
|
||||||
|
let ip_path = format!("{}/hypha/network/guest/ipv4", dom_path);
|
||||||
|
let existing_ip = self.context.xen.store.read_string_optional(&ip_path)?;
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile(&self, image: &str) -> Result<ImageInfo> {
|
||||||
|
let image = ImageName::parse(image)?;
|
||||||
|
let compiler = ImageCompiler::new(&self.context.image_cache)?;
|
||||||
|
compiler.compile(&image)
|
||||||
|
}
|
||||||
|
}
|
@ -1,32 +1,24 @@
|
|||||||
pub mod cfgblk;
|
pub mod cfgblk;
|
||||||
|
|
||||||
use crate::autoloop::AutoLoop;
|
use crate::autoloop::AutoLoop;
|
||||||
use crate::ctl::cfgblk::ConfigBlock;
|
|
||||||
use crate::image::cache::ImageCache;
|
use crate::image::cache::ImageCache;
|
||||||
use crate::image::name::ImageName;
|
|
||||||
use crate::image::{ImageCompiler, ImageInfo};
|
|
||||||
use advmac::MacAddr6;
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use hypha::{
|
|
||||||
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
|
||||||
};
|
|
||||||
use ipnetwork::Ipv4Network;
|
|
||||||
use loopdev::LoopControl;
|
use loopdev::LoopControl;
|
||||||
use std::io::{Read, Write};
|
use std::fs;
|
||||||
use std::net::Ipv4Addr;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::exit;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::{fs, io, thread};
|
|
||||||
use termion::raw::IntoRawMode;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use xenclient::{DomainConfig, DomainDisk, DomainNetworkInterface, XenClient};
|
use xenclient::XenClient;
|
||||||
use xenstore::client::{XsdClient, XsdInterface};
|
use xenstore::client::XsdInterface;
|
||||||
|
|
||||||
pub struct Controller {
|
pub mod console;
|
||||||
image_cache: ImageCache,
|
pub mod destroy;
|
||||||
autoloop: AutoLoop,
|
pub mod launch;
|
||||||
client: XenClient,
|
|
||||||
|
pub struct ControllerContext {
|
||||||
|
pub image_cache: ImageCache,
|
||||||
|
pub autoloop: AutoLoop,
|
||||||
|
pub xen: XenClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ContainerLoopInfo {
|
pub struct ContainerLoopInfo {
|
||||||
@ -44,279 +36,29 @@ pub struct ContainerInfo {
|
|||||||
pub ipv6: String,
|
pub ipv6: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller {
|
impl ControllerContext {
|
||||||
pub fn new(store_path: String) -> Result<Controller> {
|
pub fn new(store_path: String) -> Result<ControllerContext> {
|
||||||
let mut image_cache_path = PathBuf::from(store_path);
|
let mut image_cache_path = PathBuf::from(store_path);
|
||||||
image_cache_path.push("cache");
|
image_cache_path.push("cache");
|
||||||
fs::create_dir_all(&image_cache_path)?;
|
fs::create_dir_all(&image_cache_path)?;
|
||||||
|
|
||||||
let client = XenClient::open()?;
|
let xen = XenClient::open()?;
|
||||||
image_cache_path.push("image");
|
image_cache_path.push("image");
|
||||||
fs::create_dir_all(&image_cache_path)?;
|
fs::create_dir_all(&image_cache_path)?;
|
||||||
let image_cache = ImageCache::new(&image_cache_path)?;
|
let image_cache = ImageCache::new(&image_cache_path)?;
|
||||||
Ok(Controller {
|
Ok(ControllerContext {
|
||||||
image_cache,
|
image_cache,
|
||||||
autoloop: AutoLoop::new(LoopControl::open()?),
|
autoloop: AutoLoop::new(LoopControl::open()?),
|
||||||
client,
|
xen,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile(&mut self, image: &str) -> Result<ImageInfo> {
|
|
||||||
let image = ImageName::parse(image)?;
|
|
||||||
let compiler = ImageCompiler::new(&self.image_cache)?;
|
|
||||||
compiler.compile(&image)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub fn launch(
|
|
||||||
&mut self,
|
|
||||||
kernel_path: &str,
|
|
||||||
initrd_path: &str,
|
|
||||||
config_bundle_path: Option<&str>,
|
|
||||||
image: &str,
|
|
||||||
vcpus: u32,
|
|
||||||
mem: u64,
|
|
||||||
env: Option<Vec<String>>,
|
|
||||||
run: Option<Vec<String>>,
|
|
||||||
debug: bool,
|
|
||||||
) -> Result<(Uuid, u32)> {
|
|
||||||
let uuid = Uuid::new_v4();
|
|
||||||
let name = format!("hypha-{uuid}");
|
|
||||||
let image_info = self.compile(image)?;
|
|
||||||
|
|
||||||
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()?;
|
|
||||||
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,
|
|
||||||
run,
|
|
||||||
};
|
|
||||||
|
|
||||||
let cfgblk = ConfigBlock::new(&uuid, &image_info, config_bundle_path)?;
|
|
||||||
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.autoloop.loopify(image_squashfs_path)?;
|
|
||||||
let cfgblk_squashfs_loop = self.autoloop.loopify(cfgblk_squashfs_path)?;
|
|
||||||
|
|
||||||
let cmdline_options = [if 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: vcpus,
|
|
||||||
mem_mb: mem,
|
|
||||||
kernel_path,
|
|
||||||
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![],
|
|
||||||
extra_keys: vec![
|
|
||||||
("hypha/uuid".to_string(), uuid.to_string()),
|
|
||||||
(
|
|
||||||
"hypha/loops".to_string(),
|
|
||||||
format!(
|
|
||||||
"{}:{}:none,{}:{}:{}",
|
|
||||||
&image_squashfs_loop.path,
|
|
||||||
image_squashfs_path,
|
|
||||||
&cfgblk_squashfs_loop.path,
|
|
||||||
cfgblk_squashfs_path,
|
|
||||||
cfgblk_dir_path,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
("hypha/image".to_string(), image.to_string()),
|
|
||||||
(
|
|
||||||
"hypha/network/guest/ipv4".to_string(),
|
|
||||||
format!("{}/{}", guest_ipv4, ipv4_network_mask),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"hypha/network/guest/ipv6".to_string(),
|
|
||||||
format!("{}/{}", guest_ipv6, ipv6_network_mask),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"hypha/network/guest/mac".to_string(),
|
|
||||||
container_mac_string.clone(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"hypha/network/gateway/ipv4".to_string(),
|
|
||||||
format!("{}/{}", gateway_ipv4, ipv4_network_mask),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"hypha/network/gateway/ipv6".to_string(),
|
|
||||||
format!("{}/{}", gateway_ipv6, ipv6_network_mask),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"hypha/network/gateway/mac".to_string(),
|
|
||||||
gateway_mac_string.clone(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
match self.client.create(&config) {
|
|
||||||
Ok(domid) => Ok((uuid, 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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn destroy(&mut self, id: &str) -> Result<Uuid> {
|
|
||||||
let info = self
|
|
||||||
.resolve(id)?
|
|
||||||
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
|
||||||
let domid = info.domid;
|
|
||||||
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(anyhow!(
|
|
||||||
"domain {} was not found or not created by hypha",
|
|
||||||
domid
|
|
||||||
))
|
|
||||||
}
|
|
||||||
Some(value) => value,
|
|
||||||
};
|
|
||||||
if uuid.is_empty() {
|
|
||||||
return Err(anyhow!("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 = Controller::parse_loop_set(&loops);
|
|
||||||
self.client.destroy(domid)?;
|
|
||||||
for info in &loops {
|
|
||||||
self.autoloop.unloop(&info.device)?;
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn console(&mut self, id: &str) -> Result<()> {
|
|
||||||
let info = self
|
|
||||||
.resolve(id)?
|
|
||||||
.ok_or_else(|| anyhow!("unable to resolve container: {}", id))?;
|
|
||||||
let domid = info.domid;
|
|
||||||
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()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn list(&mut self) -> Result<Vec<ContainerInfo>> {
|
pub fn list(&mut self) -> Result<Vec<ContainerInfo>> {
|
||||||
let mut containers: Vec<ContainerInfo> = Vec::new();
|
let mut containers: Vec<ContainerInfo> = Vec::new();
|
||||||
for domid_candidate in self.client.store.list_any("/local/domain")? {
|
for domid_candidate in self.xen.store.list_any("/local/domain")? {
|
||||||
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
|
||||||
.client
|
.xen
|
||||||
.store
|
.store
|
||||||
.read_string_optional(&format!("{}/hypha/uuid", &dom_path))?
|
.read_string_optional(&format!("{}/hypha/uuid", &dom_path))?
|
||||||
{
|
{
|
||||||
@ -327,26 +69,26 @@ impl Controller {
|
|||||||
u32::from_str(&domid_candidate).map_err(|_| anyhow!("failed to parse domid"))?;
|
u32::from_str(&domid_candidate).map_err(|_| anyhow!("failed to parse domid"))?;
|
||||||
let uuid = Uuid::from_str(&uuid_string)?;
|
let uuid = Uuid::from_str(&uuid_string)?;
|
||||||
let image = self
|
let image = self
|
||||||
.client
|
.xen
|
||||||
.store
|
.store
|
||||||
.read_string_optional(&format!("{}/hypha/image", &dom_path))?
|
.read_string_optional(&format!("{}/hypha/image", &dom_path))?
|
||||||
.unwrap_or("unknown".to_string());
|
.unwrap_or("unknown".to_string());
|
||||||
let loops = self
|
let loops = self
|
||||||
.client
|
.xen
|
||||||
.store
|
.store
|
||||||
.read_string_optional(&format!("{}/hypha/loops", &dom_path))?
|
.read_string_optional(&format!("{}/hypha/loops", &dom_path))?
|
||||||
.unwrap_or("".to_string());
|
.unwrap_or("".to_string());
|
||||||
let ipv4 = self
|
let ipv4 = self
|
||||||
.client
|
.xen
|
||||||
.store
|
.store
|
||||||
.read_string_optional(&format!("{}/hypha/network/guest/ipv4", &dom_path))?
|
.read_string_optional(&format!("{}/hypha/network/guest/ipv4", &dom_path))?
|
||||||
.unwrap_or("unknown".to_string());
|
.unwrap_or("unknown".to_string());
|
||||||
let ipv6 = self
|
let ipv6: String = self
|
||||||
.client
|
.xen
|
||||||
.store
|
.store
|
||||||
.read_string_optional(&format!("{}/hypha/network/guest/ipv6", &dom_path))?
|
.read_string_optional(&format!("{}/hypha/network/guest/ipv6", &dom_path))?
|
||||||
.unwrap_or("unknown".to_string());
|
.unwrap_or("unknown".to_string());
|
||||||
let loops = Controller::parse_loop_set(&loops);
|
let loops = ControllerContext::parse_loop_set(&loops);
|
||||||
containers.push(ContainerInfo {
|
containers.push(ContainerInfo {
|
||||||
uuid,
|
uuid,
|
||||||
domid,
|
domid,
|
||||||
@ -389,38 +131,4 @@ impl Controller {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<ContainerLoopInfo>>()
|
.collect::<Vec<ContainerLoopInfo>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
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.client.store.list_any("/local/domain")? {
|
|
||||||
let dom_path = format!("/local/domain/{}", domid_candidate);
|
|
||||||
let ip_path = format!("{}/hypha/network/guest/ipv4", dom_path);
|
|
||||||
let existing_ip = self.client.store.read_string_optional(&ip_path)?;
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user