IPAddress for local Virtual Machines

When running Fedora as a KVM/Qemu  host for  virtual machines, you have the issue that you don’t know the IP Address for a virtual machine once you create it.  IP addresses that are assigned via

The MAC Address is in the config file saved in

/etc/libvirt/qemu/$VMNAME.xml

Once you start the virtual machine, you can fetch the IP Address from the DHCP lease file in:

/var/lib/dnsmasq/dnsmasq.leases

To correlate the two:

 

#!/bin/bash

VMNAME=$1

MAC=`cat /etc/libvirt/qemu/$VMNAME.xml |   xml2 | awk 'BEGIN{FS="="} /mac..address/ {print $2}'`

IP=`grep $MAC /var/lib/dnsmasq/dnsmasq.leases | cut -d' ' -f3`

#$VMNAME has MAC $MAC and IPAddress $IP
echo $IP

 

This must be called as root or via sudo.

UPDATE:

Chris Lalancette notes that the cannonical version of the MAC address can be found using

virsh -c qemu:///system dumpxml $VMNAME

7 thoughts on “IPAddress for local Virtual Machines

  1. The dnsmasq process will be listening on libvirt’s network interface, so as long as your VMs all send DHCP_HOSTNAME, you can just query it directly (no root required):

    dig +short @192.168.122.1 $VMNAME

  2. I think that leads to a Chicken/Egg problem. For a local VM, the DNS is managed by DNS masq, which reads from /etc/hosts. You don’t know what IP Address to put into /ect/hosts since it is served out of DHCP.

    The VMs don’t seem to be sending DHCP_HOSTNAME, but that might be possible to setup in a config option prior to launching the VM.

  3. I meant that dnsmasq will also resolve names from it’s own DHCP leases, so you don’t really need to add anything to /etc/hosts (unless that’s the goal of your exercise…)

  4. It looks like I can get it working by modifying the file
    /etc/dhclient-eth0.conf

    send fqdn.fqdn “ipa-server-3.ayoung.boston.devel.redhat.com.”;
    send fqdn.encoded on;
    send fqdn.server-update off;
    also request fqdn, dhcp6.fqdn;

    Since I already modify this file for the resolve.conf changes, I don’t think this is a problem. If that works, It will be a much better solution. Thanks, Josh. I’ll update when I can test if this works.

  5. Adam,

    I noticed somewhere (may be in one of Rich’s posts) that you could use the below command on the host to get the IP of your guest.

    #virt-cat Fedora15tbox /var/log/messages | grep ‘dhclient.*bound to’

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.