Compare commits

Author SHA1 Message Date
hulthe 27b18a778e Add --guestfwd arg to boco boot
CI / build (push) Successful in 14s
2026-10-27 12:45:11 +01:00
hulthe 3eb5cd434c Add ability to pass arbitrary args to qemu
CI / build (push) Successful in 14s
2026-10-26 10:30:14 +01:00
hulthe fe6b03e268 Add script for setting up boco-init
CI / build (push) Successful in 14s
2026-10-22 14:59:48 +02:00
hulthe 5b2acfa526 Don't work init if user is 0:0 2026-10-22 14:59:28 +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
4 changed files with 45 additions and 21 deletions

No files matched your search

+5 -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,
);
} }
} }
} }
@@ -359,6 +353,9 @@ fn exec_command(argv: &[String], user: Option<UserSpec>) -> ! {
let c_prog = cstrings[0].clone(); let c_prog = cstrings[0].clone();
let c_refs: Vec<&CString> = cstrings.iter().collect(); let c_refs: Vec<&CString> = cstrings.iter().collect();
// If UserSpec is 0:0, don't fork.
let user = user.filter(|u| !u.uid.is_root() || u.gid != Gid::from_raw(0));
match user { match user {
None => { None => {
// No privilege drop: exec directly, PID 1 becomes the command. // No privilege drop: exec directly, PID 1 becomes the command.
@@ -368,6 +365,7 @@ fn exec_command(argv: &[String], user: Option<UserSpec>) -> ! {
std::process::exit(1); std::process::exit(1);
} }
Some(user) => { Some(user) => {
warn(&format!("dropping privileges to {}:{}", user.uid, user.gid));
// Drop privileges in a child so PID 1 (root) can poweroff afterwards. // Drop privileges in a child so PID 1 (root) can poweroff afterwards.
match unsafe { fork() } { match unsafe { fork() } {
Ok(ForkResult::Child) => { Ok(ForkResult::Child) => {
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -ex
cd "$(dirname "$0")/.."
mkdir -p "$HOME/.local/share/boco"
cargo build --release --target x86_64-unknown-linux-musl -p boco-init
target_dir="${CARGO_TARGET_DIR:-target}"
cp "$target_dir/x86_64-unknown-linux-musl/release/boco-init" "$HOME/.local/share/boco/"
+22 -14
View File
@@ -1,13 +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 (under `podman unshare` for rootless
//! UID mapping) per `--mount`, plus a `-chardev socket` + `-device
//! vhost-user-fs-pci` pair on the QEMU side. 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;
@@ -16,7 +7,7 @@ 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, VirtiofsShare}, virtiofs::{self, VirtiofsShare},
}; };
@@ -33,6 +24,10 @@ pub struct BootCmd {
#[clap(long)] #[clap(long)]
forward: Vec<PortForward>, forward: Vec<PortForward>,
/// Pass a `guestfwd=...` argument to qemu `-nic` argument.
#[clap(long)]
guestfwd: Vec<String>,
/// Override the command to exec in the VM (base64-encoded on the kernel /// Override the command to exec in the VM (base64-encoded on the kernel
/// cmdline as `boco.cmd=<b64>`). Overrides the CMD inferred at build time. /// cmdline as `boco.cmd=<b64>`). Overrides the CMD inferred at build time.
#[clap(long)] #[clap(long)]
@@ -44,25 +39,34 @@ 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>,
/// Extra arguments to qemu
extra: Vec<String>,
} }
/// 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,
memory, memory,
forward, forward,
guestfwd,
cmd, cmd,
mount, mount,
extra,
}: 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()
); );
} }
@@ -154,6 +158,9 @@ pub(crate) fn boot(
for forward in &forward { for forward in &forward {
_ = write!(&mut network, ",hostfwd={forward}"); _ = write!(&mut network, ",hostfwd={forward}");
} }
for guestfwd in &guestfwd {
_ = write!(&mut network, ",guestfwd={guestfwd}");
}
let mut command = Command::new("qemu-system-x86_64"); let mut command = Command::new("qemu-system-x86_64");
@@ -170,7 +177,8 @@ pub(crate) fn boot(
.args(["-no-reboot"]) .args(["-no-reboot"])
.args(["-append", &cmdline]) .args(["-append", &cmdline])
.args(["-nographic"]) .args(["-nographic"])
.args(["-nic", &network]); .args(["-nic", &network])
.args(&extra);
println!("{command:?}"); println!("{command:?}");
+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")