Qemu Guest Agent Integration

From stoney cloud
Jump to: navigation, search


The Qemu Guest Agent permits access to a VM via a virtual serial socket. This has the advantage that the VM can be accessed via the VM node (hypervisor) without network connection or remote desktop protocol.

This is a requirement to be able to take disk-only-snapshots without downtime by issueing a disk-freeze (shadow-copy) within the guest prior to taking the snapshot.

Furthermore it is useful for orchestrating CPU and memory hotplugging:

  • guest OS must be told to enable CPU/memory after hot-plug
  • guest OS must release a CPU/memory prior to hot-unplug


The XML must be amended by the following XML snippet in the devices section:

<channel type="unix">
  <source mode="bind"/>
  <target type="virtio" name="org.qemu.guest_agent.0"/>

This will create a new virtual serial device within the VM and a new socket under /var/lib/libvirt/qemu/channel/target/

Make sure that this directory exists and is protected:

mkdir -p /var/lib/libvirt/qemu/channel/target/
chown root:qemu /var/lib/libvirt/qemu/channel/target/
chmod 770 /var/lib/libvirt/qemu/channel/target/

The sockets are named /var/lib/libvirt/qemu/channel/target/${VMNAME}.org.qemu.guest_agent.0.

Please note: since libvirt will automatically listen on that socket one can not use a tool like qemu-ga-client to do anything on it, everything has to go via libvirt.


In the VM one must install and start the qemu-guest-agent.

On Gentoo this means:

emerge qemu-guest-agent
rc-update add qemu-guest-agent default


See the links for an example on how to read a file (it is non-trivial and the returned content is base64-encoded, so it must be scripted and there is no point in duplicating it here).

Shutdown a VM via qemu-ga

The advantage of using qemu-ga instead of the ACPI-based mechanism to shutdown a VM is that you get a confirmation if the qemu-ga was able to issue shutdown -P within the VM whereas with ACPI you won't even know whether the guest OS has received the event.

virsh shutdown --mode agent $VMNAME


~ # virsh shutdown --mode agent bc58f697-1f21-4613-9f4d-469cdaff0621Domain bc58f697-1f21-4613-9f4d-469cdaff0621 is being shutdown

Manually ping qemu-ga via libvirt

Libvirt issues a guest-sync before any other command as a way to ensure availability of qemu-ga. Nevertheless it may be useful to run commands directly, like the guest-ping.

virsh qemu-agent-command $VMNAME '{"execute":"guest-ping"}'


~ # virsh qemu-agent-command bc58f697-1f21-4613-9f4d-469cdaff0621 '{"execute":"guest-ping"}'


Getting list of available qemu-ga commands

virsh qemu-agent-command ${VMNAME} '{"execute":"guest-info"}'


virsh qemu-agent-command ${VMNAME} '{"execute":"guest-info"}'

The return values will look something like: