libvirt / kvm - como obter o endereço IP de máquinas virtuais em uma rede em ponte? [fechadas]

1

Do host libvirt / KVM, quero obter o endereço IP das máquinas virtuais em execução conectadas à rede por meio de um adaptador em ponte.

Importante: Eu sei como obter o endereço IP quando a máquina virtual está conectada através da rede NAT padrão do libvirt / kvm - e é isso que a maioria das soluções na Internet descreve.

No entanto, não vejo como encontrar o endereço IP das máquinas virtuais conectadas por meio de uma ponte.

De dentro da máquina virtual:

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

A rede está funcionando:

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:~# 

Do host da máquina virtual:

(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$

ifconfig:

(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 dump da máquina virtual do host:

(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$
    
por Duke Dougal 18.02.2018 / 23:01

2 respostas

2

Isso pode funcionar para um convidado da VM chamado "pollyanna" se o host tiver se comunicado com a VM recentemente, para que seu endereço IP esteja no cache do ARP:

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

Vamos dividir isso:

# 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}'

Caso contrário, pode ser necessário verificar todo o intervalo de endereços IP plausível para forçar o endereço IP no cache do ARP e, em seguida, tente novamente a consulta:

nmap -sn '192.168.1.*'    # Ugh. Every address on the 192.168.1.0/24 network
    
por 19.02.2018 / 00:07
0
De modo geral, quando você está usando uma ponte, a VM não está efetivamente sob o controle dnsmasq interno da libvirt, mas obtém o IP do DHCP na rede com ponte ou nas configurações internas. Isso significa que a libvirt não tem meios de configurar esse IP ou obtê-lo.

Você pode fazer o que @roaima descrito acima como uma solução alternativa, monitorar seu DHCP para atribuições de IP ao MAC da VM ou instalar um agente dentro da VM que relata os detalhes da configuração interna da VM de volta para você (como ovirt -guest-agent faz). A regra geral aqui é que, com uma rede em ponte, a VM se comporta e geralmente é gerenciada como qualquer outra máquina física nessa rede

    
por 19.02.2018 / 15:05