package
0.0.0-20250212194115-ee9b0668d242
Repository: https://github.com/digitalocean/go-qemu.git
Documentation: pkg.go.dev

# README

QMP

Package qmp enables interaction with QEMU instances via the QEMU Machine Protocol (QMP).

Available Drivers

Libvirt

If your environment is managed by Libvirt, QMP interaction must be proxied through the Libvirt daemon. This can be be done through two available drivers:

RPC

The RPC driver provides a pure Go implementation of Libvirt's RPC protocol.

//conn, err := net.DialTimeout("unix", "/var/run/libvirt/libvirt-sock", 2*time.Second)
conn, err := net.DialTimeout("tcp", "192.168.1.1:16509", 2*time.Second)
monitor := libvirtrpc.New("stage-lb-1", conn)

virsh

A connection to the monitor socket is provided by proxing requests through the virsh executable.

monitor, err := qmp.NewLibvirtMonitor("qemu:///system", "stage-lb-1")

Socket

If your QEMU instances are not managed by libvirt, direct communication over its UNIX socket is available.

monitor, err := qmp.NewSocketMonitor("unix", "/var/lib/qemu/example.monitor", 2*time.Second)

Examples

Using the above to establish a new qmp.Monitor, the following examples provide a brief overview of QMP usage.

error checking omitted for the sake of brevity.

Command Execution

type StatusResult struct {
	ID     string `json:"id"`
	Return struct {
		Running    bool   `json:"running"`
		Singlestep bool   `json:"singlestep"`
		Status     string `json:"status"`
	} `json:"return"`
}

monitor.Connect()
defer monitor.Disconnect()

cmd := []byte(`{ "execute": "query-status" }`)
raw, _ := monitor.Run(cmd)

var result StatusResult
json.Unmarshal(raw, &result)

fmt.Println(result.Return.Status)
running

Event Monitor

monitor.Connect()
defer monitor.Disconnect()

stream, _ := monitor.Events()
for e := range stream {
	log.Printf("EVENT: %s", e.Event)
}

$ virsh reboot example
Domain example is being rebooted
EVENT: POWERDOWN
EVENT: SHUTDOWN
EVENT: STOP
EVENT: RESET
EVENT: RESUME
EVENT: RESET
...

More information

# Packages

Package qmptest provides types which assist in testing interactions with package qmp.
Package raw provides automatically generated QMP types based on the QMP schema.

# Functions

Listen creates a new SocketMonitor listening for a single connection to the provided socket file or address.
NewLibvirtRPCMonitor configures a new Libvirt RPC Monitor connection.
NewSocketMonitor configures a connection to the provided QEMU monitor socket.

# Variables

ErrEventsNotSupported is returned by Events() if event streams are unsupported by either QEMU or libvirt.

# Structs

Command represents a QMP command.
Event represents a QEMU QMP event.
A LibvirtRPCMonitor implements LibVirt's remote procedure call protocol.
A SocketMonitor is a Monitor which speaks directly to a QEMU Machine Protocol (QMP) socket.
Version is the QEMU version structure returned when a QMP connection is initiated.

# Interfaces

Monitor represents a QEMU Machine Protocol socket.