18

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).

Jochen
  • 1,853
  • 3
  • 20
  • 28

5 Answers5

34

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 ~]# 
Peter Lemenkov
  • 1,214
  • 11
  • 5
  • thats nice solution, but wouldnt work exactly in busybox, --------> kvm # for mac in `virsh domiflist yoc6 |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 grep: invalid option -- 'P' arp: invalid option -- 'e' BusyBox v1.22.1 (2015-11-04 15:02:18 PST)BusyBox v1.22.1 (2015-11-04 15:02:18 PST) multi-call binary. Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]... – ravi.zombie Nov 09 '15 at 19:43
  • 3
    keep in mind that the arp table could be stale or be missing entries. you might want to ping sweep the subnet prior to calling the arp command. – Trevor Merrifield May 23 '17 at 14:11
22

You can just read the lease file:

# cat /var/lib/libvirt/dnsmasq/default.leases 
1381437666 52:54:00:98:75:eb 192.168.122.240 chat *
1381437643 52:54:00:dc:ee:f8 192.168.122.112 burp *

Or, even better, use the net-dhcp-leases command from virsh:

virsh # net-dhcp-leases nat --help
  NAME
    net-dhcp-leases - print lease info for a given network

  SYNOPSIS
    net-dhcp-leases <network> [<mac>]

  DESCRIPTION
    Print lease info for a given network

  OPTIONS
    [--network] <string>  network name or uuid
    [--mac] <string>  MAC address

Or in Python:

import libvirt
conn = libvirt.open('qemu+ssh://root@localhost/system')
for lease in conn.networkLookupByName("my_network").DHCPLeases():
    print(lease)
goneri
  • 1,545
  • 1
  • 9
  • 7
  • 6
    There is a new addition now: domifaddr (http://libvirt.org/git/?p=libvirt.git;a=commit;h=2f36e6944e6eb56a00e19fcd85ec8513461597c9) – Nehal J Wani Mar 17 '15 at 23:52
10

This works on Ubuntu 16.04 host with a CentOS 7 guest running for me.

$ virsh list
 Id    Name                           State
----------------------------------------------------
 5     centos7.0                      running

$ virsh domifaddr centos7.0
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:a1:28:e5    ipv4         192.168.122.15/24
edwinksl
  • 7,005
  • 4
  • 30
  • 36
Deepak Vilakkat
  • 101
  • 1
  • 3
1

There are, in general, two options:

  1. install into the guest OS a guest agent for your hypervisor and use hypervisor-specific API to query the guest machine for the IP
  2. rely on information available on your local network - I'm using arp-scan for that

more specific info for VirtualBox or libvirt+QEMU

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
1

None of the suggestions worked in my case. Here is how I got the guest IP.

Get the network name:

$ virsh net-list

Name                 State      Autostart     Persistent
----------------------------------------------------------
default              active     yes           yes

Then:

$ virsh net-dhcp-leases default

Expiry Time          MAC address        Protocol  IP address                Hostname        Client ID or DUID
-------------------------------------------------------------------------------------------------------------
2018-11-06 15:47:33  52:54:00:1b:ee:f6  ipv4      192.168.122.6/24          vm1             -
2018-11-06 15:36:58  52:54:00:d7:1c:2e  ipv4      192.168.122.4/24          vm2             -
McMutton
  • 905
  • 13
  • 25