While creating a virtual machine on a default configuration, it is relatively easy to add network interfaces. But...
if you want to modify the KVM network for specific environments, it becomes a bit harder. In this tip, you'll read how KVM networking is organized and how to set up a virtual bridge manually.
To connect virtual machines (VMs) to the physical network on the host layer, you will need a way to share access to physical network cards on the physical machine. The default way to do this is by using a bridge device with the name br0. Every instance running on the same physical machine goes through this bridge device in order to access the network, as well as the hypervisor host operating system.
As on a physical bridge or switch, different ports, known as vnet ports, are connected to the bridge device. Every vnet interface is connected to the bridge, and the bridge connects to a real interface in either the KVM host or the virtual operating systems. Figure 1 gives an overview, where you can see that the physical network card eth0 on the host connects to vnet0, eth0 in vm1 connects to vnet1 and eth0 in vm2 connects to vnet2. The numbering of vnet interfaces is trivial and does not relate in any way to the VM and to which device in the VM is used: The numbering is just in order of appearance.
Schematic overview of KVM networking
For insight into the KVM network configuration, the brctl show command can be used. This command gives the name of the bridge and the bridge ID; it will indicate whether the Spanning Tree Protocol (STP) is disabled, and it shows the interfaces connected to the bridge.
lin:~ # brctl show
bridge name bridge id STP enabled interfaces
br0 8000.5c260a1a70aa no em1
Configuring the bridge device
To configure the bridge device on the KVM host on SUSE or Red Hat Linux systems, you will typically create a file with the name ifcfg-br0, which contains the following:
lin:~ # cat /etc/sysconfig/network/ifcfg-br0
In the above definition of a bridge device, the most important parameter is BRIDGE_PORTS='em1', which indicates the physical interface that is connected to the bridge. The physical network interfaces that are connected to the bridge won't need much configuration:
lin:~ # cat /etc/sysconfig/network/ifcfg-em1
NAME='82577LM Gigabit Network Connection'
Attaching virtual machines to the bridge
Once the host part of the configuration has been created, you can set up VM networking by using the virsh command. On the KVM host, type virsh list --all to get an overview of all VMs that are available on this host.
lin:~ # virsh list --all
setlocale: No such file or directory
Id Name State
1 SAN running
4 node2 running
6 windowsvista running
7 node1 running
Next, type virsh edit hostname to open the XML configuration for the specific host in an editor. Scroll down in the device section until you find the first network interface:
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
To add a new interface, just add an interface section. You'll need to define the interface type (which would be bridge, the source and the model.
There is no need to define the MAC addresses of the hardware specifications, as these will be added by libvirt the first time the host is started with these new settings. After adding these changes, you can restart the VM, and within it you'll see that the new interface has automatically been added.
To manage networking in a KVM environment, there are a lot of different parts are involved. You first have to create a bridge device on the host computer, after which you can add an interface in the VM that connects to the bridge device. Once this is accomplished, you'll be able to use networking on the VM in the same way that you're used to using networking on physical machines.