3837012e631fe050395e0d856472da225b184fa2
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / deploycentostools.sh
1 #!/usr/bin/env bash
2 ##
3 ## Copyright (c) 2010-2020 Intel Corporation
4 ##
5 ## Licensed under the Apache License, Version 2.0 (the "License");
6 ## you may not use this file except in compliance with the License.
7 ## You may obtain a copy of the License at
8 ##
9 ##     http://www.apache.org/licenses/LICENSE-2.0
10 ##
11 ## Unless required by applicable law or agreed to in writing, software
12 ## distributed under the License is distributed on an "AS IS" BASIS,
13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ## See the License for the specific language governing permissions and
15 ## limitations under the License.
16 ##
17
18 # Directory for package build
19 BUILD_DIR="/opt/rapid"
20 DPDK_VERSION="20.05"
21 PROX_COMMIT="7c3217fc16"
22 PROX_CHECKOUT="git checkout ${PROX_COMMIT}"
23 ## Next line is overruling the PROX_COMMIT and will replace the version with a very specific patch. Should be commented out
24 ##      if you want to use a committed version of PROX with the COMMIT ID specified above
25 ##Following line has the commit for testing IMIX, IPV6, ... It is the merge of all PROX commits on May 27th 2020
26 #PROX_CHECKOUT="git fetch \"https://gerrit.opnfv.org/gerrit/samplevnf\" refs/changes/23/70223/1 && git checkout FETCH_HEAD"
27 MULTI_BUFFER_LIB_VER="0.52"
28 export RTE_SDK="${BUILD_DIR}/dpdk-${DPDK_VERSION}"
29 export RTE_TARGET="x86_64-native-linuxapp-gcc"
30
31 # By default, do not update OS
32 OS_UPDATE="n"
33 # By default, asumming that we are in the VM
34 K8S_ENV="n"
35
36 # If already running from root, no need for sudo
37 SUDO=""
38 [ $(id -u) -ne 0 ] && SUDO="sudo"
39
40 function os_pkgs_install()
41 {
42         ${SUDO} yum install -y deltarpm yum-utils
43
44         # NASM repository for AESNI MB library
45         #${SUDO} yum-config-manager --add-repo http://www.nasm.us/nasm.repo
46
47         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
48         ${SUDO} yum install -y git wget gcc unzip libpcap-devel ncurses-devel \
49                          libedit-devel lua-devel kernel-devel iperf3 pciutils \
50                          numactl-devel vim tuna openssl-devel wireshark \
51                          make driverctl
52
53         ${SUDO} wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/linux/nasm-2.14.02-0.fc27.x86_64.rpm
54         ${SUDO} rpm -ivh nasm-2.14.02-0.fc27.x86_64.rpm
55 }
56
57 function k8s_os_pkgs_runtime_install()
58 {
59         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
60
61         # Install required dynamically linked libraries + required packages
62         ${SUDO} yum install -y numactl-libs libpcap openssh openssh-server \
63                   openssh-clients sudo
64 }
65
66 function os_cfg()
67 {
68         # huge pages to be used by DPDK
69         ${SUDO} sh -c '(echo "vm.nr_hugepages = 1024") > /etc/sysctl.conf'
70
71         ${SUDO} sh -c '(echo "options vfio enable_unsafe_noiommu_mode=1") > /etc/modprobe.d/vfio.conf'
72         ${SUDO} sh -c '(echo "vfio") > /etc/modules-load.d/vfio.conf'
73         ${SUDO} sh -c '(echo "vfio-pci") > /etc/modules-load.d/vfio.conf'
74         # Enabling tuned with the realtime-virtual-guest profile
75         pushd ${BUILD_DIR} > /dev/null 2>&1
76         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm
77         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm
78         # Install with --nodeps. The latest CentOS cloud images come with a tuned version higher than 2.8. These 2 packages however
79         # do not depend on v2.8 and also work with tuned 2.9. Need to be careful in the future
80         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm --nodeps
81         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm --nodeps
82         # Although we do no know how many cores the VM will have when begin deployed for real testing, we already put a number for the
83         # isolated CPUs so we can start the realtime-virtual-guest profile. If we don't, that command will fail.
84         # When the VM will be instantiated, the check_kernel_params service will check for the real number of cores available to this VM 
85         # and update the realtime-virtual-guest-variables.conf accordingly.
86         echo "isolated_cores=1-3" | ${SUDO} tee -a /etc/tuned/realtime-virtual-guest-variables.conf
87         ${SUDO} tuned-adm profile realtime-virtual-guest
88
89         # Install the check_tuned_params service to make sure that the grub cmd line has the right cpus in isolcpu. The actual number of cpu's
90         # assigned to this VM depends on the flavor used. We don't know at this time what that will be.
91         ${SUDO} chmod +x ${BUILD_DIR}/check_prox_system_setup.sh
92         ${SUDO} mv ${BUILD_DIR}/check_prox_system_setup.sh /usr/local/libexec/
93         ${SUDO} mv ${BUILD_DIR}/check-prox-system-setup.service /etc/systemd/system/
94         ${SUDO} systemctl daemon-reload
95         ${SUDO} systemctl enable check-prox-system-setup.service
96         popd > /dev/null 2>&1
97 }
98
99 function k8s_os_cfg()
100 {
101         [ ! -f /etc/ssh/ssh_host_rsa_key ] && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
102         [ ! -f /etc/ssh/ssh_host_ecdsa_key ] && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
103         [ ! -f /etc/ssh/ssh_host_ed25519_key ] && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
104
105         [ ! -d /var/run/sshd ] && mkdir -p /var/run/sshd
106
107         USER_NAME="centos"
108         USER_PWD="centos"
109
110         useradd -m -d /home/${USER_NAME} -s /bin/bash -U ${USER_NAME}
111         echo "${USER_NAME}:${USER_PWD}" | chpasswd
112         usermod -aG wheel ${USER_NAME}
113
114         echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheelnopass
115 }
116
117 function mblib_install()
118 {
119         export AESNI_MULTI_BUFFER_LIB_PATH="${BUILD_DIR}/intel-ipsec-mb-${MULTI_BUFFER_LIB_VER}"
120
121         # Downloading the Multi-buffer library. Note that the version to download is linked to the DPDK version being used
122         pushd ${BUILD_DIR} > /dev/null 2>&1
123         wget https://github.com/01org/intel-ipsec-mb/archive/v${MULTI_BUFFER_LIB_VER}.zip
124         unzip v${MULTI_BUFFER_LIB_VER}.zip
125         pushd ${AESNI_MULTI_BUFFER_LIB_PATH}
126         make -j`getconf _NPROCESSORS_ONLN`
127         ${SUDO} make install
128         popd > /dev/null 2>&1
129         popd > /dev/null 2>&1
130 }
131
132 function dpdk_install()
133 {
134         # Build DPDK for the latest kernel installed
135         LATEST_KERNEL_INSTALLED=`ls -v1 /lib/modules/ | tail -1`
136         export RTE_KERNELDIR="/lib/modules/${LATEST_KERNEL_INSTALLED}/build"
137
138         # Get and compile DPDK
139         pushd ${BUILD_DIR} > /dev/null 2>&1
140         wget http://fast.dpdk.org/rel/dpdk-${DPDK_VERSION}.tar.xz
141         tar -xf ./dpdk-${DPDK_VERSION}.tar.xz
142         popd > /dev/null 2>&1
143
144         ${SUDO} ln -s ${RTE_SDK} ${BUILD_DIR}/dpdk
145
146         pushd ${RTE_SDK} > /dev/null 2>&1
147         make config T=${RTE_TARGET}
148         # Starting from DPDK 20.05, the IGB_UIO driver is not compiled by default.
149     # Uncomment the sed command to enable the driver compilation
150     #${SUDO} sed -i 's/CONFIG_RTE_EAL_IGB_UIO=n/c\/CONFIG_RTE_EAL_IGB_UIO=y' ${RTE_SDK}/build/.config
151
152         # For Kubernetes environment we use host vfio module
153         if [ "${K8S_ENV}" == "y" ]; then
154                 sed -i 's/CONFIG_RTE_EAL_IGB_UIO=y/CONFIG_RTE_EAL_IGB_UIO=n/g' ${RTE_SDK}/build/.config
155                 sed -i 's/CONFIG_RTE_LIBRTE_KNI=y/CONFIG_RTE_LIBRTE_KNI=n/g' ${RTE_SDK}/build/.config
156                 sed -i 's/CONFIG_RTE_KNI_KMOD=y/CONFIG_RTE_KNI_KMOD=n/g' ${RTE_SDK}/build/.config
157     fi
158
159         # Compile with MB library
160         sed -i '/CONFIG_RTE_LIBRTE_PMD_AESNI_MB=n/c\CONFIG_RTE_LIBRTE_PMD_AESNI_MB=y' ${RTE_SDK}/build/.config
161         make -j`getconf _NPROCESSORS_ONLN`
162         ln -s ${RTE_SDK}/build ${RTE_SDK}/${RTE_TARGET}
163         popd > /dev/null 2>&1
164 }
165
166 function prox_compile()
167 {
168         # Compile PROX
169         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX
170         make -j`getconf _NPROCESSORS_ONLN`
171         ${SUDO} cp ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX/build/app/prox ${BUILD_DIR}/prox
172         popd > /dev/null 2>&1
173 }
174
175 function prox_install()
176 {
177         # Clone and compile PROX
178         pushd ${BUILD_DIR} > /dev/null 2>&1
179         git clone https://git.opnfv.org/samplevnf
180         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX > /dev/null 2>&1
181         bash -c "${PROX_CHECKOUT}"
182         popd > /dev/null 2>&1
183         prox_compile
184         popd > /dev/null 2>&1
185 }
186
187 function port_info_build()
188 {
189         [ ! -d ${BUILD_DIR}/port_info ] && echo "Skipping port_info compilation..." && return
190
191         pushd ${BUILD_DIR}/port_info > /dev/null 2>&1
192         make
193         ${SUDO} cp ${BUILD_DIR}/port_info/build/app/port_info_app ${BUILD_DIR}/port_info_app
194         popd > /dev/null 2>&1
195 }
196
197 function create_minimal_install()
198 {
199         ldd ${BUILD_DIR}/prox | awk '{ if ($(NF-1) != "=>") print $(NF-1) }' >> ${BUILD_DIR}/list_of_install_components
200
201         echo "${BUILD_DIR}/prox" >> ${BUILD_DIR}/list_of_install_components
202         echo "${BUILD_DIR}/port_info_app" >> ${BUILD_DIR}/list_of_install_components
203
204         tar -czvhf ${BUILD_DIR}/install_components.tgz -T ${BUILD_DIR}/list_of_install_components
205 }
206
207 function cleanup()
208 {
209         ${SUDO} yum autoremove -y
210         ${SUDO} yum clean all
211         ${SUDO} rm -rf /var/cache/yum
212 }
213
214 function k8s_runtime_image()
215 {
216         k8s_os_pkgs_runtime_install
217         k8s_os_cfg
218         cleanup
219
220         pushd / > /dev/null 2>&1
221         tar -xvf ${BUILD_DIR}/install_components.tgz --skip-old-files
222         popd > /dev/null 2>&1
223
224         ldconfig
225
226         #rm -rf ${BUILD_DIR}/install_components.tgz
227 }
228
229 function print_usage()
230 {
231         echo "Usage: ${0} [OPTIONS] [COMMAND]"
232         echo "Options:"
233         echo "   -u, --update     Full OS update"
234         echo "   -k, --kubernetes Build for Kubernetes environment"
235         echo "Commands:"
236         echo "   deploy           Run through all deployment steps"
237         echo "   compile          PROX compile only"
238         echo "   runtime_image    Apply runtime configuration only"
239 }
240
241 COMMAND=""
242 # Parse options and comman
243 for opt in "$@"; do
244         case ${opt} in
245                 -u|--update)
246                 echo 'Full OS update will be done!'
247                 OS_UPDATE="y"
248                 ;;
249                 -k|--kubernetes)
250                 echo "Kubernetes environment is set!"
251                 K8S_ENV="y"
252                 ;;
253                 compile)
254                 COMMAND="compile"
255                 ;;
256                 runtime_image)
257                 COMMAND="runtime_image"
258                 ;;
259                 deploy)
260                 COMMAND="deploy"
261                 ;;
262                 *)
263                 echo "Unknown option/command ${opt}"
264                 print_usage
265                 exit 1
266                 ;;
267         esac
268 done
269
270 if [ "${COMMAND}" == "compile" ]; then
271         echo "PROX compile only..."
272         prox_compile
273 elif [ "${COMMAND}" == "runtime_image" ]; then
274         echo "Runtime image intallation and configuration..."
275         k8s_runtime_image
276 elif [ "${COMMAND}" == "deploy" ]; then
277         [ ! -d ${BUILD_DIR} ] && ${SUDO} mkdir -p ${BUILD_DIR}
278         ${SUDO} chmod 0777 ${BUILD_DIR}
279
280         os_pkgs_install
281
282         if [ "${K8S_ENV}" == "y" ]; then
283                 k8s_os_cfg
284         else
285                 os_cfg
286         fi
287
288         mblib_install
289         dpdk_install
290         prox_install
291
292         if [ "${K8S_ENV}" == "y" ]; then
293                 port_info_build
294                 create_minimal_install
295         fi
296
297         cleanup
298 else
299         print_usage
300 fi