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

Last updated on August 30, 2020 by Dan Nanni

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 (OVS) are, respectively, the most popular open-source hypervisor and virtual switch used in today's data centers. Compared to simple bridged networking which is limited to MAC-based Layer-2 forwarding, OVS allows one to define highly customized forwarding and filtering policies for VMs.

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 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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean