libvirt / kvm - 如何获取桥接网络上虚拟机的 IP 地址?

libvirt / kvm - 如何获取桥接网络上虚拟机的 IP 地址?

我想从 libvirt / KVM 主机获取通过桥接适配器连接到网络的正在运行的虚拟机的 IP 地址。

重要提示:我知道当虚拟机通过 libvirt / kvm 默认 NAT 网络连接时如何获取 IP 地址 - 这就是互联网上针对此问题的大多数解决方案所描述的。

但是我看不出有什么办法可以找到通过网桥连接的虚拟机的 IP 地址。

从虚拟机内部:

root@zvm:~# ifconfig -a
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.133  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5054:ff:fed0:1e27  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:d0:1e:27  txqueuelen 1000  (Ethernet)
        RX packets 36  bytes 18540 (18.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37  bytes 4128 (4.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 334  bytes 24670 (24.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 334  bytes 24670 (24.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

网络正在运行:

root@zvm:~# curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.au/?gfe_rd=cr&amp;dcr=0&amp;ei=u-uJWqTyJPLc8weU9JHABg">here</A>.
</BODY></HTML>
root@zvm:~# curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.au/?gfe_rd=cr&amp;dcr=0&amp;ei=ge6JWv_0Co7r8wf6gKGwBQ">here</A>.
</BODY></HTML>
root@zvm:~# 

从虚拟机主机:

(venv2.7) ubuntu@kvmhost:/opt/foo$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.35           ether   52:54:00:61:1b:f2   C                     virbr0
192.168.122.210                  (incomplete)                              virbr0
192.168.1.1              ether   30:b5:c2:08:91:e3   C                     br0
192.168.122.10           ether   52:54:00:ee:c8:2a   C                     virbr0
192.168.122.80           ether   52:54:00:18:93:6e   C                     virbr0
192.168.1.126            ether   ac:87:a3:00:b1:63   C                     br0
192.168.122.21           ether   52:54:00:5f:3a:70   C                     virbr0
192.168.122.182                  (incomplete)                              virbr0
192.168.1.1                      (incomplete)                              enp3s0
192.168.122.32                   (incomplete)                              virbr0
192.168.122.112          ether   52:54:00:82:7d:75   C                     virbr0
192.168.122.183          ether   52:54:00:a4:eb:ba   C                     virbr0
192.168.122.36           ether   52:54:00:b1:e4:83   C                     virbr0
192.168.122.139                  (incomplete)                              virbr0
192.168.122.209                  (incomplete)                              virbr0
192.168.1.140                    (incomplete)                              br0
(venv2.7) ubuntu@kvmhost:/opt/foo$

如果配置:

(venv2.7) ubuntu@kvmhost:/opt/foo$ ifconfig -a
br0       Link encap:Ethernet  HWaddr d2:d6:16:58:19:e0
          inet addr:192.168.1.139  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::d0d6:16ff:fe58:19e0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:576693 errors:0 dropped:0 overruns:0 frame:0
          TX packets:381883 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:399735319 (399.7 MB)  TX bytes:39484740 (39.4 MB)

enp3s0    Link encap:Ethernet  HWaddr fc:aa:14:a9:95:bf
          inet addr:192.168.1.140  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:577654 errors:0 dropped:153 overruns:0 frame:0
          TX packets:384437 errors:1 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:407978078 (407.9 MB)  TX bytes:39670335 (39.6 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:814 errors:0 dropped:0 overruns:0 frame:0
          TX packets:814 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:168110 (168.1 KB)  TX bytes:168110 (168.1 KB)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:22:5c:7b
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:901 errors:0 dropped:0 overruns:0 frame:0
          TX packets:536 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:76426 (76.4 KB)  TX bytes:175557 (175.5 KB)

virbr0-nic Link encap:Ethernet  HWaddr 52:54:00:22:5c:7b
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vnet0     Link encap:Ethernet  HWaddr fe:54:00:d0:1e:27
          inet6 addr: fe80::fc54:ff:fed0:1e27/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:82 errors:0 dropped:0 overruns:0 frame:0
          TX packets:325 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7776 (7.7 KB)  TX bytes:69359 (69.3 KB)

wlp2s0    Link encap:Ethernet  HWaddr d0:7e:35:69:cf:b2
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

(venv2.7) ubuntu@kvmhost:/opt/foo$

来自主机的虚拟机的 XML 转储:

(venv2.7) ubuntu@kvmhost:/opt/foo$ virsh dumpxml zvm
<domain type='kvm' id='41'>
  <name>zvm</name>
  <uuid>4e544a34-5a31-43ac-a2af-dbac612110d4</uuid>
  <metadata>
    <kvirt:info xmlns:kvirt="kvirt">
      <kvirt:creationdate>18-02-2018 21:05</kvirt:creationdate>
      <kvirt:profile>zesty</kvirt:profile>
      <kvirt:template>zesty-server-cloudimg-amd64.img</kvirt:template>
      <kvirt:plan>kvirt</kvirt:plan>
    </kvirt:info>
  </metadata>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-2.5'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
    <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Westmere</model>
    <feature policy='require' name='vmx'/>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/zvm_1.img'/>
      <backingStore type='file' index='1'>
        <format type='qcow2'/>
        <source file='/var/lib/libvirt/images/zesty-server-cloudimg-amd64.img'/>
        <backingStore/>
      </backingStore>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/zvm.ISO'/>
      <backingStore/>
      <target dev='hdd' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-1'/>
      <address type='drive' controller='0' bus='1' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:d0:1e:27'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/2'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/2'>
      <source path='/dev/pts/2'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='spice' port='5900' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-4e544a34-5a31-43ac-a2af-dbac612110d4</label>
    <imagelabel>libvirt-4e544a34-5a31-43ac-a2af-dbac612110d4</imagelabel>
  </seclabel>
</domain>

(venv2.7) ubuntu@kvmhost:/opt/foo$

答案1

如果主机最近与虚拟机进行过通信,则这可能适用于名为“pollyanna”的虚拟机来宾,因此其 IP 地址位于 ARP 缓存中:

arp -na | awk -v mac=$(virsh domiflist pollyanna | awk '$2=="bridge"{print $NF}') '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}'

让我们把它分开:

# Get the MAC for a VM guest called pollyanna
vmac=$(virsh domiflist pollyanna | awk '$2=="bridge"{print $NF}')

# List the ARP cache
arp -na |

# Match the MAC on a line like "? (192.168.1.12) at 12:34:56:78:9a:bc [ether] on ethX"
# and return the IP address. The gsub() function strips the "()" characters
awk -v mac="$vmac" '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}'

否则,您可能需要扫描整个可能的 IP 地址范围,以强制将 IP 地址放入 ARP 缓存,然后重试查找:

nmap -sn '192.168.1.*'    # Ugh. Every address on the 192.168.1.0/24 network

答案2

一般来说,当您使用桥接时,虚拟机实际上不受 libvirt 的内置 dnsmasq 控制,而是从桥接网络中的 DHCP 或内部设置获取其 IP。这意味着 libvirt 无法设置或获取该 IP。

您可以执行 @roaima 上面描述的解决方法,监视 DHCP 以获取虚拟机 MAC 的 IP 分配,或者在虚拟机内安装代理,该代理将向您报告虚拟机内部配置的详细信息(例如 ovirt-guest-代理确实如此)。这里的经验法则是,通过桥接网络,虚拟机的行为和管理方式通常与该网络上的任何其他物理机一样

答案3

arp -na | grep $(virsh domiflist VM01 | tail -n +3 | awk '{print$5}' | sed '/^$/d') | cut -c 4-17

这对我来说效果很好。

要在 arp 中进行缓存,

IP=`nmap -sn '192.168.200.*' | grep 'Nmap scan' | awk '{print$5}'` ; for i in $IP ; do ping $i -c 1 ; done

相关内容