Control protocol
How CLI commands talk to a running lab: one JSON line in, one JSON line out, over a Unix socket in the state directory.
WARNING
This protocol is internal. It exists so the vz CLI can reach its own lab process, and it may change in any release without notice. For scripts, use the CLI with --json instead (see Automation).
The types are in ControlProtocol.swift. The server is ControlServer.swift, and the operations are dispatched in LabRunner.perform in LabRunner.swift.
Transport
| Socket | <state>/control.sock, a Unix stream socket |
| Permissions | 0600: only your user can connect |
| Path limit | Under 104 bytes. A longer state directory path fails with "control socket path is too long. Use a shorter state directory." |
| Framing | Newline-delimited JSON (one object per line, \n terminated) |
| Exchange | One request, one response, then the server closes the connection |
| Exception | console: after the response line the connection becomes a raw byte pipe |
The server accepts connections on a background queue and handles each one in its own task, so a slow client does not block others.
When the socket file is missing or nothing listens on it (ENOENT, ECONNREFUSED), the CLI reports "lab '<name>' is not running. Start it with vz up."
Request
{"id":"7F1C...","op":"status","nodes":["vm1"]}| Field | Type | Used by |
|---|---|---|
id | string, required | Every request. The CLI sends a UUID. It is echoed in the response. |
op | string, required | Every request. See Operations. |
nodes | array of strings | status, down, restart, pause, resume |
force | bool | down |
timeout | int, seconds | down (default 60) |
link | string | link |
up | bool | link, port |
node | string | port, media, memory, display, usb, console, console-check |
iface | string | port |
index | int | media: position in the node's disks list |
insert | bool | media: true inserts, false ejects |
bytes | uint64 | memory: balloon target in bytes |
action | string | usb: attach or detach |
device | string | usb: vvvv:pppp in hex, for example 0403:6001 |
Fields that an operation does not use are ignored. The CLI encodes with sorted keys.
Response
{"id":"7F1C...","ok":true,"result":{"name":"single-vm","nodes":[...]}}| Field | Type | Meaning |
|---|---|---|
id | string | The request's id. Empty when the request could not be decoded. |
ok | bool | true when the operation succeeded |
error | string | Human-readable reason, when ok is false |
result | object | Present when ok is true |
result is a ControlResult. Every field is optional:
| Field | Type | Meaning |
|---|---|---|
ready | bool | true in a ping reply |
pid | int | Pid of the lab process (ping) |
name | string | Lab name, or the object the operation acted on (link name, node/iface, node/index, node name) |
file | string | Absolute path of the lab file |
stateDirectory | string | Absolute path of the state directory |
nodes | array of NodeReport | Node status (see below) |
nodeNames | array of strings | Nodes started by this lab process, in start order |
A NodeReport has name, state, cpus, memory (for example "1GiB") and interfaces. state is one of stopped, running, paused, error, starting, pausing, resuming, stopping, saving, restoring, unknown.
Each InterfaceReport has:
| Field | Meaning |
|---|---|
name | Interface name from the lab file |
mac | The written MAC, or the stable derived one |
attach | switch/<name>, link/<name>, or absent for a free NIC |
connected | Whether the NIC currently has an attachment |
reservedIP | The DHCP reservation for this MAC on its switch, if any |
ip | The address the guest uses, when known |
ipSource | reservation, lease, hostname or reserved (see Address discovery) |
reservationActive | With a reservation in the lab file: whether macOS's DHCP server holds it (/etc/bootptab) |
Operations
op | Fields | Result | Notes |
|---|---|---|---|
ping | none | Status of all nodes, plus ready, pid, name, file, stateDirectory, nodeNames | vz up polls this until every node is running or paused |
status | nodes (empty: all) | name, file, stateDirectory, nodes, nodeNames | Reads the DHCP lease file once per call |
down | nodes (empty: all), force, timeout | Status of all nodes | Graceful ACPI stop, waits up to timeout, then powers off what is left. With no nodes the lab process exits afterwards |
restart | nodes (at least one) | Status of those nodes | Graceful stop with a 60 second timeout, then start. The lab process does not exit in between |
pause | nodes (at least one) | Status of those nodes | Fails unless each node is running |
resume | nodes (at least one) | Status of those nodes | Fails unless each node is paused |
link | link, up | name: the link | Plugs or unplugs both ends |
port | node, iface, up | name: node/iface | Plugs or unplugs one NIC, on a switch or a link |
media | node, index, insert | name: node/index | Only for bus: usb disks |
memory | node, bytes | name: the node | A multiple of 1 MiB, between the framework minimum and the node's memory |
display | node | name: the node | Opens or focuses the node's window in the lab process |
usb | node, action, device | name: the node | Needs a USB controller on the node and Accessory Access |
console-check | node | name: the node, nodeNames | Fails with the console part's hint when the node has no console |
console | node | Same as console-check, then a raw pipe | See below |
Console connections
console is the only operation that keeps the connection open.
- After the response line, every byte from the guest goes to the client and every byte from the client goes to the guest, unchanged.
- On attach, up to 64 KiB of recent output is replayed so the login prompt is visible. Terminal query sequences (cursor position reports, device attributes, OSC queries and similar) are removed from the replay only, so your terminal does not answer them into the guest. See
ConsoleReplay.swift. - Any number of clients can attach to the same console. A client that cannot take output for 2 seconds is dropped so it cannot stall the others.
- Closing the connection detaches. The VM keeps running.
Errors
- A line that is not a valid request gets
{"id":"","ok":false,"error":"malformed request"}. - An unknown
opgetsok: falsewith "unknown operation '<op>'". - A missing field gets a message that names what is needed, for example "link needs a name and up or down".
- Operation failures carry the runtime message, for example "node 'vm1' is paused, not running" or "unknown port vm1/eth9". vmnet failures add a hint for the common status codes (see
RuntimeError.swift). - When
downstops the whole lab, the process may exit before the reply is read. The CLI treats that as success if the process is gone within 10 seconds.
Trying it by hand
With a lab running, you can talk to the socket with nc:
sock=~/.vz/state/single-vm/control.sock
printf '{"id":"1","op":"status"}\n' | nc -U "$sock"
printf '{"id":"2","op":"port","node":"vm1","iface":"mgmt","up":false}\n' | nc -U "$sock"The id field is required: a request without it is rejected as malformed.