Add qcow2 support
CI / build (push) Successful in 13s

This commit is contained in:
2026-08-30 17:08:08 +02:00
parent 46a86116b0
commit 286def05c1
7 changed files with 303 additions and 50 deletions
Generated
+108
View File
@@ -137,6 +137,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.14" version = "0.3.14"
@@ -174,12 +180,28 @@ dependencies = [
"r-efi", "r-efi",
] ]
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 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]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
version = "1.70.2" version = "1.70.2"
@@ -266,6 +288,45 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "simd-adler32" name = "simd-adler32"
version = "0.3.10" version = "0.3.10"
@@ -280,7 +341,9 @@ dependencies = [
"clap", "clap",
"cpio", "cpio",
"flate2", "flate2",
"serde",
"tempfile", "tempfile",
"toml",
"walkdir", "walkdir",
"xdg", "xdg",
] ]
@@ -315,6 +378,45 @@ dependencies = [
"windows-sys", "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]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.24" version = "1.0.24"
@@ -361,6 +463,12 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "winnow"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81"
[[package]] [[package]]
name = "xdg" name = "xdg"
version = "3.0.0" version = "3.0.0"
+3
View File
@@ -10,6 +10,9 @@ flate2 = "1.1.9"
walkdir = "2.5.0" walkdir = "2.5.0"
cpio = "0.4.1" cpio = "0.4.1"
xdg = "3.0.0" xdg = "3.0.0"
tempfile = "3.27.0"
toml = "1.1.4"
serde = { version = "1.0.229", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
tempfile = "3.27.0" tempfile = "3.27.0"
+95 -22
View File
@@ -2,19 +2,46 @@
//! gzipped cpio initrd in the registry. //! gzipped cpio initrd in the registry.
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use clap::{Args, ValueEnum};
use flate2::write::GzEncoder; 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};
use tempfile::NamedTempFile;
use crate::command::cmd;
use crate::meta::Meta;
use crate::registry::registry_dir; 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)?; let mount_path = mount_image(image)?;
println!("Mounted at: {}", mount_path.display()); println!("Mounted at: {}", mount_path.display());
// Keep the build result so the image is unmounted even when the build fails. // 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") let unmounted = Command::new("podman")
.args(["unshare", "--", "podman", "image", "unmount", image]) .args(["unshare", "--", "podman", "image", "unmount", image])
@@ -26,38 +53,84 @@ pub(crate) fn build(image: &str) -> Result<()> {
eprintln!("warning: failed to unmount image '{}'", image); eprintln!("warning: failed to unmount image '{}'", image);
} }
Meta { init }.save(image)?;
result result
} }
fn mount_image(image: &str) -> Result<PathBuf> { fn mount_image(image: &str) -> Result<PathBuf> {
let output = Command::new("podman") let mount_path = cmd(&["podman", "unshare", "--", "podman", "image", "mount", image])?;
.args(["unshare", "--", "podman", "image", "mount", image]) Ok(PathBuf::from(mount_path.trim()))
.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))
} }
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<NamedTempFile> {
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<NamedTempFile> {
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(&reg_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 mount_path = mount_path.to_str().context("mount path is not UTF-8")?;
let vmlinuz_src = format!("{mount_path}/vmlinuz"); let vmlinuz_src = format!("{mount_path}/vmlinuz");
// The rootless overlay mount only exists inside podman unshare's user // The rootless overlay mount only exists inside podman unshare's user
// namespace, so every read of the rootfs must run there; pipes still // namespace, so every read of the rootfs must run there; pipes still
// cross the namespace boundary. // cross the namespace boundary.
let has_kernel = Command::new("podman") let _has_kernel = cmd(&["podman", "unshare", "--", "test", "-f", &vmlinuz_src])
.args(["unshare", "--", "test", "-f", &vmlinuz_src]) .context("Image missing required /vmlinuz. Place kernel at /vmlinuz in Containerfile.")?;
.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 reg_dir = registry_dir(image)?; let reg_dir = registry_dir(image)?;
fs::create_dir_all(&reg_dir)?; fs::create_dir_all(&reg_dir)?;
+27
View File
@@ -0,0 +1,27 @@
use std::process::Command;
use anyhow::{Context, anyhow, bail};
pub fn cmd(cmd: &[&str]) -> anyhow::Result<String> {
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())
}
+6 -10
View File
@@ -1,12 +1,14 @@
mod build; mod build;
mod command;
mod image; mod image;
mod meta;
mod qemu; mod qemu;
mod registry; mod registry;
use anyhow::Result; use anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use crate::{image::ImageCmd, qemu::RunCmd}; use crate::{build::BuildCmd, image::ImageCmd, qemu::RunCmd};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(about = "Build bootable initrd VMs from container images")] #[command(about = "Build bootable initrd VMs from container images")]
@@ -18,13 +20,7 @@ struct Cli {
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
enum Commands { enum Commands {
/// Build initrd from an image (podman image must exist) /// Build initrd from an image (podman image must exist)
Build { Build(BuildCmd),
/// 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,
},
/// Launch a previously built VM via QEMU. /// Launch a previously built VM via QEMU.
Run(RunCmd), Run(RunCmd),
/// List slim images in the registry /// List slim images in the registry
@@ -35,8 +31,8 @@ enum Commands {
fn main() -> Result<()> { fn main() -> Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
match cli.command { match cli.command {
Commands::Build { image, _size: _ } => { Commands::Build(cmd) => {
build::build(&image)?; build::build(cmd)?;
} }
Commands::Run(cmd) => { Commands::Run(cmd) => {
qemu::run(cmd)?; qemu::run(cmd)?;
+28
View File
@@ -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<Self> {
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(())
}
}
+36 -18
View File
@@ -5,7 +5,10 @@ use anyhow::{Context, Result, bail};
use clap::Args; use clap::Args;
use std::process::Command; use std::process::Command;
use crate::registry::registry_dir; use crate::{
meta::Meta,
registry::{registry_base_dir, registry_dir},
};
#[derive(Args, Debug)] #[derive(Args, Debug)]
pub struct RunCmd { pub struct RunCmd {
@@ -19,14 +22,38 @@ pub struct RunCmd {
pub(crate) fn run(RunCmd { name, memory }: RunCmd) -> Result<()> { pub(crate) fn run(RunCmd { name, memory }: RunCmd) -> Result<()> {
let reg_dir = registry_dir(&name)?; 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"); let initrd_path = reg_dir.join("initrd");
if !vmlinuz_path.exists() || !initrd_path.exists() { let qcow2_path = reg_dir.join("image.qcow2");
bail!( if !vmlinuz_path.exists() {
"Registry missing vmlinuz/initrd for '{}'. Run `slim build` first.", // TODO: guide user in how to set up a kernel
name 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!("Booting {name} from registry: {}", reg_dir.display());
println!( println!(
" qemu-system-x86_64 -m 256 -nographic -kernel {} -initrd {} -append 'console=ttyS0'", " 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() initrd_path.display()
); );
let cmdline = [ let cmdline = cmdline.join(" ");
"console=ttyS0,115200",
"root=/dev/ram0",
"rw",
"earlyprintk=serial",
"nokaslr",
"raid=noautodetect",
]
.join(" ");
let mut command = Command::new("qemu-system-x86_64"); let mut command = Command::new("qemu-system-x86_64");
command command
.args(["-m", &memory]) .args(["-m", &memory])
.arg("-kernel") .arg("-kernel")
.arg(vmlinuz_path.as_os_str()) .arg(vmlinuz_path.as_os_str())
.arg("-initrd") .args(fs_args)
.arg(initrd_path.as_os_str())
.args(["-append", &cmdline]) .args(["-append", &cmdline])
.args(["-nographic"]) .args(["-nographic"])
.args(["-nic", "user,model=virtio-net-pci"]); .args(["-nic", "user,model=virtio-net-pci"]);