Installing KVM, Libvirt and Open vSwitch on Fedora

Posted by Dave on 6 March 2014

In my NetOps to DevOps Training Plan I mentioned installing KVM, Libvirt and Open vSwitch. I did this a few weeks ago and documented it to produce this tutorial. My motivation was to replace my VMware environment at home with something Open Source. I am also a strong believer in "eat your own dog food" and as a lot of the work I am doing in the Open Source community centers around these 3 technologies, I should get used to using them every day...

Prerequsites

Before we get started, I'll assume that you already have a Fedora Minimal Installation that you are ready to work on...

Installing the packages

sudo yum install -y @standard @virtualization openvswitch

That was easy wasn't it!

@standard installs some useful utilities and @virtualization installs libvirt + KVM

I'm sure you can guess what openvswitch does.

Configuration

Now here comes the fun part!

Configure the services

:::bash
# Disable NetworkManager
sudo systemctl stop NetworkManager.service
sudo systemctl disable NetworkManager.service

# Enable "Proper" Networking
sudo systemctl enable network.service
sudo systemctl start network.service

# Enable the Open vSwitch service
sudo systemctl enable openvswitch.service
sudo systemctl start openvswitch.service

Setting up Networking with Open vSwitch

Our networking setup is as follows:

  • Single Bridge called ovsbr0
  • eth0 connected to ovsbr0
  • IP address 10.10.0.10 bound to ovsbr0

To achieve this we need to create/modify the ifcfg-eth0 and ifcfg-ovsbr0 scripts in /etc/sysconfig/network-scripts/. Using your favorite text editor edit these files as follows, replacing the HWADDR with the MAC address of your NIC.

ifcfg-eth0

:::bash
DEVICE=eth0
NAME=eth0
ONBOOT=yes
DEVICETYPE=ovs
TYPE=OVSPort
OVS_BRIDGE=ovsbr0
BOOTPROTO=none
HWADDR=38:EA:A7:AA:97:3E
HOTPLUG=no

Your interface may be called em0 instead of eth0. In that case you can either s/eth0/em0/ or you can mv ifcfg-em0 ifcfg-eth0 making sure you use the config above.

ifcfg-ovsbr0

:::bash
DEVICE=ovsbr0
ONBOOT=yes
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
IPV6INIT=no
DELAY=0
IPADDR=10.10.0.10
NETMASK=255.255.255.0
GATEWAY=10.10.0.254
HOTPLUG=no

NOTE: This does NOT work on Fedora 20 as there is a bug in the current package.

Now you can sudo reboot and when your machine has rebooted, your new networking configuration will have taken effect.

:::bash
ip addr

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master ovs-system state UP qlen 1000
    link/ether 38:ea:a7:aa:97:3e brd ff:ff:ff:ff:ff:ff
    inet6 fe80::3aea:a7ff:feaa:973e/64 scope link
       valid_lft forever preferred_lft forever
4: ovsbr0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/ether 38:ea:a7:aa:97:3e brd ff:ff:ff:ff:ff:ff
    inet 10.10.0.10/24 brd 10.10.0.255 scope global ovsbr0
       valid_lft forever preferred_lft forever
    inet6 fe80::8482:abff:fe84:6165/64 scope link
       valid_lft forever preferred_lft forever

sudo ovs-vsctl show

21b0f942-5cf8-4479-9eb3-53c418a881b3
Bridge "ovsbr0"
    Port "ovsbr0"
        Interface "ovsbr0"
            type: internal
    Port "eth0"
        Interface "eth0"
ovs_version: "2.0.0"

Setting up Libvirt to use Open vSwitch

To get LibVirt to use our network by default, we can edit the default network with virsh

:::bash
sudo virsh net-destory default
sudo virsh net-edit default

This will open the XML file in the editor specified in your EDITOR env variable (vi by default) Edit this file to look like this:

:::xml
<network>
  <name>default</name>
  <uuid>61f5a0f8-e81d-4685-9456-b22f03f278ab</uuid>
  <forward mode='bridge'/>
  <bridge name='ovsbr0' />
  <virtualport type='openvswitch'/>
</network>

Once you are done:

:::bash
sudo virsh net-start default

Installing a Minimal X environment for Virt-Manager

There are many different ways of managing your new Libvirt+KVM install but right now I like Virtual Machine Manager.

First you will need to install an X server on your Fedora machine:

:::bash
sudo yum install -y @basic-desktop

To use this from within OSX, I'll need to install XQuartz and use X11 Forwarding. The easiest way to do this is with Homebrew and the homebrew-cask plugin.

:::bash
brew cask install xquartz

Then when we SSH to our Server we add the -X flag for X11 forwarding.

:::bash
sudo virt-manager

The above command will open Virtual Machine Manager

If you get an error like X11 connection rejected because of wrong authentication. you can try sudo cp .Xauthority /root/ which worked for me

Create your first VM!

At this point, you are ready to create your first VM!!!

Open Virtual Machine Manager and follow the wizard for creating a new VM.

  1. Click New
  2. Enter you VM Name and Boot Type
  3. Select your OS
  4. Select your Memory and CPU
  5. Create a Volume for your disk
  6. Click Finish
  7. Fin.

Now we have a VM.

If you sudo ovs-vsctl show you can see that Libvirt created a port on your vSwitch for you!

Celebrate!

That's all there is to it. Pat yourself on the back and take a break. It feels good to be away from VMware, but I'm not totally happy with Virtual Machine Manager. To that end, I'm looking to augment or replace it with Foreman, but that's a whole other blog post!

@dave_tucker