I have several VMs running on Ubuntu 9.10 via KVM+libvirt. I want to be able to find out the IP address that has been assigned to each host without physically opening a physical "console" to each machine and invoking ifconfig.

Consider:

rascher@localhost:~$ virsh -c qemu:///system list --all
Connecting to uri: qemu:///system
 Id Name                 State
----------------------------------
  1 machine1          running
  2 machine2          running
  - machine3          shut off

My network configuration looks like:

<network>
  <name>default</name>
  <uuid>1be...</uuid>
  <forward mode='route' dev="eth0"/>
  <bridge name='virbr0' stp='on' forwardDelay='0' />
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254' />
    </dhcp>
  </ip>
</network>

So how can I get a listing which says:

machine1 IP address = 192.168.122.16
machine2 IP address = 192.168.122.238
...

I played with arp:

rascher@localhost:~$ arp
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.238          ether   00:16:36:00:61:b0   C                     virbr0
192.168.122.16           ether   00:16:36:52:e8:9c   C                     virbr0
...

But this doesn't map to a virtual machine's ID.

Is there some tool (via the command line, virsh or virt-*) I can ascertain this information? Or do I need to have some fancy script which runs on each individual VM, checks its own IP, and reports it back to the host OS?

link|improve this question

feedback

3 Answers

libvirt uses dnsmasq to provide DHCP to the guests, so you could trawl /var/log/daemon.log or dig through the leases file in /var/lib/libvirt to get an IP to hostname mapping.

link|improve this answer
feedback

I had the same problem so I created the following script:

#!/bin/bash



function showMAC(){
    virsh dumpxml ${1}|grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"
}

function showIP(){
    for mac in $($0 -m $1); do
        grep $mac /var/log/daemon.log | tail -n 1 | awk '{print $7}'
    done
}

if test -z "${1}"; then
    echo "Usage: ${0} [-i | -m] <domain>"
    echo "  -i   Show IP address (the default)."
    echo "  -m   Show MAC address."
    exit
fi

addr_type="-i"

if test ${1} = "-i" || test ${1} = "-m"; then
    addr_type=${1}
    shift
fi

domain=${1}

test $addr_type = "-i" && showIP $domain || showMAC $domain
link|improve this answer
feedback
up vote 0 down vote accepted

So, when investigating this, I found that libvirt uses dnsmasq in order to do DHCP and DNS for guest OSes.

And dnsmasq will set the hostname in the hosts's DNS table based on whatever hostname it receives from the guest.

So in accordance with these instructions and a lot of googling, I simply needed to create and add this to /etc/dhclient.conf:

send host-name "machine1"

Now, from my host OS, I can ping machine1.

Does anyone know why I need to add the trailing "." in order for the DNS entry to resolve? How can I change this?

link|improve this answer
1  
Without the trailing dot, your DNS resolver will append it's list of search domains to the hostname when it does a lookup. You could send a FQDN instead, e.g. machine1.example.com and then add example.com to your DNS search order. – James Jun 23 '10 at 22:31
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.