From 286def05c10b25534c8ee996dfadd8cb211177b6 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Sun, 30 Aug 2026 17:08:08 +0200 Subject: [PATCH] Add qcow2 support --- Cargo.lock | 108 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/build.rs | 117 +++++++++++++++++++++++++++++++++++++++---------- src/command.rs | 27 ++++++++++++ src/main.rs | 16 +++---- src/meta.rs | 28 ++++++++++++ src/qemu.rs | 54 +++++++++++++++-------- 7 files changed, 303 insertions(+), 50 deletions(-) create mode 100644 src/command.rs create mode 100644 src/meta.rs diff --git a/Cargo.lock b/Cargo.lock index 27422fd..94e5293 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -137,6 +137,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "errno" version = "0.3.14" @@ -174,12 +180,28 @@ dependencies = [ "r-efi", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "indexmap" +version = "2.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07aa2048142242915a31d35844fb311e0e53fcca590c3a0a40dcf1b841fa09eb" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -266,6 +288,45 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +dependencies = [ + "serde_core", +] + [[package]] name = "simd-adler32" version = "0.3.10" @@ -280,7 +341,9 @@ dependencies = [ "clap", "cpio", "flate2", + "serde", "tempfile", + "toml", "walkdir", "xdg", ] @@ -315,6 +378,45 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "toml" +version = "1.1.4+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3aace63f4bbcdfc2c965b059de67119c89c4017a70d633be6c104910f67056f5" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" + [[package]] name = "unicode-ident" version = "1.0.24" @@ -361,6 +463,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" + [[package]] name = "xdg" version = "3.0.0" diff --git a/Cargo.toml b/Cargo.toml index 9acdfe4..6678f4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ flate2 = "1.1.9" walkdir = "2.5.0" cpio = "0.4.1" xdg = "3.0.0" +tempfile = "3.27.0" +toml = "1.1.4" +serde = { version = "1.0.229", features = ["derive"] } [dev-dependencies] tempfile = "3.27.0" diff --git a/src/build.rs b/src/build.rs index b1a585d..db4aff4 100644 --- a/src/build.rs +++ b/src/build.rs @@ -2,19 +2,46 @@ //! gzipped cpio initrd in the registry. use anyhow::{Context, Result, bail}; +use clap::{Args, ValueEnum}; use flate2::write::GzEncoder; use std::fs::{self, File}; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; +use tempfile::NamedTempFile; +use crate::command::cmd; +use crate::meta::Meta; use crate::registry::registry_dir; -pub(crate) fn build(image: &str) -> Result<()> { +#[derive(Args, Debug)] +pub struct BuildCmd { + kind: ImageKind, + + /// Image tag / registry subdir name + name: String, + + /// The path to the `init` program for the VM. + // TODO: infer init based on `COMMAND`/`ENTRYPOINT`? + #[clap(long, default_value = "/sbin/init")] + init: String, +} + +#[derive(ValueEnum, Clone, Debug)] +enum ImageKind { + Qcow2, + Initrd, +} + +pub(crate) fn build(BuildCmd { kind, name, init }: BuildCmd) -> Result<()> { + let image = &name; let mount_path = mount_image(image)?; println!("Mounted at: {}", mount_path.display()); // Keep the build result so the image is unmounted even when the build fails. - let result = build_artifacts(image, &mount_path); + let result = match kind { + ImageKind::Initrd => build_initrd(image, &mount_path), + ImageKind::Qcow2 => build_qcow2(image, &mount_path), + }; let unmounted = Command::new("podman") .args(["unshare", "--", "podman", "image", "unmount", image]) @@ -26,38 +53,84 @@ pub(crate) fn build(image: &str) -> Result<()> { eprintln!("warning: failed to unmount image '{}'", image); } + Meta { init }.save(image)?; + result } fn mount_image(image: &str) -> Result { - let output = Command::new("podman") - .args(["unshare", "--", "podman", "image", "mount", image]) - .output() - .context("Failed to run podman image mount")?; - let mount_path = String::from_utf8_lossy(&output.stdout).trim().to_string(); - if !output.status.success() || mount_path.is_empty() { - bail!( - "podman image mount failed: {}", - String::from_utf8_lossy(&output.stderr).trim() - ); - } - Ok(PathBuf::from(mount_path)) + let mount_path = cmd(&["podman", "unshare", "--", "podman", "image", "mount", image])?; + Ok(PathBuf::from(mount_path.trim())) } -fn build_artifacts(image: &str, mount_path: &Path) -> Result<()> { +/// Copy a directory onto a new raw disk image with EXT4. +fn to_raw_ext4(dir: &Path) -> Result { + let dir = dir.to_str().context("Invalid UTF-8")?; + let du_out = cmd(&["podman", "unshare", "--", "du", "-sk", dir])?; + + let used_kb: u64 = du_out + .split_whitespace() + .next() + .context("du produced no output")? + .parse() + .context("failed to parse du output")?; + let used = used_kb * 1024; + let mb = 1024 * 1024; + let size = used + + (used / 10) // Add 10% + + 64 * mb; // Add some margin + let size = size.to_string(); + + let raw_file = NamedTempFile::new()?; + let raw_path = raw_file.path().to_str().context("Invalid UTF-8")?; + cmd(&["podman", "unshare", "--", "truncate", "-s", &size, raw_path])?; + cmd(&[ + "podman", + "unshare", + "--", + "mkfs.ext4", + "-F", + "-d", + dir, + raw_path, + ])?; + Ok(raw_file) +} + +/// Copy a directory onto a new qcow2 disk image with EXT4. +fn to_qcow2_ext4(mount_path: &Path) -> Result { + let raw = to_raw_ext4(mount_path)?; + let qcow2 = NamedTempFile::new()?; + let raw_path = raw.path().to_str().context("Invalid UTF-8")?; + let qcow2_path = qcow2.path().to_str().context("Invalid UTF-8")?; + cmd(&[ + "qemu-img", "convert", "-f", "raw", "-O", "qcow2", raw_path, qcow2_path, + ])?; + + Ok(qcow2) +} +fn build_qcow2(image: &str, mount_path: &Path) -> Result<()> { + let reg_dir = registry_dir(image)?; + fs::create_dir_all(®_dir)?; + println!("Registry: {}", reg_dir.display()); + + let qcow2 = to_qcow2_ext4(mount_path)?; + + fs::copy(qcow2.path(), reg_dir.join("image.qcow2")) + .context("Failed to copy qcow2 image to registry")?; + + Ok(()) +} + +fn build_initrd(image: &str, mount_path: &Path) -> Result<()> { let mount_path = mount_path.to_str().context("mount path is not UTF-8")?; let vmlinuz_src = format!("{mount_path}/vmlinuz"); // The rootless overlay mount only exists inside podman unshare's user // namespace, so every read of the rootfs must run there; pipes still // cross the namespace boundary. - let has_kernel = Command::new("podman") - .args(["unshare", "--", "test", "-f", &vmlinuz_src]) - .status() - .context("Failed to check for /vmlinuz inside the image mount")?; - if !has_kernel.success() { - bail!("Image missing required /vmlinuz. Place kernel at /vmlinuz in Containerfile."); - } + let _has_kernel = cmd(&["podman", "unshare", "--", "test", "-f", &vmlinuz_src]) + .context("Image missing required /vmlinuz. Place kernel at /vmlinuz in Containerfile.")?; let reg_dir = registry_dir(image)?; fs::create_dir_all(®_dir)?; diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..8ac1d53 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,27 @@ +use std::process::Command; + +use anyhow::{Context, anyhow, bail}; + +pub fn cmd(cmd: &[&str]) -> anyhow::Result { + let [cmd, args @ ..] = cmd else { + bail!("missing command"); + }; + + let mut command = Command::new(cmd); + command.args(args); + println!("$ {command:?}"); + let output = command + .output() + .with_context(|| anyhow!("Failed to run {command:?}"))?; + + let stdout = String::from_utf8_lossy(&output.stdout); + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + eprintln!("{command:?} failed"); + eprintln!("stdout:\n{stdout}\n"); + eprintln!("stderr:\n{stderr}\n"); + bail!("{command:?} failed with exit code {}", output.status); + } + + Ok(stdout.to_string()) +} diff --git a/src/main.rs b/src/main.rs index 49f42c3..2cb6a59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ mod build; +mod command; mod image; +mod meta; mod qemu; mod registry; use anyhow::Result; use clap::{Parser, Subcommand}; -use crate::{image::ImageCmd, qemu::RunCmd}; +use crate::{build::BuildCmd, image::ImageCmd, qemu::RunCmd}; #[derive(Parser, Debug)] #[command(about = "Build bootable initrd VMs from container images")] @@ -18,13 +20,7 @@ struct Cli { #[derive(Subcommand, Debug)] enum Commands { /// Build initrd from an image (podman image must exist) - Build { - /// Image name (e.g. alpine:3.15 or slim:tag) - image: String, - /// Size hint (unused in v1 initrd) - #[arg(short = 's', long, default_value = "512")] - _size: String, - }, + Build(BuildCmd), /// Launch a previously built VM via QEMU. Run(RunCmd), /// List slim images in the registry @@ -35,8 +31,8 @@ enum Commands { fn main() -> Result<()> { let cli = Cli::parse(); match cli.command { - Commands::Build { image, _size: _ } => { - build::build(&image)?; + Commands::Build(cmd) => { + build::build(cmd)?; } Commands::Run(cmd) => { qemu::run(cmd)?; diff --git a/src/meta.rs b/src/meta.rs new file mode 100644 index 0000000..cb5774a --- /dev/null +++ b/src/meta.rs @@ -0,0 +1,28 @@ +use std::fs; + +use serde::{Deserialize, Serialize}; + +use crate::registry::registry_dir; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Meta { + /// Path to the init program + pub init: String, +} + +const FILENAME: &str = "meta.toml"; + +impl Meta { + pub fn load(image: &str) -> anyhow::Result { + let path = registry_dir(image)?.join(FILENAME); + let s = fs::read_to_string(path)?; + Ok(toml::from_str(&s)?) + } + + pub fn save(&self, image: &str) -> anyhow::Result<()> { + let path = registry_dir(image)?.join(FILENAME); + let s = toml::to_string_pretty(self)?; + fs::write(path, &s)?; + Ok(()) + } +} diff --git a/src/qemu.rs b/src/qemu.rs index f676711..d8e1c04 100644 --- a/src/qemu.rs +++ b/src/qemu.rs @@ -5,7 +5,10 @@ use anyhow::{Context, Result, bail}; use clap::Args; use std::process::Command; -use crate::registry::registry_dir; +use crate::{ + meta::Meta, + registry::{registry_base_dir, registry_dir}, +}; #[derive(Args, Debug)] pub struct RunCmd { @@ -19,14 +22,38 @@ pub struct RunCmd { pub(crate) fn run(RunCmd { name, memory }: RunCmd) -> Result<()> { let reg_dir = registry_dir(&name)?; - let vmlinuz_path = reg_dir.join("vmlinuz"); + let vmlinuz_path = registry_base_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 - ); + let qcow2_path = reg_dir.join("image.qcow2"); + if !vmlinuz_path.exists() { + // TODO: guide user in how to set up a kernel + bail!("No kernel available.") } + + let meta = Meta::load(&name).context("Failed to load meta.toml. Run `slim build` first.")?; + // TODO: sanity-check for spaces + let init_arg = format!("init={}", meta.init); + + let fs_args; + let mut cmdline = vec![ + "console=ttyS0,115200", + "rw", + "earlyprintk=serial", + "nokaslr", + &init_arg, + ]; + + let qcow2_arg = format!("file={},format=qcow2,if=virtio", qcow2_path.display()); + if qcow2_path.exists() { + fs_args = vec!["-drive", &qcow2_arg]; + cmdline.extend_from_slice(&["root=/dev/vda", "rootfstype=ext4"]); + } else if initrd_path.exists() { + fs_args = vec!["-initrd", initrd_path.to_str().context("Invalid UTF-8")?]; + cmdline.push("root=/dev/ram0"); + } else { + bail!("Registry missing rootfs/initrd for '{name}'. Run `slim build` first."); + } + println!("Booting {name} from registry: {}", reg_dir.display()); println!( " qemu-system-x86_64 -m 256 -nographic -kernel {} -initrd {} -append 'console=ttyS0'", @@ -34,23 +61,14 @@ pub(crate) fn run(RunCmd { name, memory }: RunCmd) -> Result<()> { initrd_path.display() ); - let cmdline = [ - "console=ttyS0,115200", - "root=/dev/ram0", - "rw", - "earlyprintk=serial", - "nokaslr", - "raid=noautodetect", - ] - .join(" "); + let cmdline = cmdline.join(" "); let mut command = Command::new("qemu-system-x86_64"); command .args(["-m", &memory]) .arg("-kernel") .arg(vmlinuz_path.as_os_str()) - .arg("-initrd") - .arg(initrd_path.as_os_str()) + .args(fs_args) .args(["-append", &cmdline]) .args(["-nographic"]) .args(["-nic", "user,model=virtio-net-pci"]);