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.


  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/

  2. Thanks Stefan, this really helped me out.

  3. thanks!

  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?

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

  6. Thanks for the useful debugging tip!

  7. As a celebration of the incredible journeys that waders like the Little Curlew make from Australia to the Arctic and back again, BirdLife Australia is running a nationwide event called Farewell Shorebirds; the event will run from April 10th to May 10th which is, by no coincidence, World Migratory Bird Day. Melbourne Home Alarm Systems

  8. LCD monitors have an anti-reflection coating of clear film that is damaged easily by ammonia-based cleaner. Using paper towels or any wood-based product to clean your LCD screen can damage it so that it appears cloudy and blurry.
    Desktopcon's 4K Gaming Monitors

  9. Use putty telnet tool will help you connect via rs232, we use this when we program our security alarm systems perth

  10. It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. Best 240HZ Monitor 2019

  11. As you can see, the LCD provides a cheaper option while also giving what many believe to be better performance. Once again, prices vary depending on the screen size.Buzzbeasts's all in one pc