Próximo: , Anterior: , Acima: Máquinas Virtuais   [Conteúdo][Índice]


5.2 Roteamento de rede para libvirt

If the machine hosting your virtual machines is connected wirelessly to the network, you won’t be able to use a true network bridge as explained in the preceding section (veja Ponte de rede para QEMU). In this case, the next best option is to use a virtual network switch with static routing and to configure a libvirt-powered virtual machine to use it (via the virt-manager GUI for example). This is similar to the default mode of operation of QEMU/libvirt, except that instead of using NAT (Network Address Translation), it relies on static routes to join the VM (virtual machine) IP address to the LAN (local area network). This provides two-way connectivity to and from the virtual machine, which is needed for exposing services hosted on the virtual machine.

5.2.1 Creating a virtual network switch

A virtual network switch consists of a virtual network device called a ‘virtual bridge’, DHCP server (dnsmasq) and firewall rules (iptables). See the libvirt Wiki article on Virtual Networking for more details on the modes of operation, management and implementation of virtual network switches.

The virsh command, provided by the libvirt package, makes it very easy to create a virtual switch. You first need to choose a network subnet for your virtual switch; if your home LAN is in the ‘192.168.1.0/24’ network, you could opt to use e.g. ‘192.168.2.0/24’. Define an XML file, e.g. /tmp/virbr0.xml, containing the following:

<network>
  <name>virbr0</name>
  <bridge name="virbr0" />
  <forward mode="route"/>
  <ip address="192.168.2.0" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.2.1" end="192.168.2.254"/>
    </dhcp>
  </ip>
</network>

Em seguida, crie e configure a interface usando o comando virsh, como root:

virsh net-define /tmp/virbr0.xml
virsh net-autostart virbr0
virsh net-start virbr0

A interface ‘virbr0’ agora deve estar visível, por exemplo, via o comando ‘ip address’. Ela será iniciada automaticamente toda vez que sua máquina virtual libvirt for iniciada.

5.2.2 Configurando as rotas estáticas para sua ponte virtual

Se você configurou sua máquina virtual para usar sua interface de ponte virtual ‘virbr0’ recém-criada, ela já deve receber um IP via DHCP, como ‘192.168.2.15’ e ser acessível a partir do servidor que a hospeda, por exemplo, via ‘ping 192.168.2.15’. Há uma última configuração necessária para que a VM possa alcançar a rede externa: adicionar rotas estáticas ao roteador da rede.

Neste exemplo, a rede LAN é ‘192.168.1.0/24’ e a página da web de configuração do roteador pode ser acessada por meio de, por exemplo, a página http://192.168.1.1. Em um roteador executando o firmware libreCMC, você navegaria até a página Network → Static Routes (https://192.168.1.1/cgi-bin/luci/admin/network/routes) e adicionaria uma nova entrada em ‘Static IPv4 Routes’ com as seguintes informações:

Interface

rede local

Alvo

192.168.2.0

Rede-IPV4

255.255.255.0

IPv4-Ponto de entrada

servidor-ip

Tipo de rota

unicast

onde server-ip é o endereço IP da máquina que hospeda as VMs, que deve ser estático.

Depois de salvar/aplicar essa nova rota estática, a conectividade externa deve funcionar de dentro da sua VM; você pode, por exemplo, executar ‘ping gnu.org’ para verificar se ela funciona corretamente.


Próximo: Custom NAT-based network for libvirt, Anterior: Ponte de rede para QEMU, Acima: Máquinas Virtuais   [Conteúdo][Índice]