SPAWNBOX

Development

Build Spawnbox from source, run its tests with real VMs, and find your way around the repository.

Spawnbox is a Rust workspace. Install Rust with rustup. The file rust-toolchain.toml sets the Rust version.

Type these commands in the repository directory. They operate on macOS and Linux:

cargo test --workspace --locked
cargo fmt --all -- --check
cargo clippy --workspace --all-targets --locked -- -D warnings

The VM, network and API code compiles only on Linux. Thus, also run Clippy on Linux. You can write code on macOS, but you cannot run VMs on macOS.

Tests with real VMs

You must have a Linux x86_64 host with KVM that you use only for tests. On the host, type:

scripts/m0.sh          # boot, snapshot and restore times, and clone isolation
scripts/m1-smoke.sh    # the full API, with a restart, a crash and HTTPS
  • The two scripts use sudo. They remove all the items that they create.
  • They use the kernel, the base disk and Firecracker in ~/.cache/spawnbox/artifacts. scripts/build-artifacts.sh makes these files.

CAUTION: Do not run scripts/m1-smoke.sh while the installed service runs. The two use the same firewall table. First, type sudo systemctl stop spawnbox.

To run the server without the installer:

a=~/.cache/spawnbox/artifacts
scripts/build-artifacts.sh $a
cargo build --release --locked -p spawn-api
sudo target/release/spawn-api create-key --data-dir /var/lib/spawnbox
sudo target/release/spawn-api serve --data-dir /var/lib/spawnbox \
  --firecracker $a/bin/firecracker --jailer $a/bin/jailer \
  --kernel $a/vmlinux --rootfs $a/rootfs.ext4

Soak and failover tests

scripts/soak.py runs against an installed server or fleet for a long time and checks that nothing is lost, run twice or left behind. It uses only the Python standard library. It reaches the API with an org key, and each host with ssh as root, or as a user with sudo if you add --sudo. Name each host with --host NAME=TARGET. For NAME, use primary, standby or a worker’s node ID from spawnbox nodes ls.

# 72 hours at about 30 live sandboxes, with a server killed every 6 hours.
scripts/soak.py --api http://10.0.0.5:8080 --key "$KEY" \
  --host [email protected] --host [email protected] \
  --ssh-opt=-i --ssh-opt="$HOME/.ssh/fleet" \
  --density 30 --duration 72h --chaos-every 6h --summary soak.json

# Kill a worker, cut a worker off from the primary, and kill the leader.
scripts/soak.py --mode failover \
  --api http://10.0.0.5:8080 --api http://10.0.0.7:8080 --key "$KEY" \
  --host [email protected] --host [email protected] \
  --host [email protected] --primary-ip 10.0.0.5 \
  --fence-after-s 60 --fence-margin-s 30 --summary failover.json

# Show the plan and probe the API and hosts without changing anything.
scripts/soak.py --dry-run --api http://10.0.0.5:8080 --key "$KEY" \
  --host [email protected]
  • The soak creates ephemeral and persistent sandboxes in the tenant soak (--tenant). Each has a service and an env value, and each persistent sandbox has a volume. It pauses, wakes, cold starts (it kills the VM’s firecracker process), kills services in the guest and deletes sandboxes at random.
  • Every --check-every (default 5 minutes) it checks these points:
    • Each persistent sandbox answers with its volume file, its env value and, unless it was cold started, its disk file.
    • No VM runs on two hosts, and each VM runs on the host that the API names.
    • No jail, network namespace or cgroup stays behind for a sandbox that was deleted, is cold or moved.
    • A killed service runs again.
    • The memory and open files of spawn-api do not grow beyond --max-rss-growth-mib and --max-fd-growth.
  • The failover mode puts persistent sandboxes on one host, then cuts the host off. New persistent sandboxes start on the primary by default, so the script moves each one to the worker by cold starts: it kills the VM and wakes the sandbox until placement picks that worker, up to --place-tries times (default 12). A cold start prefers the host that holds the sandbox’s volume, so a sandbox with a volume reaches a worker only when the primary has no room. The worker scenarios use sandboxes without a volume unless you set --failover-volume-sandboxes. The leader scenario also uses sandboxes with a volume. It checks that no sample of the host’s processes taken after the API made a sandbox cold still shows its VM. It wakes the sandboxes without a volume while the host is still cut off, so a second copy starts elsewhere, and checks that the two never run at once, also after the host returns. Then it checks that each sandbox starts again with its data.
  • A sandbox whose host is restarting or cut off, or whose volume’s host is away, may answer 503 for a while. The script asks again for up to --verify-wait (default 3 minutes) before it counts the sandbox as lost. A wake or check that fails with another retryable error, such as 503 no_capacity on a full host, is asked again with backoff for up to --retry-wait (default 90 seconds). The summary counts these retries under errors as retried_<code>. An error that is not retryable counts as lost at once.
  • The script writes the summary (--summary) and exits with 1 if a check fails. It exits with 3 if it cannot set up what it tests, for example if it cannot write its markers or cannot get a sandbox onto the host of a scenario. These are not counted as lost sandboxes.
  • python3 scripts/soak_test.py tests the script’s own logic against a stubbed API, without a server. scripts/soak.py --help lists every option.

