Add image gc, stop VMs on kernel panic, harden first boot

- Add `wsbx gc` to delete cached images that no sandbox uses, and
  drop the superseded image automatically when a tag moves to a new
  digest (unless another ref still points at it).
- Boot the guest with panic=1. Virtualization.framework restarts a
  rebooting guest, so init now leaves a marker in the per-launch
  control share and powers off when it is already there. A kernel
  panic (or a reboot inside the VM) now ends the run with exit 1
  instead of hanging or looping forever.
- Report a VM that stops without an exit status as an error.
- Sync the first-boot image unpack before marking the sandbox
  provisioned; a crash right after first boot previously left an
  empty root filesystem.
- Validate kernel, workspace and state-dir paths before creating any
  sandbox files, so rejected runs no longer leave a sandbox behind.
- Ignore .llm-wiki/, commit .gitignore, fix the remaining Clippy
  warning, and document gc and the panic behavior.
This commit is contained in:
2026-10-02 15:33:30 +02:00
parent 9117f05db9
commit cff6dae88f
6 changed files with 104 additions and 23 deletions
+23 -6
View File
@@ -73,6 +73,8 @@ enum Cmd {
Ls,
/// Remove sandboxes
Rm { names: Vec<String> },
/// Delete cached images that no sandbox uses
Gc,
}
#[derive(Args)]
@@ -156,6 +158,17 @@ fn main() -> Result<()> {
Cmd::Gemini(a) => agent(&h, a, "gemini", "gemini", vec![], &["GEMINI_API_KEY", "GOOGLE_API_KEY"], Guest::default()),
Cmd::Pi { a, pi_auth } => pi(&h, a, pi_auth),
Cmd::Ls => ls(&h),
Cmd::Gc => {
let used: Vec<String> = fs::read_dir(h.join("sandboxes"))
.into_iter()
.flatten()
.flatten()
.filter_map(|e| serde_json::from_slice::<Value>(&fs::read(e.path().join("meta.json")).ok()?).ok())
.filter_map(|m| m["image"].as_str().map(String::from))
.collect();
println!("freed {} MB", oci::gc(&h, &used)? / 1_000_000);
Ok(())
}
Cmd::Rm { names } => {
for n in names {
if n.is_empty() || sanitize(&n) != n {
@@ -310,10 +323,10 @@ fn kernel(h: &Path, update: bool, version: Option<String>) -> Result<PathBuf> {
if k.exists() && !update && (pin.is_none() || pin == installed) {
return Ok(k);
}
if let Some(v) = &pin {
if v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)) {
bail!("invalid Kata version {v:?}");
}
if let Some(v) = &pin
&& (v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)))
{
bail!("invalid Kata version {v:?}");
}
let api = match &pin {
Some(v) => format!("https://api.github.com/repos/{KATA_REPO}/releases/tags/{v}"),
@@ -426,7 +439,10 @@ fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Gu
None => kernel(h, false, None)?,
};
let ws = o.workspace.as_ref().map(fs::canonicalize).transpose().context("workspace")?;
vf(h)?; // everything vfkit gets lives under h, plus kernel + workspace: refuse bad paths before creating anything
vf(&kernel)?;
if let Some(ws) = &ws {
vf(ws)?;
let hc = fs::canonicalize(h)?;
if ws.starts_with(&hc) || hc.starts_with(ws) {
bail!("workspace {} overlaps the wsbx state dir {} (disks, kernel, control files)", ws.display(), hc.display());
@@ -564,7 +580,8 @@ fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Gu
let x = name.bytes().fold(0xcbf29ce484222325u64, |a, b| (a ^ b as u64).wrapping_mul(0x100000001b3));
format!("02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", x as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8, (x >> 32) as u8)
};
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet";
// panic=1 reboots, which VZ turns into a restart; init powers off on any second boot (see init.sh).
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet panic=1";
let tty = std::io::stdin().is_terminal();
let mut v = Command::new(vfkit);
v.args(["--cpus", &o.cpus.to_string(), "--memory", &o.memory.to_string(), "--log-level", "error"])
@@ -611,7 +628,7 @@ fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Gu
match code {
Some(c) => std::process::exit(c),
None if !status.success() => bail!("vm failed, see {}", dir.join("vfkit.log").display()),
None => Ok(()),
None => bail!("VM stopped without an exit status (kernel panic, reboot or poweroff inside the VM)"),
}
}