qemu

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 29 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

QEMU hypervisor

The qemu backend uses q35 on amd64 and virt on arm64. These architecture-native standard boards are selected internally and are not public API options.

QEMU ships many other machine models, including versioned compatibility aliases and hardware-emulation boards that do not fit Hypeman's guest contract. Hypeman intentionally does not mirror that open-ended list in its API. If another model eventually provides a useful, supportable capability profile, expose it as another hypervisor backend with explicit lifecycle and device guarantees rather than as an unchecked machine-type string.

Firmware boot and TPM ownership

The standard amd64 qemu profile supports UEFI firmware boot, Secure Boot variable storage, qcow2 disks, and software TPM 2.0 devices. These requirements are represented as runtime capabilities so callers can validate a guest's requirements without branching on the QEMU type name. Direct-kernel profiles reject firmware and TPM configuration.

Firmware code is immutable host input. Each instance receives its own writable NVRAM file and TPM state directory, which must be preserved together with the guest disk. Hypeman starts swtpm first because QEMU connects to its control socket as a client; launching both processes concurrently would race that required ordering. Once the socket exists, QEMU starts immediately.

QEMU and swtpm run in detached process groups and keep running if Hypeman restarts. QEMU retains its TPM control connection, and swtpm --terminate exits when that connection closes. Startup cleanup owns both processes as one cleanup stack so a partial boot cannot leave either process behind. TPM output is available through the instance logs API with source=swtpm.

qemu-microvm

The qemu-microvm backend uses QEMU's Linux amd64-only microvm board. Upstream registers this board only in the x86 system emulator; qemu-system-aarch64 does not provide an equivalent microvm machine. Hypeman uses direct kernel boot, ttyS0 serial logs, and virtio-mmio transport for disks, networking, vsock, and the optional balloon.

It cannot use PCI/VFIO/vGPU devices or hotplug memory. QEMU limits it to eight virtio-mmio devices; Hypeman counts rootfs, overlay, config disk, attached-volume disks (an overlay volume consumes two), network, vsock, and the optional balloon before starting QEMU.

A qemu or qemu-microvm standby snapshot or warm fork may restore only with the exact QEMU version that wrote its memory image. Both backends use the single system-installed binary and unversioned machine aliases, so Hypeman cannot relaunch a historical writer or promise cross-version migration compatibility. Hypeman records the running binary's version in qemu-config.json and checks it before restore (older standard-QEMU snapshots fall back to the version in instance metadata). If QEMU changes, restore a stopped snapshot with target_state: Stopped and start it normally, or recreate the instance; an instance already in Stopped state can always cold-start. A stopped snapshot may switch between qemu, qemu-microvm, and other hypervisors; the target backend determines the internal QEMU board.

Boot comparison

The opt-in, non-gating Linux/KVM benchmark scripts report p50/p95 StartedAt → ProgramStartedAt latency and VMM RSS for equivalent nginx guests:

  • ./scripts/benchmark-qemu-machine-types.sh [samples] compares QEMU q35 with QEMU microvm.
  • ./scripts/benchmark-microvm-hypervisors.sh [samples] compares QEMU microvm with Firecracker.

Documentation

Overview

Package qemu implements the hypervisor.Hypervisor interface for QEMU.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildArgs

func BuildArgs(cfg hypervisor.VMConfig) []string

BuildArgs converts hypervisor.VMConfig to command-line arguments for standard QEMU. Backend starters use buildArgs with their private machine profile.

func NewVsockDialer

func NewVsockDialer(vsockSocket string, vsockCID int64) hypervisor.VsockDialer

NewVsockDialer creates a new VsockDialer for QEMU. The vsockSocket parameter is unused for QEMU (it uses CID instead). The vsockCID is the guest's Context ID assigned via vhost-vsock-pci.

func Remove

func Remove(socketPath string)

Remove closes and removes the current client for socketPath.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps go-qemu's Domain and raw.Monitor with convenience methods.

func NewClient

func NewClient(socketPath string) (*Client, error)

NewClient creates a new QEMU client connected to the given socket.

func (*Client) Balloon added in v0.1.0

func (c *Client) Balloon(bytes int64) error

Balloon updates the guest-visible target RAM for the balloon device.

func (*Client) Close

func (c *Client) Close() error

Close disconnects from the QMP socket.

func (*Client) Continue

func (c *Client) Continue() error

Continue resumes VM execution (QMP 'cont' command).

func (*Client) Events

func (c *Client) Events() (chan qmp.Event, chan struct{}, error)

