+95
-22
@@ -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<PathBuf> {
|
||||
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<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(®_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)?;
|
||||
|
||||
@@ -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
@@ -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)?;
|
||||
|
||||
+28
@@ -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
@@ -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"]);
|
||||
|
||||
Reference in New Issue
Block a user