S06-06 - Devices, Warm Start and Time
Purpose: A VM’s devices and resources, saving and restoring its state, and how the guest clock follows the host.
Design
Section titled “Design”Devices
Section titled “Devices”A VM has these devices:
- one virtio network device, whose packet transport leads to
wb-netd(S07-egress-gatewayS07-egress-gateway: Egress GatewayThe only path from a guest to any network: a userspace network stack (wb-netd) and a policy-enforcing proxy (wb-proxyd) on the host., S12-platformsS12-platforms: PlatformsWhich host and guest operating systems Wraith Box targets, how platform-specific code is isolated, what each platform uses, and how wb works inside WSL.). On macOS that is a file-handle attachment; - one host-guest socket device (vsock);
- storage: the system disk, the data disk, and the guest tools disk,
read-only (S06-12-guest-agentS06-12-guest-agent: Guest AgentWhat wb-guestd, the service in the guest, does for the host, and how it updates itself., “
wb-guestd”). In the Xcode step of an image build, the build VM inwb-vmd-layeralso has the Xcode disk, read-only (“The Xcode disk”, below); - entropy;
- a Mac graphics device with one display, and a USB keyboard and pointer.
V1V1-initial: Initial versionThe first version: what it delivers, what it leaves out, and the milestones that get there. has no shared directories, no host audio input, and no USB or serial passthrough (SEC02-no-host-fs-shareSEC02-no-host-fs-share: No host filesystem sharingNo host directory is mounted into the guest, read-only or otherwise.).
wb-vmd refuses any device not in this list (S04-02-vm-providerS04-02-vm-provider: VM Provider and StreamsHow wb-vmd hands guest connections and the NIC endpoint to other processes, the fixed device set of a VM, and where wb-proxyd takes a stream's identity from., “The device set is fixed and checked”).
Network device per slot
Section titled “Network device per slot”The set is fixed per wb-vmd slot (S04-04-vm-processesS04-04-vm-processes: Per-VM ProcessesOne wb-vmd for each VM, the wb-vmd-layer entry of the Claude Code layer build, the wb-vmd-recipe entry of a named image's recipe, and wb-diskfmt, which formats a VM's new data disk., “The slot”).
The work and isolated VMs, and the recipe VM of a named image’s
build (S06-17-named-imagesS06-17-named-images: Named Images and RecipesThe build of a named image from a recipe on a clone of the sealed base.), have exactly one network device, with a
file-handle attachment. The layer slot has none. The build VM has
none until V1-M7-image-buildV1-M7-image-build: Image buildwb image build downloads the restore image and the Command Line Tools, installs and provisions a VM over the build network, installs the minimal base layer, and installs Claude Code in its own top layer. gives it its build network, and wb-vmd
refuses every network device there (I160I160 (closed): wb-vmd: the device-set check, refusing any network or sharing deviceS04-architecture has wb-vmd check its own description of every VM configuration against a fixed device set, before it creates any Virtualization object, and refuse everything else with a named rule.). Each other kind of network attachment stays refused in
every slot. A missing network device is refused with
device-set-count, and a second one with device-set-network.
At each start wb-vmd makes the VM’s packet transport, an AF_UNIX
datagram socketpair, and gives one end to the attachment with an MTU
of 65535. It sends the other end to wb-hostd on the hand-off channel
before it starts the VM (S07-02-packet-pathS07-02-packet-path: Packet PathHow wb-netd carries the guest's frames: the packet transport, the stack, the link filter, its caps, addressing, the MTU and connections., “Packet transport”;
S04-02-vm-providerS04-02-vm-provider: VM Provider and StreamsHow wb-vmd hands guest connections and the NIC endpoint to other processes, the fixed device set of a VM, and where wb-proxyd takes a stream's identity from., “Hand-off on macOS”). When it can’t make or set up
the socketpair, it refuses the start with vm-network-failed.
wb-hostd opens the packet transport’s request before it sends
Start. A VM whose start returned no transport is stopped. The start
is refused with the receiver’s rule, with handoff-missing when the
transport doesn’t come within 5 seconds of wb-vmd’s answer, or with
vm-network-missing when the provider returned none. When wb-hostd
cancels a start, the generation gets no other transport, and a record
of it that comes late is closed and logged at info as
handoff-network-refused. The VM manager
holds the descriptor in a type with no read or write method, and
closes it when the run ends.
The Xcode disk
Section titled “The Xcode disk”The Xcode disk is a disk role of its own: read-only, from the bundle
file xcode.img (S06-14-xcode-imageS06-14-xcode-image: The Xcode ImageHow an image gets full Xcode and the simulator runtimes from the host, as a read-only disk the host builds, and who runs Xcode's first launch.). The set has it exactly once in
the layer slot when the start call sets the Xcode flag, and in no
other case. Without the flag, in any other slot, twice, or missing
when the flag is set, wb-vmd refuses the set with device-set-count.
A writable Xcode disk, or one on another bundle file, is refused with
device-set-disk, and one outside the bundle with
device-set-file-outside-bundle. Before this check, a start call with
the flag in any slot but layer is refused with
vmd-xcode-disk-refused (S04-10-vmd-installS04-10-vmd-install: Installing macOS in `wb-vmd`The wb-vmd-install entry of an image build: its install rules, the restore image it reads, the build bundle it installs into, and its Install call., “The build bundle”).
MAC address
Section titled “MAC address”Each slot has one MAC address, a random, locally administered unicast
address. wb-hostd makes it on the slot’s first start and keeps it in
state.db with the slot. No two slots have the same one. wb-hostd
sends it in each StartRequest. The guest sees the same device on
every start, and a restore keeps the address (“Disks and identity”,
below). wb-vmd refuses an address that isn’t locally administered
unicast, and one of another length, with vmd-mac-invalid. wb-netd
gets the address from wb-hostd (S07-01-per-vm-policyS07-01-per-vm-policy: Enforced per VMWhy the VM is the unit the gateway enforces, how the policies of its projects combine, and what happens when they conflict or change.).
The graphics device
Section titled “The graphics device”The graphics device, keyboard and pointer stay in the set for now
(I160I160 (closed): wb-vmd: the device-set check, refusing any network or sharing deviceS04-architecture has wb-vmd check its own description of every VM configuration against a fixed device set, before it creates any Virtualization object, and refuse everything else with a named rule.). X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment., X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest? and X25-vmd-sandboxX25-vmd-sandbox: Self-sandboxed `wb-vmd`Can wb-vmd confine itself at start with a Seatbelt profile that allows the Virtualization framework, its VM bundles under <data>/vms and nothing else, and still create, start, save and restore a macOS VM? measured
that configuration, and no macOS guest has booted without them. Some
of the sandbox extensions in wb-vmd’s profile serve these devices
(X25-vmd-sandboxX25-vmd-sandbox: Self-sandboxed `wb-vmd`Can wb-vmd confine itself at start with a Seatbelt profile that allows the Virtualization framework, its VM bundles under <data>/vms and nothing else, and still create, start, save and restore a macOS VM?), and they give the VMs, the build VM included, a
shared graphics cache (T21-shared-graphics-cacheT21-shared-graphics-cache: The VMs share a graphics cacheThe graphics device, keyboard and pointer stay in the device set for now (S06-06-devices-and-warm-start, "The graphics device").).
V1-M6-release-gateV1-M6-release-gate: Release gateThe Seatbelt profiles of wb-hostd and the sessions, confinement self-checks, warm start and suspend, the full conformance suite and benchmarks, packaging and upgrades. checks whether a guest boots and works without
these devices. If they leave the set, wb-vmd’s profile is ablated
again against the new set (S04-03-self-sandboxingS04-03-self-sandboxing: Self-SandboxingHow each host daemon confines itself at startup, and the wb-launcher that starts confined programs on macOS.).
Resources
Section titled “Resources”CPU, memory, and disk are capped per VM (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.). The defaults are 4 vCPU and 8 GB, and they are configurable.
Warm start
Section titled “Warm start”wb-hostd can have wb-vmd start the work VM at login. After a
configurable idle period, the VM’s state is saved and the VM stops. The
next session restores from saved state (NFR01-startupNFR01-startup: StartupGoals: a warm VM reaches the Claude prompt in about 4 s and a suspended VM in about 7 s, and no start takes more than 10 s (p95), excluding first-time project setup and the first boot after a host restart.,
NFR03-footprintNFR03-footprint: FootprintIdle VMs suspend after a configurable period.).
The idle period runs only while the VM has no session that is starting, running, paused, or ending. So a VM with such a session is never saved for idleness (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Idle suspend”).
Measured times (these goals aren’t a release gate):
- A restore of an idle guest takes about 4 s (X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment.). That is inside the 7 s goal of NFR01-startupNFR01-startup: StartupGoals: a warm VM reaches the Claude prompt in about 4 s and a suspended VM in about 7 s, and no start takes more than 10 s (p95), excluding first-time project setup and the first boot after a host restart. for a suspended VM.
- A cold boot takes 10.9 s (p50) and 13.5 s (p95) to the guest daemon’s vsock listener. That is over the 10 s goal (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?).
Disks and identity
Section titled “Disks and identity”- A saved state is restored only onto the disks and auxiliary storage it was saved with. Once the VM has run on from them, the state is deleted. The framework doesn’t check this, and would restore old guest memory over newer disks.
- Restoring one state more than once needs APFS clones of the state, both disks and the auxiliary storage. They are taken together at the save, with a fresh clone for each restore.
- The restored VM has the machine identifier and MAC address it was saved with. No other VM with that identifier runs during the restore.
Locked host
Section titled “Locked host”Saving needs the host user’s session unlocked. When the idle period ends while the host is locked, the VM keeps running. It is saved after the next unlock.
Restoring needs the session unlocked too. A restore while locked fails with “permission denied” (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?).
Connections
Section titled “Connections”A restore ends every host-guest connection. wb-hostd connects to
wb-guestd again after each one. wb-guestd answers within about
0.2 s of resume (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?).
Failed restores
Section titled “Failed restores”A saved state works only on the host that wrote it. It may stop working after a host update.
- A restore that fails for one of these permanent reasons deletes the
state, and
wb-vmdcold boots the VM. - A restore that fails for a temporary reason keeps the state. Examples are another VM with the same identifier still running, or a locked host.
Every restore failure has the same error code, VZErrorRestore (12),
in Apple’s VZVirtualMachine.h. Only the failure reason tells them
apart. So wb-hostd never matches on the code alone.
- “Permission denied” comes from a locked host (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?), and from a state written on another host.
- “Invalid argument” comes from an identifier in use (X02-warm-startX02-warm-start: Warm startMeasure time to restore a macOS guest from saved state with a vsock device and a file-handle network attachment.),
from a host update, and from a state saved by a
wb-vmdwhose sandbox profile let it read different host facts, such as the CPU name (X25-vmd-sandboxX25-vmd-sandbox: Self-sandboxed `wb-vmd`Can wb-vmd confine itself at start with a Seatbelt profile that allows the Virtualization framework, its VM bundles under <data>/vms and nothing else, and still create, start, save and restore a macOS VM?).
Deciding permanence
Section titled “Deciding permanence”wb-hostd decides permanence from evidence it owns. With each saved
state, it records in state.db:
- the host’s hardware identity, the
IOPlatformUUID; - the macOS build;
- Wraith Box’s build version.
A mismatch with the running host is permanent, whatever the error. A build version mismatch deletes the state before any restore is tried (S06-12-guest-agentS06-12-guest-agent: Guest AgentWhat wb-guestd, the service in the guest, does for the host, and how it updates itself., “The host checks, not the guest”).
With a match:
- “permission denied” is temporary;
- “invalid argument” is temporary while a VM with that identifier runs, and permanent otherwise.
After a failure, wb-hostd reads the lock state, and checks whether a
VM with that identifier runs. It does both after the failure, not
before the attempt.
After a temporary failure, it retries a bounded number of times within a bounded wall time. It reports the reason when it gives up (NFR06-explained-refusalsNFR06-explained-refusals: OperabilityEvery refusal names the rule that caused it and how to change it.).
wb-vmd passes the error code, the failure reason, any underlying
errno, and the description. wb-hostd logs them with its
classification.
Configuration changes
Section titled “Configuration changes”When wb-hostd changes a VM’s configuration, its resources or
devices, it deletes the saved state itself and logs why. It doesn’t
wait to learn of the change from “invalid argument” at the next
restore.
Time and sleep
Section titled “Time and sleep”The guest has no network time. NTP is UDP, and wb-netd drops it
(S07-egress-gatewayS07-egress-gateway: Egress GatewayThe only path from a guest to any network: a userspace network stack (wb-netd) and a policy-enforcing proxy (wb-proxyd) on the host., X03-network-pathX03-network-path: Network pathA macOS guest boots on the file-handle attachment with the gVisor stack: gets a DHCP lease, resolves only allowlisted names, reaches only the proxy.). The guest’s clock comes only
from wb-hostd. wb-hostd sends its own time to wb-guestd, and
wb-guestd sets the guest clock from it. The call is
GuestService.SetTime, with the reason for each update: a cold boot,
a restore, a wake, or the update every 60 seconds. Its answer is empty.
When the host sends the time
Section titled “When the host sends the time”wb-hostd starts every update. It sends the time:
- after every cold boot;
- after every restore from saved state;
- after every host wake;
- every 60 seconds while the VM runs.
After a boot, restore or wake, it sends the time before the steps that depend on it (S09-06-ca-keysS09-06-ca-keys: CA KeysHow the key of each TLS inspection certificate authority is made, stored and restored, and when a CA is valid.). Without that first update, a cold-booted guest starts from the virtual clock. A restored guest lags by the time it spent saved (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?). The periodic update corrects drift and a change of the host clock within a minute.
The guest link (S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded.) sends an update after
every accepted hello, and periodically while it is up. After a
cold boot the reason is a boot until an update of that run has succeeded, and periodic
after it, so an update after a reconnect is periodic. An update that
fails for another reason than an unanswered call is logged with
guest-time-failed, and the link is kept.
wb-hostd never reads or uses a time value from the guest. The guest
has no message that asks for the time (S04-01-design-principlesS04-01-design-principles: Design PrinciplesThe design decisions that every host and guest process follows., “Everything in the guest is untrusted”). A guest that drops the link
gets one update per new hello, and the open cap of
S06-11-reconnect-and-recoveryS06-11-reconnect-and-recovery: Reconnect, Loss and RecoveryWhat happens when the host-guest link drops, when a session is lost, and how a lost session's work is recovered or discarded. bounds those. So the guest can’t make wb-hostd send more
updates than this schedule and that cap allow.
Step or slew
Section titled “Step or slew”wb-guestd steps the clock to the host’s time:
- after a cold boot, a restore or a host wake;
- when the guest is more than 1 second off.
Otherwise it slews the clock, so a running build never sees the wall
clock go backward. On macOS a step is settimeofday and a slew is
adjtime, in internal/platform/guestclock.
Drift between two updates 60 seconds apart stays far below 1 second. A larger offset means the host clock changed or an update was missed. Slewing it off at NTP’s limit of 500 ppm would take more than half an hour.
Decisions
Section titled “Decisions”Save after the next unlock
Section titled “Save after the next unlock”When the idle period ends while the host is locked, the VM keeps running, and is saved after the next unlock.
- Decided: I16I16 (closed): X02-warm-start: Warm startHow long does it take to restore a macOS guest from saved state with a vsock device and a file-handle network attachment? (B16-warm-startB16-warm-start: How fast does a suspended VM come back, and can one saved state be restored more than once?Yes, with conditions.).
Status: Draft