Events returns a channel for receiving QEMU events.

func (*Client) Migrate

func (c *Client) Migrate(uri string) error

Migrate initiates a migration to the given URI (typically "file:///path"). This is used for saving VM state to a file for snapshot/standby.

func (*Client) QueryBalloon added in v0.1.0

func (c *Client) QueryBalloon() (int64, error)

QueryBalloon returns the current guest-visible RAM target for the balloon device.

func (*Client) QueryMigration

func (c *Client) QueryMigration() (raw.MigrationInfo, error)

QueryMigration returns the current migration status.

func (*Client) Quit

func (c *Client) Quit() error

Quit shuts down QEMU (QMP 'quit' command).

func (*Client) Run

func (c *Client) Run(cmd qmp.Command) ([]byte, error)

Run executes a raw QMP command (for commands not yet wrapped).

func (*Client) Status

func (c *Client) Status() (qemu.Status, error)

Status returns the current VM status as a typed enum.

func (*Client) StatusInfo

func (c *Client) StatusInfo() (raw.StatusInfo, error)

StatusInfo returns detailed status information from the raw monitor.

func (*Client) Stop

func (c *Client) Stop() error

Stop pauses VM execution (QMP 'stop' command).

func (*Client) SystemPowerdown

func (c *Client) SystemPowerdown() error

SystemPowerdown sends ACPI power button event (graceful shutdown).

func (*Client) SystemReset

func (c *Client) SystemReset() error

SystemReset resets the VM (hard reset).

func (*Client) Version

func (c *Client) Version() (string, error)

Version returns the QEMU version string.

func (*Client) WaitMigration

func (c *Client) WaitMigration(ctx context.Context, timeout time.Duration) error

WaitMigration polls until an outgoing migration completes or times out. Used for snapshot/standby operations where we initiate the migration. Returns nil if migration completed successfully, error otherwise.

func (*Client) WaitVMReady

func (c *Client) WaitVMReady(ctx context.Context, timeout time.Duration) error

WaitVMReady polls until the VM is ready after an incoming migration. Used for restore operations where QEMU was started with -incoming. The VM transitions from "inmigrate" to "paused" when migration data is loaded. Returns nil when VM is ready for resume, error on timeout or failure.

type MachineType added in v0.4.0

type MachineType string

MachineType identifies a QEMU machine/board type persisted in QEMU's private restore contract. It is not part of the hypervisor-agnostic VM config.

const (
	// MachineTypeQ35 is QEMU's standard x86 board.
	MachineTypeQ35 MachineType = "q35"
	// MachineTypeVirt is QEMU's standard ARM board.
	MachineTypeVirt MachineType = "virt"
	// MachineTypeMicroVM is QEMU's minimal x86-only board.
	MachineTypeMicroVM MachineType = "microvm"
)

type MicroVMProfile added in v0.4.0

type MicroVMProfile struct{}

MicroVMProfile selects QEMU's minimal x86 microvm board and enforces its virtio-mmio device contract.

type QEMU

type QEMU struct {
	// contains filtered or unexported fields
}

QEMU implements hypervisor.Hypervisor for QEMU VMM.

func GetOrCreate

func GetOrCreate(socketPath string) (*QEMU, error)

GetOrCreate returns a standard QEMU client for the socket path, or creates a new one if none exists.

func GetOrCreateForType added in v0.4.0

func GetOrCreateForType(socketPath string, hypervisorType hypervisor.Type) (*QEMU, error)

GetOrCreateForType returns a QEMU client with the requested backend identity.

func New

func New(socketPath string) (*QEMU, error)

New returns a QEMU client for the given socket path. Uses a connection pool to ensure only one connection per socket exists.

func NewForType added in v0.4.0

func NewForType(socketPath string, hypervisorType hypervisor.Type) (*QEMU, error)

NewForType returns a QEMU client with capabilities for the selected QEMU backend.

func (*QEMU) Capabilities

func (q *QEMU) Capabilities() hypervisor.Capabilities

Capabilities returns the features supported by QEMU.

func (*QEMU) DeleteVM

func (q *QEMU) DeleteVM(ctx context.Context) error

DeleteVM removes the VM configuration from QEMU. This sends a graceful shutdown signal to the guest.

func (*QEMU) GetTargetGuestMemoryBytes added in v0.1.0

func (q *QEMU) GetTargetGuestMemoryBytes(ctx context.Context) (int64, error)

func (*QEMU) GetVMInfo

