Alessandro Dotti Contra

Linux/Unix DevOps

A qemu-kvm/libvirt virtual network

This page doesn't aim to be a full fledged tutorial on how to build a virtual network using open source tools; it is more like a todo with a bit of extra wording to keep the pieces together.

Be sure to have the relevant tools installed and read on.

Lan Gateway configuration

Supposing is the IP address of the virtual host, the first step is to instruct the LAN gateway on how to reach virtual guests. This actually means that all relevant packages should be routed through the virtual host.

This is done by setting up a static route in the LAN gateway:

#> ip route add via dev eth0

Virtual network configuration

On the virtual host, define a routed virtual network. Create the XML file first:

#> vi /tmp/deepblue.xml

	<bridge name="virbr1" />
	<forward mode="route"/>
	<ip address="" netmask="">

then instruct libvirt to define the network, mark it for autostart and start it:

#> virsh net-define /tmp/deepblue.xml
#> virsh net-autostart deepblue
#> virsh net-start deepblu

Enabling NAT

In case the new virtual network is behind a firewall, we need to add at least a rule to enable virtual guests to reach the Internet:

#> iptables -t nat -A POSTROUTING -s -o  -j MASQUERADE

Create a virtual guest

Now create a disk pool to store the virtual guest hard disks and mark it for autostart:

#> virsh pool-define-as <pool_name> dir --target <pool_directory>
#> virsh pool-autostart <pool_name>

Virtual disks will be stored inside the <pool_directory>.

Then perform the actual installation:

#> virt-install -n debian-testing \
	--memory 2048 --vcpus=2 \
	--cpu host \
	-c ./netinst/debian-6.0.7-amd64-netinst.iso \
	--os-type=linux --os-variant=debiansqueeze \
	--disk pool=<pool_name>,size=2,format=qcow2 \
	-w network=<virtual_network>

To connect to the virtual guest from a remote host (provided virt-viewer is installed):

$> virt-viewer -c qemu+ssh://<server>/system <guest>

where guest is the name specified with the -n option at installation time and server is the virtual host.