feat: split up code at logical boundaries

This commit is contained in:
2026-10-02 17:40:28 +02:00
parent 9706e9aaf2
commit a1bca2e188
10 changed files with 761 additions and 679 deletions
+61
View File
@@ -0,0 +1,61 @@
//! Sandbox root disk: empty ext4 seeded with a static busybox + stub init.
use crate::oci;
use crate::util::need;
use anyhow::{Result, bail};
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Baked into each disk once; everything else comes fresh from the host share on every start.
const STUB_INIT: &str = "#!/.wsbx/busybox sh
/.wsbx/busybox mount -t virtiofs wsbx /.wsbx/host && exec /.wsbx/busybox sh /.wsbx/host/init
echo 'wsbx: cannot mount host share'; exec /.wsbx/busybox sh
";
/// Static busybox from the official busybox image; runs PID 1 + helpers in the guest.
fn busybox(h: &Path) -> Result<PathBuf> {
let p = h.join("busybox");
if p.exists() {
return Ok(p);
}
let img = oci::pull(h, "busybox:musl", false)?;
let tmp = h.join("busybox.tmp");
fs::create_dir_all(&tmp)?;
let ok = Command::new("tar").arg("-xf").arg(&img.rootfs).arg("-C").arg(&tmp).arg("bin").status()?.success();
if !ok {
bail!("failed to extract busybox");
}
// Must be a real file: a symlink would make us copy an arbitrary host file into guest disks.
if !fs::symlink_metadata(tmp.join("bin/busybox"))?.is_file() {
bail!("busybox image: bin/busybox is not a regular file");
}
fs::rename(tmp.join("bin/busybox"), &p)?;
fs::remove_dir_all(tmp)?;
Ok(p)
}
pub fn create_disk(h: &Path, disk: &Path, gib: u32) -> Result<()> {
let mke2fs = need("mke2fs", "e2fsprogs")?;
let seed = disk.with_extension("seed");
let _ = fs::remove_dir_all(&seed);
for d in [".wsbx/dev", ".wsbx/host"] {
fs::create_dir_all(seed.join(d))?;
}
fs::copy(busybox(h)?, seed.join(".wsbx/busybox"))?;
fs::write(seed.join(".wsbx/init"), STUB_INIT)?;
for f in ["busybox", "init"] {
fs::set_permissions(seed.join(".wsbx").join(f), fs::Permissions::from_mode(0o755))?;
}
let out = Command::new(mke2fs)
.args(["-q", "-F", "-t", "ext4", "-E", "root_owner=0:0", "-d"])
.arg(&seed)
.arg(disk)
.arg(format!("{gib}G"))
.output()?;
fs::remove_dir_all(&seed)?;
if !out.status.success() {
bail!("mke2fs failed: {}", String::from_utf8_lossy(&out.stderr));
}
Ok(())
}