Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529/** Boot command; launches a VM on the auto-detected hypervisor, waits for shutdown, then triggers incremental sync. KVM boots use a qcow2 overlay so only changed blocks need syncing. Hyper-V boots use the vhdx directly (checkpoint-based overlay is a future optimization). @module */
import { access, } from 'node:fs/promises';import { join, } from 'node:path';
import { BYTES_PER_GIB, BYTES_PER_MIB,} from '@monochromatic-dev/module-const/ts';import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { detectHypervisor, readConfig, vmDir, writeConfig,} from './config.ts';import { createOverlay, } from './qemu-img.ts';import { spawn, } from './spawn.ts';import { syncFromHyperv, syncFromKvm,} from './sync.ts';import { OVERLAY_FILENAME, QCOW2_FILENAME, VHDX_FILENAME,} from './types.ts';
/** Logger root for vmsync after removing the package log shim. @example ```ts const rl = tagged({ tag: someFunction.name, l, },); ``` */const l = tagged({ tag: 'vmsync', },);
//region OVMF firmware discovery
/** Common filesystem paths where OVMF UEFI firmware is installed. Checked in order; the first accessible path wins. */const OVMF_SEARCH_PATHS: readonly string[] = [ '/usr/share/edk2/ovmf/OVMF_CODE.fd', '/usr/share/OVMF/OVMF_CODE.fd', '/usr/share/edk2-ovmf/x64/OVMF_CODE.fd', '/usr/share/qemu/OVMF_CODE.fd', '/usr/share/ovmf/OVMF_CODE.fd',];
/** Locates the OVMF UEFI firmware file on the current system. @returns Absolute path to OVMF_CODE.fd @throws Error when no OVMF firmware is found in any known location @example ```ts const fw = await findOvmf(); // "/usr/share/edk2/ovmf/OVMF_CODE.fd" ``` */async function findOvmf(): Promise<string> { /** Tagged logger so OVMF-discovery entries are scoped to `findOvmf` in the output. */ const rl = tagged({ tag: findOvmf.name, l, },);
/** Check all candidates concurrently and return the first accessible one. */ const results = await Promise.all( OVMF_SEARCH_PATHS.map( async function checkCandidate(candidate,) { try { await access(candidate,); return candidate; } catch (error) { if (!(Error.isError(error,))) throw error;
rl.debug(`not found: ${candidate}`,); return undefined; } }, ), );
/** First accessible firmware path. */ const found = results.find( function isFound(r,) { return r !== undefined; }, );
if (found === undefined) { throw new Error( 'OVMF UEFI firmware not found. Install edk2-ovmf (Fedora/RHEL), ovmf (Debian/Ubuntu), or qemu-uefi-aarch64 (Alpine).', ); }
rl.info(`found OVMF at ${found}`,); return found;}
//endregion OVMF firmware discovery
//region KVM boot
/** Boots a VM via QEMU/KVM with a qcow2 overlay for change tracking. 1. Creates a fresh overlay via {@link createOverlay}, backed by the qcow2 base 2. Launches QEMU with UEFI, virtio devices, and NAT networking 3. Blocks until the QEMU process exits (user shuts down the VM) 4. Triggers incremental sync via {@link syncFromKvm}, from overlay to vhdx @param name - VM name @throws Error when QEMU or OVMF is unavailable, or when sync fails @example ```ts await bootKvm('alpine'); ``` */async function bootKvm(name: string,): Promise<void> { /** Tagged logger so KVM-boot entries are scoped to `bootKvm` in the output. */ const rl = tagged({ tag: bootKvm.name, l, },);
/** VM directory. */ const dir = vmDir(name,); /** Current configuration for boot parameters. */ const config = await readConfig(name,); /** Path to the qcow2 base image. */ const qcow2Path = join( dir, QCOW2_FILENAME, ); /** Path for the transient overlay. */ const overlayPath = join( dir, OVERLAY_FILENAME, );
rl.info('creating boot overlay',); await createOverlay({ overlayPath, backingPath: qcow2Path, },);
/** Path to the OVMF UEFI firmware. */ const ovmfPath = await findOvmf();
/** QEMU launch arguments: UEFI, KVM acceleration, virtio devices, NAT. */ const qemuArgs: readonly string[] = [ '-enable-kvm', '-m', config.boot .memory, '-smp', String(config.boot .cpus,), '-cpu', 'host', '-bios', ovmfPath, '-drive', `if=virtio,file=${overlayPath},format=qcow2`, '-device', 'virtio-gpu-pci', '-device', 'virtio-net-pci,netdev=net0', '-netdev', 'user,id=net0', '-device', 'virtio-keyboard-pci', '-device', 'virtio-mouse-pci', ];
rl.info(`launching QEMU: qemu-system-x86_64 ${qemuArgs.join(' ',)}`,); console.log( `booting "${name}" via KVM (close the VM window or shut down the guest to sync)`, );
//region Update state before boot config.state .lastBootHypervisor = 'kvm'; config.state .lastBootAt = new Date().toISOString(); config.state .synced = false; await writeConfig({ name, config, },); //endregion Update state before boot
await spawn({ command: 'qemu-system-x86_64', args: [...qemuArgs,], },);
rl.info('QEMU exited, starting sync',); await syncFromKvm(name,);}
//endregion KVM boot
//region Hyper-V boot
/** Boots a VM via Hyper-V on Windows. 1. Creates a temporary Hyper-V VM definition pointing to the managed vhdx 2. Configures Gen2, disables Secure Boot, sets NAT via Default Switch 3. Starts the VM and waits for it to stop 4. Removes the VM definition (preserving the vhdx) 5. Triggers sync via {@link syncFromHyperv}, from vhdx to qcow2 @param name - VM name @throws Error when Hyper-V is unavailable or PowerShell commands fail @example ```ts await bootHyperv('alpine'); ``` */async function bootHyperv(name: string,): Promise<void> { /** Tagged logger so Hyper-V-boot entries are scoped to `bootHyperv` in the output. */ const rl = tagged({ tag: bootHyperv.name, l, },);
/** VM directory. */ const dir = vmDir(name,); /** Current configuration. */ const config = await readConfig(name,); /** Path to the vhdx disk image. */ const vhdxPath = join( dir, VHDX_FILENAME, );
/** Hyper-V VM name prefixed to avoid collisions. */ const hvName = `vmsync-${name}`;
/** Memory in bytes for Hyper-V. */ const memoryBytes = parseMemoryToBytes(config.boot .memory,);
rl.info(`creating Hyper-V VM "${hvName}"`,);
/** PowerShell script that creates, configures, boots, waits, and cleans up the VM. */ const psScript = [ `New-VM -Name "${hvName}" -MemoryStartupBytes ${ String(memoryBytes,) } -VHDPath "${vhdxPath}" -Generation 2`, `Set-VMProcessor -VMName "${hvName}" -Count ${String(config.boot .cpus,)}`, `Set-VMFirmware -VMName "${hvName}" -EnableSecureBoot Off`, `Connect-VMNetworkAdapter -VMName "${hvName}" -SwitchName "Default Switch"`, `Start-VM -Name "${hvName}"`, `Wait-VM -Name "${hvName}" -For Stopped`, `Remove-VM -Name "${hvName}" -Force`, ] .join('; ',);
//region Update state before boot config.state .lastBootHypervisor = 'hyperv'; config.state .lastBootAt = new Date().toISOString(); config.state .synced = false; await writeConfig({ name, config, },); //endregion Update state before boot
rl.info('launching Hyper-V VM',); console.log(`booting "${name}" via Hyper-V (shut down the guest to sync)`,);
await spawn({ command: 'powershell', args: [ '-Command', psScript, ], },);
rl.info('Hyper-V VM stopped, starting sync',); await syncFromHyperv(name,);}
//endregion Hyper-V boot
//region Memory parsing
/** Parses a human-readable memory string (e.g. "4G", "2048M") to bytes. @param memory - Memory string with G or M suffix @returns Memory in bytes @throws Error when the format is not recognized @example ```ts parseMemoryToBytes('4G'); // 4294967296 parseMemoryToBytes('2048M'); // 2147483648 ``` */export function parseMemoryToBytes(memory: string,): number { /** Decimal base for `Number.parseInt`. */ const DECIMAL_RADIX = 10; /** Reports a malformed input with the same message shape the old regex version produced. Centralised so every failure path stays in sync. @throws Error with the canonical "invalid memory format" message @example ```ts fail(); // throws Error("invalid memory format: ...") ``` */ function fail(): never { throw new Error( `invalid memory format: "${memory}" (expected e.g. "4G" or "2048M")`, ); } /** Locates the exclusive end of the leading run of ASCII digits. @param idx - candidate scan offset; advances while digits are seen @returns first index whose character is not `0`-`9` @example ```ts findDigitsEnd(0); // 4 for memory === '2048M' ``` */ function findDigitsEnd(idx: number,): number { /** Scan cursor; starts at `idx` and advances past each leading ASCII digit in one linear pass. */ let cursor = idx; while (cursor < memory .length) { /** Char under the cursor; stops the scan when non-digit. */ const c = memory.charAt(cursor,); if ((c < '0') || (c > '9')) break; cursor += 1; } return cursor; } /** Skips ASCII space and tab characters starting at `idx`. @param idx - candidate scan offset; advances while whitespace is seen @returns first index whose character is not space or tab @example ```ts skipWhitespace(4); // 5 for memory === '2048 M' ``` */ function skipWhitespace(idx: number,): number { /** Scan cursor; starts at `idx` and advances past each space or tab in one linear pass. */ let cursor = idx; while (cursor < memory .length) { /** Char under the cursor; stops the skip when non-whitespace. */ const c = memory.charAt(cursor,); if ((c !== ' ') && (c !== '\t')) break; cursor += 1; } return cursor; } /** Exclusive end of the leading digit run; `0` means no digits were present. */ const digitsEnd = findDigitsEnd(0,); if (digitsEnd === 0) fail(); /** Digit substring used as the numeric portion. */ const digitsPart = memory.slice( 0, digitsEnd, ); /** Cursor positioned at the unit token after any inter-token whitespace. */ const unitStart = skipWhitespace(digitsEnd,); /** Unit portion of the input; must be exactly one character. */ const unitPart = memory.slice(unitStart,); if (unitPart.length !== 1) fail(); /** Unit suffix, normalized to uppercase. */ const unit = unitPart.toUpperCase(); if ((unit !== 'G') && (unit !== 'M')) fail(); /** Numeric part of the memory string. */ const value = Number.parseInt( digitsPart, DECIMAL_RADIX, ); return unit === 'G' ? value * BYTES_PER_GIB : value * BYTES_PER_MIB;}
//endregion Memory parsing
//region Public boot entry point
/** Boots a VM using the hypervisor {@link detectHypervisor} resolves for the current platform. Linux uses KVM/QEMU, Windows uses Hyper-V. After the VM shuts down, changes are automatically synced to the other format. @param name - VM name to boot @throws Error when the VM config is missing or the hypervisor is unavailable @example ```ts await bootVm('alpine'); ``` */export async function bootVm(name: string,): Promise<void> { /** Tagged logger so VM-boot entries are scoped to `bootVm` in the output. */ const rl = tagged({ tag: bootVm.name, l, },);
/** Detected hypervisor for the current platform. */ const hypervisor = detectHypervisor(); rl.info(`detected hypervisor: ${hypervisor}`,);
await (hypervisor === 'kvm' ? bootKvm(name,) : bootHyperv(name,));}
//endregion Public boot entry point