Question

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

Était-ce utile?

La solution

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 ~]# 

Autres conseils

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)

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

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

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             -
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top