Skip to content

Real Linux Terminal

Ved Gupta edited this page Dec 4, 2026 · 2 revisions

Real Linux Terminal

The opt-in @innovatorved/code-lab/extensions/linux extension boots a real Linux kernel compiled to WebAssembly (unkernel) with an Alpine userland, right in the learner's browser. Terminals become real ptys, so vi, top, job control and Ctrl+C behave exactly as they do on a server. It adds the sh language, whose guided steps are graded by shell scripts. The extension itself is about 5 KB gzipped and the kernel is only downloaded when it boots. The simulated POSIX shell remains the default for every other language.


Mounting a Shell Lab

Pass the extension in extensions and point distroUrl at an Alpine image you host yourself:

import CodeLab from '@innovatorved/code-lab';
import '@innovatorved/code-lab/code-lab.css';
import { linuxExtension } from '@innovatorved/code-lab/extensions/linux';

CodeLab.mount('#lab', {
  lang: 'sh',
  mode: 'guided',
  shStarterFiles: { 'backup.sh': '#!/bin/sh\n# TODO: copy /lab/data to /tmp/backup\n' },
  steps: [{
    id: 'perms',
    title: 'Make the script executable',
    instructions: 'Run `chmod +x backup.sh`, then Verify.',
    setup: 'mkdir -p /lab/data && echo hello > /lab/data/a.txt',
    check: 'test -x /workspace/backup.sh || { echo "backup.sh is not executable"; exit 1; }'
  }],
  extensions: [linuxExtension({ distroUrl: '/linux/alpine.erofs' })]
});

Editor files are written to /workspace before every Verify. The learner works as the default user (uid 1000, with passwordless sudo), while setup and check run as root through sh -c, so they can inspect anything the learner did. A step passes when check exits 0. Whatever it prints shows up in the Test Suite tab, so print a helpful hint before exit 1. Keep checks fast and idempotent because they run on every Verify. Reset machine in the status bar restores the snapshot taken when the step started.

The stock image is Alpine with BusyBox, so there is ash and BusyBox vi but no bash, and the guest has no network access.


Options

Option Default Description
distroUrl required https: URL or same-origin path to alpine.erofs
sdkUrl pinned jsDelivr build of unkernel Point at a self-hosted web.mjs to avoid the CDN
memoryMiB 256 Guest memory, 64 to 4096
persist 'none' 'opfs' keeps the guest disk across reloads; Reset then reboots instead of restoring a snapshot
workspaceDir /workspace Where editor files are written
lessonMounts none Extra lesson files written as root after boot, keyed by absolute directory
defaultTimeoutMs 15000 Timeout for setup / check when a step has no timeoutMs
autoBoot / activate true / true Boot on install, and switch the workbench to sh
onStatus none Callback for booting, ready, unsupported, error and disposed

Hosting & Browser Support

The kernel reads alpine.erofs only through HTTP Range requests and refuses any response that isn't 206 Partial Content. Running npx -p @innovatorved/code-lab codelab-fetch-unkernel --out public/linux downloads the pinned image and SDK and verifies their checksums. If the image lives on another origin it needs CORS that exposes Content-Range, Accept-Ranges and Content-Length. No COOP/COEP headers are required.

Free hosting on Cloudflare Pages

Cloudflare Pages ignores Range and always returns the whole file, so the image can't be read straight from Pages. You don't need R2 or a Pages Function to fix that. A small service worker on the lab page downloads the image once, keeps it in Cache Storage, and answers every Range request from that copy, so the whole lab stays on the free Pages plan as plain static files. The live demo at code-lab.vedgupta.in/linux-lab/ works this way.

Copy sw.js and index.html from the Cloud IDE example. Three details matter:

  • The page registers the worker with a scope that covers it (the demo uses /linux-lab/) and mounts the workbench only after the worker controls the page. The kernel's Web Workers inherit that control, and their image requests are what the worker answers.
  • Pass previewSw: false when mounting, otherwise the workbench registers its Web Preview worker at / and the two compete. Shell labs don't use Web Preview.
  • Bump CACHE_NAME in sw.js whenever you replace the image, so returning visitors download the new one.

The image is a build input, not a committed file. In the code-lab repo examples/cloud-ide/public/linux/ is gitignored, so on a fresh clone run node tools/fetch-unkernel.js --out examples/cloud-ide/public/linux before bun run build. Otherwise /linux-lab/ deploys without its image and cannot boot. Deploy with npx wrangler pages deploy dist --project-name <project> --branch main from examples/cloud-ide; every file stays under the 25 MiB Pages limit (the image is about 4 MB).

The kernel needs WebAssembly JSPI, which ships in Chrome and Edge 137+, Firefox 153+ and Safari 27+. Other browsers fall back to the simulated shell and the status bar says so.

The extension adds no inline scripts. With the default sdkUrl your Content-Security-Policy needs:

script-src 'self' https://cdn.jsdelivr.net 'wasm-unsafe-eval';
worker-src 'self' blob:;
connect-src 'self' https://cdn.jsdelivr.net;

When you self-host the SDK, drop the jsDelivr origins.


Licensing

The JavaScript SDK is MIT. The kernel (unkernel.wasm) is GPL-2.0 and is loaded unmodified at runtime; it is never bundled into the npm package. The Alpine image is built from Alpine packages under their own licenses. See docs/licenses.md for details and ADR-008 for the design decision.

Clone this wiki locally