Linux-in-WASM worker

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.

Boot and console: a guest boots and an operator attaches to its serial console.

Model

  • • exec — each request boots a guest, runs a command, returns its output.
  • • server — one resident guest per worker; each request is one vm.fetch into 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.

  1. The loader (dist/web.mjs) is flattened into one IIFE that, run as a classic script in a V8 isolate, defines globalThis.createKernel plus minimal Headers/Response/Request and WHATWG streams.
  2. 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) as ArrayBuffers.
  3. The boot script calls await createKernel({ kernel, distro }), where distro is a read-only in-memory BlockDevice over the injected erofs. Then vm.onOutput(cb) streams the console and vm.write(cmd) starts the workload.
  4. 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 an ArrayBuffer, the response comes back as a Uint8Array (no base64).
  5. V8 has no event loop, so nano drives one (timers + pump_message_loop + microtasks) until the SDK's async promises resolve. The isolate is !Send and 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. 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. 2. Build a distro from the nginx image
    scripts/build-distro-erofs.sh nginx:alpine nginx nginx.erofs

    Pulls the linux/arm64 image, sets /sbin/init to busybox with the kw inittab, and writes a kwroot erofs (block size 4096). Use any arm64 busybox/alpine-based image.

  3. 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" }
    }

    command starts the image's workload (here nginx's entrypoint); port is the port it listens on. Raise memory_mb for large images.

  4. 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. 5. Attach the console (optional, separate terminal)

    Set "console_enabled": true (and "console_allow_write": true to type) in the manifest, with workers: 1.

    nano-rs worker console nginx.local              # read-only
    nano-rs worker console nginx.local --write --raw # interactive; detach Ctrl-]

    In --write mode 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.