//! 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 "; /// busybox:musl pinned by digest: this binary is PID 1 in every VM, so a moved tag must not change it. /// Bump deliberately (index digest of `docker.io/library/busybox:musl`). const BUSYBOX: &str = "busybox@sha256:ea2b9914a16a4ac1981994af97b318f7c7d4db76b580c56177f08bf76f4a0be8"; /// Static busybox from the official busybox image; runs PID 1 + helpers in the guest. fn busybox(h: &Path) -> Result { let p = h.join("busybox"); if p.exists() { return Ok(p); } let img = oci::pull(h, BUSYBOX, 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(()) }