blob: 08951d175dd1ba8876bff092b7c0d87b10c2e641 [file] [log] [blame]
Ian Wienand77835632021-05-13 13:14:42 +10001#!/bin/bash
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15# Defaults
16# --------
17
18# Set variables for building OVS from source
19OVS_REPO=${OVS_REPO:-https://github.com/openvswitch/ovs.git}
20OVS_REPO_NAME=$(basename ${OVS_REPO} | cut -f1 -d'.')
21OVS_REPO_NAME=${OVS_REPO_NAME:-ovs}
22OVS_BRANCH=${OVS_BRANCH:-0047ca3a0290f1ef954f2c76b31477cf4b9755f5}
23
24# Functions
25
26# load_module() - Load module using modprobe module given by argument and dies
27# on failure
28# - fatal argument is optional and says whether function should
29# exit if module can't be loaded
30function load_module {
31 local module=$1
32 local fatal=$2
33
34 if [ "$(trueorfalse True fatal)" == "True" ]; then
35 sudo modprobe $module || (dmesg && die $LINENO "FAILED TO LOAD $module")
36 else
37 sudo modprobe $module || (echo "FAILED TO LOAD $module" && dmesg)
38 fi
39}
40
41# prepare_for_compilation() - Fetch ovs git repository and install packages needed for
42# compilation.
43function prepare_for_ovs_compilation {
44 local build_modules=${1:-False}
45 OVS_DIR=$DEST/$OVS_REPO_NAME
46
47 if [ ! -d $OVS_DIR ] ; then
48 # We can't use git_clone here because we want to ignore ERROR_ON_CLONE
49 git_timed clone $OVS_REPO $OVS_DIR
50 cd $OVS_DIR
51 git checkout $OVS_BRANCH
52 else
53 # Even though the directory already exists, call git_clone to update it
54 # if needed based on the RECLONE option
55 git_clone $OVS_REPO $OVS_DIR $OVS_BRANCH
56 cd $OVS_DIR
57 fi
58
59 # TODO: Can you create package list files like you can inside devstack?
60 install_package autoconf automake libtool gcc patch make
61
62 # If build_modules is False, we don't need to install the kernel-*
63 # packages. Just return.
64 if [[ "$build_modules" == "False" ]]; then
65 return
66 fi
67
68 KERNEL_VERSION=`uname -r`
69 if is_fedora ; then
70 # is_fedora covers Fedora, RHEL, CentOS, etc...
71 if [[ "$os_VENDOR" == "Fedora" ]]; then
72 install_package elfutils-libelf-devel
73 KERNEL_VERSION=`echo $KERNEL_VERSION | cut --delimiter='-' --field 1`
74 elif [[ ${KERNEL_VERSION:0:2} != "3." ]]; then
75 # dash is illegal character in rpm version so replace
76 # them with underscore like it is done in the kernel
77 # https://github.com/torvalds/linux/blob/master/scripts/package/mkspec#L25
78 # but only for latest series of the kernel, not 3.x
79
80 KERNEL_VERSION=`echo $KERNEL_VERSION | tr - _`
81 fi
82
83 echo NOTE: if kernel-devel-$KERNEL_VERSION or kernel-headers-$KERNEL_VERSION installation
84 echo failed, please, provide a repository with the package, or yum update / reboot
85 echo your machine to get the latest kernel.
86
87 install_package kernel-devel-$KERNEL_VERSION
88 install_package kernel-headers-$KERNEL_VERSION
89
90 elif is_ubuntu ; then
91 install_package linux-headers-$KERNEL_VERSION
92 fi
93}
94
95# load_ovs_kernel_modules() - load openvswitch kernel module
96function load_ovs_kernel_modules {
97 load_module openvswitch
98 load_module vport-geneve False
99 dmesg | tail
100}
101
102# reload_ovs_kernel_modules() - reload openvswitch kernel module
103function reload_ovs_kernel_modules {
104 set +e
105 ovs_system=$(sudo ovs-dpctl dump-dps | grep ovs-system)
106 if [ -n "$ovs_system" ]; then
107 sudo ovs-dpctl del-dp ovs-system
108 fi
109 set -e
110 sudo modprobe -r vport_geneve
111 sudo modprobe -r openvswitch
112 load_ovs_kernel_modules
113}
114
115# compile_ovs() - Compile OVS from source and load needed modules.
116# Accepts two parameters:
117# - first one is False by default and means that modules are not built and installed.
118# - second optional parameter defines prefix for ovs compilation
119# - third optional parameter defines localstatedir for ovs single machine runtime
120# Env variables OVS_REPO_NAME, OVS_REPO and OVS_BRANCH must be set
121function compile_ovs {
122 local _pwd=$PWD
123 local build_modules=${1:-False}
124 local prefix=$2
125 local localstatedir=$3
126
127 if [ -n "$prefix" ]; then
128 prefix="--prefix=$prefix"
129 fi
130
131 if [ -n "$localstatedir" ]; then
132 localstatedir="--localstatedir=$localstatedir"
133 fi
134
135 prepare_for_ovs_compilation $build_modules
136
137 KERNEL_VERSION=$(uname -r)
138 major_version=$(echo "${KERNEL_VERSION}" | cut -d '.' -f1)
139 patch_level=$(echo "${KERNEL_VERSION}" | cut -d '.' -f2)
140 if [ "${major_version}" -gt 5 ] || [ "${major_version}" == 5 ] && [ "${patch_level}" -gt 5 ]; then
141 echo "NOTE: KERNEL VERSION is ${KERNEL_VERSION} and OVS doesn't support compiling "
142 echo "Kernel module for version higher than 5.5. Skipping module compilation..."
143 build_modules="False"
144 fi
145
146 if [ ! -f configure ] ; then
147 ./boot.sh
148 fi
149 if [ ! -f config.status ] || [ configure -nt config.status ] ; then
150 if [[ "$build_modules" == "True" ]]; then
151 ./configure $prefix $localstatedir --with-linux=/lib/modules/$(uname -r)/build
152 else
153 ./configure $prefix $localstatedir
154 fi
155 fi
156 make -j$(($(nproc) + 1))
157 sudo make install
158 if [[ "$build_modules" == "True" ]]; then
159 sudo make INSTALL_MOD_DIR=kernel/net/openvswitch modules_install
160 reload_ovs_kernel_modules
161 else
162 load_ovs_kernel_modules
163 fi
164
165 cd $_pwd
166}
167
168# action_service - call an action over openvswitch service
169# Accepts one parameter that can be either
170# 'start', 'restart' and 'stop'.
171function action_openvswitch {
172 local action=$1
173
174 if is_ubuntu; then
175 ${action}_service openvswitch-switch
176 elif is_fedora; then
177 ${action}_service openvswitch
178 elif is_suse; then
179 if [[ $DISTRO == "sle12" ]] && [[ $os_RELEASE -lt 12.2 ]]; then
180 ${action}_service openvswitch-switch
181 else
182 ${action}_service openvswitch
183 fi
184 fi
185}
186
187# start_new_ovs() - removes old ovs database, creates a new one and starts ovs
188function start_new_ovs {
189 sudo rm -f /etc/openvswitch/conf.db /etc/openvswitch/.conf.db~lock~
190 sudo /usr/share/openvswitch/scripts/ovs-ctl start
191}
192
193# stop_new_ovs() - stops ovs
194function stop_new_ovs {
195 local ovs_ctl='/usr/share/openvswitch/scripts/ovs-ctl'
196
197 if [ -x $ovs_ctl ] ; then
198 sudo $ovs_ctl stop
199 fi
200}
201
202# remove_ovs_packages() - removes old ovs packages from the system
203function remove_ovs_packages {
204 for package in openvswitch openvswitch-switch openvswitch-common; do
205 if is_package_installed $package; then
206 uninstall_package $package
207 fi
208 done
209}
210
211
212# load_conntrack_gre_module() - loads nf_conntrack_proto_gre kernel module
213function load_conntrack_gre_module {
Slawek Kaplonskib4e683e2021-10-05 20:44:57 +0200214 load_module nf_conntrack_proto_gre False
Ian Wienand77835632021-05-13 13:14:42 +1000215}