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 oncevm.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));