Dissecting pvm: A Go Binary for QEMU VMs and Fail-Closed Network Egress

It is easy to mistake pvm for a QEMU wrapper with create and ssh commands. The project is doing something broader: it puts local development VMs, cluster networking, DNS policy, WireGuard exits, and a guest control channel into one Go system.

This article is based on pvm v1.1.6. It answers three questions: how a VM becomes a usable development machine, why the network has a Gateway VM in the middle, and how pvm makes an exit failure drop traffic instead of silently returning to host egress. Hostnames, addresses, and tokens below are examples.

TL;DR

  • The host pvm process is a CLI and control-plane client. A resident daemon owns the VM lifecycle.
  • The daemon maintains one Gateway VM per cluster. Development VMs join its layer-2 segment, and the Gateway selects direct egress, a VPS WireGuard exit, or Cloudflare WARP.
  • cmd/agent and cmd/gateway are compressed and embedded during the build, so the installed pvm can unpack guest components into VMs. The QEMU runtime is managed as a separate asset.
  • Once an exit is selected, default traffic uses the tunnel. When the tunnel disappears, policy routing and nftables/iptables rules drop the traffic instead of falling back to the host uplink.

What problem does pvm solve?

pvm is for people who want a personal development machine that can be rebuilt at any time and whose network egress can be stated and checked explicitly.

bash
pvm create --image ubuntu:26.04 --name dev1 --user dev
pvm ssh dev1
pvm desktop dev1
pvm map dev1 8080:80

A VM can start from a cloud image or from a baked golden image. A golden image includes Docker, common development tools, and an XFCE desktop, reducing first-boot work. The VM disk is a qcow2 overlay; /share and extra host mounts are exposed through virtio-9p.

The egress workflow is equally central:

bash
pvm exit add my-exit --server vps.example.com:18443 --token YOUR_TOKEN
pvm route dev1 --via my-exit

The goal is to switch the VM's default network path to an auditable exit. Access to an internal network or host resources is expressed through direct, internal, or explicit CIDR rules.

Control plane: a thin CLI over a stateful daemon

cmd/pvm/main.go dispatches top-level commands and hands stateful work to internal/cli. The CLI calls the daemon through /v1/... HTTP endpoints for create, start, stop, clone, snapshot, mount, port mapping, route, desktop, and audit operations.

Only one daemon owns the state. Unix systems use ~/.pvm/run/daemon.sock; Windows uses loopback TCP and adds the ~/.pvm/run/admin.token bearer token to every API except health checks. A lifetime lock prevents two daemons from mutating cluster state at the same time.

The default state root looks like this:

text
~/.pvm/
├── images/ base images and immutable golden images
├── vms/ disks, seeds, specs, and run state
├── clusters/ IPs, MACs, port pools, and route rules
├── keys/ pvm-managed SSH keys
├── runtime/ guest agent, gateway, and QEMU runtime
└── run/ daemon sockets, locks, logs, and transient state

Cluster and VM metadata use temporary files, fsync, and rename-based atomic writes. The purpose is simple: a route or port-map update must not be persisted halfway through if the process crashes.

What happens during pvm create?

The source breaks creation into four useful stages.

1. Prepare the base image

internal/image resolves ubuntu:26.04 or debian:13, downloads with resume support, and verifies the distribution checksum. If an independent trust anchor is required, gpgv can verify a locally supplied keyring as well.

pvm image build ubuntu:26.04 starts a temporary build VM, installs Docker, XFCE, and development tools, then seals the result as a new golden qcow2. Golden versions are appended and kept immutable, so existing VMs do not silently change backing images.

2. Create the overlay and seed

internal/vm.Create validates the base disk, creates a qcow2 overlay, UEFI vars, and a NoCloud seed, then generates a management SSH key for the VM. The seed contains the user, SSH public key, locale, timezone, mounts, and an optional neutral Git identity.

The host's ~/.ssh and ~/.gitconfig are not copied into the guest automatically. That is a useful boundary: a development machine can be convenient without inheriting personal host credentials during creation.

3. Assemble QEMU

QEMU uses two network modes:

  • Gateway and standalone build VMs use slirp, with loopback hostfwd entries for SSH and the port pool.
  • User VMs use cluster mode, attaching a virtio NIC to the Gateway's layer-2 network through a QEMU stream socket.

internal/qemu assembles the disk, seed, QMP, serial console, virtio-serial agent, and 9p mounts. After launch, the daemon records the QEMU process identity and arguments, then waits for SSH, cloud-init, the guest agent, and the cluster link to become ready as separate stages.

4. Make failure recoverable

Cluster membership, VM state, QEMU launch, and Gateway configuration each have side effects. The daemon keeps rollback paths for those operations. During recovery it first restores relays and links to the current persisted state, then exposes the guest control channel. A running QEMU process alone is never treated as a ready VM.

Why is there a Gateway VM?

Development VMs join a managed Gateway instead of pointing their default route directly at the host. The Gateway has a slirp uplink and maintains the bridge, DHCP, DNS, policy routing, and exit tunnels.


Cluster state stores VM addresses in 100.99.x.0/24, MACs, port pools, and routing rules. The daemon writes changes to /etc/pvm/gateway.json; the guest pvm-gateway process periodically reads it and converges the network state.

