ps, rm --disk, release workflow
release / release (push) Successful in 2s
release / build (ubuntu-latest) (push) Successful in 1m0s
release / build (macos-arm64) (push) Successful in 21s

This commit is contained in:
2026-10-02 17:08:19 +02:00
parent cff6dae88f
commit 9706e9aaf2
3 changed files with 95 additions and 12 deletions
+34 -10
View File
@@ -69,10 +69,17 @@ enum Cmd {
#[arg(long)]
pi_auth: bool,
},
/// List sandboxes
/// List sandboxes and whether they're running
#[command(visible_alias = "ps")]
Ls,
/// Remove sandboxes
Rm { names: Vec<String> },
/// Remove stopped sandboxes
Rm {
#[arg(required = true)]
names: Vec<String>,
/// Only delete the disk (fresh disk on next start; name and settings kept)
#[arg(long)]
disk: bool,
},
/// Delete cached images that no sandbox uses
Gc,
}
@@ -169,7 +176,7 @@ fn main() -> Result<()> {
println!("freed {} MB", oci::gc(&h, &used)? / 1_000_000);
Ok(())
}
Cmd::Rm { names } => {
Cmd::Rm { names, disk } => {
for n in names {
if n.is_empty() || sanitize(&n) != n {
bail!("invalid sandbox name {n:?}");
@@ -178,8 +185,17 @@ fn main() -> Result<()> {
if !d.join("meta.json").exists() {
bail!("no sandbox {n}");
}
fs::remove_dir_all(d)?;
println!("removed {n}");
// Held across the delete so the sandbox can't be started meanwhile.
let _lock = lock_stopped(&d).with_context(|| format!("sandbox {n} is running"))?;
if disk {
match fs::remove_file(d.join("disk.img")) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => println!("removed disk of {n}"),
}
} else {
fs::remove_dir_all(d)?;
println!("removed {n}");
}
}
Ok(())
}
@@ -453,8 +469,7 @@ fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Gu
let name = sanitize(&o.name.clone().unwrap_or_else(|| format!("{short}-{}", ws.as_deref().map(base).unwrap_or("vm".into()))));
let dir = h.join("sandboxes").join(&name);
fs::create_dir_all(&dir)?;
let lock = fs::File::create(dir.join("lock"))?;
lock.try_lock().map_err(|_| anyhow!("sandbox {name} is already running"))?;
let _lock = lock_stopped(&dir).map_err(|_| anyhow!("sandbox {name} is already running"))?;
// The control share is writable by the guest (virtio-fs has no host-enforced read-only).
// Recreate it empty on every start -- while this sandbox's VM is stopped and we hold the lock --
@@ -728,8 +743,15 @@ fn now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
}
/// The lock `run` holds for the VM's lifetime; Err if the sandbox is running.
fn lock_stopped(dir: &Path) -> Result<fs::File> {
let f = fs::File::create(dir.join("lock"))?;
f.try_lock().map_err(|_| anyhow!("locked by a running VM"))?;
Ok(f)
}
fn ls(h: &Path) -> Result<()> {
println!("{:<28} {:<40} {:>8} WORKSPACE", "NAME", "IMAGE", "DISK");
println!("{:<28} {:<8} {:<40} {:>8} WORKSPACE", "NAME", "STATUS", "IMAGE", "DISK");
let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(()) };
let mut rows: Vec<_> = rd.flatten().collect();
rows.sort_by_key(|e| e.file_name());
@@ -737,9 +759,11 @@ fn ls(h: &Path) -> Result<()> {
let Ok(m) = fs::read(e.path().join("meta.json")) else { continue };
let m: Value = serde_json::from_slice(&m)?;
let used = fs::metadata(e.path().join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0);
let status = if lock_stopped(&e.path()).is_err() { "running" } else { "stopped" };
println!(
"{:<28} {:<40} {:>6}MB {}",
"{:<28} {:<8} {:<40} {:>6}MB {}",
e.file_name().to_string_lossy(),
status,
m["image"].as_str().unwrap_or("?"),
used / 1_000_000,
m["workspace"].as_str().unwrap_or("-")