From 9706e9aaf28a3da2c02cf92f58cbddfd74b37d26 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Fri, 2 Oct 2026 17:08:19 +0200 Subject: [PATCH] ps, rm --disk, release workflow --- .gitea/workflows/release.yml | 57 ++++++++++++++++++++++++++++++++++++ README.md | 6 ++-- src/main.rs | 44 +++++++++++++++++++++------- 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 .gitea/workflows/release.yml diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..edd5b7a --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,57 @@ +name: release + +on: + push: + tags: ["v*"] + +env: + API: ${{ github.server_url }}/api/v1/repos/${{ github.repository }} + TOKEN: ${{ secrets.GITEA_TOKEN }} + TAG: ${{ github.ref_name }} + +jobs: + release: + runs-on: ubuntu-latest + outputs: + id: ${{ steps.r.outputs.id }} + steps: + - id: r + run: | + auth="Authorization: token $TOKEN" + # Create, or reuse when re-running for an existing tag. + curl -sf -H "$auth" -H 'Content-Type: application/json' -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\"}" "$API/releases" >/dev/null || true + id=$(curl -sf -H "$auth" "$API/releases/tags/$TAG" | grep -o '^{"id":[0-9]*' | cut -d: -f2) + [ -n "$id" ] + echo "id=$id" >> "$GITHUB_OUTPUT" + + build: + needs: release + strategy: + matrix: + os: [macos-arm64, ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - name: toolchain + run: | + command -v rustup >/dev/null || curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal + echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" + if ! command -v go >/dev/null; then + if command -v brew >/dev/null; then brew install go; else + curl -sSfL "https://go.dev/dl/$(curl -sSf 'https://go.dev/VERSION?m=text' | head -1).linux-$(dpkg --print-architecture).tar.gz" | tar -xz -C "$HOME" + echo "$HOME/go/bin" >> "$GITHUB_PATH" + fi + fi + - name: build + run: | + rustup toolchain install stable --profile minimal && rustup default stable + cargo test --locked + cargo build --release --locked + - name: upload + run: | + f="wsbx-$TAG-$(uname -s | tr A-Z a-z)-$(uname -m).tar.gz" + tar -czf "$f" -C target/release wsbx + (sha256sum "$f" 2>/dev/null || shasum -a 256 "$f") > "$f.sha256" + for a in "$f" "$f.sha256"; do + curl -sf -H "Authorization: token $TOKEN" -F "attachment=@$a" "$API/releases/${{ needs.release.outputs.id }}/assets?name=$a" >/dev/null + done diff --git a/README.md b/README.md index de6ba82..e63bcab 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Docker-Sandboxes-style microVMs on Apple Silicon macOS. Each sandbox is a real L ```sh brew install vfkit e2fsprogs go # go: builds the static guest tunnel helper -cargo install --path . +cargo install --path . # or grab wsbx-*-darwin-arm64.tar.gz from a release (push a v* tag to build one) wsbx claude # Claude Code in a VM, current dir mounted at the same path wsbx codex | wsbx gemini # other agents (Docker's sandbox-templates images) @@ -13,7 +13,9 @@ wsbx pi # pi coding agent, same version + config as wsbx run alpine # bare VM, interactive shell wsbx run -w . ubuntu -- make test # one-off command, exit code propagated wsbx run -p 8000 alpine # host's localhost:8000 reachable as localhost:8000 in the VM -wsbx ls / wsbx rm NAME / wsbx pull IMAGE +wsbx ps # sandboxes: running/stopped, image, disk use, workspace (alias: ls) +wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start) +wsbx pull IMAGE wsbx gc # delete cached images no sandbox uses wsbx kernel [--update | --version TAG] # latest Kata release by default, sha256-verified ``` diff --git a/src/main.rs b/src/main.rs index 7c3a5ce..dad6d3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 }, + /// Remove stopped sandboxes + Rm { + #[arg(required = true)] + names: Vec, + /// 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, 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 { + 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("-")