This gives pvm one place to push routing and DNS policy, and keeps WireGuard and proxy software out of each development VM.

Routing and DNS: real IP by default, fake IP by explicit rule

bash
pvm route default --via my-exit
pvm route add direct --domain-suffix example.com
pvm route add proxy --domain-keyword video --via my-exit
pvm route add internal --domain-suffix corp.example --dns 10.0.0.53
pvm route explain dev1 example.com

Routing has three layers: a cluster default exit, a per-VM default exit, and rules matching domains or CIDRs. direct and internal are explicit bypass paths; proxy binds a match to a selected exit.

DNS follows the same policy. Domains using the default exit resolve through that exit to real IPv4 addresses and use direct layer-3 forwarding through the tunnel. Only explicit proxy-domain rules allocate fake IPs and hand the flow to the Gateway's fake TCP, UDP, or ICMP relay. This keeps ordinary traffic on a simple path while preserving domain-based routing where it is needed.

How fail-closed routing works

internal/gateway does more than create WireGuard devices. It maintains policy routing, nftables rules, endpoint exceptions, and a dynamic bypass set.

The important rules are:

  1. IPv6 traffic from the cluster is dropped; the managed data plane is IPv4-only.
  2. With an exit selected, forwarded traffic that does not match direct or internal policy must use the tunnel table.
  3. When a tunnel device disappears, its policy routes are removed and the exit rules remain default-deny. Packets are not handed back to the main route table.
  4. pvm owns a scoped nftables table and checks the live kernel state during recovery. Detected drift is reapplied.

That is why pvm audit checks route, DNS, TCP, UDP, ICMP, and IPv6 separately. A successful WireGuard handshake proves only that the tunnel can handshake; it does not prove that DNS and data forwarding work end to end.

VPS exit-agent: HTTPS control plane, WireGuard data plane

The VPS runs the same host binary through the exit-agent subcommand. It starts a TLS control plane for peer registration and health checks. Control requests require a bearer token, and the client pins the server certificate fingerprint so a trusted first connection cannot be silently replaced later.

Peers receive a tunnel IP per transport. The data plane supports:

  • UDP: standard WireGuard over UDP.
  • TCP: WireGuard over TCP for networks where UDP is unreliable.
  • NAT: nftables first, then iptables, with a userspace/gVisor backend when kernel firewall tools are unavailable.

The exit also blocks private, link-local, metadata, and multicast destinations unless a narrow CIDR allowance is configured. Its job is to forward decrypted tunnel traffic safely; it does not make an application anonymous.

If deploying a VPS is unnecessary, pvm exit add-warp registers Cloudflare WARP. The WARP private key and device token stay on the host, while the Gateway receives only the WireGuard data-plane parameters it needs.

The guest agent keeps an offline VM manageable

cmd/agent is a small Linux guest binary. It exposes pvm.agent over virtio-serial, and the host connects through a Unix socket. pvm exec dev1 ... therefore works without guest networking, which is useful when the network itself needs repair.

The guest agent also exposes port-map operations for the current VM. Requests do not contain a VM name; the daemon derives identity from the virtio-serial channel and authorizes the request against that VM. A guest cannot select another VM by changing a field in the request.

The build script compiles the Gateway and guest agent, compresses them with zstd, writes a SHA256 manifest, and finally builds the host pvm with -tags embedassets. The guest agent uses garble and a fixed seed to reduce repository identifiers and extra build metadata in the release asset.

The security boundaries are part of the design

The project's pvm security guide is explicit about its trust model: the host user, daemon, QEMU, guest kernel, Gateway, and selected exit are trusted components. Several boundaries are easy to misunderstand:

  • A VPS exit or Gateway can see destinations and unencrypted traffic. Application TLS still provides the confidentiality.
  • VMs in one cluster share a layer-2 network. MAC/IP validation is not a cryptographic identity and cannot contain a hostile root guest.
  • --share global and extra mounts are explicit trust boundaries. 9p uses security_model=none, so mounted content can carry keys, code, and credentials.
  • Application-managed DoH/DoQ can bypass domain rules. pvm does not decrypt HTTPS or identify every encrypted DNS service.
  • pvm desktop --public exposes noVNC to the network. The password in the URL must be treated as a secret.

pvm is best understood as a personal development environment with a verifiable egress path. It makes risky paths explicit and provides audit and recovery tools, while the real deployment still needs tunnel-loss, DNS, IPv6, and mount testing.

Where to start reading the source

A useful reading order is:

  1. cmd/pvm/main.go: CLI commands and WSL backend dispatch.
  2. internal/daemon/daemon.go: API handlers, recovery, and readiness.
  3. internal/vm/vm.go and internal/qemu/argv.go: turning a VM spec into qcow2 and QEMU arguments.
  4. internal/gateway/gateway.go and internal/gateway/exittun.go: DNS, policy routing, and the kill switch.
  5. internal/exit/server.go: VPS control plane, peer enrollment, and tunnel startup.
  6. SECURITY.md: trust boundaries before evaluating a feature against your threat model.

The interesting part is not the number of commands in one CLI. It is the decision to model development VMs, network exits, and failure recovery as one system: persist state first, keep side effects rollbackable, model readiness in stages, and drop packets when the tunnel is gone. For a personal homelab, that is closer to maintainable infrastructure than another proxy toggle.


Comments