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