How to Install and use Open vSwitch with KVM on CentOS 7 / RHEL 7

Open vSwitch is a free and open source multi-layer software switch, which is used to manage the traffic between virtual machines and physical or logical networks.  It provides the features like traffic isolation in OpenStack tenants using the overlay techniques likes GRE, VXLAN and 802.1Q VLANs.

Using open vSwitch packet forwarding engine in kernel space or user space can be implemented and Link aggregation can also be accomplished through LACP.

In this article we will discuss how to install latest version of Open vSwitch 2.9.2 on CentOS 7 and RHEL 7 Server. Apart from this we will see how open vSwitch can be used in KVM virtual machines for their networking.

Install-openvswitch-KVM-CentOS7-RHEL7

Read More On : How to Install and Configure KVM on Ubuntu 18.04 LTS Server

I am assuming you have already have either CentOS 7 or RHEL 7 server configured with KVM. Open vswitch (OVS) 2.0 is available in the CentOS 7 & RHEL 7 default yum repositories, but if you install latest version of open vSwitch refer the below steps.

Step 1) Install the required packages using yum command

Login to your CentOS 7 or RHEL 7 server and run the beneath yum command,

[[email protected] ~]# yum install wget openssl-devel  python-sphinx gcc make python-devel openssl-devel kernel-devel graphviz kernel-debug-devel autoconf automake rpm-build redhat-rpm-config libtool python-twisted-core python-zope-interface PyQt4 desktop-file-utils libcap-ng-devel groff checkpolicy selinux-policy-devel -y

Step 2) Create ovs user and download Open vSwitch 2.9

Create a user with name ovs using below command

[[email protected] ~]# useradd ovs
[[email protected] ~]# su - ovs
[[email protected] ~]$

Download OVS 2.9 tar file and build the openvswitch rpm using beneath commands,

[email protected] ~]$ mkdir -p ~/rpmbuild/SOURCES
[[email protected] ~]$ wget http://openvswitch.org/releases/openvswitch-2.9.2.tar.gz
[[email protected] ~]$ cp openvswitch-2.9.2.tar.gz ~/rpmbuild/SOURCES/
[[email protected] ~]$ tar xfz openvswitch-2.9.2.tar.gz
[[email protected] ~]$ rpmbuild -bb --nocheck openvswitch-2.9.2/rhel/openvswitch-fedora.spec
[[email protected] ~]$ exit
logout
[[email protected] ~]#

Now install Open vSwitch rpm using below yum command,

[[email protected] ~]# yum localinstall /home/ovs/rpmbuild/RPMS/x86_64/openvswitch-2.9.2-1.el7.x86_64.rpm -y

Step 3) Start and enable Open vSwitch Service

Use below systemctl commands to start and enable open vSwitch service

[[email protected] ~]# systemctl start openvswitch.service
[[email protected] ~]# systemctl enable openvswitch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/openvswitch.service to /usr/lib/systemd/system/openvswitch.service.
[[email protected] ~]# systemctl status openvswitch.service
● openvswitch.service - Open vSwitch
Loaded: loaded (/usr/lib/systemd/system/openvswitch.service; enabled; vendor preset: disabled)
Active: active (exited) since Sun 2018-08-05 10:16:12 EDT; 17s ago
Main PID: 73958 (code=exited, status=0/SUCCESS)
Aug 05 10:16:12 compute02 systemd[1]: Starting Open vSwitch...
Aug 05 10:16:12 compute02 systemd[1]: Started Open vSwitch.
[[email protected] ~]#

Use below command to check the OVS version

[[email protected] ~]# ovs-vsctl -V
ovs-vsctl (Open vSwitch) 2.9.2
DB Schema 7.15.1
[[email protected] ~]#

Step:4 Create the OVS bridge and add interfaces to it.

Use the below command to create ovs bridge,

[[email protected] ~]# ovs-vsctl add-br ovs-br0

Now flush or remove IP from the interface, in my case IP as was assigned to eno16777736

[[email protected] ~]# ip addr flush dev eno16777736

Now assign this IP address to ovs bridge(ovs-br0)

[[email protected] ~]# ip addr add 192.168.1.4/24 dev ovs-br0

Add interface as port in ovs-br0 using below command,

[[email protected] ~]# ovs-vsctl add-port ovs-br0 eno16777736

Now bring up the bridge using below ‘ip link’ command,

[[email protected] ~]# ip link set dev ovs-br0 up

Note : Above changes are not persistent, so make these changes persistent across the reboot, we have to create ovs bridge file, steps are shown below

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eno16777736 ifcfg-ovs-br0
[[email protected] network-scripts]#
[[email protected] network-scripts]# vi ifcfg-eno16777736
DEVICE=eno16777736
HWADDR="00:0c:29:c1:c3:4e"
TYPE=OVSPort
DEVICETYPE=ovs
OVS_BRIDGE=ovs-br0
ONBOOT=yes

Save and exit the file

[[email protected] network-scripts]# vi ifcfg-ovs-br0
DEVICE=ovs-br0
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
IPADDR=192.168.1.4
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes

