How to install and configure KVM and Open vSwitch on Ubuntu or Debian

In today's multi-tenant data centers, the concept of virtualization is being extended from hypervisor-driven server virtualization to network virtualization. In this environment, software-based virtual switches are being installed on servers along with hypervisor, bridging traffic among different virtual machines (VMs).

In this tutorial, I am going to demonstrate how to install and configure KVM and Open vSwitch (OVS) on Ubuntu or Debian. KVM and Open vSwitch are, respectively, the most popular open-source hypervisor and virtual switch used in today's data centers.

Install Open vSwitch on Ubuntu or Debian

While OVS comes as a package on Ubuntu or Debian, here I am going to build it from the source, which will have the latest features and bug fixes.

First, install dependencies for building OVS.

$ sudo apt-get install build-essential libssl-dev linux-headers-$(uname -r)

Build OVS from the source as follows. The steps below will build OVS user-space tools as well as its kernel module.

$ wget http://openvswitch.org/releases/openvswitch-1.9.3.tar.gz
$ tar xvfvz openvswitch-1.9.3.tar.gz
$ cd openvswitch-1.9.3
$ ./configure --with-linux=/lib/modules/`uname -r`/build
$ make

Go ahead and install OVS user-space components under /usr/local/share/

$ sudo make install

The next step is to test the OVS kernel module (before installing it). For that, load the kernel module in the kernel first.

$ sudo insmod ./datapath/linux/openvswitch.ko

Verify that the OVS kernel module is loaded successfully.

$ lsmod | grep openvswitch
openvswitch            97934  0

Once you verify that openvswitch.ko is successfully loaded in the kernel, go ahead and install the kernel module as follows.

$ sudo make modules_install

Configure and Start Open vSwitch

Create a skeleton OVS configuration database.

$ sudo mkdir /etc/openvswitch
$ sudo ovsdb-tool create /etc/openvswitch/conf.db ./vswitchd/vswitch.ovsschema

Start OVS database server.

$ sudo ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock - remote=db:Open_vSwitch,manager_options --pidfile --detach

Initialize OVS configuration database.

$ sudo ovs-vsctl --no-wait init

Finally, start OVS daemon.

$ sudo ovs-vswitchd --pidfile --detach

Install KVM on Ubuntu or Debian

Install KVM and necessary user-space tools with apt-get command.

$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-manager

Add a non-root regular user (e.g., alice) to libvirtd group, so that the user can launch VMs without root privilege.

$ sudo adduser alice libvirtd

Log out and log back in as the user to make the group membership change effective.

Run the following command. If you should see an empty list of VMs, that means KVM is set up successfully.

$ virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

Configure Open vSwitch for KVM

Now it is time to create OVS bridge startup scripts, so that OVS can be automatically configured when a VM is started or stopped.

Install dependencies (user-mode Linux utilities), which will be used to handle Linux bridging modes.

$ sudo apt-get install uml-utilities

Create bridge startup scripts as follows.

$ sudo vi /etc/openvswitch/ovs-ifup
#!/bin/sh

switch='br0'
/sbin/ifconfig $1 0.0.0.0 up
ovs-vsctl add-port ${switch} $1
$ sudo vi /etc/openvswitch/ovs-ifdown
#!/bin/sh

switch='br0'
/sbin/ifconfig $1 0.0.0.0 down
ovs-vsctl del-port ${switch} $1
$ sudo chmod +x /etc/openvswitch/ovs-if*

Then, create a default bridge br0, and add a physical network interface via which VMs will communicate with external networks. In this tutorial, I assume such a network interface is eth5.

$ sudo ovs-vsctl add-br br0
$ sudo ovs-vsctl add-port br0 eth5

Launch a VM with KVM

Now you are ready to launch a guest VM.

I assume that you already prepared a guest VM image (e.g., ubuntu-client.img). Use the following command to launch a guest VM.

$ sudo kvm -m 1024 -net nic,macaddr=11:11:11:EE:EE:EE -net tap,script=/etc/openvswitch/ovs-ifup,downscript=/etc/openvswitch/ovs-ifdown -vnc :1 -drive file=/home/dev/images/ubuntu-client.img,boot=on

This will create and launch a guest VM whose virtual interface is automatically added to OVS bridge br0 upon launch.

You can verify the OVS status by using ovs-vsctl command as follows.

This is the remote desktop session for the launched VM.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

2 thoughts on “How to install and configure KVM and Open vSwitch on Ubuntu or Debian

  1. Great article! Some notes... newer versions of OpenVSwitch do not require the kernel module, but then again these will require the newer kernels. Also, you can name your bridge whatever you'd like. Br0 is used for historical reasons.

  2. When i give the last command "kvm -m ...", the cursor just keeps on blinking in the terminal and nothing happens. Even the '#' prompt doesn't appear until I press Ctrl+C. Is this an error? How to rectify it?

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *