Tag Archives: virsh

Tip: Wake up a guest from screen blank

A few years ago Dan Berrange added a way to send fake keyboard events to libvirt guests. You can use this to inject just a press on the Left Shift key to wake up a guest from screen blank. Very useful if you need to take a screenshot!

$ virsh send-key guest KEY_LEFTSHIFT
$ sleep 1
$ virsh screenshot guest /tmp/screenshot.ppm

Update: A word of warning though. If you try this for Windows guests you’ll hit this message:


The solution is to hit other keys randomly. Grrr.

Leave a comment

Filed under Uncategorized

Tip: Find the IP address of a virtual machine

Update: see comments

Virtual machines are given a virtual network, but have to decide on their own IP address, which they would usually get from DHCP or by static assignment. This is analogous to the physical case: a physical machine is plugged into a network port, but has to choose its own IP address somehow.

The problem with this is how do you know what IP address the virtual machine has picked?

There are several methods, but by far the simplest is to look at the output of the arp command on the same network segment (usually, just running arp -an on the host will do). As long as the VM has sent anything over the network, it will have sent out its IP address in an ARP response, and the host will have picked that up and added it to the ARP table.

For example, the one virtual machine I am running now has broadcast its IP address to the host:

$ arp -an
? (192.168.0.**) at **:**:**:23:06:bb [ether] on eth0
? (192.168.0.**) at **:**:**:74:02:28 [ether] on eth0
? ( at 52:54:00:18:04:63 [ether] on virbr0
? (192.168.0.**) at **:**:**:7c:8b:7e [ether] on eth0

I already described this and it works well if you have one or a few virtual machines, but if you have many, how do you know which MAC address corresponds to the one you want?

libvirt knows the MAC address which was assigned to each virtual machine’s network card. It is in the libvirt XML for the domain, and you can get it from libvirt using the command virsh dumpxml.

If we put all this together, we can write a short script which queries libvirt for the MAC address, then looks that up in the ARP table. All you need to do is to write:

# virt-addr F14x64

Note: As it stands this would only work as root, but you can make it also work for non-root by setting this environment variable:

$ export LIBVIRT_DEFAULT_URI=qemu:///system
$ virt-addr F14x64
$ ssh $(virt-addr F14x64)
rjones@'s password: ***
Last login: Sat Oct 16 16:56:17 2010 from
[rjones@f14x64 ~]$ 

The script follows below.

#!/usr/bin/perl -w

use strict;
use XML::XPath;
use XML::XPath::XMLParser;
use Sys::Virt;

# Open libvirt connection and get the domain.
my $conn = Sys::Virt->new (readonly => 1);
my $dom = $conn->get_domain_by_name ($ARGV[0]);

# Get the libvirt XML for the domain.
my $xml = $dom->get_xml_description ();

# Parse out the MAC addresses using an XPath expression.
my $xp = XML::XPath->new (xml => $xml);
my $nodes = $xp->find ("//devices/interface[\@type='network']/mac/\@address");
my $node;
my @mac_addrs;
foreach $node ($nodes->get_nodelist) {
    push @mac_addrs, lc ($node->getData)

# Look up the MAC addresses in the output of 'arp -an'.
my @arp_lines = split /\n/, `arp -an`;
foreach (@arp_lines) {
    if (/\((.*?)\) at (.*?) /) {
        my $this_addr = lc $2;
        if (list_member ($this_addr, @mac_addrs)) {
            print "$1\n";

sub list_member
    local $_;
    my $item = shift;
    foreach (@_) {
        return 1 if $item eq $_;
    return 0;


Filed under Uncategorized

virt-tools.org: Install a VM using the command line

Read our guide to installing virtual machines using the command line tools.

Leave a comment

Filed under Uncategorized

virt-tools.org: Listing VMs using “virsh”

Read our guide to listing out virtual machines using the command line or from shell scripts.


Filed under Uncategorized