mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 03:39:26 +08:00
guest: show why a vm-run.ts boot failed
A failed run (a script that exits non-zero, no prompt for --trace, a --get that does not match) now prints the end of the console, and on a guest booted with systemd (/run/systemd/system) also systemctl --failed, its jobs and the boot's journal; the other images print nothing more. The traced boot's prompt detector also strips OSC sequences ended by ST (ESC \), as systemd sends them, and CSI with a ! intermediate. Co-authored-by: Vineel Sai <mail@vineelsai.com>
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
// SCRIPT then starts once the console shows the shell's prompt (output
|
||||
// ending in "# ", "$ " or the distros' "⇒ "), so that the trace holds a
|
||||
// whole boot whatever the timing.
|
||||
// A failed run prints the end of the console, and on a guest booted with
|
||||
// systemd also `systemctl --failed`, its jobs and the boot's journal.
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { bundledBoot, createKernel, directBackend, FileDisk, NetStack, type BlockDevice } from "../../js/node.ts";
|
||||
@@ -66,14 +68,20 @@ if (trace) {
|
||||
};
|
||||
}
|
||||
// The console shell's prompt (--trace): the end of the output, without
|
||||
// escape sequences, is "# ", "$ " or "⇒ ".
|
||||
// escape sequences (CSI, and OSC ended by BEL or ST, as systemd sends
|
||||
// them), is "# ", "$ " or "⇒ ".
|
||||
let tail = "", prompted = () => {};
|
||||
const prompt = new Promise<void>((r) => (prompted = r));
|
||||
const dec = new TextDecoder();
|
||||
const onOutput = trace ? (b: Uint8Array) => {
|
||||
tail = (tail + dec.decode(b, { stream: true })).slice(-512);
|
||||
if (/[#$⇒] $/.test(tail.replace(/\x1b\][^\x07]*\x07|\x1b\[[0-9;?]*[A-Za-z]/g, ""))) prompted();
|
||||
} : undefined;
|
||||
/** The console's end, printed when the run fails. */
|
||||
let consoleEnd = "";
|
||||
const onOutput = (b: Uint8Array) => {
|
||||
const t = dec.decode(b, { stream: true });
|
||||
consoleEnd = (consoleEnd + t).slice(-6000);
|
||||
if (!trace) return;
|
||||
tail = (tail + t).slice(-2048);
|
||||
if (/[#$⇒] $/.test(tail.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;?!]*[A-Za-z]/g, ""))) prompted();
|
||||
};
|
||||
const vm = await createKernel({
|
||||
kernel: module, memoryMiB: mem, distro, log: () => {}, onOutput, shm,
|
||||
...(append ? { cmdline: `${bundledBoot(module).cmdline} ${append}` } : {}),
|
||||
@@ -111,6 +119,13 @@ try {
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`vm-run: ${(e as Error).message}`);
|
||||
console.error(`vm-run: the console's end:\n${consoleEnd}`);
|
||||
// A guest booted with systemd (sd_booted(3): /run/systemd/system) says
|
||||
// why itself, if it got as far as kwtty; the others print nothing.
|
||||
const why = "[ -d /run/systemd/system ] && command -v journalctl >/dev/null || exit 0; " +
|
||||
"systemctl --failed --no-pager; systemctl list-jobs --no-pager; journalctl -b --no-pager -p notice -n 60";
|
||||
const d = await vm.exec("sh", ["-c", why], { timeout: 30000, user: "root" }).catch(() => null);
|
||||
if (d?.stdout) console.error(`vm-run: the guest's journal:\n${d.stdout}`);
|
||||
code = 1;
|
||||
}
|
||||
vm.write("\npoweroff\n");
|
||||
|
||||
Reference in New Issue
Block a user