Repository navigation
feat(minvmd): add image resolution and VM configuration builder #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d12ea49
9c6cb9f
bde8b06
d3f9bee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| //! Kernel and rootfs image resolution. | ||
| //! | ||
| //! Paths are supplied at runtime via environment variables so the binary is | ||
| //! not tied to a fixed install layout. | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
| use crate::error::VmError; | ||
|
|
||
| /// Arch-appropriate libkrun kernel format constant. | ||
| /// | ||
| /// - `aarch64`: `virtio-linux` ships `Image.gz` → `KRUN_KERNEL_FORMAT_IMAGE_GZ` | ||
| /// - `x86_64`: `virtio-linux` ships `bzImage` → `KRUN_KERNEL_FORMAT_ELF` | ||
| #[cfg(target_os = "macos")] | ||
| pub fn kernel_format() -> u32 { | ||
| #[cfg(target_arch = "aarch64")] | ||
| { | ||
| crate::krun::KRUN_KERNEL_FORMAT_IMAGE_GZ | ||
| } | ||
| #[cfg(target_arch = "x86_64")] | ||
| { | ||
| crate::krun::KRUN_KERNEL_FORMAT_ELF | ||
| } | ||
| } | ||
|
|
||
| /// Resolve the kernel path from `MINVMD_KERNEL_PATH`. | ||
| /// | ||
| /// Returns `VmError::MissingEnv` when the variable is unset or empty. | ||
| pub fn resolve_kernel_path() -> Result<PathBuf, VmError> { | ||
| let val = std::env::var("MINVMD_KERNEL_PATH").map_err(|_| VmError::MissingEnv { | ||
| var: "MINVMD_KERNEL_PATH", | ||
| })?; | ||
| if val.is_empty() { | ||
| return Err(VmError::MissingEnv { | ||
| var: "MINVMD_KERNEL_PATH", | ||
| }); | ||
| } | ||
| Ok(PathBuf::from(val)) | ||
| } | ||
|
|
||
| /// Resolve the rootfs path from `MINVMD_ROOTFS_PATH`. | ||
| /// | ||
| /// Returns `VmError::MissingEnv` when the variable is unset or empty. | ||
| pub fn resolve_rootfs_path() -> Result<PathBuf, VmError> { | ||
| let val = std::env::var("MINVMD_ROOTFS_PATH").map_err(|_| VmError::MissingEnv { | ||
| var: "MINVMD_ROOTFS_PATH", | ||
| })?; | ||
| if val.is_empty() { | ||
| return Err(VmError::MissingEnv { | ||
| var: "MINVMD_ROOTFS_PATH", | ||
| }); | ||
| } | ||
| Ok(PathBuf::from(val)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Mutex; | ||
|
|
||
| use super::*; | ||
|
|
||
| // Serialise all tests that mutate MINVMD_KERNEL_PATH so they don't race | ||
| // when `cargo test` runs them in parallel. | ||
| static KERNEL_PATH_LOCK: Mutex<()> = Mutex::new(()); | ||
|
|
||
| #[test] | ||
| fn resolve_kernel_path_errors_on_missing_env() { | ||
| let _g = KERNEL_PATH_LOCK.lock().unwrap(); | ||
| unsafe { std::env::remove_var("MINVMD_KERNEL_PATH") }; | ||
| let err = resolve_kernel_path().unwrap_err(); | ||
| assert!( | ||
| matches!( | ||
| err, | ||
|
Comment on lines
+70
to
+73
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM — Correctness Three tests in this file ( If two of these tests run concurrently, one may set the var while the other expects it unset, causing nondeterministic failures. Consider either:
|
||
| VmError::MissingEnv { | ||
| var: "MINVMD_KERNEL_PATH" | ||
| } | ||
| ), | ||
| "expected MissingEnv, got {err:?}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_kernel_path_returns_path_when_set() { | ||
| let _g = KERNEL_PATH_LOCK.lock().unwrap(); | ||
| unsafe { std::env::set_var("MINVMD_KERNEL_PATH", "/boot/Image.gz") }; | ||
| let path = resolve_kernel_path().unwrap(); | ||
| assert_eq!(path, std::path::PathBuf::from("/boot/Image.gz")); | ||
| unsafe { std::env::remove_var("MINVMD_KERNEL_PATH") }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_kernel_path_errors_on_empty_env() { | ||
| let _g = KERNEL_PATH_LOCK.lock().unwrap(); | ||
| unsafe { std::env::set_var("MINVMD_KERNEL_PATH", "") }; | ||
| let err = resolve_kernel_path().unwrap_err(); | ||
| assert!( | ||
| matches!( | ||
| err, | ||
| VmError::MissingEnv { | ||
| var: "MINVMD_KERNEL_PATH" | ||
| } | ||
| ), | ||
| "expected MissingEnv, got {err:?}" | ||
| ); | ||
| unsafe { std::env::remove_var("MINVMD_KERNEL_PATH") }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_rootfs_path_errors_on_missing_env() { | ||
| unsafe { std::env::remove_var("MINVMD_ROOTFS_PATH") }; | ||
| let err = resolve_rootfs_path().unwrap_err(); | ||
| assert!( | ||
| matches!( | ||
| err, | ||
| VmError::MissingEnv { | ||
| var: "MINVMD_ROOTFS_PATH" | ||
| } | ||
| ), | ||
| "expected MissingEnv, got {err:?}" | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| #[test] | ||
| fn kernel_format_is_valid() { | ||
| let fmt = kernel_format(); | ||
| let valid = [ | ||
| crate::krun::KRUN_KERNEL_FORMAT_ELF, | ||
| crate::krun::KRUN_KERNEL_FORMAT_IMAGE_GZ, | ||
| ]; | ||
| assert!( | ||
| valid.contains(&fmt), | ||
| "unexpected kernel format constant {fmt}" | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //! VM configuration builder. | ||
| //! | ||
| //! [`VmConfig`] collects the parameters needed to configure a libkrun context | ||
| //! (vcpus, RAM, kernel, rootfs). No network device is added in v0.1; gvproxy/ | ||
| //! TSI integration is tracked in #160. | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
| /// Configuration parameters for a single microVM. | ||
| /// | ||
| /// Build with [`VmConfig::new`] and apply to a libkrun | ||
| /// [`Context`][crate::krun::Context] with [`VmConfig::apply`]. | ||
| #[derive(Debug, Clone)] | ||
| pub struct VmConfig { | ||
| /// Number of virtual CPUs. | ||
| pub num_vcpus: u8, | ||
| /// RAM in mebibytes. | ||
| pub ram_mib: u32, | ||
| /// Kernel image path. | ||
| pub kernel_path: PathBuf, | ||
| /// Rootfs directory path. | ||
| pub rootfs_path: PathBuf, | ||
| } | ||
|
|
||
| impl VmConfig { | ||
| /// Construct a new `VmConfig`. | ||
| #[must_use] | ||
| pub fn new(num_vcpus: u8, ram_mib: u32, kernel_path: PathBuf, rootfs_path: PathBuf) -> Self { | ||
| Self { | ||
| num_vcpus, | ||
| ram_mib, | ||
| kernel_path, | ||
| rootfs_path, | ||
| } | ||
| } | ||
|
|
||
| /// Apply this configuration to an existing libkrun [`Context`][crate::krun::Context]. | ||
| /// | ||
| /// Configures vcpus, RAM, kernel (with arch-appropriate format), and | ||
| /// rootfs. No network device is added (R2.5). | ||
| #[cfg(target_os = "macos")] | ||
| pub fn apply(&self, ctx: &mut crate::krun::Context) -> Result<(), crate::error::VmError> { | ||
| ctx.set_vm_config(self.num_vcpus, self.ram_mib)?; | ||
| ctx.set_kernel( | ||
| &self.kernel_path, | ||
| crate::image::kernel_format(), | ||
| None::<&std::path::Path>, | ||
| None, | ||
| )?; | ||
| ctx.set_root(&self.rootfs_path)?; | ||
| // R2.5: no network device in v0.1. | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn vm_config_stores_fields() { | ||
| let cfg = VmConfig::new( | ||
| 2, | ||
| 512, | ||
| PathBuf::from("/boot/Image.gz"), | ||
| PathBuf::from("/var/lib/alpine"), | ||
| ); | ||
| assert_eq!(cfg.num_vcpus, 2); | ||
| assert_eq!(cfg.ram_mib, 512); | ||
| assert_eq!(cfg.kernel_path, PathBuf::from("/boot/Image.gz")); | ||
| assert_eq!(cfg.rootfs_path, PathBuf::from("/var/lib/alpine")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn vm_config_clone() { | ||
| let cfg = VmConfig::new(1, 256, PathBuf::from("/k"), PathBuf::from("/r")); | ||
| let cfg2 = cfg.clone(); | ||
| assert_eq!(cfg.num_vcpus, cfg2.num_vcpus); | ||
| assert_eq!(cfg.ram_mib, cfg2.ram_mib); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.