wasm-vm worker
Boot an unkernel (Linux-in-WASM) guest inside a V8 isolate and serve it over HTTP. A worker mode for sandboxes and non-JS workers. Built with the wasm-vm cargo feature.
Model
- • exec — each request boots a guest, runs a command, returns its output.
- • server — one resident guest per worker; each request is one
vm.fetchinto the guest's HTTP server over vsock. - • Optional operator console over a 0600 Unix socket (read-only, or interactive raw-TTY).
- • Optional container rootfs mounted read-only as an erofs distro.
- • Not a full container runtime: no image layers at run time, no orchestration; request/response bodies are buffered.
How it works
unkernel ships a JavaScript loader whose entry point is the async function createKernel(options); it compiles and runs the kernel wasm and returns a vm handle. nano drives that SDK from Rust through V8 — it never touches virtio/vsock directly.
- The loader (
dist/web.mjs) is flattened into one IIFE that, run as a classic script in a V8 isolate, definesglobalThis.createKernelplus minimalHeaders/Response/Requestand WHATWG streams. - A worker creates a heap-capped V8 isolate, installs the host globals the SDK expects (
setTimeout,crypto.getRandomValues,performance,TextEncoder/Decoder, …), and injects the kernel wasm (and distro erofs) asArrayBuffers. - The boot script calls
await createKernel({ kernel, distro }), wheredistrois a read-only in-memoryBlockDeviceover the injected erofs. Thenvm.onOutput(cb)streams the console andvm.write(cmd)starts the workload. - Each request is
vm.fetch(url, { method, headers, body })— HTTP into the guest's server over unkernel's vsock. The request body goes in as anArrayBuffer, the response comes back as aUint8Array(no base64). - V8 has no event loop, so nano drives one (timers +
pump_message_loop+ microtasks) until the SDK's async promises resolve. The isolate is!Sendand lives on its worker thread; in server mode one resident guest is reused across requests.
Example: run the nginx container
End to end: a distro built from the nginx:alpine Docker image, served over HTTP through vm.fetch. Build-time tools (node, docker, erofs-utils, curl, jq): scripts/wasm-vm-doctor.sh. Needs an unpacked unkernel npm package (npm pack unkernel && tar -xzf unkernel-*.tgz) for the kernel wasm and loader bundle.
-
1. Build nano and the loader bundle
cargo build --features wasm-vm --bin nano-rs scripts/build-wasm-vm-bundle.sh package unkernel.bundle.fetch.js
-
2. Build a distro from the nginx image
scripts/build-distro-erofs.sh nginx:alpine nginx nginx.erofs
Pulls the
linux/arm64image, sets/sbin/initto busybox with the kw inittab, and writes akwrooterofs (block size 4096). Use any arm64 busybox/alpine-based image. -
3. Write the manifest (nginx.json)
{ "apps": [{ "hostname": "nginx.local", "entrypoint": "", "wasm_vm": { "kernel": "/abs/package/dist/unkernel.wasm", "bundle": "/abs/unkernel.bundle.fetch.js", "distro": "/abs/nginx.erofs", "command": "/docker-entrypoint.sh nginx -g 'daemon off;' &", "mode": "server", "port": 80, "eager_boot": true }, "limits": { "memory_mb": 1024, "workers": 1, "timeout_secs": 60 } }], "server": { "port": 8100, "host": "127.0.0.1" } }commandstarts the image's workload (here nginx's entrypoint);portis the port it listens on. Raisememory_mbfor large images. -
4. Run and reach it
nano-rs run --config nginx.json # logs: resident wasm-vm image ready in <N> ms (distro) curl -H 'Host: nginx.local' http://127.0.0.1:8100/ # <title>Welcome to nginx!</title> ... (HTTP 200)
-
5. Attach the console (optional, separate terminal)
Set
"console_enabled": true(and"console_allow_write": trueto type) in the manifest, withworkers: 1.nano-rs worker console nginx.local # read-only nano-rs worker console nginx.local --write --raw # interactive; detach Ctrl-]
In
--writemode Ctrl-C reaches the guest; detach with Ctrl-]. Run the daemon and console in separate terminals.
Built on unkernel
The guest is unkernel (github.com/pithings/unkernel) — a real Linux kernel compiled to WebAssembly. nano loads the unkernel module and its JavaScript SDK (createKernel) in a V8 isolate, boots a guest, and reaches its HTTP server through vm.fetch over vsock. nano bundles no unkernel code: you supply the kernel wasm, loader bundle, and distro images from the unkernel release. unkernel is MIT AND GPL-2.0-only (SDK MIT; the kernel is GPL-2.0, Linux).
Reference
Operator guide, manifest, licensing, limits: docs/wasm-vm.md. Architecture and internals: docs/wasm-vm-architecture.md. Runnable example: examples/wasm-vm.