Files
unkernel/docs/1.sdk/1.console.md
2026-11-27 21:11:51 +00:00

1.1 KiB

icon
icon
ph:terminal-duotone

Console and Lifecycle

Read and write the guest's console, and wait for it to end.

// vm.output: a ReadableStream, everything since boot and then live
vm.output.pipeTo(new WritableStream({ write: (b) => process.stdout.write(b) }));
vm.write("ls /\n"); // or write to vm.input, a WritableStream
vm.resize(120, 40); // the foreground program gets SIGWINCH

await vm.sync(); // flush the guest's disks
console.log(vm.stats()); // memory, disk, network and JIT counters
const { code, reason, kind } = await vm.exited; // stop() parks it at once

vm.onOutput(callback) is the callback form of vm.output.

::note stop() doesn't shut the guest down cleanly, so prefer poweroff in the guest. ::

Kernel log and program output

ConsoleRouter tells kernel log lines apart from program output:

import { ConsoleRouter } from "unkernel";

const router = new ConsoleRouter({
  onKernelLine: (line, { important }) => important && console.warn(line),
  onUserOutput: (text) => process.stdout.write(text),
});
vm.onOutput((bytes) => router.write(bytes));