WARM
The machine object. You launch it from a warm-system-aarch64 command line and control it through QMP and the human monitor, just like a QEMU process.
WARM is Warm64's one public interface. You launch it with the same command line you'd give qemu-system-aarch64, and control it with QEMU's own protocol (QMP) and monitor (HMP). There's no separate options object: the command line is the machine's configuration.
import { WARM } from "@warm-virtual/warm64";
import { nodeWARMHost } from "@warm-virtual/warm64/warm-node";
const vm = await WARM.launch(
"-M virt -cpu cortex-a76 -m 1G -S -display none -kernel vmlinuz-virt -initrd initramfs-virt -append console=ttyAMA0",
nodeWARMHost(),
);
vm.serial[0].onData((bytes) => process.stdout.write(bytes));
await vm.qmp("cont");
const { code } = await vm.wait();Static methods
WARM.launch()
Builds a machine from a command line and starts it.
WARM.launch(args, host): Promise<WARM>Parameters
| Parameter | Type | Description |
|---|---|---|
args | string, string[] or WARMConfig | A warm-system-aarch64 command line: one string, split the way a POSIX shell would split it (a leading qemu-system-aarch64 or warm-system-aarch64 is dropped), an argv array, or a config already parsed by parseWARMArgs(). See the command-line reference. |
host | WARMHost | What the platform provides to the machine: its WebAssembly modules, files, disks, sockets, display and sound. Use nodeWARMHost() in Node or browserWARMHost() in a browser. |
Return value
A promise that resolves to the running (or, with -S, paused) WARM once the machine is built.
Exceptions
WARMErrorwhen the command line is refused, in QEMU's words: an unknown option (-bogus: invalid option), a bad value, or something QEMU on a Mac also refuses (invalid accelerator kvm).WARMErrorwhen a file can't be read, in QEMU's words where QEMU has them (could not load kernel 'vmlinuz-virt'), otherwise in the host's, such as<url>: 404 File not foundin a browser.WARMErrorcarrying the text for-versionand-help.- An
ErrornamedWARMListingfor a help listing (-device help,-cpu help…): itsmessageis the listing,codeis the exit status, andstreamis where the command prints it.
If anything fails after the machine is built, launch() releases it before rethrowing.
Tip Unless the command line has -S, the machine starts running inside launch(), before your code can attach listeners, so its first console output is lost. Launch with -S, attach onData, then await vm.qmp("cont").
Instance properties
| Property | Type | Description |
|---|---|---|
config | WARMConfig | The parsed command line the machine was built from. |
host | WARMHost | The host it was launched with. |
serial | WARMChardevPort[] | The serial ports. serial[0] is the guest's ttyAMA0 console and always exists; serial[1] exists with a second -serial. |
chardevs | Map<string, WARMChardevPort> | Every character device by id: serial0, serial1 and each -chardev. |
shares | Map<string, WARMShare> | The 9p shared folders by mount tag. See WARMShare. |
ports | Map<string, WARMChardevPort> | The virtio-serial ports by name (-device virtserialport,name=). See Guest agent. |
guestAgent | WARMGuestAgent | null | The guest agent's client when a port named org.qemu.guest_agent.0 is on the command line. |
status | WARMRunState | The run state, as query-status reports it (read-only). See Run states. |
Instance methods
qmp()
Runs a QMP command and resolves with its return value.
vm.qmp(command, args = {}): Promise<unknown>It rejects with a QmpError whose class and desc are what QEMU would send (GenericError, CommandNotFound, DeviceNotFound…). The commands are listed in the QMP reference.
const { status } = await vm.qmp("query-status"); // "running"
await vm.qmp("screendump", { filename: "shot.png", format: "png" });qmpJson()
Runs one QMP message given as JSON text and resolves with the reply as JSON text. It never rejects: errors come back as QMP error replies. Use it to bridge a QMP client you already have.
const reply = await vm.qmpJson('{"execute":"query-status"}');
// the reply as JSON text: {"return": {"status": "running", ...}}hmp()
Runs a human monitor command and resolves with the text it prints.
console.log(await vm.hmp("info block"));
await vm.hmp("savevm before-upgrade");It rejects with QmpError("GenericError", …) for an unknown command or bad arguments, for example unknown command: 'x'.
on()
Listens for a QMP event by name, or for every event with "*". Returns a function that removes the listener. Listeners are called synchronously, with a QmpEvent: { event, data?, timestamp: { seconds, microseconds } }.
const off = vm.on("SHUTDOWN", (e) => console.log(e.data.reason));
vm.on("*", (e) => console.log(e.event));See Events for the common ones.
wait()
Resolves with { code } when the machine exits: on quit, or when the guest powers off (unless -no-shutdown keeps the machine). The code is the one the warm-system-aarch64 process would exit with: 0 normally, including after a guest panic with -action panic=shutdown (the default), and 1 after a panic with -action panic=exit-failure.
setUIInfo()
Offers the guest a display size, the way resizing a QEMU window does. Returns false if there's no virtio-gpu device or the size is refused.
vm.setUIInfo({ width: 1280, height: 800 });quit()
Stops the machine and releases it.
vm.quit(reason = "host-qmp-quit"): voidEmits SHUTDOWN with reason, one of QEMU's: host-qmp-quit, host-signal, host-ui or host-error. Resolves wait() with { code: 0 }. Calling it again does nothing.
process.once("SIGINT", () => vm.quit("host-signal"));Run states
status and query-status report QEMU's run states:
| State | When |
|---|---|
running | Running. |
prelaunch | Launched with -S and not yet continued. |
paused | Stopped with stop. |
inmigrate | Launched with -incoming defer, waiting for migrate-incoming. |
postmigrate | After migrate to a file. |
guest-panicked | The guest panicked with -action panic=pause, or with -no-shutdown. |
shutdown | The guest powered off with -no-shutdown. cont is refused until the machine is reset. |
Events
The events are QEMU's, sent just as QEMU 11.1.1 sends them for the devices the machine has. QEMU's QMP reference lists them all. The common ones:
| Event | Data | When |
|---|---|---|
STOP | none | The machine paused: stop, or a guest panic that pauses it. |
RESUME | none | It continued. |
RESET | { guest, reason } | system_reset, or a guest reboot. |
POWERDOWN | none | system_powerdown pressed the power button. |
SHUTDOWN | { guest, reason } | The guest powered off (guest-shutdown), rebooted with -no-reboot (guest-reset) or panicked (guest-panic), or the host quit (host-qmp-quit, or host-signal for Ctrl+C on the command line). |
GUEST_PANICKED | { action } | The guest kernel panicked. action is poweroff (the default: SHUTDOWN follows), pause or run. The reason goes to the host's warn. |
MIGRATION | { status } | A migrate or migrate-incoming changed state. |
JOB_STATUS_CHANGE | { id, status } | A job moved on: snapshot-save, snapshot-load or snapshot-delete, or a block job. |
DUMP_COMPLETED | none | dump-guest-memory finished. |
WARMChardevPort
A serial port or -chardev: a stream of bytes to and from the guest.
| Member | Description |
|---|---|
id | The character device's id, for example serial0. |
onData(listener) | Calls listener(bytes: Uint8Array) with what the guest writes. The bytes are a fresh copy you may keep. Returns a function that removes the listener. |
write(data) | Sends a Uint8Array, or a string as UTF-8, to the guest. |
sendBreak() | Sends a serial break (only on serial0). |
setOpen(open) | For a virtio-serial port, connects or disconnects the host side. The guest's reads and writes on the port wait until the host is connected. A port with a ringbuf, file or stdio chardev is connected from the start, and the Node host connects a socket one while a client is connected. |
hasRing | true for a ringbuf chardev, which QMP's ringbuf-read and ringbuf-write reach. |
vm.serial[0].onData((bytes) => process.stdout.write(bytes));
vm.serial[0].write("uname -a\n");WARMGuestAgent
vm.guestAgent: a client for QEMU's guest agent (qemu-ga in the guest), over the port named org.qemu.guest_agent.0. See Guest agent.
| Member | Description |
|---|---|
port | The WARMChardevPort it speaks over. |
command(name, arguments?, { timeout, reply, match }) | Sends any of the agent's commands. Resolves with the reply's return, and rejects with a QmpError on an error reply or after timeout ms with no reply (30 s by default). reply: false sends without waiting. |
waitReady({ timeout, every }) | Resolves once the agent answers a guest-sync-delimited, trying every 500 ms for up to 120 s. |
sync(), ping(), info() | guest-sync-delimited, guest-ping, guest-info. |
exec(path, args, { env, input, capture }) | guest-exec: starts a program, resolves with its pid. input goes to its stdin. |
execStatus(pid) | guest-exec-status: null while it runs, then a WARMGuestExecResult. |
execWait(pid, { every, timeout }) | Polls execStatus (every 100 ms) until the program exits; with no timeout, it waits as long as that takes. |
run(path, args, options) | exec then execWait. |
shutdown(mode) | guest-shutdown: powerdown (default), halt or reboot. |
fileRead(path) | The file's bytes (guest-file-open, -read, -close). |
fileWrite(path, data, mode) | Writes the file; mode is w (default) or a. |
await vm.guestAgent.waitReady();
const r = await vm.guestAgent.run("/usr/bin/id");
console.log(r.exitcode, new TextDecoder().decode(r.stdout));WARMGuestExecResult
The result of a program run through the agent. execStatus, execWait and run resolve with it.
| Field | Description |
|---|---|
exitcode | The exit code, or null when a signal ended the program. |
signal | The signal that ended it, or null. |
stdout, stderr | What it wrote, as a Uint8Array each. |
truncated | true when the agent cut the captured output short. |
WARMShare
A shared folder the guest mounts with 9p (see Shared folders). Its tree lives in memory: put files in before or while the guest runs, and read back what the guest wrote.
| Method | Description |
|---|---|
put(path, data, mode = 0o644) | Writes a file, creating its parent directories. Throws on failure. |
get(path) | The file's bytes, or null when it's absent or a directory. |
list(path = "/") | The directory's entries (subdirectories end with /), or null when it isn't a directory. |
mkdir(path) | Creates a directory. |
remove(path) | Removes a file or an empty directory; false when it can't. |
vm.shares.get("host").put("input/config.json", new TextEncoder().encode("{}"));See also
- Hosts:
WARMHost,nodeWARMHost(),browserWARMHost() - Node helpers and utilities
- QMP reference and monitor reference