使用 KVM+libvirt 获取 DHCP 客户端列表?

ras*_*her 11 virtualization libvirt kvm-virtualization

我有几个通过 KVM+libvirt 在 Ubuntu 9.10 上运行的虚拟机。我希望能够找出分配给每台主机的 IP 地址,而无需为每台机器打开物理“控制台”并调用ifconfig.

考虑:

rascher@localhost:~$ virsh -c qemu:///system list --all
连接到uri:qemu:///system
 ID 名称 状态
----------------------------------
  1 台机器 1 正在运行
  2 台机器 2 正在运行
  - machine3 关闭

我的网络配置如下:

<network>
  <name>default</name>
  <uuid>1be...</uuid>
  <forward mode='route' dev="eth0"/>
  <bridge name='virbr0' stp='on' forwardDelay='0' />
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254' />
    </dhcp>
  </ip>
</network>
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能得到一个清单,上面写着:

machine1 IP 地址 = 192.168.122.16
machine2 IP 地址 = 192.168.122.238
...

我玩过arp

rascher@localhost:~$ arp
地址 HWtype HWaddress Flags 掩码接口
192.168.122.238 以太 00:16:36:00:61:b0 C virbr0
192.168.122.16 以太 00:16:36:52:e8:9c C virbr0
...

但这不会映射到虚拟机的 ID。

是否有一些工具(通过命令行,virshvirt-*)我可以确定这些信息?或者我是否需要一些花哨的脚本来运行在每个单独的 VM 上,检查自己的 IP,并将其报告给主机操作系统?

Neh*_*ani 10

很久以前就要求使用此功能。现在 libvirt 通过提供两个新命令来支持它:domifaddrnet-dhcp- leases

 Usage: domifaddr <domain> [interface] [--full] [--source lease|agent]

 Example outputs:
 virsh # domifaddr f20 --source agent
 Name       MAC address          Protocol     Address
 -------------------------------------------------------------------------------
 lo         00:00:00:00:00:00    ipv4         127.0.0.1/8
 -          -                    ipv6         ::1/128
 eth0       52:54:00:2e:45:ce    ipv4         10.1.33.188/24
 -          -                    ipv6         2001:db8:0:f101::2/64
 -          -                    ipv6         fe80::5054:ff:fe2e:45ce/64
 eth1       52:54:00:b1:70:19    ipv4         192.168.105.201/16
 -          -                    ipv4         192.168.201.195/16
 -          -                    ipv6         2001:db8:ca2:2:1::bd/128
 eth2       52:54:00:36:2a:e5    N/A          N/A
 eth3       52:54:00:20:70:3d    ipv4         192.168.105.240/16
 -          -                    ipv6         fe80::5054:ff:fe20:703d/64

 virsh # domifaddr f20 --full
 Name       MAC address          Protocol     Address
 -------------------------------------------------------------------------------
 vnet0      52:54:00:2e:45:ce    ipv6         2001:db8:0:f101::2/64
 vnet1      52:54:00:b1:70:19    ipv4         192.168.105.201/16
 vnet1      52:54:00:b1:70:19    ipv6         2001:db8:ca2:2:1::bd/128
 vnet3      52:54:00:20:70:3d    ipv4         192.168.105.240/16

 virsh # domifaddr f20 eth0 --source agent --full
 Name       MAC address          Protocol     Address
 -------------------------------------------------------------------------------
 eth0       52:54:00:2e:45:ce    ipv4         10.1.33.188/24
 eth0       52:54:00:2e:45:ce    ipv6         2001:db8:0:f101::2/128
 eth0       52:54:00:2e:45:ce    ipv6         fe80::5054:ff:fe2e:45ce/64

For eth0, ipv6 is managed by libvirt, but ipv4 is not.
For eth1, the second IP is created using ip aliasing.
For eth2, there is no IP configured as of yet.
For eth3, only ipv4 has been configured.
fd00::/8 are private ipv6 ranges. Hence not visible through --source lease
Run Code Online (Sandbox Code Playgroud)

在不同的场景中:

 Example Usage: net-dhcp-leases <network> [mac]

 virsh # net-dhcp-leases --network default6
 Expiry Time          MAC address        Protocol  IP address                Hostname        Client ID or DUID
 -------------------------------------------------------------------------------------------------------------------
 2014-06-16 03:40:14  52:54:00:85:90:e2  ipv4      192.168.150.231/24        fedora20-test   01:52:54:00:85:90:e2
 2014-06-16 03:40:17  52:54:00:85:90:e2  ipv6      2001:db8:ca2:2:1::c0/64   fedora20-test   00:04:b1:d8:86:42:e1:6a:aa:cf:d5:86:94:23:6f:94:04:cd
 2014-06-16 03:34:42  52:54:00:e8:73:eb  ipv4      192.168.150.181/24        ubuntu14-vm     -
 2014-06-16 03:34:46  52:54:00:e8:73:eb  ipv6      2001:db8:ca2:2:1::5b/64   -               00:01:00:01:1b:30:c6:aa:52:54:00:e8:73:eb
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

libvirt 使用 dnsmasq 为来宾提供 DHCP,因此您可以拖网 /var/log/daemon.log 或挖掘 /var/lib/libvirt 中的租用文件以获取 IP 到主机名的映射。