Skip to content

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
Permissions0600: only your user can connect
Path limitUnder 104 bytes. A longer state directory path fails with "control socket path is too long. Use a shorter state directory."
FramingNewline-delimited JSON (one object per line, \n terminated)
ExchangeOne request, one response, then the server closes the connection
Exceptionconsole: 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 ​

json
{"id":"7F1C...","op":"status","nodes":["vm1"]}
FieldTypeUsed by
idstring, requiredEvery request. The CLI sends a UUID. It is echoed in the response.
opstring, requiredEvery request. See Operations.
nodesarray of stringsstatus, down, restart, pause, resume
forcebooldown
timeoutint, secondsdown (default 60)
linkstringlink
upboollink, port
nodestringport, media, memory, display, usb, console, console-check
ifacestringport
indexintmedia: position in the node's disks list
insertboolmedia: true inserts, false ejects
bytesuint64memory: balloon target in bytes
actionstringusb: attach or detach
devicestringusb: vvvv:pppp in hex, for example 0403:6001

Fields that an operation does not use are ignored. The CLI encodes with sorted keys.

Response ​

json
{"id":"7F1C...","ok":true,"result":{"name":"single-vm","nodes":[...]}}
FieldTypeMeaning
idstringThe request's id. Empty when the request could not be decoded.
okbooltrue when the operation succeeded
errorstringHuman-readable reason, when ok is false
resultobjectPresent when ok is true

result is a ControlResult. Every field is optional:

FieldTypeMeaning
readybooltrue in a ping reply
pidintPid of the lab process (ping)
namestringLab name, or the object the operation acted on (link name, node/iface, node/index, node name)
filestringAbsolute path of the lab file
stateDirectorystringAbsolute path of the state directory
nodesarray of NodeReportNode status (see below)
nodeNamesarray of stringsNodes 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:

FieldMeaning
nameInterface name from the lab file
macThe written MAC, or the stable derived one
attachswitch/<name>, link/<name>, or absent for a free NIC
connectedWhether the NIC currently has an attachment
reservedIPThe DHCP reservation for this MAC on its switch, if any
ipThe address the guest uses, when known
ipSourcereservation, lease, hostname or reserved (see Address discovery)
reservationActiveWith a reservation in the lab file: whether macOS's DHCP server holds it (/etc/bootptab)

Operations ​

opFieldsResultNotes
pingnoneStatus of all nodes, plus ready, pid, name, file, stateDirectory, nodeNamesvz up polls this until every node is running or paused
statusnodes (empty: all)name, file, stateDirectory, nodes, nodeNamesReads the DHCP lease file once per call
downnodes (empty: all), force, timeoutStatus of all nodesGraceful ACPI stop, waits up to timeout, then powers off what is left. With no nodes the lab process exits afterwards
restartnodes (at least one)Status of those nodesGraceful stop with a 60 second timeout, then start. The lab process does not exit in between
pausenodes (at least one)Status of those nodesFails unless each node is running
resumenodes (at least one)Status of those nodesFails unless each node is paused
linklink, upname: the linkPlugs or unplugs both ends
portnode, iface, upname: node/ifacePlugs or unplugs one NIC, on a switch or a link
medianode, index, insertname: node/indexOnly for bus: usb disks
memorynode, bytesname: the nodeA multiple of 1 MiB, between the framework minimum and the node's memory
displaynodename: the nodeOpens or focuses the node's window in the lab process
usbnode, action, devicename: the nodeNeeds a USB controller on the node and Accessory Access
console-checknodename: the node, nodeNamesFails with the console part's hint when the node has no console
consolenodeSame as console-check, then a raw pipeSee 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 op gets ok: false with "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 down stops 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:

bash
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.

See also ​