func (q *QEMU) GetVMInfo(ctx context.Context) (*hypervisor.VMInfo, error)

GetVMInfo returns current VM state.

func (*QEMU) Pause

func (q *QEMU) Pause(ctx context.Context) error

Pause suspends VM execution.

func (*QEMU) ResizeMemory

func (q *QEMU) ResizeMemory(ctx context.Context, bytes int64) error

ResizeMemory changes the VM's memory allocation. Not implemented in first pass.

func (*QEMU) ResizeMemoryAndWait

func (q *QEMU) ResizeMemoryAndWait(ctx context.Context, bytes int64, timeout time.Duration) error

ResizeMemoryAndWait changes the VM's memory allocation and waits for it to stabilize. Not implemented in first pass.

func (*QEMU) Resume

func (q *QEMU) Resume(ctx context.Context) error

Resume continues VM execution.

func (*QEMU) SetTargetGuestMemoryBytes added in v0.1.0

func (q *QEMU) SetTargetGuestMemoryBytes(ctx context.Context, bytes int64) error

func (*QEMU) Shutdown

func (q *QEMU) Shutdown(ctx context.Context) error

Shutdown stops the QEMU process.

func (*QEMU) Snapshot

func (q *QEMU) Snapshot(ctx context.Context, destPath string) error

Snapshot creates a VM snapshot using QEMU's migrate-to-file mechanism. The VM state is saved to destPath/memory file. The VM config is copied to destPath for restore (QEMU requires exact arg match).

type StandardProfile added in v0.4.0

type StandardProfile struct{}

StandardProfile selects QEMU's architecture-native general-purpose board.

type Starter

type Starter struct {
	// contains filtered or unexported fields
}

Starter applies a backend profile over the shared QEMU implementation.

func NewMicroVMStarter added in v0.4.0

func NewMicroVMStarter() *Starter

NewMicroVMStarter creates a QEMU starter using the minimal x86 microvm board.

func NewStarter

func NewStarter() *Starter

NewStarter creates a QEMU starter using the architecture-native standard board.

func (*Starter) GetBinaryPath

func (s *Starter) GetBinaryPath(p *paths.Paths, version string) (string, error)

GetBinaryPath returns the path to the QEMU binary. QEMU is expected to be installed on the system.

func (*Starter) GetVersion

func (s *Starter) GetVersion(p *paths.Paths) (string, error)

GetVersion returns the installed QEMU version.

func (*Starter) PrepareFork added in v0.0.7

PrepareFork prepares QEMU fork state by rewriting snapshot VM config when a snapshot path is provided. For stopped forks (no snapshot), this is a no-op.

func (*Starter) ResolveVersion added in v0.4.0

func (s *Starter) ResolveVersion(p *paths.Paths, requested string) (string, error)

ResolveVersion binds QEMU metadata to the one host-installed binary.

func (*Starter) RestoreVM

func (s *Starter) RestoreVM(ctx context.Context, p *paths.Paths, version string, socketPath string, snapshotPath string) (int, hypervisor.Hypervisor, error)

RestoreVM starts QEMU and restores VM state from a snapshot. The VM is in paused state after restore; caller should call Resume() to continue execution.

func (*Starter) SocketName

func (s *Starter) SocketName() string

SocketName returns the socket filename for QEMU.

func (*Starter) StartVM

func (s *Starter) StartVM(ctx context.Context, p *paths.Paths, version string, socketPath string, config hypervisor.VMConfig) (int, hypervisor.Hypervisor, error)

StartVM launches QEMU with the VM configuration and returns a Hypervisor client. QEMU receives all configuration via command-line arguments at process start.

func (*Starter) ValidateConfig added in v0.4.0

func (s *Starter) ValidateConfig(config hypervisor.VMConfig) error

ValidateConfig applies the selected profile's side-effect-free constraints.

type VsockDialer

type VsockDialer struct {
	// contains filtered or unexported fields
}

VsockDialer implements hypervisor.VsockDialer for QEMU. QEMU with vhost-vsock-pci uses the kernel's native AF_VSOCK socket family. Connections are made using the guest's CID (Context ID) and port number.

func (*VsockDialer) DialVsock

func (d *VsockDialer) DialVsock(ctx context.Context, port int) (net.Conn, error)

DialVsock connects to the guest on the specified port using AF_VSOCK. This uses the kernel's vsock infrastructure with the guest's CID.

func (*VsockDialer) Key

func (d *VsockDialer) Key() string

Key returns a unique identifier for this dialer, used for connection pooling.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL