Files
unkernel/docs/1.sdk/2.programs.md
pi0x 9772730203 js: warn once when a distro image without the account user runs programs as root (#158)
exec() and spawn() run programs as the account "user" by default, and kwtty falls back to root where /etc/passwd has no such account. On an image of your own that lacks it, programs ran as root without any sign of it.

For a kernel that boots a distro image, the first program that names no user now asks kwtty for "user" by name (not critical, so a guest from before the USER option runs it as root as before). When kwtty answers "no user user", the SDK logs one warning through createKernel's log and opens the same session with the default, so the account used does not change. Later programs send the default as before. The base shell, which has root only, gets no check and no warning.

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-12-02 21:03:01 +01:00

4.3 KiB

icon
icon
ph:terminal-window-duotone

Run Programs

Run guest programs on pipes or on their own pty.

exec()

const { stdout, exitCode } = await vm.exec("uname", ["-a"]);
const { stdout: sum } = await vm.exec("sha256sum", [], { input: "hello" });

exec() runs a program on pipes and resolves with { stdout, stderr, exitCode, signal }, also for a non-zero exit. Pass encoding: "buffer" for bytes, and cwd or env as you would to child_process.

On the distro images programs run as the user user (uid 1000, in /home/user), who has sudo without a password. Pass user: "root" (or another name or uid) to run as someone else. The default is the account user where the guest's /etc/passwd has one, and root otherwise: the base shell is root's, and so is an image of your own without that account. For such an image the SDK logs one warning per kernel (through log) at the first program that names no user; pass user: "root" to run as root without it.

A program gets a fixed environment, not a login shell's: the account's HOME, USER, LOGNAME and SHELL, PATH=/run/kw/bin:$HOME/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/kw/bin, and TERM on a pty. Nothing is read from the image's /etc/profile, /etc/profile.d or /etc/environment. env is applied over these and may replace them; for the profile, run a login shell (exec("sh", ["-lc", "..."]), or spawn() without a program). The working directory is the account's home unless cwd says otherwise.

A program that cannot start rejects exec() and spawn() with a PtyError whose code is "EREMOTE" and whose message is the guest's. stage says what failed and guestCode is the guest's errno name, so there is no message to parse (a shell exits 127 when exec fails with ENOENT, 126 otherwise):

Cause Message stage guestCode
no such program, or not executable kwtty: exec NAME: No such file or directory (or Permission denied) "exec" "ENOENT" (or "EACCES")
a bad cwd kwtty: chdir DIR: No such file or directory (or Not a directory) "chdir" "ENOENT" (or "ENOTDIR")
a user the guest does not have kwtty: no user NAME "user" none

Other errors leave both undefined; an errno the SDK does not know leaves guestCode undefined.

A program that starts and fails is not an error: exec() resolves with its exitCode. A guest that does not run unkernel's terminal server rejects with ETIMEDOUT after timeout (30 s by default).

On every distro image, node, npm, npx, bun, bunx, uv, uvx, claude, opencode and fx are shims at the end of PATH (a version the image installs comes first). The first call downloads the current release for the user (Node's newest LTS), puts it in ~/.local/bin and runs it; that takes a network and a while (node --version took 14 s on Debian, 2026-12-01), and without a data disk the install is in guest memory and gone at the next boot. Without a network the shim exits 127. KW_NODE_VERSION (lts, latest, a major such as 22, or X.Y.Z), KW_BUN_VERSION, KW_UV_VERSION and KW_CLAUDE_VERSION in env pick another version.

spawn() on a pty

const sh = await vm.spawn(); // a login shell on its own pty
sh.output.pipeTo(new WritableStream({ write: (b) => process.stdout.write(b) }));
await sh.write("cat /etc/alpine-release; exit\n");
console.log(await sh.ended); // { type: "exit", code: 0 }

spawn(file, args, { cols, rows }) runs a program on a pty, in the zigpty / node-pty shape, with resize(), kill(signal) and close() (a hangup). Run as many as you like. With detachable: true the session survives a snapshot and restore, to be taken over with attach(); sessions on pipes and exec() cannot be detachable, and end at a restore.

::warning Keep reading output: the guest program waits while nobody does. ::

spawn() on pipes

const gz = await vm.spawn("gzip", ["-c"], { pty: false });
gz.stderr.cancel(); // read both outputs, or cancel one
const zipped = new Response(gz.stdout).bytes();
await new Response("hello\n").body.pipeTo(gz.stdin); // then EOF
console.log((await zipped).length);

With pty: false the program gets pipes: stdin, stdout and stderr web streams, byte for byte.