@nosana/node 1.1.84 → 1.1.86-rc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.env +2 -1
- package/dist/package.json +1 -1
- package/dist/packages/common/src/formatTime.d.ts +2 -2
- package/dist/packages/common/src/formatTime.js +9 -5
- package/dist/packages/job-runtime/src/host/HostClient.d.ts +20 -4
- package/dist/packages/job-runtime/src/host/HostClient.js +35 -2
- package/dist/packages/job-runtime/src/host/contract.d.ts +27 -7
- package/dist/packages/job-runtime/src/host/contract.js +43 -7
- package/dist/packages/job-runtime/src/runtime/runJob.d.ts +8 -5
- package/dist/packages/job-runtime/src/runtime/runJob.js +44 -17
- package/dist/packages/vm-orchestrator/src/CidRegistry.d.ts +67 -0
- package/dist/packages/vm-orchestrator/src/CidRegistry.js +168 -0
- package/dist/packages/vm-orchestrator/src/GpuPool.d.ts +27 -0
- package/dist/packages/vm-orchestrator/src/GpuPool.js +41 -0
- package/dist/packages/vm-orchestrator/src/Hypervisor.d.ts +66 -0
- package/dist/packages/vm-orchestrator/src/Hypervisor.js +7 -0
- package/dist/packages/vm-orchestrator/src/QemuHypervisor.d.ts +62 -0
- package/dist/packages/vm-orchestrator/src/QemuHypervisor.js +185 -0
- package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.d.ts +61 -0
- package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.js +37 -0
- package/dist/packages/vm-orchestrator/src/QemuVmInstance.d.ts +37 -0
- package/dist/packages/vm-orchestrator/src/QemuVmInstance.js +44 -0
- package/dist/packages/vm-orchestrator/src/QmpClient.d.ts +43 -0
- package/dist/packages/vm-orchestrator/src/QmpClient.js +103 -0
- package/dist/packages/vm-orchestrator/src/VmSpec.d.ts +21 -0
- package/dist/packages/vm-orchestrator/src/VmSpec.js +30 -0
- package/dist/packages/vm-orchestrator/src/VmStateManager.d.ts +16 -0
- package/dist/packages/vm-orchestrator/src/VmStateManager.js +30 -0
- package/dist/packages/vm-orchestrator/src/bootAttempt.d.ts +31 -0
- package/dist/packages/vm-orchestrator/src/bootAttempt.js +193 -0
- package/dist/packages/vm-orchestrator/src/bootErrors.d.ts +33 -0
- package/dist/packages/vm-orchestrator/src/bootErrors.js +67 -0
- package/dist/packages/vm-orchestrator/src/buildCloudInit.d.ts +33 -0
- package/dist/packages/vm-orchestrator/src/buildCloudInit.js +55 -0
- package/dist/packages/vm-orchestrator/src/buildQemuArgs.d.ts +33 -0
- package/dist/packages/vm-orchestrator/src/buildQemuArgs.js +127 -0
- package/dist/packages/vm-orchestrator/src/diskSizing.d.ts +18 -0
- package/dist/packages/vm-orchestrator/src/diskSizing.js +41 -0
- package/dist/packages/vm-orchestrator/src/firmware.d.ts +5 -0
- package/dist/packages/vm-orchestrator/src/firmware.js +18 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.d.ts +22 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.js +33 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.d.ts +3 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.js +17 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.d.ts +9 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.js +17 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.d.ts +6 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.js +42 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/index.d.ts +1 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/index.js +1 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.d.ts +6 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.js +18 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.d.ts +7 -0
- package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.js +30 -0
- package/dist/packages/vm-orchestrator/src/hostDevices.d.ts +12 -0
- package/dist/packages/vm-orchestrator/src/hostDevices.js +12 -0
- package/dist/packages/vm-orchestrator/src/hostTools.d.ts +25 -0
- package/dist/packages/vm-orchestrator/src/hostTools.js +39 -0
- package/dist/packages/vm-orchestrator/src/imageCache.d.ts +25 -0
- package/dist/packages/vm-orchestrator/src/imageCache.js +73 -0
- package/dist/packages/vm-orchestrator/src/imageDownload.d.ts +31 -0
- package/dist/packages/vm-orchestrator/src/imageDownload.js +85 -0
- package/dist/packages/vm-orchestrator/src/index.d.ts +19 -0
- package/dist/packages/vm-orchestrator/src/index.js +15 -0
- package/dist/packages/vm-orchestrator/src/memlockLimit.d.ts +5 -0
- package/dist/packages/vm-orchestrator/src/memlockLimit.js +23 -0
- package/dist/packages/vm-orchestrator/src/nvidiaHolders.d.ts +11 -0
- package/dist/packages/vm-orchestrator/src/nvidiaHolders.js +74 -0
- package/dist/packages/vm-orchestrator/src/pciNuma.d.ts +7 -0
- package/dist/packages/vm-orchestrator/src/pciNuma.js +18 -0
- package/dist/packages/vm-orchestrator/src/pciSysfs.d.ts +21 -0
- package/dist/packages/vm-orchestrator/src/pciSysfs.js +47 -0
- package/dist/packages/vm-orchestrator/src/processes.d.ts +37 -0
- package/dist/packages/vm-orchestrator/src/processes.js +102 -0
- package/dist/packages/vm-orchestrator/src/sockets.d.ts +21 -0
- package/dist/packages/vm-orchestrator/src/sockets.js +58 -0
- package/dist/packages/vm-orchestrator/src/sweepVmImageCache.d.ts +19 -0
- package/dist/packages/vm-orchestrator/src/sweepVmImageCache.js +132 -0
- package/dist/packages/vm-orchestrator/src/util.d.ts +14 -0
- package/dist/packages/vm-orchestrator/src/util.js +33 -0
- package/dist/packages/vm-orchestrator/src/vfioBinding.d.ts +27 -0
- package/dist/packages/vm-orchestrator/src/vfioBinding.js +163 -0
- package/dist/packages/vm-orchestrator/src/vmImageResolver.d.ts +20 -0
- package/dist/packages/vm-orchestrator/src/vmImageResolver.js +62 -0
- package/dist/packages/vm-orchestrator/src/vsockBridge.d.ts +20 -0
- package/dist/packages/vm-orchestrator/src/vsockBridge.js +27 -0
- package/dist/packages/vm-orchestrator/src/workDirs.d.ts +49 -0
- package/dist/packages/vm-orchestrator/src/workDirs.js +79 -0
- package/dist/src/NodeManager/configs/configs.d.ts +4 -0
- package/dist/src/NodeManager/configs/configs.js +3 -0
- package/dist/src/NodeManager/db/index.d.ts +10 -0
- package/dist/src/NodeManager/db/index.js +4 -0
- package/dist/src/NodeManager/monitoring/log/NodeLog.d.ts +2 -0
- package/dist/src/NodeManager/monitoring/log/NodeLog.js +21 -0
- package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.d.ts +8 -0
- package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.js +29 -5
- package/dist/src/NodeManager/monitoring/log/vmSetupLog.d.ts +11 -0
- package/dist/src/NodeManager/monitoring/log/vmSetupLog.js +33 -0
- package/dist/src/NodeManager/node/Node.d.ts +2 -0
- package/dist/src/NodeManager/node/Node.js +12 -5
- package/dist/src/NodeManager/node/job/ContainerJobRuntime.d.ts +141 -0
- package/dist/src/NodeManager/node/job/ContainerJobRuntime.js +419 -0
- package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.d.ts +74 -0
- package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.js +1 -0
- package/dist/src/NodeManager/node/job/JobRuntimeHost.d.ts +6 -2
- package/dist/src/NodeManager/node/job/JobRuntimeHost.js +13 -2
- package/dist/src/NodeManager/node/job/JobRuntimeSession.d.ts +12 -9
- package/dist/src/NodeManager/node/job/JobRuntimeSession.js +42 -71
- package/dist/src/NodeManager/node/job/VmJobRuntime.d.ts +109 -0
- package/dist/src/NodeManager/node/job/VmJobRuntime.js +258 -0
- package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.d.ts +33 -0
- package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.js +1 -0
- package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.d.ts +41 -0
- package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.js +25 -0
- package/dist/src/NodeManager/node/job/followFile.d.ts +14 -0
- package/dist/src/NodeManager/node/job/followFile.js +57 -0
- package/dist/src/NodeManager/node/job/jobHandler.d.ts +3 -3
- package/dist/src/NodeManager/node/job/jobHandler.js +7 -7
- package/dist/src/NodeManager/node/job/keepFailedConsole.d.ts +7 -0
- package/dist/src/NodeManager/node/job/keepFailedConsole.js +52 -0
- package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.d.ts +50 -0
- package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.js +117 -0
- package/dist/src/NodeManager/node/market/benchmark.d.ts +3 -3
- package/dist/src/NodeManager/node/market/benchmark.js +3 -3
- package/dist/src/NodeManager/node/market/marketHandler.d.ts +3 -3
- package/dist/src/NodeManager/node/market/marketHandler.js +3 -3
- package/dist/src/NodeManager/node/resource/resourceManager.d.ts +1 -1
- package/dist/src/NodeManager/node/resource/resourceManager.js +1 -1
- package/dist/src/NodeManager/provider/Provider.d.ts +3 -121
- package/dist/src/NodeManager/provider/Provider.js +3 -369
- package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.d.ts +14 -0
- package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.js +1 -0
- package/dist/src/NodeManager/provider/vmHost/capacity.d.ts +16 -0
- package/dist/src/NodeManager/provider/vmHost/capacity.js +47 -0
- package/dist/src/NodeManager/provider/vmHost/gpus.d.ts +19 -0
- package/dist/src/NodeManager/provider/vmHost/gpus.js +83 -0
- package/dist/src/NodeManager/provider/vmHost/index.d.ts +5 -0
- package/dist/src/NodeManager/provider/vmHost/index.js +4 -0
- package/dist/src/NodeManager/provider/vmHost/readVmSpec.d.ts +11 -0
- package/dist/src/NodeManager/provider/vmHost/readVmSpec.js +22 -0
- package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.d.ts +27 -0
- package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.js +25 -0
- package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.d.ts +6 -0
- package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.js +22 -0
- package/dist/src/NodeManager/provider/vmHost/setup/hostUser.d.ts +15 -0
- package/dist/src/NodeManager/provider/vmHost/setup/hostUser.js +25 -0
- package/dist/src/NodeManager/provider/vmHost/setup/index.d.ts +2 -0
- package/dist/src/NodeManager/provider/vmHost/setup/index.js +2 -0
- package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.d.ts +6 -0
- package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.js +12 -0
- package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.d.ts +17 -0
- package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.js +90 -0
- package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.d.ts +5 -0
- package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.js +35 -0
- package/dist/src/NodeManager/provider/vmHost/vmHostEnv.d.ts +20 -0
- package/dist/src/NodeManager/provider/vmHost/vmHostEnv.js +24 -0
- package/dist/src/NodeManager/provider/vmHost/workRoot.d.ts +14 -0
- package/dist/src/NodeManager/provider/vmHost/workRoot.js +84 -0
- package/dist/src/NodeManager/repository/NodeRepository.d.ts +12 -2
- package/dist/src/NodeManager/repository/NodeRepository.js +21 -1
- package/dist/src/cli/commands/prune/command.js +2 -1
- package/dist/src/cli/commands/run/action.d.ts +2 -2
- package/dist/src/cli/commands/run/action.js +15 -34
- package/dist/src/cli/commands/run/command.js +7 -2
- package/dist/src/cli/commands/sharedOptions/--config.d.ts +2 -0
- package/dist/src/cli/commands/sharedOptions/--config.js +2 -0
- package/dist/src/cli/commands/sharedOptions/--environment.d.ts +6 -0
- package/dist/src/cli/commands/sharedOptions/--environment.js +6 -0
- package/dist/src/cli/commands/sharedOptions/--gpu.js +5 -2
- package/dist/src/cli/commands/sharedOptions/--node-limits.d.ts +9 -0
- package/dist/src/cli/commands/sharedOptions/--node-limits.js +24 -0
- package/dist/src/cli/commands/sharedOptions/index.d.ts +3 -0
- package/dist/src/cli/commands/sharedOptions/index.js +3 -0
- package/dist/src/cli/commands/start/command.js +7 -2
- package/dist/src/cli/commands/vm-setup/action.d.ts +10 -0
- package/dist/src/cli/commands/vm-setup/action.js +14 -0
- package/dist/src/cli/commands/vm-setup/command.d.ts +3 -0
- package/dist/src/cli/commands/vm-setup/command.js +11 -0
- package/dist/src/cli/createNosanaCli.d.ts +1 -0
- package/dist/src/cli/createNosanaCli.js +4 -1
- package/npm-shrinkwrap.json +10 -2
- package/package.json +1 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ChildProcess } from 'child_process';
|
|
2
|
+
import type { VmInstance, VmRecord } from './Hypervisor.js';
|
|
3
|
+
import { type KillLike } from './processes.js';
|
|
4
|
+
import type { QmpClient } from './QmpClient.js';
|
|
5
|
+
import type { VmStateManager } from './VmStateManager.js';
|
|
6
|
+
/** A VM as an instance drives it. */
|
|
7
|
+
export interface QemuVm {
|
|
8
|
+
record: VmRecord;
|
|
9
|
+
/** The QMP control connection. */
|
|
10
|
+
qmp: QmpClient;
|
|
11
|
+
/** The serial console, streaming to the work dir's log. */
|
|
12
|
+
consoleLog: VmStateManager;
|
|
13
|
+
/** QEMU's ChildProcess when this process spawned it; absent after attach. */
|
|
14
|
+
qemu?: ChildProcess;
|
|
15
|
+
}
|
|
16
|
+
/** How an instance acts on the host; the hypervisor provides it. */
|
|
17
|
+
export interface QemuVmInstanceSystem {
|
|
18
|
+
/** Is a pid alive? */
|
|
19
|
+
isPidAlive: (pid: number) => boolean;
|
|
20
|
+
/** Signal a process (a negative pid: its process group). */
|
|
21
|
+
killPid: KillLike;
|
|
22
|
+
/** Free the VM's GPUs and CID; called once QEMU is reaped. */
|
|
23
|
+
release: () => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/** A VM booted or attached by {@link QemuHypervisor}. */
|
|
26
|
+
export declare class QemuVmInstance implements VmInstance {
|
|
27
|
+
private readonly vm;
|
|
28
|
+
private readonly system;
|
|
29
|
+
readonly record: VmRecord;
|
|
30
|
+
private exit?;
|
|
31
|
+
constructor(vm: QemuVm, system: QemuVmInstanceSystem);
|
|
32
|
+
waitForExit(): Promise<number | null>;
|
|
33
|
+
shutdown(): Promise<void>;
|
|
34
|
+
destroy(): Promise<void>;
|
|
35
|
+
/** A QEMU this process did not spawn: poll its pid until it is gone. */
|
|
36
|
+
private pollExit;
|
|
37
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { QEMU_REAP_TIMEOUT_MS } from './GpuPool.js';
|
|
2
|
+
import { childExit, killProcess, killProcessGroup } from './processes.js';
|
|
3
|
+
import { sleep, waitAtMost } from './util.js';
|
|
4
|
+
import { removeWorkDir } from './workDirs.js';
|
|
5
|
+
/** How often the pid of a QEMU this process did not spawn is checked for its exit. */
|
|
6
|
+
const EXIT_POLL_MS = 250;
|
|
7
|
+
/** A VM booted or attached by {@link QemuHypervisor}. */
|
|
8
|
+
export class QemuVmInstance {
|
|
9
|
+
constructor(vm, system) {
|
|
10
|
+
this.vm = vm;
|
|
11
|
+
this.system = system;
|
|
12
|
+
this.record = vm.record;
|
|
13
|
+
}
|
|
14
|
+
waitForExit() {
|
|
15
|
+
this.exit ??= this.vm.qemu ? childExit(this.vm.qemu) : this.pollExit();
|
|
16
|
+
return this.exit;
|
|
17
|
+
}
|
|
18
|
+
async shutdown() {
|
|
19
|
+
try {
|
|
20
|
+
await this.vm.qmp.execute('system_powerdown');
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// best-effort; destroy() force-kills if this doesn't take
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async destroy() {
|
|
27
|
+
const { qmp, consoleLog, record } = this.vm;
|
|
28
|
+
qmp.close();
|
|
29
|
+
consoleLog.stop();
|
|
30
|
+
killProcessGroup(this.system.killPid, record.bridgePid);
|
|
31
|
+
killProcess(this.system.killPid, record.qemuPid);
|
|
32
|
+
// The GPU and CID are freed only once QEMU has been reaped, not merely
|
|
33
|
+
// signalled (see QEMU_REAP_TIMEOUT_MS). If QEMU outlives the wait, they
|
|
34
|
+
// stay reserved until it does exit.
|
|
35
|
+
await waitAtMost(this.waitForExit().then(() => this.system.release()), QEMU_REAP_TIMEOUT_MS);
|
|
36
|
+
await removeWorkDir(record.workdir).catch(() => undefined);
|
|
37
|
+
}
|
|
38
|
+
/** A QEMU this process did not spawn: poll its pid until it is gone. */
|
|
39
|
+
async pollExit() {
|
|
40
|
+
while (this.system.isPidAlive(this.record.qemuPid))
|
|
41
|
+
await sleep(EXIT_POLL_MS);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** A QMP error reply's body. */
|
|
2
|
+
export interface QmpError {
|
|
3
|
+
class?: string;
|
|
4
|
+
desc?: string;
|
|
5
|
+
}
|
|
6
|
+
/** One message from QEMU: a greeting, a command reply, or an async event. */
|
|
7
|
+
export interface QmpMessage {
|
|
8
|
+
event?: string;
|
|
9
|
+
data?: unknown;
|
|
10
|
+
return?: unknown;
|
|
11
|
+
error?: QmpError;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Minimal QMP (QEMU Machine Protocol) client over a unix socket.
|
|
16
|
+
*
|
|
17
|
+
* QMP is newline-delimited JSON. On connect, QEMU sends a greeting
|
|
18
|
+
* (`{"QMP": …}`); the client must answer with `qmp_capabilities` before issuing
|
|
19
|
+
* commands, which {@link connect} does before resolving. Command replies arrive
|
|
20
|
+
* in order (async events interleave and are ignored), so replies are
|
|
21
|
+
* correlated FIFO. This is the control channel the hypervisor uses to power
|
|
22
|
+
* the guest down.
|
|
23
|
+
*/
|
|
24
|
+
export declare class QmpClient {
|
|
25
|
+
private socket?;
|
|
26
|
+
private buffer;
|
|
27
|
+
private isGreeted;
|
|
28
|
+
private isClosed;
|
|
29
|
+
private readonly pending;
|
|
30
|
+
/** Settles {@link connect} once the capabilities handshake is done. */
|
|
31
|
+
private ready?;
|
|
32
|
+
/** Connect to QEMU's QMP socket and negotiate capabilities. */
|
|
33
|
+
connect(socketPath: string): Promise<void>;
|
|
34
|
+
/** Run a QMP command; resolves with its `return`, rejects with its error. */
|
|
35
|
+
execute(command: string, args?: object): Promise<unknown>;
|
|
36
|
+
/** Close the connection; commands in flight are rejected. */
|
|
37
|
+
close(): void;
|
|
38
|
+
private onData;
|
|
39
|
+
private dispatch;
|
|
40
|
+
/** Answer the greeting: once QEMU accepts `qmp_capabilities`, the client is ready. */
|
|
41
|
+
private negotiateCapabilities;
|
|
42
|
+
private onClose;
|
|
43
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
/**
|
|
3
|
+
* Minimal QMP (QEMU Machine Protocol) client over a unix socket.
|
|
4
|
+
*
|
|
5
|
+
* QMP is newline-delimited JSON. On connect, QEMU sends a greeting
|
|
6
|
+
* (`{"QMP": …}`); the client must answer with `qmp_capabilities` before issuing
|
|
7
|
+
* commands, which {@link connect} does before resolving. Command replies arrive
|
|
8
|
+
* in order (async events interleave and are ignored), so replies are
|
|
9
|
+
* correlated FIFO. This is the control channel the hypervisor uses to power
|
|
10
|
+
* the guest down.
|
|
11
|
+
*/
|
|
12
|
+
export class QmpClient {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.buffer = '';
|
|
15
|
+
this.isGreeted = false;
|
|
16
|
+
this.isClosed = false;
|
|
17
|
+
this.pending = [];
|
|
18
|
+
}
|
|
19
|
+
/** Connect to QEMU's QMP socket and negotiate capabilities. */
|
|
20
|
+
connect(socketPath) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const socket = net.createConnection(socketPath);
|
|
23
|
+
this.socket = socket;
|
|
24
|
+
this.ready = { resolve: () => resolve(), reject };
|
|
25
|
+
socket.on('data', (chunk) => this.onData(chunk));
|
|
26
|
+
// After the greeting a socket error surfaces as 'close', which rejects
|
|
27
|
+
// the commands in flight.
|
|
28
|
+
socket.on('error', (error) => {
|
|
29
|
+
if (!this.isGreeted)
|
|
30
|
+
reject(error);
|
|
31
|
+
});
|
|
32
|
+
socket.on('close', () => this.onClose());
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Run a QMP command; resolves with its `return`, rejects with its error. */
|
|
36
|
+
execute(command, args) {
|
|
37
|
+
const { socket } = this;
|
|
38
|
+
if (!socket || this.isClosed) {
|
|
39
|
+
return Promise.reject(new Error('QMP not connected'));
|
|
40
|
+
}
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
this.pending.push({ resolve, reject });
|
|
43
|
+
const payload = args ? { execute: command, arguments: args } : { execute: command };
|
|
44
|
+
socket.write(JSON.stringify(payload) + '\n');
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** Close the connection; commands in flight are rejected. */
|
|
48
|
+
close() {
|
|
49
|
+
this.isClosed = true;
|
|
50
|
+
this.socket?.destroy();
|
|
51
|
+
this.socket = undefined;
|
|
52
|
+
}
|
|
53
|
+
onData(chunk) {
|
|
54
|
+
this.buffer += chunk.toString('utf8');
|
|
55
|
+
let newline;
|
|
56
|
+
while ((newline = this.buffer.indexOf('\n')) !== -1) {
|
|
57
|
+
const message = parseMessage(this.buffer.slice(0, newline));
|
|
58
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
59
|
+
if (message)
|
|
60
|
+
this.dispatch(message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
dispatch(message) {
|
|
64
|
+
if (!this.isGreeted && 'QMP' in message) {
|
|
65
|
+
this.negotiateCapabilities();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!('return' in message || 'error' in message))
|
|
69
|
+
return;
|
|
70
|
+
const reply = this.pending.shift();
|
|
71
|
+
if (!reply)
|
|
72
|
+
return;
|
|
73
|
+
if (message.error) {
|
|
74
|
+
reply.reject(new Error(message.error.desc ?? 'QMP command failed'));
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
reply.resolve(message.return);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Answer the greeting: once QEMU accepts `qmp_capabilities`, the client is ready. */
|
|
81
|
+
negotiateCapabilities() {
|
|
82
|
+
this.isGreeted = true;
|
|
83
|
+
this.execute('qmp_capabilities').then((value) => this.ready?.resolve(value), (error) => this.ready?.reject(error));
|
|
84
|
+
}
|
|
85
|
+
onClose() {
|
|
86
|
+
this.isClosed = true;
|
|
87
|
+
for (const reply of this.pending.splice(0)) {
|
|
88
|
+
reply.reject(new Error('QMP connection closed'));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** A line of QMP as a message; undefined for a blank, partial or garbage line. */
|
|
93
|
+
function parseMessage(line) {
|
|
94
|
+
const trimmed = line.trim();
|
|
95
|
+
if (!trimmed)
|
|
96
|
+
return undefined;
|
|
97
|
+
try {
|
|
98
|
+
return JSON.parse(trimmed);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a VM is booted with: the node builds it once at start and boots every job's VM from
|
|
3
|
+
* it.
|
|
4
|
+
*/
|
|
5
|
+
export interface VmSpec {
|
|
6
|
+
/** vCPU count (>= 1). */
|
|
7
|
+
vcpus: number;
|
|
8
|
+
/** Guest memory in MiB. */
|
|
9
|
+
memoryMiB: number;
|
|
10
|
+
/** Host PCI addresses (BDFs) of the GPUs passed through (VFIO); empty for none. */
|
|
11
|
+
gpus: string[];
|
|
12
|
+
/** The disk size in MiB, capped by the space free; undefined: sized from the free space. */
|
|
13
|
+
diskMiB?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parse a memory/disk size into MiB. A bare number is already MiB; a string may
|
|
17
|
+
* carry a binary unit suffix, so "4G" is 4096, "512M" is 512, "2048" is 2048.
|
|
18
|
+
* Throws on a non-positive or unparseable value so a malformed spec fails
|
|
19
|
+
* loudly rather than booting a 0 MiB VM.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseMemoryToMiB(value: number | string): number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** MiB per unit of a size suffix. */
|
|
2
|
+
const MIB_PER_UNIT = {
|
|
3
|
+
k: 1 / 1024,
|
|
4
|
+
m: 1,
|
|
5
|
+
g: 1024,
|
|
6
|
+
t: 1024 * 1024,
|
|
7
|
+
};
|
|
8
|
+
/** A size: a number with an optional binary unit (`K`/`M`/`G`/`T`, optionally `i`/`B`). */
|
|
9
|
+
const SIZE_PATTERN = /^\s*(\d+(?:\.\d+)?)\s*([kKmMgGtT])?(?:i?[bB])?\s*$/;
|
|
10
|
+
/**
|
|
11
|
+
* Parse a memory/disk size into MiB. A bare number is already MiB; a string may
|
|
12
|
+
* carry a binary unit suffix, so "4G" is 4096, "512M" is 512, "2048" is 2048.
|
|
13
|
+
* Throws on a non-positive or unparseable value so a malformed spec fails
|
|
14
|
+
* loudly rather than booting a 0 MiB VM.
|
|
15
|
+
*/
|
|
16
|
+
export function parseMemoryToMiB(value) {
|
|
17
|
+
if (typeof value === 'number') {
|
|
18
|
+
if (!Number.isFinite(value) || value <= 0)
|
|
19
|
+
throw new Error(`invalid memory value: ${value}`);
|
|
20
|
+
return Math.round(value);
|
|
21
|
+
}
|
|
22
|
+
const match = SIZE_PATTERN.exec(value);
|
|
23
|
+
if (!match)
|
|
24
|
+
throw new Error(`invalid memory value: "${value}"`);
|
|
25
|
+
const amount = parseFloat(match[1]);
|
|
26
|
+
if (amount <= 0)
|
|
27
|
+
throw new Error(`invalid memory value: "${value}"`);
|
|
28
|
+
const unit = (match[2] ?? 'm').toLowerCase();
|
|
29
|
+
return Math.round(amount * MIB_PER_UNIT[unit]);
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Readable } from 'stream';
|
|
2
|
+
/**
|
|
3
|
+
* Streams a VM's serial console to a log file in its work dir (`console.log`).
|
|
4
|
+
* Appends, so a node that attaches to a running VM again carries the same file
|
|
5
|
+
* on. Output the guest writes while nobody is connected is dropped by QEMU.
|
|
6
|
+
*/
|
|
7
|
+
export declare class VmStateManager {
|
|
8
|
+
private readonly serial;
|
|
9
|
+
private readonly logPath;
|
|
10
|
+
private file?;
|
|
11
|
+
constructor(serial: Readable, logPath: string);
|
|
12
|
+
/** Start streaming the console to the log file. */
|
|
13
|
+
start(): void;
|
|
14
|
+
/** Stop streaming: close the console connection and the file. */
|
|
15
|
+
stop(): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createWriteStream } from 'fs';
|
|
2
|
+
/**
|
|
3
|
+
* Streams a VM's serial console to a log file in its work dir (`console.log`).
|
|
4
|
+
* Appends, so a node that attaches to a running VM again carries the same file
|
|
5
|
+
* on. Output the guest writes while nobody is connected is dropped by QEMU.
|
|
6
|
+
*/
|
|
7
|
+
export class VmStateManager {
|
|
8
|
+
constructor(serial, logPath) {
|
|
9
|
+
this.serial = serial;
|
|
10
|
+
this.logPath = logPath;
|
|
11
|
+
}
|
|
12
|
+
/** Start streaming the console to the log file. */
|
|
13
|
+
start() {
|
|
14
|
+
this.file = createWriteStream(this.logPath, { flags: 'a' });
|
|
15
|
+
this.file.on('error', () => {
|
|
16
|
+
// a log that cannot be written must not take the VM down with it
|
|
17
|
+
});
|
|
18
|
+
this.serial.on('error', () => {
|
|
19
|
+
// the console connection dies with the VM; nothing to report
|
|
20
|
+
});
|
|
21
|
+
this.serial.pipe(this.file);
|
|
22
|
+
}
|
|
23
|
+
/** Stop streaming: close the console connection and the file. */
|
|
24
|
+
stop() {
|
|
25
|
+
if (this.file)
|
|
26
|
+
this.serial.unpipe(this.file);
|
|
27
|
+
this.serial.destroy();
|
|
28
|
+
this.file?.end();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ChildProcess } from 'child_process';
|
|
2
|
+
import type { CidRegistry } from './CidRegistry.js';
|
|
3
|
+
import type { BootOptions, VmRecord } from './Hypervisor.js';
|
|
4
|
+
import type { QemuHypervisorSystem, ResolvedQemuHypervisorConfig } from './QemuHypervisorConfig.js';
|
|
5
|
+
import { type VmConnection } from './sockets.js';
|
|
6
|
+
import type { VmSpec } from './VmSpec.js';
|
|
7
|
+
/** What a boot attempt runs on: the hypervisor's config, host, CID registry and firmware. */
|
|
8
|
+
export interface BootContext {
|
|
9
|
+
config: ResolvedQemuHypervisorConfig;
|
|
10
|
+
system: QemuHypervisorSystem;
|
|
11
|
+
cids: CidRegistry;
|
|
12
|
+
/** The OVMF firmware a GPU VM boots; undefined: the default firmware. */
|
|
13
|
+
biosPath?: string;
|
|
14
|
+
}
|
|
15
|
+
/** One boot attempt's inputs: the VM (its GPUs reserved for it) and the CID claimed for it. */
|
|
16
|
+
export interface BootRequest {
|
|
17
|
+
spec: VmSpec;
|
|
18
|
+
options: BootOptions;
|
|
19
|
+
cid: number;
|
|
20
|
+
}
|
|
21
|
+
/** A booted VM: its record, its control and console connections, and its QEMU. */
|
|
22
|
+
export interface BootedVm extends VmConnection {
|
|
23
|
+
record: VmRecord;
|
|
24
|
+
qemu: ChildProcess;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Boot a VM once: a disk overlay over the base image, the cloud-init seed, QEMU
|
|
28
|
+
* and the host bridge spawned detached, then QMP and the console connected.
|
|
29
|
+
* A failed attempt reaps what it spawned and removes its work dir.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bootAttempt(context: BootContext, request: BootRequest): Promise<BootedVm>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import { buildCloudInitSeed } from './buildCloudInit.js';
|
|
3
|
+
import { buildQemuArgs } from './buildQemuArgs.js';
|
|
4
|
+
import { BridgeExitedError, QemuExitedError, readLogTail } from './bootErrors.js';
|
|
5
|
+
import { resolveAutoDiskMiB } from './diskSizing.js';
|
|
6
|
+
import { QEMU_REAP_TIMEOUT_MS } from './GpuPool.js';
|
|
7
|
+
import { ISO_BINARY, NUMA_TOOL, QEMU_BINARY, QEMU_IMG_BINARY } from './hostTools.js';
|
|
8
|
+
import { lockVmImage } from './imageCache.js';
|
|
9
|
+
import { childExit, hasExited, killProcess, killProcessGroup, spawnDetached, } from './processes.js';
|
|
10
|
+
import { connectVmSockets } from './sockets.js';
|
|
11
|
+
import { describeError, waitAtMost } from './util.js';
|
|
12
|
+
import { hostVsockPort, spawnBridge } from './vsockBridge.js';
|
|
13
|
+
import { makeWorkDir } from './workDirs.js';
|
|
14
|
+
/**
|
|
15
|
+
* Boot a VM once: a disk overlay over the base image, the cloud-init seed, QEMU
|
|
16
|
+
* and the host bridge spawned detached, then QMP and the console connected.
|
|
17
|
+
* A failed attempt reaps what it spawned and removes its work dir.
|
|
18
|
+
*/
|
|
19
|
+
export async function bootAttempt(context, request) {
|
|
20
|
+
const { config, system, cids } = context;
|
|
21
|
+
const { spec, options, cid } = request;
|
|
22
|
+
const { hostSocket, cloudInit, log } = options;
|
|
23
|
+
const hostPort = hostVsockPort(cid);
|
|
24
|
+
const paths = await makeWorkDir(config.workRoot);
|
|
25
|
+
const spawned = {};
|
|
26
|
+
try {
|
|
27
|
+
await createDisk(context, request, paths.overlay);
|
|
28
|
+
await writeSeedIso(system.run, paths, { ...cloudInit, hostVsockPort: hostPort }, log);
|
|
29
|
+
log?.('Starting QEMU');
|
|
30
|
+
const apiHostPort = await system.allocatePort();
|
|
31
|
+
const qemu = spawnQemu(context, spec, paths, { apiHostPort, cid }, log);
|
|
32
|
+
spawned.qemu = qemu;
|
|
33
|
+
const qemuEarlyExit = watchForEarlyExit(qemu, paths.qemuLog);
|
|
34
|
+
const bridge = startBridge(system, paths, hostPort, hostSocket, log);
|
|
35
|
+
spawned.bridge = bridge;
|
|
36
|
+
const connection = await connectVm(system, paths, qemuEarlyExit, log);
|
|
37
|
+
const bridgePid = await assertBridgeRunning(bridge, connection, paths.bridgeLog);
|
|
38
|
+
if (qemu.pid === undefined)
|
|
39
|
+
throw new Error('QEMU has no pid');
|
|
40
|
+
// The CID is QEMU's from here on: its lock lives as long as the VM, not as
|
|
41
|
+
// long as this node process.
|
|
42
|
+
await cids.transfer(cid, qemu.pid);
|
|
43
|
+
const { workdir } = paths;
|
|
44
|
+
const { gpus } = spec;
|
|
45
|
+
const record = { workdir, qemuPid: qemu.pid, bridgePid, cid, apiHostPort, hostSocket, gpus };
|
|
46
|
+
return { ...connection, record, qemu };
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
await reapAttempt(system.killPid, spawned, paths.workdir);
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function createDisk(context, request, overlay) {
|
|
54
|
+
const { imagePath, log } = request.options;
|
|
55
|
+
log?.('Creating disk overlay');
|
|
56
|
+
await createOverlay(context.system.run, imagePath, overlay);
|
|
57
|
+
await growDisk(context, request, overlay);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create the qcow2 overlay over the immutable base image, under the image's
|
|
61
|
+
* lock so the image cache sweep never deletes it between the resolve and the
|
|
62
|
+
* overlay naming it. A base image whose lock cannot be written (a local image
|
|
63
|
+
* in a read-only dir) is used unlocked.
|
|
64
|
+
*/
|
|
65
|
+
async function createOverlay(run, imagePath, overlay) {
|
|
66
|
+
const releaseImage = await lockVmImage(imagePath).catch(() => undefined);
|
|
67
|
+
try {
|
|
68
|
+
await run(QEMU_IMG_BINARY, ['create', '-f', 'qcow2', '-F', 'qcow2', '-b', imagePath, overlay]);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await releaseImage?.();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Grow the overlay to the auto-sized disk (see resolveAutoDiskMiB). */
|
|
75
|
+
async function growDisk(context, request, overlay) {
|
|
76
|
+
const { config, system } = context;
|
|
77
|
+
const { spec, options } = request;
|
|
78
|
+
const { log } = options;
|
|
79
|
+
const diskMiB = await resolveAutoDiskMiB(system.freeSpace, config.workRoot, spec.diskMiB);
|
|
80
|
+
if (!diskMiB)
|
|
81
|
+
return;
|
|
82
|
+
try {
|
|
83
|
+
await system.run(QEMU_IMG_BINARY, ['resize', overlay, `${diskMiB}M`]);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
// A target below the image's virtual size makes qemu-img refuse to shrink,
|
|
87
|
+
// a safe no-op: keep the base size and boot on.
|
|
88
|
+
log?.(`Disk auto-resize skipped (${describeError(error).message}); using image size`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** Write the cloud-init NoCloud seed ISO: the guest's job dir. */
|
|
92
|
+
async function writeSeedIso(run, paths, cloudInit, log) {
|
|
93
|
+
log?.('Building cloud-init seed');
|
|
94
|
+
const { userData, metaData } = buildCloudInitSeed(cloudInit);
|
|
95
|
+
await fs.writeFile(paths.userData, userData);
|
|
96
|
+
await fs.writeFile(paths.metaData, metaData);
|
|
97
|
+
await run(ISO_BINARY, [
|
|
98
|
+
'-output',
|
|
99
|
+
paths.seedIso,
|
|
100
|
+
'-volid',
|
|
101
|
+
'cidata',
|
|
102
|
+
'-joliet',
|
|
103
|
+
'-rock',
|
|
104
|
+
paths.userData,
|
|
105
|
+
paths.metaData,
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
/** Spawn QEMU detached, wrapped in numactl when its first GPU is on a NUMA node. */
|
|
109
|
+
function spawnQemu(context, spec, paths, devices, log) {
|
|
110
|
+
const { config, system, biosPath } = context;
|
|
111
|
+
const { apiHostPort, cid } = devices;
|
|
112
|
+
const args = buildQemuArgs(spec, paths, {
|
|
113
|
+
accel: config.accel,
|
|
114
|
+
apiHostPort,
|
|
115
|
+
guestCid: cid,
|
|
116
|
+
biosPath,
|
|
117
|
+
});
|
|
118
|
+
// The full argv, so a boot that "succeeds" but behaves wrong (only some
|
|
119
|
+
// passed-through GPUs initialise) can be diagnosed against the exact command.
|
|
120
|
+
log?.(`QEMU args: ${args.join(' ')}`);
|
|
121
|
+
const numaNode = readGpuNumaNode(system, spec.gpus);
|
|
122
|
+
if (numaNode === undefined)
|
|
123
|
+
return spawnDetached(system.spawn, QEMU_BINARY, args, paths.qemuLog);
|
|
124
|
+
log?.(`Pinning to NUMA node ${numaNode}`);
|
|
125
|
+
const pinned = [`--cpunodebind=${numaNode}`, `--membind=${numaNode}`, QEMU_BINARY, ...args];
|
|
126
|
+
return spawnDetached(system.spawn, NUMA_TOOL, pinned, paths.qemuLog);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The NUMA node to pin a VM's cores and memory to: its first GPU's, so passthrough is
|
|
130
|
+
* NUMA-local on multi-socket hosts; none without GPUs or on a single-node host. numactl execs
|
|
131
|
+
* QEMU, so the pid is QEMU's either way.
|
|
132
|
+
*/
|
|
133
|
+
function readGpuNumaNode(system, gpus) {
|
|
134
|
+
const [gpu] = gpus;
|
|
135
|
+
return gpu === undefined ? undefined : system.readPciNumaNode(gpu);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Rejects when QEMU fails to start or exits, with the reason from its log, so
|
|
139
|
+
* a launch failure (VFIO, memlock, NUMA membind, a guest-cid collision the
|
|
140
|
+
* kernel arbitrated) surfaces instead of an opaque QMP ECONNREFUSED later.
|
|
141
|
+
*/
|
|
142
|
+
function watchForEarlyExit(qemu, qemuLog) {
|
|
143
|
+
const exited = new Promise((_, reject) => {
|
|
144
|
+
qemu.once('error', (error) => reject(new Error(`QEMU failed to start: ${error.message}`)));
|
|
145
|
+
qemu.once('exit', (code, signal) => {
|
|
146
|
+
void readLogTail(qemuLog).then((tail) => reject(new QemuExitedError(code, signal, tail)));
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
// A normal end-of-VM exit rejects this too; keep it from ever surfacing as
|
|
150
|
+
// an unhandled rejection once the boot has moved past this race.
|
|
151
|
+
exited.catch(() => undefined);
|
|
152
|
+
return exited;
|
|
153
|
+
}
|
|
154
|
+
function startBridge(system, paths, hostPort, hostSocket, log) {
|
|
155
|
+
log?.('Bridging vsock host channel');
|
|
156
|
+
const bridge = spawnBridge(system.spawn, paths.bridgeLog, hostPort, hostSocket);
|
|
157
|
+
bridge.on('error', (error) => log?.(`vsock host bridge error: ${error.message}`));
|
|
158
|
+
return bridge;
|
|
159
|
+
}
|
|
160
|
+
/** Connect QMP and the console once QEMU has made their sockets, or fail with its exit. */
|
|
161
|
+
function connectVm(system, paths, qemuEarlyExit, log) {
|
|
162
|
+
log?.('Connecting to the VM');
|
|
163
|
+
return Promise.race([connectVmSockets(system, paths), qemuEarlyExit]);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The bridge's pid, or {@link BridgeExitedError}. socat binds its VSOCK
|
|
167
|
+
* listener at spawn, so by the time QEMU is connectable a bind failure has long
|
|
168
|
+
* surfaced as an exit: caught here rather than shipping a VM whose host channel
|
|
169
|
+
* would hang up requests.
|
|
170
|
+
*/
|
|
171
|
+
async function assertBridgeRunning(bridge, connection, bridgeLog) {
|
|
172
|
+
if (bridge.pid !== undefined && !hasExited(bridge))
|
|
173
|
+
return bridge.pid;
|
|
174
|
+
connection.qmp.close();
|
|
175
|
+
connection.serial.destroy();
|
|
176
|
+
throw new BridgeExitedError(await readLogTail(bridgeLog));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Reap everything a failed attempt spawned, so it leaves no QEMU or socat
|
|
180
|
+
* bridge holding host-global vsock resources, and remove its work dir.
|
|
181
|
+
*/
|
|
182
|
+
async function reapAttempt(kill, spawned, workdir) {
|
|
183
|
+
const { qemu, bridge } = spawned;
|
|
184
|
+
if (bridge?.pid !== undefined)
|
|
185
|
+
killProcessGroup(kill, bridge.pid);
|
|
186
|
+
if (qemu?.pid !== undefined) {
|
|
187
|
+
killProcess(kill, qemu.pid);
|
|
188
|
+
// A retry (or the caller's next boot) reuses this GPU: wait until the
|
|
189
|
+
// killed QEMU has released it (see QEMU_REAP_TIMEOUT_MS).
|
|
190
|
+
await waitAtMost(childExit(qemu), QEMU_REAP_TIMEOUT_MS);
|
|
191
|
+
}
|
|
192
|
+
await fs.rm(workdir, { recursive: true, force: true }).catch(() => undefined);
|
|
193
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** QEMU exited before the boot finished connecting to it. */
|
|
2
|
+
export declare class QemuExitedError extends Error {
|
|
3
|
+
readonly outputTail: string;
|
|
4
|
+
constructor(exitCode: number | null, signal: string | null, outputTail: string);
|
|
5
|
+
}
|
|
6
|
+
/** The vsock bridge died before the boot completed (a bind failure). */
|
|
7
|
+
export declare class BridgeExitedError extends Error {
|
|
8
|
+
readonly outputTail: string;
|
|
9
|
+
constructor(outputTail: string);
|
|
10
|
+
}
|
|
11
|
+
/** The last {@link LOG_TAIL_BYTES} of a log file, or '' when unreadable. */
|
|
12
|
+
export declare function readLogTail(file: string): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Did a boot fail because our vsock CID (QEMU) or host vsock port (socat) is
|
|
15
|
+
* already taken on the host? The kernel is the final arbiter of both, so this
|
|
16
|
+
* is retriable with a fresh CID even when the lock registry believed it free
|
|
17
|
+
* (an unregistered vsock user, or a lost reclaim race).
|
|
18
|
+
*/
|
|
19
|
+
export declare function isVsockCollision(error: unknown): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Did QEMU fail to bind a forwarded host port? A port is allocated free and
|
|
22
|
+
* released before QEMU binds it, so another process can take it in between:
|
|
23
|
+
* retriable with a fresh port.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isHostPortCollision(error: unknown): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Did QEMU fail to bind the passed-through GPU to its iommufd because the
|
|
28
|
+
* device is still open? VFIO's cdev path refuses a second open. Waiting for
|
|
29
|
+
* the previous QEMU to be reaped (QEMU_REAP_TIMEOUT_MS) should make this
|
|
30
|
+
* unreachable; if it still happens, the refused bind has already stranded the
|
|
31
|
+
* holder's VM, so it is logged loudly and the boot retried.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isGpuStillHeld(error: unknown): boolean;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
/** Bytes of a process log kept for a boot failure's message. */
|
|
3
|
+
const LOG_TAIL_BYTES = 8192;
|
|
4
|
+
/** QEMU exited before the boot finished connecting to it. */
|
|
5
|
+
export class QemuExitedError extends Error {
|
|
6
|
+
constructor(exitCode, signal, outputTail) {
|
|
7
|
+
super(describeQemuExit(exitCode, signal, outputTail));
|
|
8
|
+
this.outputTail = outputTail;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** The vsock bridge died before the boot completed (a bind failure). */
|
|
12
|
+
export class BridgeExitedError extends Error {
|
|
13
|
+
constructor(outputTail) {
|
|
14
|
+
super('vsock host bridge exited during boot' + (outputTail ? `: ${outputTail.trim()}` : ''));
|
|
15
|
+
this.outputTail = outputTail;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** The last {@link LOG_TAIL_BYTES} of a log file, or '' when unreadable. */
|
|
19
|
+
export async function readLogTail(file) {
|
|
20
|
+
try {
|
|
21
|
+
return (await fs.readFile(file, 'utf8')).slice(-LOG_TAIL_BYTES);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Did a boot fail because our vsock CID (QEMU) or host vsock port (socat) is
|
|
29
|
+
* already taken on the host? The kernel is the final arbiter of both, so this
|
|
30
|
+
* is retriable with a fresh CID even when the lock registry believed it free
|
|
31
|
+
* (an unregistered vsock user, or a lost reclaim race).
|
|
32
|
+
*/
|
|
33
|
+
export function isVsockCollision(error) {
|
|
34
|
+
if (error instanceof QemuExitedError) {
|
|
35
|
+
return /unable to set guest cid/i.test(error.outputTail);
|
|
36
|
+
}
|
|
37
|
+
if (error instanceof BridgeExitedError) {
|
|
38
|
+
return /address already in use/i.test(error.outputTail);
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Did QEMU fail to bind a forwarded host port? A port is allocated free and
|
|
44
|
+
* released before QEMU binds it, so another process can take it in between:
|
|
45
|
+
* retriable with a fresh port.
|
|
46
|
+
*/
|
|
47
|
+
export function isHostPortCollision(error) {
|
|
48
|
+
return (error instanceof QemuExitedError &&
|
|
49
|
+
/could not set up host forwarding rule/i.test(error.outputTail));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Did QEMU fail to bind the passed-through GPU to its iommufd because the
|
|
53
|
+
* device is still open? VFIO's cdev path refuses a second open. Waiting for
|
|
54
|
+
* the previous QEMU to be reaped (QEMU_REAP_TIMEOUT_MS) should make this
|
|
55
|
+
* unreachable; if it still happens, the refused bind has already stranded the
|
|
56
|
+
* holder's VM, so it is logged loudly and the boot retried.
|
|
57
|
+
*/
|
|
58
|
+
export function isGpuStillHeld(error) {
|
|
59
|
+
return (error instanceof QemuExitedError &&
|
|
60
|
+
/vfio .*error bind device fd=\d+ to iommufd=\d+: Invalid argument/i.test(error.outputTail));
|
|
61
|
+
}
|
|
62
|
+
function describeQemuExit(exitCode, signal, outputTail) {
|
|
63
|
+
const how = signal ? `signal ${signal}` : `code ${exitCode}`;
|
|
64
|
+
const output = outputTail.trim();
|
|
65
|
+
const detail = output ? `\n${output}` : ' No QEMU output was captured.';
|
|
66
|
+
return `QEMU exited during boot (${how}).${detail}`;
|
|
67
|
+
}
|