blob: ff049db2e6695399b95bfea576b56f832b7050ab [file] [log] [blame]
Mirabal92c96c62016-12-07 16:10:56 +00001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openvim
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
Mirabalbb334592016-12-29 13:55:40 +000024# Authors: Antonio Lopez, Pablo Montes, Alfonso Tierno, Leonardo Mirabal
Mirabal92c96c62016-12-07 16:10:56 +000025# June 2015
26
27# Personalize RHEL7.1 on compute nodes
28# Prepared to work with the following network card drivers:
29# tg3, igb drivers for management interfaces
30# ixgbe (Intel Niantic) and i40e (Intel Fortville) drivers for data plane interfaces
31
32# To download:
33# wget https://raw.githubusercontent.com/nfvlabs/openmano/master/scripts/configure-compute-node-UBUNTU14.04.sh
34# To execute:
35# chmod +x ./configure-compute-node-UBUNTU14.04.sh
36# sudo ./configure-compute-node-RHEL7.1.sh <user> <iface>
37
38# Assumptions:
39# All virtualization options activated on BIOS (vt-d, vt-x, SR-IOV, no power savings...)
40# RHEL7.1 installed without /home partition and with the following packages selection:
41# @base, @core, @development, @network-file-system-client, @virtualization-hypervisor, @virtualization-platform, @virtualization-tools
42
43
Mirabalbb334592016-12-29 13:55:40 +000044
Mirabal92c96c62016-12-07 16:10:56 +000045function usage(){
Mirabalbb334592016-12-29 13:55:40 +000046 echo -e "Usage: sudo $0 [-f] <user-name> [<iface-for-overlay-bridges>]"
Mirabal92c96c62016-12-07 16:10:56 +000047 echo -e " Configure compute host for VIM usage. (version 0.4). Params:"
Mirabalbb334592016-12-29 13:55:40 +000048 echo -e " -f do not prompt for confirmation. If a new user is created, the user name is set as password"
Mirabal92c96c62016-12-07 16:10:56 +000049 echo -e " <user-name> Create if not exist and configure this user for openvim to connect"
Mirabalbb334592016-12-29 13:55:40 +000050 echo -e " [<iface-for-overlay-bridges>] Only needed for old openvim versions. Iface to create pre-provioned bridges for network conectivity"
51
Mirabal92c96c62016-12-07 16:10:56 +000052}
53
54
55#1 CHECK input parameters
56#1.1 root privileges
57[ "$USER" != "root" ] && echo "Needed root privileges" && usage && exit -1
58
59#1.2 input parameters
60FORCE=""
Mirabalbb334592016-12-29 13:55:40 +000061while getopts "f" o; do
Mirabal92c96c62016-12-07 16:10:56 +000062 case "${o}" in
Mirabalbb334592016-12-29 13:55:40 +000063 f)
Mirabal92c96c62016-12-07 16:10:56 +000064 FORCE="yes"
65 ;;
66 *)
67 usage
68 exit -1
69 ;;
70 esac
71done
72shift $((OPTIND-1))
73
74
75
76if [ $# -lt 1 ]
77then
78 usage
79 exit
80fi
81
82
83user_name=$1
84interface=$2
Mirabal92c96c62016-12-07 16:10:56 +000085
Mirabalbb334592016-12-29 13:55:40 +000086
87if [ -z $interface ] && ! ifconfig $interface &> /dev/null
Mirabal92c96c62016-12-07 16:10:56 +000088then
89 echo "Error: interface '$interface' is not present in the system"
90 usage
91 exit 1
92fi
93
94echo '
95#################################################################
96##### INSTALL NEEDED PACKETS #####
97#################################################################'
98
99# Required packages
Mirabalbb334592016-12-29 13:55:40 +0000100apt-get -y update
101apt-get -y install grub-common screen virt-manager ethtool build-essential x11-common x11-utils \
102 libguestfs-tools hwloc libguestfs-tools numactl vlan nfs-common nfs-kernel-server openvswitch-switch
Mirabal92c96c62016-12-07 16:10:56 +0000103echo "Remove unneeded packages....."
104apt-get -y autoremove
105# Selinux management
106#yum install -y policycoreutils-python
107
Mirabal92c96c62016-12-07 16:10:56 +0000108echo '
109#################################################################
110##### INSTALL USER #####
111#################################################################'
112
113# Add required groups
114groupadd -f admin
115groupadd -f libvirt #for other operating systems may be libvirtd
116
117# Adds user, default password same as name
118if grep -q "^${user_name}:" /etc/passwd
119then
120 #user exist, add to group
121 echo "adding user ${user_name} to groups libvirt,admin"
122 usermod -a -G libvirt,admin -g admin $user_name
123else
124 #create user if it does not exist
125 [ -z "$FORCE" ] && read -p "user '${user_name}' does not exist, create (Y/n)" kk
126 if ! [ -z "$kk" -o "$kk"="y" -o "$kk"="Y" ]
127 then
128 exit
129 fi
130 echo "creating and configuring user {user_name}"
131 useradd -m -G libvirt,admin -g admin $user_name
132 #Password
133 if [ -z "$FORCE" ]
134 then
135 echo "Provide a password for $user_name"
136 passwd $user_name
137 else
138 echo -e "$user_name\n$user_name" | passwd --stdin $user_name
139 fi
140fi
141
Mirabalbb334592016-12-29 13:55:40 +0000142# Allow admin users to access without password
143if ! grep -q "#openmano" /etc/sudoers
144then
145 cat >> /home/${user_name}/script_visudo.sh << EOL
146#!/bin/bash
147echo "#openmano allow to group admin to grant root privileges without password" >> \$1
148echo "${user_name} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
149EOL
150 chmod +x /home/${user_name}/script_visudo.sh
151 echo "allowing admin user to get root privileges withut password"
152 export EDITOR=/home/${user_name}/script_visudo.sh && sudo -E visudo
153 rm -f /home/${user_name}/script_visudo.sh
154fi
Mirabal92c96c62016-12-07 16:10:56 +0000155
156echo '
157#################################################################
158##### INSTALL HUGEPAGES ISOLCPUS GRUB #####
159#################################################################'
160
161# Huge pages 1G auto mount
162mkdir -p /mnt/huge
163if ! grep -q "Huge pages" /etc/fstab
164then
165 echo "" >> /etc/fstab
166 echo "# Huge pages" >> /etc/fstab
167 echo "nodev /mnt/huge hugetlbfs pagesize=1GB 0 0" >> /etc/fstab
168 echo "" >> /etc/fstab
169fi
170
171# Huge pages reservation service
172if ! [ -f /lib/systemd/system/hugetlb-gigantic-pages.service ]
173then
174 echo "configuring huge pages service"
175 echo "[Unit]
176Description=HugeTLB Gigantic Pages Reservation
177DefaultDependencies=no
178Before=dev-hugepages.mount
179ConditionPathExists=/sys/devices/system/node
180ConditionKernelCommandLine=hugepagesz=1G
181
182[Service]
183Type=oneshot
184RemainAfterExit=yes
185ExecStart=/usr/lib/systemd/hugetlb-reserve-pages
186
187[Install]
188WantedBy=sysinit.target" >> /lib/systemd/system/hugetlb-gigantic-pages.service
189
190 # cat > /usr/lib/systemd/system/hugetlb-gigantic-pages.service << EOL
191 #[Unit]
192 #Description=HugeTLB Gigantic Pages Reservation
193 #DefaultDependencies=no
194 #Before=dev-hugepages.mount
195 #ConditionPathExists=/sys/devices/system/node
196 #ConditionKernelCommandLine=hugepagesz=1G
197 #
198 #[Service]
199 #Type=oneshot
200 #RemainAfterExit=yes
201 #ExecStart=/usr/lib/systemd/hugetlb-reserve-pages
202 #
203 #[Install]
204 #WantedBy=sysinit.target
205 #EOL
206fi
207
208
209# Get isolcpus
210isolcpus=`gawk 'BEGIN{pre=-2;}
211 ($1=="processor"){pro=$3;}
212 ($1=="core" && $4!=0){
213 if (pre+1==pro){endrange="-" pro}
214 else{cpus=cpus endrange sep pro; sep=","; endrange="";};
215 pre=pro;}
216 END{printf("%s",cpus endrange);}' /proc/cpuinfo`
217
218
219echo "CPUS: $isolcpus"
220
221# Huge pages reservation file: reserving all memory apart from 4GB per NUMA node
222# Get the number of hugepages: all memory but 8GB reserved for the OS
223#totalmem=`dmidecode --type 17|grep Size |grep MB |gawk '{suma+=$2} END {print suma/1024}'`
224#hugepages=$(($totalmem-8))
225
226if ! [ -f /usr/lib/systemd/hugetlb-reserve-pages ]
227then
228 cat > /usr/lib/systemd/hugetlb-reserve-pages << EOL
229#!/bin/bash
230nodes_path=/sys/devices/system/node/
231if [ ! -d \$nodes_path ]; then
232 echo "ERROR: \$nodes_path does not exist"
233 exit 1
234fi
235
236reserve_pages()
237{
238 echo \$1 > \$nodes_path/\$2/hugepages/hugepages-1048576kB/nr_hugepages
239}
240
241# This example reserves all available memory apart from 4 GB for linux
242# using 1GB size. You can modify it to your needs or comment the lines
243# to avoid reserve memory in a numa node
244EOL
245 for f in /sys/devices/system/node/node?/meminfo
246 do
247 node=`head -n1 $f | gawk '($5=="kB"){print $2}'`
248 memory=`head -n1 $f | gawk '($5=="kB"){print $4}'`
249 memory=$((memory+1048576-1)) #memory must be ceiled
250 memory=$((memory/1048576)) #from `kB to GB
251 #if memory
252 [ $memory -gt 4 ] && echo "reserve_pages $((memory-4)) node$node" >> /usr/lib/systemd/hugetlb-reserve-pages
253 done
254
255 # Run the following commands to enable huge pages early boot reservation:
256 chmod +x /usr/lib/systemd/hugetlb-reserve-pages
257 systemctl enable hugetlb-gigantic-pages
258fi
259
260# Prepares the text to add at the end of the grub line, including blacklisting ixgbevf driver in the host
261memtotal=`grep MemTotal /proc/meminfo | awk '{ print $2 }' `
262hpages=$(( ($memtotal/(1024*1024))-8 ))
263
264memtotal=$((memtotal+1048576-1)) #memory must be ceiled
265memtotal=$((memtotal/1048576)) #from `kB to GBa
266hpages=$((memtotal-8))
267[[ $hpages -lt 0 ]] && hpages=0
268
269
270echo "------> memtotal: $memtotal"
271
272textokernel=" intel_iommu=on default_hugepagesz=1G hugepagesz=1G hugepages=$hpages isolcpus=$isolcpus modprobe.blacklist=ixgbevf modprobe.blacklist=i40evf"
273
274echo "Text to kernel: $textokernel"
275
276
277# Add text to the kernel line
278if ! grep -q " intel_iommu=on default_hugepagesz=1G hugepagesz=1G" /etc/default/grub
279then
280 echo ">>>>>>> adding cmdline ${textokernel}"
281 sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT=/s/\"\$/${textokernel}\"/" /etc/default/grub
282 sed -i "/^GRUB_CMDLINE_LINUX=/s/\"\$/${textokernel}\"/" /etc/default/grub
283
284 update-grub
285 # BIOS based systems
286 grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfg
287 # UEFI based systems
288 grub-mkconfig -o /boot/grub/grub.cfg
289fi
290
291echo '
292#################################################################
293##### OTHER CONFIGURATION #####
294#################################################################'
295
296# Links the OpenMANO required folder /opt/VNF/images to /var/lib/libvirt/images. The OS installation
297# should have only a / partition with all possible space available
298
299echo " link /opt/VNF/images to /var/lib/libvirt/images"
300if [ "$user_name" != "" ]
301then
302 #mkdir -p /home/${user_name}/VNF_images
303 #chown -R ${user_name}:admin /home/${user_name}/VNF_images
304 #chmod go+x $HOME
305
306 # The orchestator needs to link the images folder
307 rm -f /opt/VNF/images
308 mkdir -p /opt/VNF/
309 ln -s /var/lib/libvirt/images /opt/VNF/images
310 chown -R ${user_name}:admin /opt/VNF
311 chown -R root:admin /var/lib/libvirt/images
312 chmod g+rwx /var/lib/libvirt/images
313
314 # Selinux management
315 #echo "configure Selinux management"
316 #semanage fcontext -a -t virt_image_t "/home/${user_name}/VNF_images(/.*)?"
317 #cat /etc/selinux/targeted/contexts/files/file_contexts.local |grep virt_image
318 #restorecon -R -v /home/${user_name}/VNF_images
319else
320 mkdir -p /opt/VNF/images
321 chmod o+rx /opt/VNF/images
322fi
323
324echo "creating local information /opt/VNF/images/hostinfo.yaml"
325echo "#By default openvim assumes control plane interface naming as em1,em2,em3,em4 " > /opt/VNF/images/hostinfo.yaml
326echo "#and bridge ifaces as virbrMan1, virbrMan2, ..." >> /opt/VNF/images/hostinfo.yaml
327echo "#if compute node contain a different name it must be indicated in this file" >> /opt/VNF/images/hostinfo.yaml
328echo "#with the format extandard-name: compute-name" >> /opt/VNF/images/hostinfo.yaml
329if [ "$interface" != "" -a "$interface" != "em1" ]
330then
331 echo "iface_names:" >> /opt/VNF/images/hostinfo.yaml
332 echo " em1: ${interface}" >> /opt/VNF/images/hostinfo.yaml
333fi
334chmod o+r /opt/VNF/images/hostinfo.yaml
335
336# deactivate memory overcommit
337#echo "deactivate memory overcommit"
338#service ksmtuned stop
339#service ksm stop
340#chkconfig ksmtuned off
341#chkconfig ksm off
342
343
344# Libvirt options (uncomment the following)
345echo "configure Libvirt options"
346sed -i 's/#unix_sock_group = "libvirt"/unix_sock_group = "libvirt"/' /etc/libvirt/libvirtd.conf
347sed -i 's/#unix_sock_rw_perms = "0770"/unix_sock_rw_perms = "0770"/' /etc/libvirt/libvirtd.conf
348sed -i 's/#unix_sock_dir = "\/var\/run\/libvirt"/unix_sock_dir = "\/var\/run\/libvirt"/' /etc/libvirt/libvirtd.conf
349sed -i 's/#auth_unix_rw = "none"/auth_unix_rw = "none"/' /etc/libvirt/libvirtd.conf
350
351
352echo '
353#################################################################
354##### NETWORK CONFIGURATION #####
355#################################################################'
356# Network config (if the second parameter is net)
357echo "Interface ==> $interface"
358if [ -n "$interface" ]
359then
360
Mirabalbb334592016-12-29 13:55:40 +0000361 # For management and data interfaces
362 rm -f /etc/udev/rules.d/pci_config.rules # it will be created to define VFs
Mirabal92c96c62016-12-07 16:10:56 +0000363
364
Mirabalbb334592016-12-29 13:55:40 +0000365 # Set ONBOOT=on and MTU=9000 on the interface used for the bridges
366 echo "configuring iface $interface"
Mirabal92c96c62016-12-07 16:10:56 +0000367
Mirabalbb334592016-12-29 13:55:40 +0000368 #MTU for interfaces and bridges
369 MTU=9000
Mirabal92c96c62016-12-07 16:10:56 +0000370
Mirabalbb334592016-12-29 13:55:40 +0000371 cp /etc/network/interfaces interfaces.tmp
Mirabal92c96c62016-12-07 16:10:56 +0000372
373
Mirabalbb334592016-12-29 13:55:40 +0000374 #Create infrastructure bridge, normally used for connecting to compute nodes, openflow controller, ...
Mirabal92c96c62016-12-07 16:10:56 +0000375 #Create VLAN for infrastructure bridge
376
Mirabalbb334592016-12-29 13:55:40 +0000377echo "
Mirabal92c96c62016-12-07 16:10:56 +0000378######### CUTLINE #########
379
380auto ${interface}
381iface ${interface} inet manual
Mirabalbb334592016-12-29 13:55:40 +0000382post-up ip link set dev ${interface} mtu ${MTU}
Mirabal92c96c62016-12-07 16:10:56 +0000383
384auto ${interface}.1001
385iface ${interface}.1001 inet manual
Mirabalbb334592016-12-29 13:55:40 +0000386vlan-raw-device ${interface}
387post-up ip link set mtu $MTU dev ${interface}.1001
Mirabal92c96c62016-12-07 16:10:56 +0000388" >> interfaces.tmp
389
Mirabalbb334592016-12-29 13:55:40 +0000390 # echo "ifconfig ${interface} mtu $MTU
391 # ifconfig ${interface} up
392 #" > mtu.tmp
Mirabal92c96c62016-12-07 16:10:56 +0000393
394
Mirabalbb334592016-12-29 13:55:40 +0000395 #Create bridge interfaces
396 echo "Creating bridge ifaces: "
397 for ((i=1;i<=20;i++))
398 do
399 i2digits=$i
400 [ $i -lt 10 ] && i2digits="0$i"
401 echo " virbrMan$i vlan 20$i2digits"
Mirabal92c96c62016-12-07 16:10:56 +0000402
Mirabalbb334592016-12-29 13:55:40 +0000403 j=$i
Mirabal92c96c62016-12-07 16:10:56 +0000404
Mirabalbb334592016-12-29 13:55:40 +0000405echo "
Mirabal92c96c62016-12-07 16:10:56 +0000406auto ${interface}.20$i2digits
407iface ${interface}.20$i2digits inet manual
Mirabalbb334592016-12-29 13:55:40 +0000408vlan-raw-device ${interface}
409post-up ip link set mtu $MTU dev ${interface}.20$i2digits
Mirabal92c96c62016-12-07 16:10:56 +0000410
411auto virbrMan$j
412iface virbrMan$j inet manual
Mirabalbb334592016-12-29 13:55:40 +0000413bridge_ports ${interface}.20$i2digits
414post-up ip link set dev virbrMan$j && ip link set mtu $MTU dev virbrMan$j
Mirabal92c96c62016-12-07 16:10:56 +0000415" >> interfaces.tmp
416
Mirabalbb334592016-12-29 13:55:40 +0000417 done
Mirabal92c96c62016-12-07 16:10:56 +0000418
Mirabalbb334592016-12-29 13:55:40 +0000419 # echo "ifconfig em2.1001 mtu $MTU
420 #ifconfig virbrInf mtu $MTU
421 #ifconfig virbrInf up
422 #" >> mtu.tmp
Mirabal92c96c62016-12-07 16:10:56 +0000423
Mirabalbb334592016-12-29 13:55:40 +0000424 if ! grep -q "#### CUTLINE ####" /etc/network/interfaces
425 then
426 echo "====== Copying interfaces.tmp to /etc/network/interfaces"
427 cp interfaces.tmp /etc/network/interfaces
428 fi
Mirabal92c96c62016-12-07 16:10:56 +0000429
430
431 #popd
432fi
433
Mirabalbb334592016-12-29 13:55:40 +0000434################### Activate 8 Virtual Functions per PF on Niantic cards (ixgbe driver)
Mirabal92c96c62016-12-07 16:10:56 +0000435if [[ `lsmod | cut -d" " -f1 | grep "ixgbe" | grep -v vf` ]]
Mirabalbb334592016-12-29 13:55:40 +0000436 then
437 if ! grep -q "ixgbe" /etc/modprobe.d/ixgbe.conf
438 then
439 echo "options ixgbe max_vfs=8" >> /etc/modprobe.d/ixgbe.conf
440 fi
Mirabal92c96c62016-12-07 16:10:56 +0000441
Mirabalbb334592016-12-29 13:55:40 +0000442 fi
Mirabal92c96c62016-12-07 16:10:56 +0000443
444echo "#!/bin/bash" > /etc/activate-vfs.sh
445chmod +x /etc/activate-vfs.sh
446for iface in `ip -o link show | awk -F': ' '{print $2}' | grep -v -e "\." -e "lo" -e "virbr" -e "tap"`
447do
448# 10/40 Gbps interfaces
449# Intel X520 cards: driver ixgbe
450# Intel XL710 Fortville cards: driver i40e
451driver=`ethtool -i $iface| awk '($0~"driver"){print $2}'`
452if [ "$driver" = "i40e" -o "$driver" = "ixgbe" ]
453then
454 echo "configuring dataplane iface $iface"
455
456 # Create 8 SR-IOV per PF by udev rules only for Fortville cards (i40e driver)
457 if [ "$driver" = "i40e" ]
458 then
459 pci=`ethtool -i $iface | awk '($0~"bus-info"){print $2}'`
460 echo "echo 8 > /sys/bus/pci/devices/$pci/sriov_numvfs" >> /etc/activate-vfs.sh
461 fi
462fi
463done
464
465# Set dataplane MTU
466
467#echo "sleep 10" >> mtu.tmp
468
469#interfaces=`ifconfig -a | grep ^p | cut -d " " -f 1`
470#for ph in $interfaces
471#do
472# echo "ifconfig $ph mtu $MTU" >> mtu.tmp
473# echo "ifconfig $ph up" >> mtu.tmp
474#done
475
476
477
478#cp mtu.tmp /etc/setmtu.sh
479#chmod +x /etc/setmtu.sh
480
481# To define 8 VFs per PF we do it on rc.local, because the driver needs to be unloaded and loaded again
482#if ! grep -q "NFV" /etc/rc.local
483#then
484 echo "#!/bin/sh -e
485" > /etc/rc.local
486 echo "# NFV" >> /etc/rc.local
487 echo "modprobe -r ixgbe" >> /etc/rc.local
488 echo "modprobe ixgbe max_vfs=8" >> /etc/rc.local
489 echo "/etc/activate-vfs.sh" >> /etc/rc.local
490 #echo "/etc/setmtu.sh" >> /etc/rc.local
491 echo "
492exit 0" >> /etc/rc.local
493 echo "" >> /etc/rc.local
494
495 chmod +x /etc/rc.local
496 chmod +x /etc/activate-vfs.sh
497
498#fi
499
500chmod a+rwx /var/lib/libvirt/images
Mirabalbb334592016-12-29 13:55:40 +0000501mkdir -p /usr/libexec/
Mirabal92c96c62016-12-07 16:10:56 +0000502pushd /usr/libexec/
503ln -s /usr/bin/qemu-system-x86_64 qemu-kvm
504popd
505
506#Deactivating apparmor while looking for a better solution
507/etc/init.d/apparmor stop
508update-rc.d -f apparmor remove
509
510echo
511echo "Do not forget to create a shared (NFS, Samba, ...) where original virtual machine images are allocated"
512echo
513echo "Do not forget to copy the public ssh key into /home/${user_name}/.ssh/authorized_keys for authomatic login from openvim controller"
514echo
515
516echo "Reboot the system to make the changes effective"
517