@nosana/node 1.1.84-rc → 1.1.85-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.
Files changed (180) hide show
  1. package/dist/.env +2 -1
  2. package/dist/package.json +1 -1
  3. package/dist/packages/job-runtime/src/host/HostClient.d.ts +20 -4
  4. package/dist/packages/job-runtime/src/host/HostClient.js +35 -2
  5. package/dist/packages/job-runtime/src/host/contract.d.ts +27 -7
  6. package/dist/packages/job-runtime/src/host/contract.js +43 -7
  7. package/dist/packages/job-runtime/src/runtime/runJob.d.ts +8 -5
  8. package/dist/packages/job-runtime/src/runtime/runJob.js +44 -17
  9. package/dist/packages/vm-orchestrator/src/CidRegistry.d.ts +67 -0
  10. package/dist/packages/vm-orchestrator/src/CidRegistry.js +168 -0
  11. package/dist/packages/vm-orchestrator/src/GpuPool.d.ts +27 -0
  12. package/dist/packages/vm-orchestrator/src/GpuPool.js +41 -0
  13. package/dist/packages/vm-orchestrator/src/Hypervisor.d.ts +66 -0
  14. package/dist/packages/vm-orchestrator/src/Hypervisor.js +7 -0
  15. package/dist/packages/vm-orchestrator/src/QemuHypervisor.d.ts +62 -0
  16. package/dist/packages/vm-orchestrator/src/QemuHypervisor.js +185 -0
  17. package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.d.ts +61 -0
  18. package/dist/packages/vm-orchestrator/src/QemuHypervisorConfig.js +37 -0
  19. package/dist/packages/vm-orchestrator/src/QemuVmInstance.d.ts +37 -0
  20. package/dist/packages/vm-orchestrator/src/QemuVmInstance.js +44 -0
  21. package/dist/packages/vm-orchestrator/src/QmpClient.d.ts +43 -0
  22. package/dist/packages/vm-orchestrator/src/QmpClient.js +103 -0
  23. package/dist/packages/vm-orchestrator/src/VmSpec.d.ts +21 -0
  24. package/dist/packages/vm-orchestrator/src/VmSpec.js +30 -0
  25. package/dist/packages/vm-orchestrator/src/VmStateManager.d.ts +16 -0
  26. package/dist/packages/vm-orchestrator/src/VmStateManager.js +30 -0
  27. package/dist/packages/vm-orchestrator/src/bootAttempt.d.ts +31 -0
  28. package/dist/packages/vm-orchestrator/src/bootAttempt.js +193 -0
  29. package/dist/packages/vm-orchestrator/src/bootErrors.d.ts +33 -0
  30. package/dist/packages/vm-orchestrator/src/bootErrors.js +67 -0
  31. package/dist/packages/vm-orchestrator/src/buildCloudInit.d.ts +33 -0
  32. package/dist/packages/vm-orchestrator/src/buildCloudInit.js +55 -0
  33. package/dist/packages/vm-orchestrator/src/buildQemuArgs.d.ts +33 -0
  34. package/dist/packages/vm-orchestrator/src/buildQemuArgs.js +127 -0
  35. package/dist/packages/vm-orchestrator/src/diskSizing.d.ts +18 -0
  36. package/dist/packages/vm-orchestrator/src/diskSizing.js +41 -0
  37. package/dist/packages/vm-orchestrator/src/firmware.d.ts +5 -0
  38. package/dist/packages/vm-orchestrator/src/firmware.js +18 -0
  39. package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.d.ts +22 -0
  40. package/dist/packages/vm-orchestrator/src/hostAccess/VmHostAccess.js +33 -0
  41. package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.d.ts +3 -0
  42. package/dist/packages/vm-orchestrator/src/hostAccess/deviceProblems.js +17 -0
  43. package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.d.ts +9 -0
  44. package/dist/packages/vm-orchestrator/src/hostAccess/findVmHostAccessProblems.js +17 -0
  45. package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.d.ts +6 -0
  46. package/dist/packages/vm-orchestrator/src/hostAccess/gpuProblems.js +42 -0
  47. package/dist/packages/vm-orchestrator/src/hostAccess/index.d.ts +1 -0
  48. package/dist/packages/vm-orchestrator/src/hostAccess/index.js +1 -0
  49. package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.d.ts +6 -0
  50. package/dist/packages/vm-orchestrator/src/hostAccess/memlockProblems.js +18 -0
  51. package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.d.ts +7 -0
  52. package/dist/packages/vm-orchestrator/src/hostAccess/workRootProblems.js +30 -0
  53. package/dist/packages/vm-orchestrator/src/hostDevices.d.ts +12 -0
  54. package/dist/packages/vm-orchestrator/src/hostDevices.js +12 -0
  55. package/dist/packages/vm-orchestrator/src/hostTools.d.ts +25 -0
  56. package/dist/packages/vm-orchestrator/src/hostTools.js +39 -0
  57. package/dist/packages/vm-orchestrator/src/imageCache.d.ts +25 -0
  58. package/dist/packages/vm-orchestrator/src/imageCache.js +73 -0
  59. package/dist/packages/vm-orchestrator/src/imageDownload.d.ts +31 -0
  60. package/dist/packages/vm-orchestrator/src/imageDownload.js +85 -0
  61. package/dist/packages/vm-orchestrator/src/index.d.ts +19 -0
  62. package/dist/packages/vm-orchestrator/src/index.js +15 -0
  63. package/dist/packages/vm-orchestrator/src/memlockLimit.d.ts +5 -0
  64. package/dist/packages/vm-orchestrator/src/memlockLimit.js +23 -0
  65. package/dist/packages/vm-orchestrator/src/nvidiaHolders.d.ts +11 -0
  66. package/dist/packages/vm-orchestrator/src/nvidiaHolders.js +74 -0
  67. package/dist/packages/vm-orchestrator/src/pciNuma.d.ts +7 -0
  68. package/dist/packages/vm-orchestrator/src/pciNuma.js +18 -0
  69. package/dist/packages/vm-orchestrator/src/pciSysfs.d.ts +21 -0
  70. package/dist/packages/vm-orchestrator/src/pciSysfs.js +47 -0
  71. package/dist/packages/vm-orchestrator/src/processes.d.ts +37 -0
  72. package/dist/packages/vm-orchestrator/src/processes.js +102 -0
  73. package/dist/packages/vm-orchestrator/src/sockets.d.ts +21 -0
  74. package/dist/packages/vm-orchestrator/src/sockets.js +58 -0
  75. package/dist/packages/vm-orchestrator/src/sweepVmImageCache.d.ts +19 -0
  76. package/dist/packages/vm-orchestrator/src/sweepVmImageCache.js +132 -0
  77. package/dist/packages/vm-orchestrator/src/util.d.ts +14 -0
  78. package/dist/packages/vm-orchestrator/src/util.js +33 -0
  79. package/dist/packages/vm-orchestrator/src/vfioBinding.d.ts +27 -0
  80. package/dist/packages/vm-orchestrator/src/vfioBinding.js +163 -0
  81. package/dist/packages/vm-orchestrator/src/vmImageResolver.d.ts +20 -0
  82. package/dist/packages/vm-orchestrator/src/vmImageResolver.js +62 -0
  83. package/dist/packages/vm-orchestrator/src/vsockBridge.d.ts +20 -0
  84. package/dist/packages/vm-orchestrator/src/vsockBridge.js +27 -0
  85. package/dist/packages/vm-orchestrator/src/workDirs.d.ts +49 -0
  86. package/dist/packages/vm-orchestrator/src/workDirs.js +79 -0
  87. package/dist/src/NodeManager/configs/configs.d.ts +4 -0
  88. package/dist/src/NodeManager/configs/configs.js +3 -0
  89. package/dist/src/NodeManager/db/index.d.ts +10 -0
  90. package/dist/src/NodeManager/db/index.js +4 -0
  91. package/dist/src/NodeManager/monitoring/log/NodeLog.d.ts +2 -0
  92. package/dist/src/NodeManager/monitoring/log/NodeLog.js +21 -0
  93. package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.d.ts +8 -0
  94. package/dist/src/NodeManager/monitoring/log/console/ConsoleLogger.js +29 -5
  95. package/dist/src/NodeManager/monitoring/log/vmSetupLog.d.ts +11 -0
  96. package/dist/src/NodeManager/monitoring/log/vmSetupLog.js +33 -0
  97. package/dist/src/NodeManager/node/Node.d.ts +2 -0
  98. package/dist/src/NodeManager/node/Node.js +12 -5
  99. package/dist/src/NodeManager/node/job/ContainerJobRuntime.d.ts +141 -0
  100. package/dist/src/NodeManager/node/job/ContainerJobRuntime.js +419 -0
  101. package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.d.ts +74 -0
  102. package/dist/src/NodeManager/node/job/JobRuntimeEnvironment.js +1 -0
  103. package/dist/src/NodeManager/node/job/JobRuntimeHost.d.ts +6 -2
  104. package/dist/src/NodeManager/node/job/JobRuntimeHost.js +13 -2
  105. package/dist/src/NodeManager/node/job/JobRuntimeSession.d.ts +12 -9
  106. package/dist/src/NodeManager/node/job/JobRuntimeSession.js +42 -71
  107. package/dist/src/NodeManager/node/job/VmJobRuntime.d.ts +109 -0
  108. package/dist/src/NodeManager/node/job/VmJobRuntime.js +258 -0
  109. package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.d.ts +33 -0
  110. package/dist/src/NodeManager/node/job/VmJobRuntimeConfig.js +1 -0
  111. package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.d.ts +41 -0
  112. package/dist/src/NodeManager/node/job/buildJobRuntimeEnv.js +25 -0
  113. package/dist/src/NodeManager/node/job/followFile.d.ts +14 -0
  114. package/dist/src/NodeManager/node/job/followFile.js +57 -0
  115. package/dist/src/NodeManager/node/job/jobHandler.d.ts +3 -3
  116. package/dist/src/NodeManager/node/job/jobHandler.js +7 -7
  117. package/dist/src/NodeManager/node/job/keepFailedConsole.d.ts +7 -0
  118. package/dist/src/NodeManager/node/job/keepFailedConsole.js +52 -0
  119. package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.d.ts +50 -0
  120. package/dist/src/NodeManager/node/job/selectJobRuntimeEnvironment.js +117 -0
  121. package/dist/src/NodeManager/node/market/benchmark.d.ts +3 -3
  122. package/dist/src/NodeManager/node/market/benchmark.js +3 -3
  123. package/dist/src/NodeManager/node/market/marketHandler.d.ts +3 -3
  124. package/dist/src/NodeManager/node/market/marketHandler.js +3 -3
  125. package/dist/src/NodeManager/node/resource/resourceManager.d.ts +1 -1
  126. package/dist/src/NodeManager/node/resource/resourceManager.js +1 -1
  127. package/dist/src/NodeManager/provider/Provider.d.ts +3 -121
  128. package/dist/src/NodeManager/provider/Provider.js +3 -369
  129. package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.d.ts +14 -0
  130. package/dist/src/NodeManager/provider/vmHost/VmNodeOptions.js +1 -0
  131. package/dist/src/NodeManager/provider/vmHost/capacity.d.ts +16 -0
  132. package/dist/src/NodeManager/provider/vmHost/capacity.js +47 -0
  133. package/dist/src/NodeManager/provider/vmHost/gpus.d.ts +19 -0
  134. package/dist/src/NodeManager/provider/vmHost/gpus.js +83 -0
  135. package/dist/src/NodeManager/provider/vmHost/index.d.ts +5 -0
  136. package/dist/src/NodeManager/provider/vmHost/index.js +4 -0
  137. package/dist/src/NodeManager/provider/vmHost/readVmSpec.d.ts +11 -0
  138. package/dist/src/NodeManager/provider/vmHost/readVmSpec.js +22 -0
  139. package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.d.ts +27 -0
  140. package/dist/src/NodeManager/provider/vmHost/setup/PrepareVmHostSystem.js +25 -0
  141. package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.d.ts +6 -0
  142. package/dist/src/NodeManager/provider/vmHost/setup/fileOwnership.js +22 -0
  143. package/dist/src/NodeManager/provider/vmHost/setup/hostUser.d.ts +15 -0
  144. package/dist/src/NodeManager/provider/vmHost/setup/hostUser.js +25 -0
  145. package/dist/src/NodeManager/provider/vmHost/setup/index.d.ts +2 -0
  146. package/dist/src/NodeManager/provider/vmHost/setup/index.js +2 -0
  147. package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.d.ts +6 -0
  148. package/dist/src/NodeManager/provider/vmHost/setup/memlockLimits.js +12 -0
  149. package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.d.ts +17 -0
  150. package/dist/src/NodeManager/provider/vmHost/setup/prepareVmHost.js +90 -0
  151. package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.d.ts +5 -0
  152. package/dist/src/NodeManager/provider/vmHost/setup/sudoCaller.js +35 -0
  153. package/dist/src/NodeManager/provider/vmHost/vmHostEnv.d.ts +20 -0
  154. package/dist/src/NodeManager/provider/vmHost/vmHostEnv.js +24 -0
  155. package/dist/src/NodeManager/provider/vmHost/workRoot.d.ts +14 -0
  156. package/dist/src/NodeManager/provider/vmHost/workRoot.js +84 -0
  157. package/dist/src/NodeManager/repository/NodeRepository.d.ts +12 -2
  158. package/dist/src/NodeManager/repository/NodeRepository.js +21 -1
  159. package/dist/src/cli/commands/prune/command.js +2 -1
  160. package/dist/src/cli/commands/run/action.d.ts +2 -2
  161. package/dist/src/cli/commands/run/action.js +15 -34
  162. package/dist/src/cli/commands/run/command.js +7 -2
  163. package/dist/src/cli/commands/sharedOptions/--config.d.ts +2 -0
  164. package/dist/src/cli/commands/sharedOptions/--config.js +2 -0
  165. package/dist/src/cli/commands/sharedOptions/--environment.d.ts +6 -0
  166. package/dist/src/cli/commands/sharedOptions/--environment.js +6 -0
  167. package/dist/src/cli/commands/sharedOptions/--gpu.js +5 -2
  168. package/dist/src/cli/commands/sharedOptions/--node-limits.d.ts +9 -0
  169. package/dist/src/cli/commands/sharedOptions/--node-limits.js +24 -0
  170. package/dist/src/cli/commands/sharedOptions/index.d.ts +3 -0
  171. package/dist/src/cli/commands/sharedOptions/index.js +3 -0
  172. package/dist/src/cli/commands/start/command.js +7 -2
  173. package/dist/src/cli/commands/vm-setup/action.d.ts +10 -0
  174. package/dist/src/cli/commands/vm-setup/action.js +14 -0
  175. package/dist/src/cli/commands/vm-setup/command.d.ts +3 -0
  176. package/dist/src/cli/commands/vm-setup/command.js +11 -0
  177. package/dist/src/cli/createNosanaCli.d.ts +1 -0
  178. package/dist/src/cli/createNosanaCli.js +4 -1
  179. package/npm-shrinkwrap.json +10 -2
  180. package/package.json +1 -1