Save & exit the file

Restart the network service using below command,

[[email protected] network-scripts]# systemctl restart network

Now Verify ovs bridge and its ports using “ovs-vsctl” command

[[email protected] ~]# ovs-vsctl show
8dc5f8e7-0e54-4d9d-ba7a-cd6b9b94f470
    Bridge "ovs-br0"
        Port "ovs-br0"
            Interface "ovs-br0"
                type: internal
        Port "eno16777736"
            Interface "eno16777736"
    ovs_version: "2.9.2"
[[email protected] ~]#

Step:5 ) Create and define virsh ovs network

Create ovs network file with the following contents ,

[[email protected] ~]# vi /tmp/ovs-network.xml
<network>
<name>ovs-network</name>
<forward mode='bridge'/>
<bridge name='ovs-br0'/>
<virtualport type='openvswitch'/>
</network>

Now define the ovs-network using below virsh command,

[[email protected] ~]# virsh net-define /tmp/ovs-network.xml
Network ovs-network defined from /tmp/ovs-network.xml
[[email protected] ~]# virsh net-start ovs-network
Network ovs-network started
[[email protected] ~]# virsh net-autostart ovs-network
Network ovs-network marked as autostarted
[[email protected] ~]#

Now verify the virsh network using beneath command,

[[email protected] ~]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 ovs-network          active     yes           yes
[[email protected] ~]#

Step:6) Create Virtual machines and attach ovs-network

Let’s create two test VMs using below virt-install command, specify the network for these VMs as ovs-network,

[[email protected] ~]# virt-install  -n testvm1  --description "Test VM1 for OVS "  --os-type=Linux  --os-variant=rhel7  --ram=1096  --vcpus=1  --disk path=/var/lib/libvirt/images/testvm1.img,bus=virtio,size=10  --network network:ovs-network --graphics none  --location /root/CentOS-7-x86_64-DVD-1511.iso --extra-args console=ttyS0

Similarly Create a second test vm with name “testvm2”

[[email protected] ~]# virt-install  -n testvm2  --description "Test VM2 for OVS "  --os-type=Linux  --os-variant=rhel7  --ram=1096  --vcpus=1  --disk path=/var/lib/libvirt/images/testvm2.img,bus=virtio,size=10  --network network:ovs-network --graphics none  --location /root/CentOS-7-x86_64-DVD-1511.iso --extra-args console=ttyS0

Once the VMs are created then their interfaces should be added in ovs bridge (ov-br0) automatically and we can verify this from ovs-vsctl command,

[[email protected] ~]# ovs-vsctl show
8dc5f8e7-0e54-4d9d-ba7a-cd6b9b94f470
    Bridge "ovs-br0"
        Port "ovs-br0"
            Interface "ovs-br0"
                type: internal
        Port "eno16777736"
            Interface "eno16777736"
        Port "vnet0"
            Interface "vnet0"
        Port "vnet1"
            Interface "vnet1"
    ovs_version: "2.9.2"
[[email protected] ~]# ovs-vsctl list-ports ovs-br0
eno16777736
vnet0
vnet1
[[email protected] ~]#

In the above command vnet0 & vnet1 are VMs tap interface. When we shutdown these VMS then these ports will be removed from OVS Bridge (ovs-br0) automatically. Log files for ovs (openvswitch) are kept under the folder “/var/log/openvswitch“.

This conclude this article, I hope you got an idea how to install and use Open vSwitch 2.9 with KVM on CentOS 7 and RHEL 7 Servers. If you like article, please do share your feedback and comments.

13 Responses

  1. jab2805 says:

    Excellent post!! Really enjoyed it. One question how can we add are wifi port to the bridge.

    • Pradeep Kumar says:

      You can add wifi interface to the ovs bridge using below command,

      $ ovs-vsctl add-port {bridge-name} {wireless-adapter }

      e.g : ovs-vsctl add-port ovs-br0 wlan0

      Remove the IP from Wireless Adapter and assign the same IP to bridge

  2. Arun says:

    Superb article

  3. cyclism says:

    Real good, easy steps. What would the steps be if you wanted to take an existing KVM setup with bridges in place and convert it over to OVS?

  4. Joe Davola says:

    Is the persistent network configs on the ovs for network-manager and not for networkd?

  5. loic says:

    This was one the best article I have seen for OVS

  6. Saurabh says:

    Great Article, Very well explained!!
    Keep up good work.

  7. sairaj says:

    how to change password for ovs root

  8. Flaviof says:

    Great job! Thank you for writing this.

    • Flaviof says:

      Nit: maybe add “python-six ” to initial yum install commad:

      $ rpmbuild -bb –nocheck openvswitch-2.9.2/rhel/openvswitch-fedora.spec
      error: Failed build dependencies:
      python-six is needed by openvswitch-2.9.2-1.el7

      sudo yum install -y python-six

  9. Doyoung says:

    This is not working on CentOS 8. When Do you think openvswitch with CentOS 8 is supported?

Leave a Reply

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

Pin It on Pinterest