Wake benchmark

scripts/wake_bench.py times how fast a small web app in a sandbox answers its first request. It uses only the Python standard library and a tenant key. It builds a template with a tiny Node HTTP server, then sends requests through the preview listener, as a browser would.

# Cold and resumed starts, 30 samples each after 3 warm-up runs.
scripts/wake_bench.py --api http://10.0.0.5:8080 --preview-addr 10.0.0.5:8081 \
  --key "$KEY" --phases cold --samples 30 --summary cold.json

# On the host: fill it with idle sandboxes, then wake them all at once.
scripts/wake_bench.py --api http://10.0.0.5:8080 --preview-addr 10.0.0.5:8081 \
  --key "$KEY" --phases density,wake --mem-stop-pct 80 \
  --data-dir /var/lib/spawnbox --summary wake.json
  • The phases are cold, resume (run with cold), burst, density and wake. scripts/wake_bench.py --help lists every option.
  • cold creates a sandbox with the app as a service and times it to the first 200. resume pauses that sandbox and times the request that resumes it. The two alternate, so drift touches both.
  • burst creates --burst sandboxes at once. wake pauses every density sandbox and wakes them all at once.
  • density stops before host memory would pass --mem-stop-pct, when the server refuses a sandbox, or at --density-max. It reads host memory and firecracker memory from /proc, so run it on the host.
  • Without --health, the server sends requests to the app as soon as it starts, so the script asks again every --poll-ms until the app answers. Many quick retries load a busy host, so a small --poll-ms slows a burst. With --health, the server holds each request until the app passes its check.
  • The script deletes every sandbox it made, checks the listing, and exits with 1 if one is still there. python3 scripts/wake_bench_test.py tests its own logic.

CAUTION: The failover mode kills servers and adds iptables rules on the hosts. Use it only on a test fleet. Do not run the soak on a fleet with real users.

Repository layout

Path Contents
crates/spawn-api The HTTP API server (spawn-api serve)
crates/spawn-node Builds, restores, pauses and connects VMs on one host
crates/spawn-egress The egress proxy: policy, records and secrets
crates/spawn-guest The agent that runs as PID 1 in each VM
crates/spawn-proto The gRPC contract between the host and the guest
crates/spawn-bench The boot and restore benchmark
crates/spawnbox-cli The spawnbox CLI
guest/ The guest kernel settings and the Debian base image, with Node.js and Python
templates/ Ready-made template files for coding agent CLIs
install.sh The one-line installer: downloads a release (or the code) and runs deploy/install.sh
deploy/install.sh The installer for one server, from a release bundle or the code
deploy/backup.sh Encrypted backups and restores. Refer to Back up and restore.
deploy/prometheus/ Prometheus alert rules and what to do for each alert
.github/workflows/release.yml Builds the release bundle for each v* tag
scripts/ Build scripts and end-to-end tests

This page is the “Development” section of README.md ↗. Found a mistake? Edit it on GitHub ↗