@@ -0,0 +1,83 @@
1
+ import { readdirSync, readFileSync } from 'node:fs';
2
+ import { SYSFS_PCI_ROOT } from '../../../../packages/vm-orchestrator/src/index.js';
3
+ /** A PCI domain:bus:device.function address in sysfs form, e.g. `0000:01:00.0`. */
4
+ const BDF_RE = /^[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-9a-f]$/i;
5
+ /** A PCI bus id with a domain of up to 8 digits, as `nvidia-smi` writes it. */
6
+ const PCI_BUS_ID_RE = /^([0-9a-f]{1,8}):([0-9a-f]{2}):([0-9a-f]{2})\.([0-9a-f])$/i;
7
+ /** A `--gpu` entry that is an NVIDIA device index. */
8
+ const GPU_INDEX_RE = /^\d+$/;
9
+ /** PCI vendor id of NVIDIA, whose GPUs both the container and the VM paths target. */
10
+ const NVIDIA_VENDOR_ID = '0x10de';
11
+ /** PCI class prefix of display and 3D controllers: a GPU, not its HDMI audio function. */
12
+ const DISPLAY_CLASS_PREFIX = '0x03';
13
+ /**
14
+ * The BDFs of the GPUs a `--gpu` selection passes through, whatever driver they are on now:
15
+ * each VM's boot moves its GPUs to vfio-pci. `'all'` (or none) is every NVIDIA GPU on the host.
16
+ * Otherwise each comma-separated entry is an NVIDIA index or a PCI address. An index counts the
17
+ * host's NVIDIA GPUs in PCI-bus order, the order `nvidia-smi` numbers them in while they are
18
+ * on its driver; it is read from sysfs, which lists a GPU on any driver, so an index names the
19
+ * same GPU after its move to vfio-pci. An entry that names no NVIDIA GPU throws, naming it.
20
+ */
21
+ export function resolveGpuSelection(gpuSelection, options = {}) {
22
+ const { root = SYSFS_PCI_ROOT } = options;
23
+ const hostGpus = scanNvidiaGpus(root);
24
+ if (gpuSelection === undefined || gpuSelection === 'all')
25
+ return hostGpus;
26
+ return splitGpuSelection(gpuSelection).map((entry) => resolveGpuEntry(entry, hostGpus));
27
+ }
28
+ /**
29
+ * Whether a `--gpu` selection names a GPU by PCI address: only a `--vm` node can, as the
30
+ * container runtime selects GPUs by index.
31
+ */
32
+ export function hasPciAddress(gpuSelection) {
33
+ if (gpuSelection === undefined)
34
+ return false;
35
+ return splitGpuSelection(gpuSelection).some((entry) => PCI_BUS_ID_RE.test(entry));
36
+ }
37
+ /** The NVIDIA GPUs a sysfs scan finds, in PCI-bus (BDF) order. */
38
+ function scanNvidiaGpus(root) {
39
+ let devices;
40
+ try {
41
+ devices = readdirSync(`${root}/devices`);
42
+ }
43
+ catch {
44
+ return [];
45
+ }
46
+ return devices.filter((bdf) => BDF_RE.test(bdf) && isNvidiaGpu(root, bdf)).sort();
47
+ }
48
+ function isNvidiaGpu(root, bdf) {
49
+ try {
50
+ if (readPciAttribute(root, bdf, 'vendor').toLowerCase() !== NVIDIA_VENDOR_ID)
51
+ return false;
52
+ return readPciAttribute(root, bdf, 'class').startsWith(DISPLAY_CLASS_PREFIX);
53
+ }
54
+ catch {
55
+ return false;
56
+ }
57
+ }
58
+ /** The trimmed entries of a comma-separated `--gpu` selection. */
59
+ function splitGpuSelection(gpuSelection) {
60
+ return gpuSelection.split(',').map((entry) => entry.trim());
61
+ }
62
+ /** The BDF of the host GPU one `--gpu` entry names; else it throws. */
63
+ function resolveGpuEntry(entry, hostGpus) {
64
+ const bdf = GPU_INDEX_RE.test(entry) ? hostGpus[Number(entry)] : normalizePciBdf(entry);
65
+ if (bdf === undefined || !hostGpus.includes(bdf)) {
66
+ throw new Error(`--gpu ${entry} names no NVIDIA GPU on this host`);
67
+ }
68
+ return bdf;
69
+ }
70
+ /**
71
+ * A PCI bus id in sysfs form: `nvidia-smi` writes an 8-digit domain (`00000000:07:00.0`), sysfs
72
+ * 4 (`0000:07:00.0`). Undefined when it is not a bus id.
73
+ */
74
+ function normalizePciBdf(busId) {
75
+ const match = PCI_BUS_ID_RE.exec(busId.trim());
76
+ if (!match)
77
+ return undefined;
78
+ const [, domain, bus, device, fn] = match;
79
+ return `${domain.padStart(4, '0').slice(-4)}:${bus}:${device}.${fn}`.toLowerCase();
80
+ }
81
+ function readPciAttribute(root, bdf, attribute) {
82
+ return readFileSync(`${root}/devices/${bdf}/${attribute}`, 'utf8').trim();
83
+ }
@@ -0,0 +1,5 @@
1
+ /** How a `--vm` node runs its VMs on this host: read once at start. */
2
+ export { readVmSpec } from './readVmSpec.js';
3
+ export type { VmNodeOptions } from './VmNodeOptions.js';
4
+ export { hasPciAddress, resolveGpuSelection } from './gpus.js';
5
+ export { makeVmWorkRoot } from './workRoot.js';
@@ -0,0 +1,4 @@
1
+ /** How a `--vm` node runs its VMs on this host: read once at start. */
2
+ export { readVmSpec } from './readVmSpec.js';
3
+ export { hasPciAddress, resolveGpuSelection } from './gpus.js';
4
+ export { makeVmWorkRoot } from './workRoot.js';
@@ -0,0 +1,11 @@
1
+ import type { VmSpec } from '../../../../packages/vm-orchestrator/src/index.js';
2
+ import { type GpuSelectionOptions } from './gpus.js';
3
+ import type { VmNodeOptions } from './VmNodeOptions.js';
4
+ /**
5
+ * The VM every job of a `--vm` node boots, read once at start from its CLI limits
6
+ * (`options`), their `VM_*` fallbacks and the host: the vCPUs and memory (see
7
+ * detectVmCapacity), the `--gpu` selection resolved to BDFs (see resolveGpuSelection), and the
8
+ * `--disk` size. Throws when `--gpu` names no GPU on the host. `gpuSources` is where the GPUs
9
+ * are looked up; the host's by default.
10
+ */
11
+ export declare function readVmSpec(options?: VmNodeOptions, gpuSources?: GpuSelectionOptions): VmSpec;
@@ -0,0 +1,22 @@
1
+ import { detectVmCapacity } from './capacity.js';
2
+ import { resolveGpuSelection } from './gpus.js';
3
+ import { readVmHostEnv } from './vmHostEnv.js';
4
+ const MIB_PER_GIB = 1024;
5
+ /**
6
+ * The VM every job of a `--vm` node boots, read once at start from its CLI limits
7
+ * (`options`), their `VM_*` fallbacks and the host: the vCPUs and memory (see
8
+ * detectVmCapacity), the `--gpu` selection resolved to BDFs (see resolveGpuSelection), and the
9
+ * `--disk` size. Throws when `--gpu` names no GPU on the host. `gpuSources` is where the GPUs
10
+ * are looked up; the host's by default.
11
+ */
12
+ export function readVmSpec(options = {}, gpuSources = {}) {
13
+ return {
14
+ ...detectVmCapacity(options, readVmHostEnv()),
15
+ gpus: resolveGpuSelection(options.gpu, gpuSources),
16
+ diskMiB: readDiskMiB(options),
17
+ };
18
+ }
19
+ /** `--disk` in MiB; undefined without it, and the hypervisor sizes the disk from free space. */
20
+ function readDiskMiB({ disk }) {
21
+ return disk && disk > 0 ? Math.floor(disk * MIB_PER_GIB) : undefined;
22
+ }
@@ -0,0 +1,27 @@
1
+ import type { Chown } from './fileOwnership.js';
2
+ import { type HostUser } from './hostUser.js';
3
+ /** What preparing the host touches; tests inject a fake one. */
4
+ export interface PrepareVmHostSystem {
5
+ isRoot: boolean;
6
+ /** The user of that name; throws when there is none. */
7
+ lookupUser: (name: string) => HostUser;
8
+ /** The id of the group of that name; throws when there is none. */
9
+ lookupGroupId: (name: string) => number;
10
+ /** Runs a host command to completion; throws when it fails. */
11
+ run: (command: string, args: string[]) => void;
12
+ /** Moves the GPUs to vfio-pci and loads the modules QEMU's passthrough needs. */
13
+ bindGpus: (gpus: string[]) => Promise<void>;
14
+ chown: Chown;
15
+ /** The pid of the shell that ran sudo; undefined when sudo did not run this. */
16
+ findSudoCaller: () => number | undefined;
17
+ /** The sysfs PCI root (`/sys/bus/pci`). */
18
+ pciRoot: string;
19
+ vsockDevice: string;
20
+ iommufdDevice: string;
21
+ /** Where the kernel puts each vfio-pci device's node (`/dev/vfio/devices`). */
22
+ vfioDevicesDir: string;
23
+ /** The pam_limits file the user's memlock limit is written to. */
24
+ limitsFile: string;
25
+ }
26
+ /** The real host. */
27
+ export declare function defaultPrepareVmHostSystem(): PrepareVmHostSystem;
@@ -0,0 +1,25 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import { bindGpusToVfio, IOMMUFD_DEVICE, SYSFS_PCI_ROOT, VFIO_DEVICES_DIR, VSOCK_DEVICE, } from '../../../../../packages/vm-orchestrator/src/index.js';
4
+ import { lookupGroupId, lookupUser } from './hostUser.js';
5
+ import { MEMLOCK_LIMITS_FILE } from './memlockLimits.js';
6
+ import { findSudoCaller } from './sudoCaller.js';
7
+ /** The real host. */
8
+ export function defaultPrepareVmHostSystem() {
9
+ return {
10
+ isRoot: process.geteuid?.() === 0,
11
+ lookupUser,
12
+ lookupGroupId,
13
+ run: (command, args) => {
14
+ execFileSync(command, args, { stdio: ['ignore', 'ignore', 'pipe'] });
15
+ },
16
+ bindGpus: bindGpusToVfio,
17
+ chown: fs.lchownSync,
18
+ findSudoCaller: () => findSudoCaller(process.pid),
19
+ pciRoot: SYSFS_PCI_ROOT,
20
+ vsockDevice: VSOCK_DEVICE,
21
+ iommufdDevice: IOMMUFD_DEVICE,
22
+ vfioDevicesDir: VFIO_DEVICES_DIR,
23
+ limitsFile: MEMLOCK_LIMITS_FILE,
24
+ };
25
+ }
@@ -0,0 +1,6 @@
1
+ /** Changes a file's owner without following a symlink (lchown). */
2
+ export type Chown = (file: string, uid: number, gid: number) => void;
3
+ /** Gives a device to the group, read-write, keeping its owner. Throws when it is missing. */
4
+ export declare function grantToGroup(device: string, gid: number, chown: Chown): void;
5
+ /** Gives the owner everything under `dir`, `dir` too, never following a symlink. */
6
+ export declare function chownTree(dir: string, uid: number, gid: number, chown: Chown): void;
@@ -0,0 +1,22 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ /** Read-write for the owner and the group. */
4
+ const GROUP_READ_WRITE = 0o660;
5
+ /** Gives a device to the group, read-write, keeping its owner. Throws when it is missing. */
6
+ export function grantToGroup(device, gid, chown) {
7
+ if (!fs.existsSync(device))
8
+ throw new Error(`${device} is missing`);
9
+ chown(device, fs.statSync(device).uid, gid);
10
+ fs.chmodSync(device, GROUP_READ_WRITE);
11
+ }
12
+ /** Gives the owner everything under `dir`, `dir` too, never following a symlink. */
13
+ export function chownTree(dir, uid, gid, chown) {
14
+ chown(dir, uid, gid);
15
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
16
+ const file = path.join(dir, entry.name);
17
+ if (entry.isDirectory())
18
+ chownTree(file, uid, gid, chown);
19
+ else
20
+ chown(file, uid, gid);
21
+ }
22
+ }
@@ -0,0 +1,15 @@
1
+ /** A host user, as the passwd and group databases know them. */
2
+ export interface HostUser {
3
+ name: string;
4
+ uid: number;
5
+ gid: number;
6
+ home: string;
7
+ /** The names of the groups the user is in. */
8
+ groups: string[];
9
+ }
10
+ /** The user of that name; throws when there is none. */
11
+ export declare function lookupUser(name: string): HostUser;
12
+ /** The id of the group of that name; throws when there is none. */
13
+ export declare function lookupGroupId(name: string): number;
14
+ /** A path with a leading `~` read as the user's home, as their shell would. */
15
+ export declare function expandHome(file: string, user: HostUser): string;
@@ -0,0 +1,25 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ /** The user of that name; throws when there is none. */
3
+ export function lookupUser(name) {
4
+ const [, , uid, gid, , home] = getent('passwd', name, `no user ${name} on this host`).split(':');
5
+ const groups = execFileSync('id', ['-Gn', name], { encoding: 'utf8' }).trim().split(/\s+/);
6
+ return { name, uid: Number(uid), gid: Number(gid), home, groups };
7
+ }
8
+ /** The id of the group of that name; throws when there is none. */
9
+ export function lookupGroupId(name) {
10
+ const [, , gid] = getent('group', name, `no ${name} group on this host: install QEMU/KVM`).split(':');
11
+ return Number(gid);
12
+ }
13
+ /** A path with a leading `~` read as the user's home, as their shell would. */
14
+ export function expandHome(file, user) {
15
+ return file.replace(/^~(?=$|\/)/, user.home);
16
+ }
17
+ /** One entry of a host database; throws `missing` when there is none. */
18
+ function getent(database, key, missing) {
19
+ try {
20
+ return execFileSync('getent', [database, key], { encoding: 'utf8' }).trim();
21
+ }
22
+ catch {
23
+ throw new Error(missing);
24
+ }
25
+ }
@@ -0,0 +1,2 @@
1
+ /** `nosana-node vm-setup`: prepares the host, as root, for a `--vm` node run without root. */
2
+ export { prepareVmHost } from './prepareVmHost.js';
@@ -0,0 +1,2 @@
1
+ /** `nosana-node vm-setup`: prepares the host, as root, for a `--vm` node run without root. */
2
+ export { prepareVmHost } from './prepareVmHost.js';
@@ -0,0 +1,6 @@
1
+ /** Where pam_limits reads per-user limits at login. */
2
+ export declare const MEMLOCK_LIMITS_FILE = "/etc/security/limits.d/99-nosana-vm.conf";
3
+ /** The pam_limits lines that let the user lock any amount of memory, as VFIO pins a VM's RAM. */
4
+ export declare function unlimitedMemlockLimits(user: string): string;
5
+ /** The prlimit arguments that let a running process lock any amount of memory. */
6
+ export declare function unlimitedMemlockPrlimitArgs(pid: number): string[];
@@ -0,0 +1,12 @@
1
+ /** Where pam_limits reads per-user limits at login. */
2
+ export const MEMLOCK_LIMITS_FILE = '/etc/security/limits.d/99-nosana-vm.conf';
3
+ /** The pam_limits lines that let the user lock any amount of memory, as VFIO pins a VM's RAM. */
4
+ export function unlimitedMemlockLimits(user) {
5
+ return ("# Written by nosana-node vm-setup: VFIO pins a VM's RAM.\n" +
6
+ `${user} soft memlock unlimited\n` +
7
+ `${user} hard memlock unlimited\n`);
8
+ }
9
+ /** The prlimit arguments that let a running process lock any amount of memory. */
10
+ export function unlimitedMemlockPrlimitArgs(pid) {
11
+ return ['--pid', String(pid), '--memlock=unlimited:unlimited'];
12
+ }
@@ -0,0 +1,17 @@
1
+ import { type PrepareVmHostSystem } from './PrepareVmHostSystem.js';
2
+ export interface PrepareVmHostOptions {
3
+ /** The user the node will run as. */
4
+ user: string;
5
+ /** The BDFs of the GPUs its VMs pass through. */
6
+ gpus: string[];
7
+ /** The node's data directory (`--config`); `~` is the user's home. */
8
+ configDir: string;
9
+ }
10
+ /**
11
+ * Prepares the host, as root, for a `--vm` node run by `user`: puts the user in the kvm
12
+ * group, moves the GPUs to vfio-pci, gives the kvm group QEMU's devices (vhost-vsock, iommufd,
13
+ * the GPUs' VFIO nodes), lifts the user's locked-memory limit (VFIO pins the guest's RAM) and
14
+ * hands the user the VM work root an earlier root run left. The binding and the devices'
15
+ * modes last until reboot. Answers what it did, one line each.
16
+ */
17
+ export declare function prepareVmHost({ user, gpus, configDir }: PrepareVmHostOptions, system?: PrepareVmHostSystem): Promise<string[]>;
@@ -0,0 +1,90 @@
1
+ import fs from 'node:fs';
2
+ import { missingVfioNodeMessage, readVfioDeviceNodes } from '../../../../../packages/vm-orchestrator/src/index.js';
3
+ import { vmWorkRootPath } from '../workRoot.js';
4
+ import { chownTree, grantToGroup } from './fileOwnership.js';
5
+ import { expandHome } from './hostUser.js';
6
+ import { unlimitedMemlockLimits, unlimitedMemlockPrlimitArgs } from './memlockLimits.js';
7
+ import { defaultPrepareVmHostSystem } from './PrepareVmHostSystem.js';
8
+ /** The group given QEMU's devices; a user in it may boot VMs. */
9
+ const KVM_GROUP = 'kvm';
10
+ /**
11
+ * Prepares the host, as root, for a `--vm` node run by `user`: puts the user in the kvm
12
+ * group, moves the GPUs to vfio-pci, gives the kvm group QEMU's devices (vhost-vsock, iommufd,
13
+ * the GPUs' VFIO nodes), lifts the user's locked-memory limit (VFIO pins the guest's RAM) and
14
+ * hands the user the VM work root an earlier root run left. The binding and the devices'
15
+ * modes last until reboot. Answers what it did, one line each.
16
+ */
17
+ export async function prepareVmHost({ user, gpus, configDir }, system = defaultPrepareVmHostSystem()) {
18
+ if (!system.isRoot)
19
+ throw new Error('vm-setup needs root: run it with sudo');
20
+ const account = system.lookupUser(user);
21
+ if (account.uid === 0) {
22
+ throw new Error('vm-setup prepares the host for a node run as a normal user, not root: name the user ' +
23
+ 'with --user');
24
+ }
25
+ // Looked up before anything changes: a host without it has no QEMU/KVM to prepare.
26
+ const kvmGid = system.lookupGroupId(KVM_GROUP);
27
+ const done = joinKvmGroup(account, system);
28
+ // Every VM needs vhost-vsock; GPU VMs also need the GPUs, iommufd and their RAM locked.
29
+ system.run('modprobe', ['vhost_vsock']);
30
+ const devices = [system.vsockDevice];
31
+ if (gpus.length > 0) {
32
+ done.push(...(await bindGpus(gpus, system)), ...liftMemlockLimit(account, system));
33
+ devices.push(system.iommufdDevice, ...vfioNodes(gpus, system));
34
+ }
35
+ done.push(...grantDevices(devices, kvmGid, system));
36
+ done.push(...handOverWorkRoot(account, configDir, system));
37
+ return done;
38
+ }
39
+ function joinKvmGroup(user, system) {
40
+ if (user.groups.includes(KVM_GROUP))
41
+ return [];
42
+ system.run('usermod', ['-aG', KVM_GROUP, user.name]);
43
+ return [`Added ${user.name} to the ${KVM_GROUP} group (it applies at their next login)`];
44
+ }
45
+ /** Moves the GPUs to vfio-pci, which loads the modules QEMU's passthrough needs. */
46
+ async function bindGpus(gpus, system) {
47
+ await system.bindGpus(gpus);
48
+ // The VFIO nodes appear once udev has handled the binds.
49
+ system.run('udevadm', ['settle']);
50
+ return [`Bound GPU ${gpus.join(', ')} to vfio-pci (until reboot)`];
51
+ }
52
+ /** Gives the kvm group the devices QEMU opens. */
53
+ function grantDevices(devices, kvmGid, system) {
54
+ for (const device of devices)
55
+ grantToGroup(device, kvmGid, system.chown);
56
+ return [`Gave the ${KVM_GROUP} group ${devices.join(', ')} (until reboot)`];
57
+ }
58
+ function vfioNodes(gpus, system) {
59
+ return gpus.flatMap((gpu) => {
60
+ const nodes = readVfioDeviceNodes(system.pciRoot, system.vfioDevicesDir, gpu);
61
+ if (nodes.length === 0)
62
+ throw new Error(missingVfioNodeMessage(gpu));
63
+ return nodes;
64
+ });
65
+ }
66
+ /**
67
+ * Lifts the user's locked-memory limit for their next login (pam_limits), and at once in the
68
+ * shell that ran sudo, so it can start the node now.
69
+ */
70
+ function liftMemlockLimit(user, system) {
71
+ fs.writeFileSync(system.limitsFile, unlimitedMemlockLimits(user.name));
72
+ const done = [
73
+ `Lifted ${user.name}'s locked-memory limit in ${system.limitsFile} (it applies at their ` +
74
+ 'next login; a systemd unit needs LimitMEMLOCK=infinity)',
75
+ ];
76
+ const caller = system.findSudoCaller();
77
+ if (caller !== undefined) {
78
+ system.run('prlimit', unlimitedMemlockPrlimitArgs(caller));
79
+ done.push(`Lifted the locked-memory limit of the shell that ran sudo (pid ${caller})`);
80
+ }
81
+ return done;
82
+ }
83
+ /** Gives the user the VM work root, which an earlier run as root left root's. */
84
+ function handOverWorkRoot(user, configDir, system) {
85
+ const workRoot = vmWorkRootPath(expandHome(configDir, user));
86
+ if (!fs.existsSync(workRoot))
87
+ return [];
88
+ chownTree(workRoot, user.uid, user.gid, system.chown);
89
+ return [`Gave ${user.name} the VM work root ${workRoot}`];
90
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The process that ran sudo: the parent of the outermost `sudo` among `pid`'s ancestors (sudo
3
+ * may fork itself for a terminal). Undefined when no ancestor is sudo.
4
+ */
5
+ export declare function findSudoCaller(pid: number, procRoot?: string): number | undefined;
@@ -0,0 +1,35 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ /**
4
+ * The process that ran sudo: the parent of the outermost `sudo` among `pid`'s ancestors (sudo
5
+ * may fork itself for a terminal). Undefined when no ancestor is sudo.
6
+ */
7
+ export function findSudoCaller(pid, procRoot = '/proc') {
8
+ let sudo;
9
+ for (let current = pid; current > 1;) {
10
+ const stat = readProcStat(procRoot, current);
11
+ if (!stat)
12
+ return undefined;
13
+ if (stat.comm === 'sudo')
14
+ sudo = current;
15
+ else if (sudo !== undefined)
16
+ return current;
17
+ current = stat.ppid;
18
+ }
19
+ return undefined;
20
+ }
21
+ /** A process's stat; undefined when it cannot be read. */
22
+ function readProcStat(procRoot, pid) {
23
+ let stat;
24
+ try {
25
+ stat = fs.readFileSync(path.join(procRoot, String(pid), 'stat'), 'utf8');
26
+ }
27
+ catch {
28
+ return undefined;
29
+ }
30
+ // `pid (comm) state ppid ...`: comm may hold spaces and parentheses, so split at the last ')'.
31
+ const close = stat.lastIndexOf(')');
32
+ const comm = stat.slice(stat.indexOf('(') + 1, close);
33
+ const [, ppid] = stat.slice(close + 2).split(' ');
34
+ return Number.isInteger(Number(ppid)) ? { comm, ppid: Number(ppid) } : undefined;
35
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The `VM_*` environment variables a `--vm` node reads: the fallbacks for `--cores` and
3
+ * `--ram`, and what the host keeps when they are detected. Sizes (`32G`) stay strings, parsed only
4
+ * where they are used, so one a CLI flag overrides is never parsed.
5
+ */
6
+ export interface VmHostEnv {
7
+ /** `VM_NODE_CPUS`: the node's vCPU budget. */
8
+ nodeCpus?: number;
9
+ /** `VM_NODE_MEMORY`: the node's memory budget, a size. */
10
+ nodeMemory?: string;
11
+ /** `VM_HOST_RESERVE_CPUS`: logical CPUs kept for the host when the budget is detected. */
12
+ hostReserveCpus?: number;
13
+ /** `VM_HOST_RESERVE_MEMORY`: memory kept for the host when the budget is detected, a size. */
14
+ hostReserveMemory?: string;
15
+ }
16
+ /**
17
+ * Reads the `VM_*` variables from `env`: the one place a `--vm` node reads them. A count
18
+ * that is not a valid integer reads as unset.
19
+ */
20
+ export declare function readVmHostEnv(env?: NodeJS.ProcessEnv): VmHostEnv;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Reads the `VM_*` variables from `env`: the one place a `--vm` node reads them. A count
3
+ * that is not a valid integer reads as unset.
4
+ */
5
+ export function readVmHostEnv(env = process.env) {
6
+ return {
7
+ nodeCpus: parsePositiveInt(env.VM_NODE_CPUS),
8
+ nodeMemory: env.VM_NODE_MEMORY,
9
+ hostReserveCpus: parseNonNegativeInt(env.VM_HOST_RESERVE_CPUS),
10
+ hostReserveMemory: env.VM_HOST_RESERVE_MEMORY,
11
+ };
12
+ }
13
+ /** An integer of at least 1, or undefined when absent or invalid. */
14
+ function parsePositiveInt(value) {
15
+ const n = parseNonNegativeInt(value);
16
+ return n !== undefined && n >= 1 ? n : undefined;
17
+ }
18
+ /** An integer of at least 0, or undefined when absent or invalid. */
19
+ function parseNonNegativeInt(value) {
20
+ if (value === undefined)
21
+ return undefined;
22
+ const n = Number(value);
23
+ return Number.isInteger(n) && n >= 0 ? n : undefined;
24
+ }
@@ -0,0 +1,14 @@
1
+ /** Where the VMs' work root is under the node's data directory (`--config`): `<dataDir>/vms`. */
2
+ export declare function vmWorkRootPath(dataDir: string): string;
3
+ /**
4
+ * The VMs' work root (see vmWorkRootPath), made when missing; refused on a tmpfs (see
5
+ * assertNotTmpfs).
6
+ */
7
+ export declare function makeVmWorkRoot(dataDir: string, mountsPath?: string, platform?: NodeJS.Platform): string;
8
+ /**
9
+ * Refuses a VM work root on a tmpfs: overlays grow with the guests' writes, so on a tmpfs they
10
+ * would eat the host's RAM. Judges the mount holding `dir` (the longest mount point that
11
+ * contains it) from the mount table. Linux only; elsewhere, or with no readable mount table,
12
+ * it checks nothing.
13
+ */
14
+ export declare function assertNotTmpfs(dir: string, mountsPath?: string, platform?: NodeJS.Platform): void;
@@ -0,0 +1,84 @@
1
+ import { mkdirSync, readFileSync, realpathSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ /** Where Linux lists its mounts. */
4
+ const MOUNTS_PATH = '/proc/mounts';
5
+ /** The VMs' work root under the node's data directory: work dirs and the image cache. */
6
+ const WORK_ROOT_DIR_NAME = 'vms';
7
+ /** Where the VMs' work root is under the node's data directory (`--config`): `<dataDir>/vms`. */
8
+ export function vmWorkRootPath(dataDir) {
9
+ return path.join(dataDir, WORK_ROOT_DIR_NAME);
10
+ }
11
+ /**
12
+ * The VMs' work root (see vmWorkRootPath), made when missing; refused on a tmpfs (see
13
+ * assertNotTmpfs).
14
+ */
15
+ export function makeVmWorkRoot(dataDir, mountsPath = MOUNTS_PATH, platform = process.platform) {
16
+ const workRoot = vmWorkRootPath(dataDir);
17
+ assertNotTmpfs(workRoot, mountsPath, platform);
18
+ mkdirSync(workRoot, { recursive: true });
19
+ return workRoot;
20
+ }
21
+ /**
22
+ * Refuses a VM work root on a tmpfs: overlays grow with the guests' writes, so on a tmpfs they
23
+ * would eat the host's RAM. Judges the mount holding `dir` (the longest mount point that
24
+ * contains it) from the mount table. Linux only; elsewhere, or with no readable mount table,
25
+ * it checks nothing.
26
+ */
27
+ export function assertNotTmpfs(dir, mountsPath = MOUNTS_PATH, platform = process.platform) {
28
+ if (platform !== 'linux')
29
+ return;
30
+ const mounts = readMountTable(mountsPath);
31
+ if (!mounts)
32
+ return;
33
+ const mount = findMountHolding(mounts, resolveRealPath(dir));
34
+ if (mount?.fsType !== 'tmpfs')
35
+ return;
36
+ throw new Error(`VM work root ${dir} is on a tmpfs (${mount.mountPoint}); ` +
37
+ "put the node's data directory (--config) on disk");
38
+ }
39
+ /** The mounts in the table at `mountsPath`, in its order; undefined when it cannot be read. */
40
+ function readMountTable(mountsPath) {
41
+ let table;
42
+ try {
43
+ table = readFileSync(mountsPath, 'utf8');
44
+ }
45
+ catch {
46
+ return undefined;
47
+ }
48
+ return table.split('\n').flatMap((line) => {
49
+ const [, mountPoint, fsType] = line.split(' ');
50
+ if (!mountPoint || !fsType)
51
+ return [];
52
+ return [{ mountPoint: unescapeMountPoint(mountPoint), fsType }];
53
+ });
54
+ }
55
+ /** The mount table octal-escapes spaces and the like (`\040`). */
56
+ function unescapeMountPoint(raw) {
57
+ return raw.replace(/\\([0-7]{3})/g, (_, octal) => String.fromCharCode(parseInt(octal, 8)));
58
+ }
59
+ /** `dir` resolved, through symlinks once it exists. */
60
+ function resolveRealPath(dir) {
61
+ const resolved = path.resolve(dir);
62
+ try {
63
+ return realpathSync(resolved);
64
+ }
65
+ catch {
66
+ // Not created yet: judged as given.
67
+ return resolved;
68
+ }
69
+ }
70
+ /** The mount `target` is on: the longest mount point that contains it. */
71
+ function findMountHolding(mounts, target) {
72
+ let holding;
73
+ for (const mount of mounts) {
74
+ if (!isWithin(target, mount.mountPoint))
75
+ continue;
76
+ // A later line for the same mount point over-mounts the earlier one.
77
+ if (!holding || mount.mountPoint.length >= holding.mountPoint.length)
78
+ holding = mount;
79
+ }
80
+ return holding;
81
+ }
82
+ function isWithin(target, mountPoint) {
83
+ return mountPoint === '/' || target === mountPoint || target.startsWith(`${mountPoint}/`);
84
+ }
@@ -1,6 +1,6 @@
1
1
  import type { LowSync } from 'lowdb';
2
- import type { ContainerResource, NodeDb, ResourceHistory, VolumeResource } from '../db/index.js';
3
- /** The node's own records: its info, and the images, volumes and containers it keeps. */
2
+ import type { ContainerResource, NodeDb, ResourceHistory, VmRecord, VolumeResource } from '../db/index.js';
3
+ /** The node's own records: its info, and the images, volumes, containers and VMs it keeps. */
4
4
  export declare class NodeRepository {
5
5
  private readonly db;
6
6
  constructor(db: LowSync<NodeDb>);
@@ -43,4 +43,14 @@ export declare class NodeRepository {
43
43
  trackContainer(id: string, fields: Omit<ContainerResource, 'createdAt'>): void;
44
44
  /** Drop a container's record once it has been deliberately removed. */
45
45
  untrackContainer(id: string): void;
46
+ /** The VM the job's runtime runs in, while it has one. */
47
+ getVm(jobAddress: string): VmRecord | undefined;
48
+ /** Record the VM a job's runtime runs in, so a later node process finds it. */
49
+ setVm(jobAddress: string, record: VmRecord): void;
50
+ /** Drop a job's VM record once the VM is gone. */
51
+ deleteVm(jobAddress: string): void;
52
+ /** Every job's VM record, by job address. */
53
+ listVms(): {
54
+ [jobAddress: string]: VmRecord;
55
+ };
46
56
  }
@@ -28,7 +28,7 @@ function describePersistFailure(message) {
28
28
  }
29
29
  return `Failed to save node state: ${message}. ${advice}`;
30
30
  }
31
- /** The node's own records: its info, and the images, volumes and containers it keeps. */
31
+ /** The node's own records: its info, and the images, volumes, containers and VMs it keeps. */
32
32
  export class NodeRepository {
33
33
  constructor(db) {
34
34
  this.db = db;
@@ -140,4 +140,24 @@ export class NodeRepository {
140
140
  delete this.db.data.resources.containers[id];
141
141
  this.persist();
142
142
  }
143
+ /** The VM the job's runtime runs in, while it has one. */
144
+ getVm(jobAddress) {
145
+ return this.db.data.resources.vms[jobAddress];
146
+ }
147
+ /** Record the VM a job's runtime runs in, so a later node process finds it. */
148
+ setVm(jobAddress, record) {
149
+ this.db.data.resources.vms[jobAddress] = record;
150
+ this.persist();
151
+ }
152
+ /** Drop a job's VM record once the VM is gone. */
153
+ deleteVm(jobAddress) {
154
+ if (!this.db.data.resources.vms[jobAddress])
155
+ return;
156
+ delete this.db.data.resources.vms[jobAddress];
157
+ this.persist();
158
+ }
159
+ /** Every job's VM record, by job address. */
160
+ listVms() {
161
+ return this.db.data.resources.vms;
162
+ }
143
163
  }