Tuesday, 22 March 2011

How to access the QEMU monitor through libvirt

It is sometimes useful to issue QEMU monitor commands to VMs managed by libvirt. Since libvirt takes control of the monitor socket it is not possible to interact with the QEMU monitor in the same way as when running QEMU or KVM manually.

Daniel Berrange shared the following techniques on IRC a while back. It is actually pretty easy to get at the QEMU monitor even while libvirt is managing the VM:

Method 1: virsh qemu-monitor-command


There is a virsh command available in libvirt ≥0.8.8 that allows you to access the QEMU monitor through virsh:

virsh qemu-monitor-command --hmp <domain> '<command> [...]'

Method 2: Connecting directly to the monitor socket


On older libvirt versions the only option is shutting down libvirt, using the monitor socket directly, and then restarting libvirt:

sudo service libvirt-bin stop  # or "libvirtd" on Red Hat-based distros
sudo nc -U /var/lib/libvirt/qemu/<domain>.monitor
...
sudo service libvirt-bin start

Either way works fine. I hope this is useful for folks troubleshooting QEMU or KVM. In the future I will post more libvirt tips :).

Update: Daniel Berrange adds that using the QEMU monitor essentially voids your libvirt warranty :). Try to only use query commands like info qtree rather than commands that change the state of QEMU like adding/removing devices.

5 comments:

  1. Thanks, this was really useful. In fact IMHO there *are* valid use cases for changing QEMU state in a way which should not void the libvirt warranty, such as setting up TCP port redirects from the host to the guest. I have blogged about this here: http://blog.adamspiers.org/2012/01/23/port-redirection-from-kvm-host-to-guest/

    ReplyDelete
  2. Thanks Stefan, this really helped me out.

    ReplyDelete
  3. thanks!
    http://ubuntuforums.org/showthread.php?t=1933391

    ReplyDelete
  4. Hi Stefan,
    Thanks for this sharing.
    I have a hvm guest running on Xen,
    and I need to dump its memory from the host command line.
    So I've applied method 1 with command:
    # virsh qemu-monitor-command --hmp winxp "memsave 0 536870912 mem.dump"
    But the error message returned:
    "this function is not supported by the connection driver: virDomainQemuMonitorCommand"
    I've tried other monitor commands such as info version etc., and results is the same.
    The libvirt installed is 0.9.12 and Xen is 4.0.3
    Could you give me some suggestion?
    Thanks!

    ReplyDelete
  5. You can also use qmp-shell to connect to the qemu monitor socket file

    ReplyDelete