+40
-35
@@ -1,11 +1,11 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use flate2::write::GzEncoder;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(name = "slim")]
|
|
||||||
#[command(about = "Build bootable initrd VMs from container images")]
|
#[command(about = "Build bootable initrd VMs from container images")]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
@@ -42,10 +42,15 @@ fn main() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn registry_dir(image: &str) -> PathBuf {
|
fn registry_dir(image: &str) -> Result<PathBuf> {
|
||||||
let base = xdg::BaseDirectories::with_prefix("slim-rs");
|
let base = xdg::BaseDirectories::with_prefix("slim-rs");
|
||||||
let initrd_path = base.place_data_file(format!("registry/{}/initrd", image));
|
let initrd_path = base
|
||||||
initrd_path.unwrap().parent().unwrap().to_path_buf()
|
.place_data_file(format!("registry/{}/initrd", image))
|
||||||
|
.context("Failed to write to XDG_DATA_HOME")?;
|
||||||
|
Ok(initrd_path
|
||||||
|
.parent()
|
||||||
|
.expect("data file has a parent")
|
||||||
|
.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(image: &str) -> Result<()> {
|
fn build(image: &str) -> Result<()> {
|
||||||
@@ -75,7 +80,7 @@ fn mount_image(image: &str) -> Result<PathBuf> {
|
|||||||
.context("Failed to run podman image mount")?;
|
.context("Failed to run podman image mount")?;
|
||||||
let mount_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
let mount_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||||
if !output.status.success() || mount_path.is_empty() {
|
if !output.status.success() || mount_path.is_empty() {
|
||||||
anyhow::bail!(
|
bail!(
|
||||||
"podman image mount failed: {}",
|
"podman image mount failed: {}",
|
||||||
String::from_utf8_lossy(&output.stderr).trim()
|
String::from_utf8_lossy(&output.stderr).trim()
|
||||||
);
|
);
|
||||||
@@ -95,12 +100,10 @@ fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> {
|
|||||||
.status()
|
.status()
|
||||||
.context("Failed to check for /vmlinuz inside the image mount")?;
|
.context("Failed to check for /vmlinuz inside the image mount")?;
|
||||||
if !has_kernel.success() {
|
if !has_kernel.success() {
|
||||||
anyhow::bail!(
|
bail!("Image missing required /vmlinuz. Place kernel at /vmlinuz in Containerfile.");
|
||||||
"Image missing required /vmlinuz. Place kernel at /vmlinuz in Containerfile."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let reg_dir = registry_dir(image);
|
let reg_dir = registry_dir(image)?;
|
||||||
fs::create_dir_all(®_dir)?;
|
fs::create_dir_all(®_dir)?;
|
||||||
println!("Registry: {}", reg_dir.display());
|
println!("Registry: {}", reg_dir.display());
|
||||||
|
|
||||||
@@ -116,7 +119,7 @@ fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> {
|
|||||||
.status()
|
.status()
|
||||||
.context("Failed to copy vmlinuz out of the image mount")?;
|
.context("Failed to copy vmlinuz out of the image mount")?;
|
||||||
if !cp_status.success() {
|
if !cp_status.success() {
|
||||||
anyhow::bail!("Failed to copy vmlinuz to registry");
|
bail!("Failed to copy vmlinuz to registry");
|
||||||
}
|
}
|
||||||
println!("Copied vmlinuz -> {}", vmlinuz_dst.display());
|
println!("Copied vmlinuz -> {}", vmlinuz_dst.display());
|
||||||
|
|
||||||
@@ -132,7 +135,7 @@ fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> {
|
|||||||
.context("Failed to spawn the cpio pipeline")?;
|
.context("Failed to spawn the cpio pipeline")?;
|
||||||
|
|
||||||
let initrd_file = File::create(&initrd_path).context("Failed to create initrd file")?;
|
let initrd_file = File::create(&initrd_path).context("Failed to create initrd file")?;
|
||||||
let mut encoder = flate2::write::GzEncoder::new(initrd_file, flate2::Compression::default());
|
let mut encoder = GzEncoder::new(initrd_file, flate2::Compression::default());
|
||||||
let cpio_stdout = cpio.stdout.take().context("cpio stdout was not piped")?;
|
let cpio_stdout = cpio.stdout.take().context("cpio stdout was not piped")?;
|
||||||
let copied = std::io::copy(&mut std::io::BufReader::new(cpio_stdout), &mut encoder);
|
let copied = std::io::copy(&mut std::io::BufReader::new(cpio_stdout), &mut encoder);
|
||||||
let stream_result = copied
|
let stream_result = copied
|
||||||
@@ -148,7 +151,7 @@ fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> {
|
|||||||
}
|
}
|
||||||
if !cpio_status.success() {
|
if !cpio_status.success() {
|
||||||
let _ = fs::remove_file(&initrd_path);
|
let _ = fs::remove_file(&initrd_path);
|
||||||
anyhow::bail!("find/cpio pipeline failed with status {cpio_status}");
|
bail!("find/cpio pipeline failed with status {cpio_status}");
|
||||||
}
|
}
|
||||||
println!("Created initrd -> {}", initrd_path.display());
|
println!("Created initrd -> {}", initrd_path.display());
|
||||||
|
|
||||||
@@ -156,11 +159,11 @@ fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(name: &str) -> Result<()> {
|
fn run(name: &str) -> Result<()> {
|
||||||
let reg_dir = registry_dir(name);
|
let reg_dir = registry_dir(name)?;
|
||||||
let vmlinuz_path = reg_dir.join("vmlinuz");
|
let vmlinuz_path = reg_dir.join("vmlinuz");
|
||||||
let initrd_path = reg_dir.join("initrd");
|
let initrd_path = reg_dir.join("initrd");
|
||||||
if !vmlinuz_path.exists() || !initrd_path.exists() {
|
if !vmlinuz_path.exists() || !initrd_path.exists() {
|
||||||
anyhow::bail!(
|
bail!(
|
||||||
"Registry missing vmlinuz/initrd for '{}'. Run `slim build` first.",
|
"Registry missing vmlinuz/initrd for '{}'. Run `slim build` first.",
|
||||||
name
|
name
|
||||||
);
|
);
|
||||||
@@ -171,34 +174,36 @@ fn run(name: &str) -> Result<()> {
|
|||||||
vmlinuz_path.display(),
|
vmlinuz_path.display(),
|
||||||
initrd_path.display()
|
initrd_path.display()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let cmdline = [
|
||||||
|
"console=ttyS0,115200",
|
||||||
|
"root=/dev/ram0",
|
||||||
|
"rw",
|
||||||
|
"earlyprintk=serial",
|
||||||
|
"nokaslr",
|
||||||
|
"raid=noautodetect",
|
||||||
|
]
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
// Launch with a 5-second timeout for quick smoke verification
|
// Launch with a 5-second timeout for quick smoke verification
|
||||||
let status = Command::new("timeout")
|
let status = Command::new("qemu-system-x86_64")
|
||||||
.args([
|
.args(["-m", "1024M"])
|
||||||
"5",
|
.arg("-kernel")
|
||||||
"qemu-system-x86_64",
|
.arg(vmlinuz_path.as_os_str())
|
||||||
"-m",
|
.arg("-initrd")
|
||||||
"256",
|
.arg(initrd_path.as_os_str())
|
||||||
"-nographic",
|
.args(["-append", &cmdline])
|
||||||
"-kernel",
|
.args(["-nographic"])
|
||||||
vmlinuz_path.to_str().unwrap(),
|
|
||||||
"-initrd",
|
|
||||||
initrd_path.to_str().unwrap(),
|
|
||||||
"-append",
|
|
||||||
"console=ttyS0",
|
|
||||||
])
|
|
||||||
.status()
|
.status()
|
||||||
.context("Failed to launch qemu-system-x86_64")?;
|
.context("Failed to launch qemu-system-x86_64")?;
|
||||||
|
|
||||||
// `timeout` exits 124 when it kills QEMU after the 5s smoke-test
|
|
||||||
// window; any other non-zero status is a real failure.
|
|
||||||
match status.code() {
|
match status.code() {
|
||||||
Some(0) => println!("QEMU exited cleanly."),
|
Some(0) => println!("QEMU exited cleanly."),
|
||||||
Some(124) => println!("QEMU timed out after 5s (expected during smoke boot)."),
|
|
||||||
Some(126) | Some(127) => {
|
Some(126) | Some(127) => {
|
||||||
anyhow::bail!("Failed to start qemu-system-x86_64 (is it installed and in PATH?)")
|
bail!("Failed to start qemu-system-x86_64 (is it installed and in PATH?)")
|
||||||
}
|
}
|
||||||
Some(code) => anyhow::bail!("QEMU exited with code {code}"),
|
Some(code) => bail!("QEMU exited with code {code}"),
|
||||||
None => anyhow::bail!("QEMU terminated by a signal"),
|
None => bail!("QEMU terminated by a signal"),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user