Compare commits

..
Author SHA1 Message Date
marvin e7fa17f8dc Add Containerfile: build boco (musl) + slim kernel; runnable runner image
CI / build (pull_request) Successful in 14s
- 3-stage build: musl boco+boco-init (rust>=1.88, alpine:edge), linux-7.2
  bzImage from kernel/config (pinned tarball, sha256-verified), slim runtime
  with qemu/virtiofsd/podman.
- .containerignore: deny-all context (Cargo files, src/, init/, kernel/config)
  so the linux submodule no longer ships into every build.
- Verified end-to-end: kernel boots under KVM, ext4 rootfs, userspace init.
- NOTE: nested rootless podman cannot 'podman unshare -> container mount'
  (build path); boot path works as-is.
2026-10-20 17:26:54 +02:00
hulthe 0dac80b487 cargo fmt
CI / build (push) Successful in 13s
2026-10-20 14:48:27 +02:00
hulthe aa4bc0401b Move vmlinuz out of registry dir.
CI / build (push) Failing after 22s
2026-10-20 14:45:14 +02:00
6 changed files with 111 additions and 95 deletions

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
# Build context: deny everything, then allow only what the Containerfile needs.
# Keeps the linux submodule (and example/, .gitea/, README, etc.) out of the build.
*
!Cargo.toml
!Cargo.lock
!src/
!src/**
!init/
!init/**
!kernel/config
+65
View File
@@ -0,0 +1,65 @@
# boco runner: compiles boco (static musl) AND the shared boco kernel (vmlinuz)
# with the slim config checked into the boco repo, then serves as the runtime
# image for `boco build` / `boco boot` under podman.
#
# Runtime requirements (boco shells out to nested rootless podman):
# --device /dev/kvm KVM accel for the VMs (entrypoint chmods it)
# --device /dev/net/tun only if nested container runs need pasta networking
# volume at /home/boco/.local/share/boco persistent registry (images+kernel)
# NO --privileged: the container's own userns (single mapping) is what makes
# nested `podman unshare` work; ignore_chown_errors handles unmapped uids.
#
# Kernel source: pinned tarball linux-7.2 (= 8d3ae59288f1e7d58d76558a6ee96d533bc5019f,
# verified == refs/tags/v7.2{} via git ls-remote), sha256-checked.
FROM alpine:edge AS build
RUN apk add --no-cache rust cargo git
WORKDIR /src
COPY . .
# alpine's rust targets x86_64-alpine-linux-musl natively (static, no glibc dep)
RUN cargo build --release -p boco -p boco-init \
&& strip target/release/boco target/release/boco-init
FROM alpine:3.22 AS kernel
# g++ for host tools, libelf for objtool, openssl for extract-cert
RUN apk add --no-cache make gcc g++ musl-dev perl flex bison bc \
elfutils-dev openssl-dev findutils tar xz zstd curl linux-headers diffutils
WORKDIR /kbuild
# Pinned upstream tarball: linux-7.2.tar.xz, sha256
# f9fef3d14c0df53819026f4be74459835c2a0b0dcbf5b5bbd9ea19f0829402b3.
# (Equivalent to the upstream shallow pin 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
# = refs/tags/v7.2{} — verified via `git ls-remote`; the tarball avoids
# a multi-GB shallow clone through flaky slirp NAT.)
RUN curl -fsSLo /tmp/linux-7.2.tar.xz https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.2.tar.xz \
&& echo "f9fef3d14c0df53819026f4be74459835c2a0b0dcbf5b5bbd9ea19f0829402b3 /tmp/linux-7.2.tar.xz" | sha256sum -c - \
&& tar -xJf /tmp/linux-7.2.tar.xz \
&& mv linux-7.2 linux
# Install the slim kernel config checked into the boco repo; olddefconfig
# normalizes it against this tree and fails nothing silently afterwards.
COPY kernel/config /kbuild/linux/.config
RUN make -C linux olddefconfig HOSTCC=gcc CC=gcc \
&& grep -qE '^(# )?CONFIG_MODULES' linux/.config
RUN make -C linux -j"$(nproc)" bzImage HOSTCC=gcc CC=gcc \
&& cp linux/arch/x86/boot/bzImage /vmlinuz \
&& ls -lh /vmlinuz
FROM alpine:3.22
RUN apk add --no-cache \
qemu-system-x86_64 qemu-img virtiofsd \
podman fuse3 openssh-client ca-certificates iptables ip6tables \
e2fsprogs cpio passt \
&& adduser -D -s /bin/sh boco \
&& echo boco:100000:65536 > /etc/subuid \
&& echo boco:100000:65536 > /etc/subgid \
&& mkdir -p /etc/containers \
&& printf '[storage]\ndriver = "vfs"\nrunroot = "/run/containers/storage"\ngraphroot = "/var/lib/containers/storage"\n[storage.options]\nignore_chown_errors = "true"\n' > /etc/containers/storage.conf \
&& printf '#!/bin/sh\n[ -e /dev/kvm ] && chmod 666 /dev/kvm\nexec /usr/local/bin/boco "$@"\n' > /usr/local/bin/entrypoint.sh \
&& chmod 755 /usr/local/bin/entrypoint.sh
COPY --from=build /src/target/release/boco /usr/local/bin/boco
COPY --from=build /src/target/release/boco-init /home/boco/.local/share/boco/boco-init
COPY --from=kernel /vmlinuz /home/boco/.local/share/boco/vmlinuz
RUN chmod 755 /usr/local/bin/boco /home/boco/.local/share/boco/boco-init /home/boco/.local/share/boco/vmlinuz
ENV XDG_DATA_HOME=/home/boco/.local/share
WORKDIR /data
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["image", "list"]
+1 -7
View File
@@ -245,13 +245,7 @@ fn setup_virtiofs_shares(workdir: &str) {
}; };
try_io(&format!("mkdir {dest}"), || fs::create_dir_all(&dest)); try_io(&format!("mkdir {dest}"), || fs::create_dir_all(&dest));
try_mount( try_mount(Some(tag), &dest, Some("virtiofs"), MsFlags::empty(), None);
Some(tag),
&dest,
Some("virtiofs"),
MsFlags::empty(),
None,
);
} }
} }
} }
+10 -25
View File
@@ -1,15 +1,4 @@
//! QEMU: boot a registry entry with qemu-system-x86_64 using direct kernel //! See [`boot`]
//! boot (-kernel/-initrd or -drive). The kernel is shared across all VMs
//! at $XDG_DATA_HOME/boco/registry/vmlinuz. A universal /boco/init script (injected
//! at build time) handles VM bootstrap; /boco/exec runs the container's
//! CMD/ENTRYPOINT. Runtime overrides are delivered as `boco.cmd=<base64>`
//! on the kernel cmdline. Host directories are shared into the guest via
//! virtio-fs: one virtiofsd daemon per `--mount`, plus a `-chardev socket`
//! + `-device vhost-user-fs-pci` pair on the QEMU side. virtiofsd runs
//! under `podman unshare` (rootless UID mapping) by default, or directly
//! as the invoking user with `--mount-mode direct` (1:1 UID mapping).
//! The guest mounts each share with `mount -t virtiofs <tag> <dest>`
//! (see `boco.mount=` cmdline).
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use base64::Engine; use base64::Engine;
@@ -18,8 +7,8 @@ use std::{fmt::Write as _, process::Command};
use crate::{ use crate::{
forward::PortForward, forward::PortForward,
registry::{registry_base_dir, registry_dir}, registry::{registry_dir, vmlinuz_path},
virtiofs::{self, MountMode, VirtiofsShare}, virtiofs::{self, VirtiofsShare},
}; };
#[derive(Args, Debug)] #[derive(Args, Debug)]
@@ -46,15 +35,12 @@ pub struct BootCmd {
/// relative to the image's WORKINGDIR. Can be repeated. /// relative to the image's WORKINGDIR. Can be repeated.
#[clap(long)] #[clap(long)]
mount: Vec<String>, mount: Vec<String>,
/// How virtiofsd is launched: `unshare` runs it inside
/// `podman unshare` (rootless UID mapping, guest UIDs map into the
/// subuid range); `direct` runs it as the invoking user (guest UIDs
/// map 1:1 to host UIDs — needed for nested virtiofs shares).
#[clap(long, value_enum, default_value = "unshare")]
mount_mode: MountMode,
} }
/// Invoke `qemu` to boot a boco image. See [`BootCmd`] for options
///
/// This uses qemu's direct-kernel-boot with `-kernel`, so it does not require a bootloader.
/// The boco image is just an ext4 rootfs.
pub(crate) fn boot( pub(crate) fn boot(
BootCmd { BootCmd {
name, name,
@@ -62,17 +48,16 @@ pub(crate) fn boot(
forward, forward,
cmd, cmd,
mount, mount,
mount_mode,
}: BootCmd, }: BootCmd,
) -> Result<()> { ) -> Result<()> {
let reg_dir = registry_dir(&name)?; let reg_dir = registry_dir(&name)?;
let vmlinuz_path = registry_base_dir()?.join("vmlinuz"); let vmlinuz_path = vmlinuz_path()?;
let initrd_path = reg_dir.join("initrd"); let initrd_path = reg_dir.join("initrd");
let qcow2_path = reg_dir.join("image.qcow2"); let qcow2_path = reg_dir.join("image.qcow2");
if !vmlinuz_path.exists() { if !vmlinuz_path.exists() {
// TODO: guide user in how to set up a kernel // TODO: guide user in how to set up a kernel
bail!( bail!(
"No kernel available. Place a vmlinuz at {}", "No kernel available. Expected vmlinuz at {}",
vmlinuz_path.display() vmlinuz_path.display()
); );
} }
@@ -118,7 +103,7 @@ pub(crate) fn boot(
Some((h, g)) => (h, g.to_string()), Some((h, g)) => (h, g.to_string()),
None => (spec.as_str(), spec.to_string()), None => (spec.as_str(), spec.to_string()),
}; };
let share = virtiofs::spawn_share(i, host_path, guest_path, mount_mode)?; let share = virtiofs::spawn_share(i, host_path, guest_path)?;
let tag = share.tag.clone(); let tag = share.tag.clone();
let chardev = format!("char{i}"); let chardev = format!("char{i}");
let socket = share let socket = share
+6
View File
@@ -26,6 +26,12 @@ pub(crate) fn validate_image_name(name: &str) -> Result<()> {
Ok(()) Ok(())
} }
pub(crate) fn vmlinuz_path() -> Result<PathBuf> {
let base = xdg::BaseDirectories::with_prefix("boco");
base.place_data_file("vmlinuz")
.context("Failed to create XDG_DATA_HOME subdirectory")
}
pub(crate) fn registry_base_dir() -> Result<PathBuf> { pub(crate) fn registry_base_dir() -> Result<PathBuf> {
let base = xdg::BaseDirectories::with_prefix("boco"); let base = xdg::BaseDirectories::with_prefix("boco");
base.create_data_directory("registry") base.create_data_directory("registry")
+19 -63
View File
@@ -6,21 +6,13 @@
//! hands the socket path to QEMU via `-chardev socket,path=...`, and //! hands the socket path to QEMU via `-chardev socket,path=...`, and
//! cleans up when the VM exits. //! cleans up when the VM exits.
//! //!
//! Launch modes ([`MountMode`]): //! To serve files owned by arbitrary guest UIDs (e.g. when an image
//! //! uses the `USER` directive) without running as root, virtiofsd is
//! * `unshare` (default): virtiofsd runs under `podman unshare` in a //! launched under `podman unshare` in a user namespace with
//! user namespace with `--sandbox chroot`. This serves files owned //! `--sandbox chroot`. This reuses rootless podman's existing
//! by arbitrary guest UIDs (e.g. when an image uses the `USER` //! subuid/subgid mapping: guest UID 0 maps to the invoking user, and
//! directive) without running as root, reusing rootless podman's //! other guest UIDs map into the subuid range, so each guest UID
//! subuid/subgid mapping: guest UID 0 maps to the invoking user, and //! becomes a distinct host UID.
//! other guest UIDs map into the subuid range, so each guest UID
//! becomes a distinct host UID.
//!
//! * `direct`: virtiofsd runs as the invoking user with `--sandbox
//! none`. Guest UIDs map 1:1 to host UIDs. Use this when the
//! shared directory lives on a filesystem that rejects subuid-range
//! UIDs (e.g. nested virtiofs), or when the guest should act as the
//! invoking user rather than the subuid range.
//! //!
//! Lifecycle: boco spawns all virtiofsd children before launching QEMU, //! Lifecycle: boco spawns all virtiofsd children before launching QEMU,
//! waits for each listening socket to appear, then drops the //! waits for each listening socket to appear, then drops the
@@ -36,26 +28,10 @@ use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail}; use anyhow::{Context, Result, anyhow, bail};
use clap::ValueEnum;
use tempfile::TempDir; use tempfile::TempDir;
use crate::command::cmd; use crate::command::cmd;
/// How virtiofsd is launched for a share.
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum MountMode {
/// Run virtiofsd inside `podman unshare` (rootless UID mapping:
/// guest UID 0 maps to the invoking user, other guest UIDs map
/// into the subuid range).
Unshare,
/// Run virtiofsd directly as the invoking user. Guest UIDs map 1:1
/// to host UIDs. Needed when the shared directory itself lives on
/// a virtiofs/fuse mount that rejects subuid-range UIDs (e.g.
/// nested boco), where writes through the subuid mapping fail with
/// EINVAL.
Direct,
}
/// Locations searched for the virtiofsd binary, in order. /// Locations searched for the virtiofsd binary, in order.
const VIRTIOFSD_CANDIDATES: &[&str] = &[ const VIRTIOFSD_CANDIDATES: &[&str] = &[
"/usr/libexec/virtiofsd", "/usr/libexec/virtiofsd",
@@ -138,12 +114,7 @@ fn wait_for_socket(socket: &Path) -> Result<()> {
/// ///
/// `index` becomes the suffix of the tag (`boco{index}`) and is used to /// `index` becomes the suffix of the tag (`boco{index}`) and is used to
/// derive a unique socket filename. /// derive a unique socket filename.
pub fn spawn_share( pub fn spawn_share(index: usize, host_path: &str, guest_path: String) -> Result<VirtiofsShare> {
index: usize,
host_path: &str,
guest_path: String,
mode: MountMode,
) -> Result<VirtiofsShare> {
let virtiofsd = find_virtiofsd()?; let virtiofsd = find_virtiofsd()?;
let canonical = resolve_host_dir(host_path)?; let canonical = resolve_host_dir(host_path)?;
let host_str = canonical let host_str = canonical
@@ -157,29 +128,17 @@ pub fn spawn_share(
let tag = format!("boco{index}"); // short tag to fit on cmdline let tag = format!("boco{index}"); // short tag to fit on cmdline
let socket = dir.path().join(format!("{tag}.sock")); let socket = dir.path().join(format!("{tag}.sock"));
// In `unshare` mode, run virtiofsd under `podman unshare` so it can // TODO: Make rootless mode optional
// serve files owned by arbitrary guest UIDs without root, mapping
// them into the host user's subuid range. `--sandbox chroot` is // Run virtiofsd under `podman unshare` so it can serve files owned
// required because the outer `podman unshare` already entered a user // by arbitrary guest UIDs without root, mapping them into the host
// namespace (nested user namespaces are not allowed); chroot still // user's subuid range. `--sandbox chroot` is required because the
// prevents symlink escapes from the shared dir. // outer `podman unshare` already entered a user namespace (nested
// // user namespaces are not allowed); chroot still prevents symlink
// In `direct` mode, run virtiofsd as the invoking user with 1:1 UID // escapes from the shared dir.
// mapping. This cannot use the chroot sandbox (unprivileged let mut command = Command::new("podman");
// chroot is not permitted), so it falls back to `--sandbox none`.
let mut command = match mode {
MountMode::Unshare => {
let mut command = Command::new("podman");
command.args(["unshare", "--", virtiofsd_str, "--socket-path"]);
command
}
MountMode::Direct => {
let mut command = Command::new(virtiofsd_str);
command.arg("--socket-path");
command
}
};
command command
.args(["unshare", "--", virtiofsd_str, "--socket-path"])
.arg(&socket) .arg(&socket)
.args([ .args([
"--shared-dir", "--shared-dir",
@@ -187,10 +146,7 @@ pub fn spawn_share(
"--tag", "--tag",
&tag, &tag,
"--sandbox", "--sandbox",
match mode { "chroot",
MountMode::Unshare => "chroot",
MountMode::Direct => "none",
},
"--cache", "--cache",
"auto", "auto",
]) ])