VM assembly, platforms and image makers
How a node in the lab file becomes a VZVirtualMachineConfiguration, how platforms plug into the engine without the engine knowing any machine type or OS, and how images are prepared.
The split
A VM is assembled from parts. Two sources choose them:
- The lab file decides the hardware: CPU, memory, disks, NICs, shares, USB, sound, clipboard. The engine picks these parts in
LabHardware.swift. They are the same for every platform. - The node's platform decides the rest: firmware, console, display, first-boot setup and an install step, returned as a
MachinePlan. A node names its platform withplatform:(genericby default, orapple), andPlatformTablelooks it up by name. That lookup is the only place a machine type is chosen: there is noif appleanywhere else.
Image makers are a third, separate concern: they prepare files at vz image pull and never take part in building a VM.
The engine sees platforms only through the MachinePlanner protocol. The CLI injects PlatformTable from Composition.swift; tests can inject their own. This is why LabRuntime has no if debian or if macOS branches. Which distribution a disk came from does not change the machine: the node lists the file under disks.
Protocols
These are in MachinePart.swift (engine) and Platform.swift (platforms).
| Type | Role |
|---|---|
MachinePart | One piece of a VM. install(into: MachineAssembly) adds devices to the machine being built. |
ConsolePart | A MachinePart that also gives the running node's console: multiplexer(), start(), close(). |
DisplayPart | A MachinePart that can open(node:vm:in:) a window for the running node. |
InstallStep | Work before the first normal boot (installing macOS): prepare, isNeeded, install (in the background, with progress) and firstStart. |
MachinePlan | firmware: [MachinePart], console: ConsolePart, display: DisplayPart, setup: [MachinePart], install: InstallStep?. |
MachinePlanner | plan(for: Node, in: LoadedLab) -> MachinePlan. |
Platform | One machine type: name, hostLimit, resetFileNames, validate(_:at:in:) and plan(for:in:). |
PlatformTable | The platforms by name. validate(_:) reports unknown names, each node's platform rules and host limits; as a MachinePlanner it plans each node with its platform. |
StandardPlatforms.all | The platforms vz ships: GenericPlatform and ApplePlatform. |
NoConsole and NoDisplay are real answers, not missing values. They install nothing, and multiplexer() or open() throws a hint that says what to use instead. ApplePlatform returns NoConsole with "Use vz node display <node>, or SSH"; displays: [] gives NoDisplay.
Install order
VirtualMachineBuilder.build creates a MachineAssembly and installs parts in this order:
plan.firmware: platform and boot loader- Lab hardware, from
LabHardware.parts(for:plugs:claimed:) plan.consoleplan.displayplan.setup: first-boot setup, so a seed disk comes after the node's own disks
MachineAssembly collects device lists (storage, USB, shares, NICs, serial ports, graphics, keyboards, pointers, console devices, audio) because Virtualization takes each list whole. finish() writes them into the configuration, adds an XHCI USB controller when a USB disk or a passthrough device needs one, and calls validate().
When a plan has an install step, LabRunner calls prepare before building the VM. If isNeeded is true after the VM exists, it runs install in the background instead of starting the VM (status installing N%, or install failed), then firstStart. vz up counts an installing node as started.
Lab hardware parts
| Part | Always | What it installs |
|---|---|---|
Resources | yes | cpuCount (checked against maximumAllowedCPUCount) and memorySize |
Disks | yes | Each disk from DiskBuilder.prepare, in list order. Files that are not disk images (a .ipsw) are skipped and keep their index. USB disks that start ejected are kept as cold VZUSBMassStorageDevice objects for media insert |
USBPassthrough | yes | Host USB devices claimed before the VMs are built |
Shares | yes | VirtioFS shares, plus the rosetta share when rosetta: true |
NICs | yes | Virtio NICs in list order (PCI order in the guest). The attachment is kept even when connected: false |
EntropyAndBalloon | yes | Virtio entropy and the traditional memory balloon |
Clipboard | clipboard: true | SPICE agent console port with clipboard sharing |
Sound | sound with output or input | Virtio sound bound to the Mac's default devices |
A platform does not remove hardware parts. It rejects in validate what its machine cannot use (for example clipboard or rosetta on apple).
Firmware, console, display and install parts
| Part | File | Notes |
|---|---|---|
GenericArmPlatform | Firmware.swift | Generic Arm platform. The machine identifier is stored in <state>/<node>/machine-id. Nested virtualization when asked and supported |
EFIBoot | Firmware.swift | UEFI with the variable store in efi-vars.fd. With secureBoot, enrolls the default signatures once |
MacPlatformBoot | MacOS.swift | Mac platform (hardware model, machine identifier, auxiliary storage from the mac-* files) and the macOS boot loader |
VirtioSerialConsole | Consoles.swift | hvc0 in Linux, through a ConsoleMultiplexer. With consoleLog, one log file each time the lab starts the node |
NoConsole | Consoles.swift | Explains what to use instead |
VirtioGraphicsDisplay | Displays.swift | One virtio graphics device per display, with a USB keyboard and pointer |
MacGraphicsDisplay | MacOS.swift | Mac graphics, a Mac keyboard and a trackpad |
NoDisplay | Displays.swift | Explains why there is no window |
NoCloudSeed | FirstBoot.swift | Builds cidata.iso with hdiutil makehybrid and attaches it read-only |
MacOSInstall | MacOS.swift | The InstallStep for macOS: picks the hardware model from the restore image, installs with VZMacOSInstaller, writes macos-installed, and starts the first boot with Apple's provisioning options when there is an account (MacAccount) |
Platforms
GenericPlatform
GenericPlatform is the default. It uses GenericUEFI, a building block (not a base class) that returns the plan most arm64 operating systems boot with:
MachinePlan(
firmware: [
GenericArmPlatform(nestedVirtualization: node.nestedVirtualization),
EFIBoot(secureBoot: node.secureBoot),
],
console: console, // chosen by the caller
display: display(for: node), // the lab file's displays, or NoDisplay
setup: node.`init`.map { [NoCloudSeed(path: $0)] } ?? []
)GenericPlatform passes GenericUEFI.serialConsole(for: node). That is the console every generic node gets, whether it boots a pulled cloud disk, an ISO or a disk you built. Its validation rejects a .ipsw under disks, and node reset deletes its efi-vars.fd.
ApplePlatform
ApplePlatform is Apple's Mac machine for macOS:
- Plan:
MacPlatformBoot,NoConsole,MacGraphicsDisplay(orNoDisplay), no setup parts (no seed disk), andMacOSInstallwith the.ipswfound underdisks(resolved against the lab file;nilwhen the node lists none, which is fine once macOS is installed). - Account:
initis read on the Mac byMacFirstBoot: the first user'sname,plain_text_passwd(or a text password underchpasswd.users) andgecos, andssh_pwauthfor Remote Login (on unlessfalse). Other keys become warnings. - Validation: rejects
rosetta,nestedVirtualization,secureBoot,clipboardanddisplays: []; warns aboutconsoleLog; wants one.ipswand a writable, non-overlay first other disk to install onto; adds the experimental warning. - Host limit and reset:
hostLimitis 2;resetFileNamesaremac-hardware-model,mac-machine-id,mac-aux-storageandmacos-installed, so a reset installs again.
Adding a machine type is covered in Adding a platform.
Image makers
Image makers live in Sources/LabOS/Images. An image maker is a catalog entry for vz image pull: it knows how one OS publishes one image and how to turn the download into the file a node lists under disks. It never builds a VM, and the lab file never names it.
| Type | Role |
|---|---|
ImageMaker | Something vz image pull can fetch: reference, title, notes, aliases, support, fileName, labFileLines(path:), and fetch(with:events:) |
DownloadImageMaker | An ImageMaker whose fetch hands an Artifact to ImageFetcher. The Linux disks and ISOs are these |
Support | .tested (boot-tested to a login prompt or its own ready signal) or .experimental(reason). Experimental images print a warning on pull |
StandardImages.all | The list vz ships, in ImageCatalog.swift |
ImageCatalog | Looks a maker up by kind and name or alias, for image ls and image pull. It refuses two makers for the same reference |
MacOSImage | Fetches the newest macOS restore image this Mac supports through Virtualization, after checking the free space. Its pasted lines include platform: apple |
Rules for image makers (ImageMaker.swift):
- One maker per catalog image, one file per family. A maker does not plan the VM.
- Makers never import or call each other.
- When two images share behavior (two Debian releases, the Debian cloud disk and netinst ISO), they share code in their own family's file, never through a base class. See
Debian.swift.
Adding one is covered step by step in Adding an OS image.
Disks and the overlay stack
DiskBuilder.swift turns a Disk into a storage device:
- The format comes from the extension:
.asif,.rawor.img,.iso,.ipsw. ISOs are always read-only. A.ipswis never attached (theDiskspart skips it). - A missing file is created when
sizeis set andoverlayis not:.rawas a sparse file,.asifwithdiskutil image create blank --format ASIF. - The bus picks the device:
virtio(VZVirtioBlockDeviceConfiguration),nvme(VZNVMExpressControllerDeviceConfiguration) orusb(VZUSBMassStorageDeviceConfiguration).
With overlay: true the disk is a DiskImageKit stack, built bottom to top:
Each layer only stores its own blocks, so the stack is always rebuilt from the base. Opening the overlay file on its own gives an empty disk. The overlay is size large, or 10GiB without size (Disk.defaultOverlaySize; a larger base keeps its size). It can be larger than the base (the extra space reads as zeros) and can grow on a later start, but never shrink. The rule is Disk.overlaySize in LabCore, tested without a VM. vz node reset deletes the overlay and cache files.
The image pipeline
vz image pull hands a DownloadImageMaker's Artifact to ImageFetcher. The fetcher knows no distribution.
| Piece | What it is |
|---|---|
Artifact | source (a FileSource), checksums (URL of the published list), digest (sha256 or sha512), unpacker, fileName in the images directory |
FixedFile | A FileSource with a fixed URL |
NewestListedFile | A FileSource for names that change with each point release: the newest name in the checksum list with a given prefix and suffix (Debian netinst) |
ChecksumList | Reads GNU (<hex> <name>, <hex> *<name>), BSD (SHA256 (<name>) = <hex>, possibly PGP signed) and one-digest files |
Unpacker | TarXZMember (one member of a tar.xz), QCOW2ToRaw (qemu-img when installed, else the built-in converter in QCOW2.swift), XZToRaw, KeepAsIs |
Downloading | The network, behind a protocol so tests can replace it |
The staging rename means an interrupted pull never leaves a half-written file under the final name. The cache is safe to delete.
The built-in qcow2 converter supports versions 2 and 3, zero clusters and zlib-compressed clusters. It refuses backing files, encryption, external data files, extended L2 entries and zstd compression with a clear error. The raw output is sparse.