Cyberwizzard.nl

Welcome to my world

Gentoo KVM virtual machine support plus networking

The Gentoo Wiki article about KVM uses the less flexible networking setup with dedicated TAP devices like 'tap0'. While this setup works fine, other distributions use the bridge device in a different way. By creating a virtual bridge and forcing the host system to connect through this bridge, virtual machines can simply connect to the bridge device and 'plug in'. No need to manually create TAP devices. In this guide we will set up host and guest networking using the virtual bridge device and DHCP for wired ethernet.

For this to work, you will need most of the tools from the wiki article as well as a couple more (I like the management GUI from virt-manager to manage VMs without a text console). Install the following tools:

  1. app-emulation/qemu-kvm - QEMU + Kernel-based Virtual Machine userland tools
  2. net-misc/bridge-utils - Virtual network bridging
  3. app-emulation/virt-manager - libvirt management GUI
  4. app-emulation/libvirt - Toolkit to manipulate virtual machines, supporting virtualisation frameworks like KVM and Xen

Install all the tools and modify the kernel to support the bridge devices. Note that tunnel support is omitted (unlike in the original wiki article).

[ * ] Virtualization ---> --- Virtualization <m> Kernel-based Virtual Machine (KVM) support < > KVM for Intel processors support < > KVM for AMD processors support Networking support ---> Networking options ---> <*> 802.1d Ethernet Bridging <*> 802.1Q VLAN Support

Build the kernel, the modules and install both. Do not forget to reboot.

Next up, lets configure the network on the host machine. Disable NetworkManager as it will not understand the bridge device and break networking altogether in an attempt to activate things they usually are.

We will connect the network as shown below. The interface 'eth0' is now a passthrough for the virtual network bridge - note that the eth0 device has no IP address. The new 'br0' device is a new bridge. This is the new networking interface for the host PC - it gets an IP address and is the designated end point for communication. Other virtual NICs connect to this bridge.

HOST +---------------+ LAN ---+--- eth0 | | ^ | KVM GUEST1 | | | +--------------+ | +-----+ | | | | |vnet0+----+---+---- nic0 | | |vnet1+----+-+ |192.168.100.2 | KVM GUEST2 | +-----+ | | +--------------+ +--------------+ | br0 | | | | |192.168.100.1 | +--------------------+---- nic0 | +---------------+ |192.168.100.3 | +--------------+

In order to automatically configure the network, edit '/etc/conf.d/net' and insert the following:

# Disable all configuration on eth0 config_eth0=("null") # Use eth0 to connect the virtual bridge bridge_br0="eth0" # Statically configure br0 config_br0=("192.168.100.1 netmask 255.255.255.0") routes_br0=("default via 192.168.100.254") dns_servers_br0="192.168.100.254" # Uncomment the next line and comment the lines above to use DHCP #config_br0=("dhcp")

Now we need to symlink '/etc/init.d/net.lo' to '/etc/init.d/net.br0'. We can now stop 'net.eth0' if you still had it running and start 'br0' instead. Note that the Gentoo networking scripts create the bridge interface 'br0' and bring everything up (including eth0, but without assigning an IP).

The Gentoo scripts do this under water (in case you want to manually configure things):

# Create the br0 device and connect eth0 to it brctl addbr br0 brctl addif br0 eth0

If you want to make this networking setup permanent, remember to remove 'eth0' from the default runlevel and add 'br0' instead.

Now start the service 'libvirt' and run 'virt-manager' to start managing Virtual Machines. If everything worked as planned, creating new VMs will automatically (read: libvirt creates these) result in new virtual NICs for the virtual network. Good luck!