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

이는 호스트가 최근 VM과 통신하여 해당 IP 주소가 ARP 캐시에 있는 경우 "pollyanna"라는 VM 게스트에 적용될 수 있습니다.

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에서 설명한 해결 방법을 수행하거나, VM MAC의 IP 할당을 위해 DHCP를 모니터링하거나, VM 내부 구성의 세부 정보를 보고하는 에이전트(예: ovirt-guest-agent)를 VM 내부에 설치할 수 있습니다. 경험상 브리지된 네트워크를 사용하면 가상 머신은 일반적으로 해당 네트워크의 다른 물리적 머신처럼 작동하고 관리됩니다.

답변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

관련 정보