refactor: split main.rs into registry, build, and qemu modules
main.rs keeps only the CLI definition and command dispatch: - registry.rs: image-name validation + XDG registry path handling (tests) - build.rs: podman mount/unmount, vmlinuz extraction, cpio/gzip initrd - qemu.rs: direct-kernel-boot VM launch No behavior change; cargo fmt/clippy/test pass, build/run paths verified against the mock-vm image.
This commit was merged in pull request #5.
This commit is contained in:
+56
@@ -0,0 +1,56 @@
|
||||
//! QEMU: boot a registry entry with qemu-system-x86_64 using direct kernel
|
||||
//! boot (-kernel/-initrd).
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use std::process::Command;
|
||||
|
||||
use crate::registry::registry_dir;
|
||||
|
||||
pub(crate) fn run(name: &str) -> Result<()> {
|
||||
let reg_dir = registry_dir(name)?;
|
||||
let vmlinuz_path = reg_dir.join("vmlinuz");
|
||||
let initrd_path = reg_dir.join("initrd");
|
||||
if !vmlinuz_path.exists() || !initrd_path.exists() {
|
||||
bail!(
|
||||
"Registry missing vmlinuz/initrd for '{}'. Run `slim build` first.",
|
||||
name
|
||||
);
|
||||
}
|
||||
println!("Booting {} from registry: {}", name, reg_dir.display());
|
||||
println!(
|
||||
" qemu-system-x86_64 -m 256 -nographic -kernel {} -initrd {} -append 'console=ttyS0'",
|
||||
vmlinuz_path.display(),
|
||||
initrd_path.display()
|
||||
);
|
||||
|
||||
let cmdline = [
|
||||
"console=ttyS0,115200",
|
||||
"root=/dev/ram0",
|
||||
"rw",
|
||||
"earlyprintk=serial",
|
||||
"nokaslr",
|
||||
"raid=noautodetect",
|
||||
]
|
||||
.join(" ");
|
||||
|
||||
let status = Command::new("qemu-system-x86_64")
|
||||
.args(["-m", "1024M"])
|
||||
.arg("-kernel")
|
||||
.arg(vmlinuz_path.as_os_str())
|
||||
.arg("-initrd")
|
||||
.arg(initrd_path.as_os_str())
|
||||
.args(["-append", &cmdline])
|
||||
.args(["-nographic"])
|
||||
.status()
|
||||
.context("Failed to launch qemu-system-x86_64")?;
|
||||
|
||||
match status.code() {
|
||||
Some(0) => println!("QEMU exited cleanly."),
|
||||
Some(126) | Some(127) => {
|
||||
bail!("Failed to start qemu-system-x86_64 (is it installed and in PATH?)")
|
||||
}
|
||||
Some(code) => bail!("QEMU exited with code {code}"),
|
||||
None => bail!("QEMU terminated by a signal"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user