I am looking for a solution to fetch the ipv4 address or other metadata of a VM running on qemu/kvm with libvirt? I've also looked into ovirt guest agent and qemu guest agent, but I was wondering if there's a better/easier way to fetch this data?
Basically I have a couple of hosts running KVM and for each specific private ip address I need to be able to know which VM is running with that ip address (provided by a DHCP server).
I'm installing avahi on each VM, so they will advertise their own addresses. However that's not the only option available (especiall if you VM contains something different from Linux). So enter magical world of virsh options!
*) First you need to get MAC addresses of your VM's NICs:
[root@5844 ~]# virsh domiflist b2bua
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network default virtio 52:54:00:aa:bb:cc
vnet1 bridge br1 virtio 52:54:00:dd:ee:ff
[root@5844 ~]#
*) Now let's take a look at the ARP table
[root@5844 ~]# arp -e
Address HWtype HWaddress Flags Mask Iface
xx.xx.xx.xx ether xx:xx:xx:xx:xx:xx C br0
192.168.122.14 ether 52:54:00:xx:xx:xx C virbr0
192.168.122.51 ether 52:54:00:aa:bb:cc C virbr0
[root@5844 ~]#
*) Now let's glue everything together (and adding a bit of shell/regex magic):
[root@5844 ~]# for mac in `virsh domiflist b2bua |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do arp -e |grep $mac |grep -o -P "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ; done
192.168.122.51
[root@5844 ~]#