Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libvirt: fetch ipv4 address from guest

Tags:

qemu

kvm

libvirt

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

like image 318
Jochen Avatar asked Sep 27 '13 18:09

Jochen


People also ask

How do I find the IP address of my VM?

Checking an IP Address in VMware vSphere ClientGo to Hosts and Clusters, select the needed VM by name and check the Summary tab. The VMware IP addresses of the virtual machine are displayed in the IP addresses section. One VM can have multiple virtual network adapters and multiple IP addresses.

What is Virsh in Linux?

The virsh command allows you to manage VMs interactively or in batch. It's also helpful for controlling VMs from the Linux shell and integrates with scripts or automation tools.

How do I log into VM with Virsh?

Open a shell prompt or login using ssh. Login to a host server called server1. Use the virsh console command to log in to a running VM called 'centos7' type: virsh console centos7. To exit a virsh console session, type CTRL + Shift followed by ] .


2 Answers

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 ~]# 
like image 193
Peter Lemenkov Avatar answered Oct 01 '22 04:10

Peter Lemenkov


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)
like image 24
goneri Avatar answered Oct 01 '22 02:10

goneri