chore: rework oci crate to be more composable

This commit is contained in:
Alex Zenla
2024-04-15 05:06:02 +00:00
parent 24c71e9725
commit 5511fb2fa1
14 changed files with 564 additions and 492 deletions

View File

@ -1,7 +1,7 @@
use anyhow::Result;
use backhand::{FilesystemWriter, NodeHeader};
use krata::launchcfg::LaunchInfo;
use krataoci::compiler::ImageInfo;
use krataoci::packer::OciImagePacked;
use log::trace;
use std::fs;
use std::fs::File;
@ -9,28 +9,24 @@ use std::path::PathBuf;
use uuid::Uuid;
pub struct ConfigBlock<'a> {
pub image_info: &'a ImageInfo,
pub image: &'a OciImagePacked,
pub file: PathBuf,
pub dir: PathBuf,
}
impl ConfigBlock<'_> {
pub fn new<'a>(uuid: &Uuid, image_info: &'a ImageInfo) -> Result<ConfigBlock<'a>> {
pub fn new<'a>(uuid: &Uuid, image: &'a OciImagePacked) -> Result<ConfigBlock<'a>> {
let mut dir = std::env::temp_dir().clone();
dir.push(format!("krata-cfg-{}", uuid));
fs::create_dir_all(&dir)?;
let mut file = dir.clone();
file.push("config.squashfs");
Ok(ConfigBlock {
image_info,
file,
dir,
})
Ok(ConfigBlock { image, file, dir })
}
pub fn build(&self, launch_config: &LaunchInfo) -> Result<()> {
trace!("build launch_config={:?}", launch_config);
let manifest = self.image_info.config.to_string()?;
let manifest = self.image.config.to_string()?;
let launch = serde_json::to_string(launch_config)?;
let mut writer = FilesystemWriter::default();
writer.push_dir(

View File

@ -10,8 +10,8 @@ use krata::launchcfg::{
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
LaunchPackedFormat, LaunchRoot,
};
use krataoci::packer::OciPackerFormat;
use krataoci::progress::OciProgressContext;
use krataoci::packer::service::OciPackerService;
use krataoci::packer::{OciImagePacked, OciPackedFormat};
use tokio::sync::Semaphore;
use uuid::Uuid;
use xenclient::{DomainChannel, DomainConfig, DomainDisk, DomainNetworkInterface};
@ -19,11 +19,7 @@ use xenstore::XsdInterface;
use crate::cfgblk::ConfigBlock;
use crate::RuntimeContext;
use krataoci::{
cache::ImageCache,
compiler::{ImageInfo, OciImageCompiler},
name::ImageName,
};
use krataoci::name::ImageName;
use super::{GuestInfo, GuestState};
@ -55,15 +51,14 @@ impl GuestLauncher {
) -> Result<GuestInfo> {
let uuid = request.uuid.unwrap_or_else(Uuid::new_v4);
let xen_name = format!("krata-{uuid}");
let image_info = self
let packed = self
.compile(
&uuid.to_string(),
request.image,
&context.image_cache,
&context.oci_progress_context,
&context.packer,
match request.format {
LaunchPackedFormat::Squashfs => OciPackerFormat::Squashfs,
LaunchPackedFormat::Erofs => OciPackerFormat::Erofs,
LaunchPackedFormat::Squashfs => OciPackedFormat::Squashfs,
LaunchPackedFormat::Erofs => OciPackedFormat::Erofs,
},
)
.await?;
@ -116,11 +111,11 @@ impl GuestLauncher {
run: request.run,
};
let cfgblk = ConfigBlock::new(&uuid, &image_info)?;
let cfgblk = ConfigBlock::new(&uuid, &packed)?;
cfgblk.build(&launch_config)?;
let image_squashfs_path = image_info
.image
let image_squashfs_path = packed
.path
.to_str()
.ok_or_else(|| anyhow!("failed to convert image path to string"))?;
@ -265,13 +260,11 @@ impl GuestLauncher {
&self,
id: &str,
image: &str,
image_cache: &ImageCache,
progress: &OciProgressContext,
format: OciPackerFormat,
) -> Result<ImageInfo> {
packer: &OciPackerService,
format: OciPackedFormat,
) -> Result<OciImagePacked> {
let image = ImageName::parse(image)?;
let compiler = OciImageCompiler::new(image_cache, None, progress.clone())?;
compiler.compile(id, &image, format).await
packer.pack(id, image, format).await
}
async fn allocate_ipv4(&self, context: &RuntimeContext) -> Result<Ipv4Addr> {

View File

@ -17,7 +17,9 @@ use self::{
autoloop::AutoLoop,
launch::{GuestLaunchRequest, GuestLauncher},
};
use krataoci::{cache::ImageCache, progress::OciProgressContext};
use krataoci::{
packer::service::OciPackerService, progress::OciProgressContext, registry::OciPlatform,
};
pub mod autoloop;
pub mod cfgblk;
@ -52,7 +54,7 @@ pub struct GuestInfo {
#[derive(Clone)]
pub struct RuntimeContext {
pub oci_progress_context: OciProgressContext,
pub image_cache: ImageCache,
pub packer: OciPackerService,
pub autoloop: AutoLoop,
pub xen: XenClient,
pub kernel: String,
@ -68,13 +70,18 @@ impl RuntimeContext {
let xen = XenClient::open(0).await?;
image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?;
let packer = OciPackerService::new(
None,
&image_cache_path,
OciPlatform::current(),
oci_progress_context.clone(),
)?;
let kernel = RuntimeContext::detect_guest_file(&store, "kernel")?;
let initrd = RuntimeContext::detect_guest_file(&store, "initrd")?;
Ok(RuntimeContext {
oci_progress_context,
image_cache,
packer,
autoloop: AutoLoop::new(LoopControl::open()?),
xen,
kernel,