aev-vm 0.1.0

Manage QEMU virtual machines in Rust and open their consoles in Aen
Documentation
# Architecture

## Boundaries

KnottVM deliberately separates trusted local control, guest-facing protocol
decoding, and display policy.

1. `manager` owns definitions, owner-only paths, lifecycle locks, QEMU launch,
   rollback, and offline image operations.
2. `qemu` converts a validated definition into an argument vector without a
   shell. QEMU/KVM provides CPU virtualization and the device model.
3. `qmp` negotiates QEMU Machine Protocol on a private Unix socket. Status,
   pause, resume, reset, memory ballooning, framebuffer capture, ACPI shutdown,
   and quit never rely on parsing process output.
4. `qga` sends bounded JSON commands to an optional agent installed inside the
   guest through a private QEMU virtio-serial proxy socket.
5. `rfb` negotiates RFB 3.3/3.8 on a separate private Unix socket. Only raw
   XRGB-compatible rectangles and desktop resize are requested.
6. `console` uses the native `aen-window` SDK to submit bounded opaque BGRA
   frames and receive configure, input, acknowledgement and close events.

KnottVM does not link KnottCompositor's renderer, DRM or input crates. The
native Aen application window socket is the display boundary; KnottCompositor
owns desktop policy, focus, placement and physical presentation. The SDK is
vendored from `KnottCompositor/crates/aen-window` for standalone builds.

## Startup

1. Resolve and secure XDG roots. Validate every runtime ancestor; when
   XDG_RUNTIME_DIR is absent, atomically create the per-user fallback beneath
   sticky `/tmp`.
2. Lock the VM operation file.
3. Validate schema, paths, firmware, accelerator, and disk state.
4. Copy a per-VM OVMF variable template when needed. Secure UEFI additionally
   selects SMM-capable code and protected pflash on Q35.
5. Remove only known stale files after proving no live QEMU or software TPM owns
   them. When TPM 2 is enabled, start the per-VM `swtpm` daemon first and wait
   for its private command and control sockets.
6. Launch QEMU with no default devices, QEMU's seccomp sandbox, private QMP/RFB
   endpoints, virtio storage/network/RNG/balloon/9p, an absolute USB tablet,
   and any explicitly configured host USB devices.
7. Wait for PID, QMP, and RFB endpoints.
8. Atomically publish runtime state. Any post-launch failure requests QMP quit,
   stops swtpm only after QEMU is confirmed stopped, and removes only known
   KnottVM-owned runtime metadata.

## Console Loop

The RFB decoder blocks on its own reader thread. The native Aen transport
worker progresses independently of rendering. The console owner handles input,
guest state and frame production.

- Guest updates enter a bounded canonical framebuffer.
- The newest framebuffer is aspect-fitted into an opaque BGRA native frame.
- One outstanding frame credit prevents unbounded upload queues.
- Exact configure revisions reject stale frames; discarded frames stay dirty.
- Pointer coordinates follow the adopted frame's letterbox geometry during
  resizes, until a replacement frame is accepted.
- Keyboard events use compositor keysyms, preserving the original press symbol
  for matching releases when modifiers change.
- Focus loss, close and disconnect release guest keys/buttons.

The owner waits on native Aen and RFB event descriptors instead of polling.
The RFB transport retains its own timer, and nonblocking submission backpressure
preserves dirty state for the next frame credit.

## Failure Invariants

- A malformed or oversized RFB message cannot allocate more than 256 MiB.
- A native configure cannot allocate a frame above 8192 pixels per dimension or
  32 MiB total. The SDK permits only one outstanding frame per window.
- A stale PID is never signaled directly. QMP socket ownership is required for
  destructive process control.
- Runtime directories have an owner-only leaf and no replaceable cross-user
  ancestor; an unsafe XDG path or preexisting writable fallback fails closed.
- A live but unreachable QMP process blocks restart and cleanup instead of
  becoming an untracked daemon.
- Snapshots cannot mutate the system disk while QMP reports a running VM.
- Internal snapshots cover only the qcow2 disk. Creation and reversion are
  rejected for software-TPM VMs until disk and TPM state can be paired; portable
  backups retain both while the VM is stopped.
- Cloning locks both names in deterministic order, requires the source to be
  stopped, creates a full independent qcow2 image, and publishes the new
  definition last. A clone keeps the optional TPM setting but starts with an
  empty TPM state directory and therefore a fresh TPM identity.
- A stable VM Generation ID persists across ordinary starts and changes on
  clone, restore, explicit regeneration, and snapshot rollback.
- Secure UEFI code is immutable host firmware; writable key enrollment and
  policy state remains in the cloned and backed-up per-VM variable image.
- User paths are passed as individual arguments or JSON block-node values, not
  interpolated into a shell command.
- Media changes, disk growth, port-forward changes, USB assignments, and host
  shares require a stopped VM and replace the strict TOML definition atomically.
- USB selectors are exact validated Linux bus/address pairs. They are cleared
  from clones and portable backups, and device-open failures abort startup.
- Host share paths are canonicalized, use bounded safe mount tags, default to
  read-only mapped-xattr 9p, and are cleared from clones and portable backups.
- Automatic TCG fallback occurs only for recognizable KVM initialization
  failures, never for device-model or host-resource errors.
- Runtime balloon targets stay between 256 MiB and configured boot memory;
  QEMU reports the asynchronous guest-cooperative result.
- Framebuffer capture uses a fixed private staging path, validates bounded PPM
  output, and exclusively creates rather than replaces the destination.
- Compute, accelerator, network, and PipeWire audio settings use the same
  stopped-VM atomic update path.
- Backup manifests bind fixed regular-file artifacts and bounded flat software
  TPM state files to exact sizes and SHA-256 digests; restore verifies hashes
  and qcow2 structure before publishing a destination definition.
- Deletion removes the definition first, stops both managed processes, then
  removes only known managed regular files; external disks and unknown files
  remain untouched.

## Extension Points

Near-term additions should preserve these boundaries:

- SPICE or virtio-gpu display transports can implement a new guest-display
  module while retaining the native Aen presenter.
- A GPU presenter can replace CPU frame uploads when KnottCompositor has real DMA-BUF import
  and explicit synchronization.
- PCI passthrough, tap networking, and host-managed Secure Boot enrollment
  belong in the versioned VM definition and QEMU command builder.
- Cross-client clipboard uses native KnottCompositor policy. KnottVM copies
  bounded guest text through its focused Aen window scope; host-to-guest paste
  remains an unsafe opt-in until focus-generation fencing covers final delivery.
- Relative mouse capture requires native relative-motion and capture
  ownership from KnottCompositor.