Skip to content

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 with platform: (generic by default, or apple), and PlatformTable looks it up by name. That lookup is the only place a machine type is chosen: there is no if apple anywhere 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).

TypeRole
MachinePartOne piece of a VM. install(into: MachineAssembly) adds devices to the machine being built.
ConsolePartA MachinePart that also gives the running node's console: multiplexer(), start(), close().
DisplayPartA MachinePart that can open(node:vm:in:) a window for the running node.
InstallStepWork before the first normal boot (installing macOS): prepare, isNeeded, install (in the background, with progress) and firstStart.
MachinePlanfirmware: [MachinePart], console: ConsolePart, display: DisplayPart, setup: [MachinePart], install: InstallStep?.
MachinePlannerplan(for: Node, in: LoadedLab) -> MachinePlan.
PlatformOne machine type: name, hostLimit, resetFileNames, validate(_:at:in:) and plan(for:in:).
PlatformTableThe 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.allThe 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:

  1. plan.firmware: platform and boot loader
  2. Lab hardware, from LabHardware.parts(for:plugs:claimed:)
  3. plan.console
  4. plan.display
  5. plan.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 ​

PartAlwaysWhat it installs
ResourcesyescpuCount (checked against maximumAllowedCPUCount) and memorySize
DisksyesEach 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
USBPassthroughyesHost USB devices claimed before the VMs are built
SharesyesVirtioFS shares, plus the rosetta share when rosetta: true
NICsyesVirtio NICs in list order (PCI order in the guest). The attachment is kept even when connected: false
EntropyAndBalloonyesVirtio entropy and the traditional memory balloon
Clipboardclipboard: trueSPICE agent console port with clipboard sharing
Soundsound with output or inputVirtio 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 ​

PartFileNotes
GenericArmPlatformFirmware.swiftGeneric Arm platform. The machine identifier is stored in <state>/<node>/machine-id. Nested virtualization when asked and supported
EFIBootFirmware.swiftUEFI with the variable store in efi-vars.fd. With secureBoot, enrolls the default signatures once
MacPlatformBootMacOS.swiftMac platform (hardware model, machine identifier, auxiliary storage from the mac-* files) and the macOS boot loader
VirtioSerialConsoleConsoles.swifthvc0 in Linux, through a ConsoleMultiplexer. With consoleLog, one log file each time the lab starts the node
NoConsoleConsoles.swiftExplains what to use instead
VirtioGraphicsDisplayDisplays.swiftOne virtio graphics device per display, with a USB keyboard and pointer
MacGraphicsDisplayMacOS.swiftMac graphics, a Mac keyboard and a trackpad
NoDisplayDisplays.swiftExplains why there is no window
NoCloudSeedFirstBoot.swiftBuilds cidata.iso with hdiutil makehybrid and attaches it read-only
MacOSInstallMacOS.swiftThe 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:

swift
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 (or NoDisplay), no setup parts (no seed disk), and MacOSInstall with the .ipsw found under disks (resolved against the lab file; nil when the node lists none, which is fine once macOS is installed).
  • Account: init is read on the Mac by MacFirstBoot: the first user's name, plain_text_passwd (or a text password under chpasswd.users) and gecos, and ssh_pwauth for Remote Login (on unless false). Other keys become warnings.
  • Validation: rejects rosetta, nestedVirtualization, secureBoot, clipboard and displays: []; warns about consoleLog; wants one .ipsw and a writable, non-overlay first other disk to install onto; adds the experimental warning.
  • Host limit and reset: hostLimit is 2; resetFileNames are mac-hardware-model, mac-machine-id, mac-aux-storage and macos-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.

TypeRole
ImageMakerSomething vz image pull can fetch: reference, title, notes, aliases, support, fileName, labFileLines(path:), and fetch(with:events:)
DownloadImageMakerAn 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.allThe list vz ships, in ImageCatalog.swift
ImageCatalogLooks a maker up by kind and name or alias, for image ls and image pull. It refuses two makers for the same reference
MacOSImageFetches 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, .raw or .img, .iso, .ipsw. ISOs are always read-only. A .ipsw is never attached (the Disks part skips it).
  • A missing file is created when size is set and overlay is not: .raw as a sparse file, .asif with diskutil image create blank --format ASIF.
  • The bus picks the device: virtio (VZVirtioBlockDeviceConfiguration), nvme (VZNVMExpressControllerDeviceConfiguration) or usb (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.

PieceWhat it is
Artifactsource (a FileSource), checksums (URL of the published list), digest (sha256 or sha512), unpacker, fileName in the images directory
FixedFileA FileSource with a fixed URL
NewestListedFileA FileSource for names that change with each point release: the newest name in the checksum list with a given prefix and suffix (Debian netinst)
ChecksumListReads GNU (<hex> <name>, <hex> *<name>), BSD (SHA256 (<name>) = <hex>, possibly PGP signed) and one-digest files
UnpackerTarXZMember (one member of a tar.xz), QCOW2ToRaw (qemu-img when installed, else the built-in converter in QCOW2.swift), XZToRaw, KeepAsIs
DownloadingThe 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.

See also ​