libvirt:从来宾获取ipv4地址 [英] libvirt: fetch ipv4 address from guest

查看:66
本文介绍了libvirt:从来宾获取ipv4地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,以使用libvirt来获取在qemu/kvm上运行的VM的ipv4地址或其他元数据?我也研究了ovirt客户代理和qemu客户代理,但是我想知道是否有更好/更简便的方法来获取此数据?

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?

基本上,我有几个运行KVM的主机,对于每个特定的私有IP地址,我需要能够知道哪个IP地址正在运行哪个VM(由DHCP服务器提供).

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

推荐答案

我在每个VM上都安装了avahi,因此它们将公布自己的地址.但这不是唯一可用的选项(特别是如果您的VM包含与Linux不同的内容).因此,进入virsh选项的神奇世界!

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!

*)首先,您需要获取VM的NIC的MAC地址:

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

*)现在让我们看一下ARP表

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

*)现在,将所有内容粘合在一起(并添加一些shell/regex魔术):

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

这篇关于libvirt:从来宾获取ipv4地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