blob: 0cff780b43dccb7a499705912cf27ec91087da5d [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# 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
24# Authors: Antonio Lopez, Pablo Montes, Alfonso Tierno
25# 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-RHEL7.1.sh
34# To execute:
35# chmod +x ./configure-compute-node-RHEL7.1.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
44function usage(){
45 echo -e "Usage: sudo $0 [-y] <user-name> [ <iface-name> [<ip-address>|dhcp] ]"
46 echo -e " Configure compute host for VIM usage. (version 0.4). Params:"
47 echo -e " -y do not prompt for confirmation. If a new user is created, the user name is set as password"
48 echo -e " <user-name> Create if not exist and configure this user for openvim to connect"
49 echo -e " <iface-name> if suplied creates bridge interfaces on this interface, needed for openvim"
50 echo -e " ip or dhcp if suplied, configure the interface with this ip address (/24) or 'dhcp' "
51}
52
53
54#1 CHECK input parameters
55#1.1 root privileges
56[ "$USER" != "root" ] && echo "Needed root privileges" && usage && exit -1
57
58#1.2 input parameters
59FORCE=""
60while getopts "y" o; do
61 case "${o}" in
62 y)
63 FORCE="yes"
64 ;;
65 *)
66 usage
67 exit -1
68 ;;
69 esac
70done
71shift $((OPTIND-1))
72
73
74
75if [ $# -lt 1 ]
76then
77 usage
78 exit
79fi
80
81
82user_name=$1
83interface=$2
84ip_iface=$3
85
86if [ -n "$interface" ] && ! ifconfig $interface &> /dev/null
87then
88 echo "Error: interface '$interface' is not present in the system"
89 usage
90 exit 1
91fi
92
93echo '
94#################################################################
95##### INSTALL NEEDED PACKETS #####
96#################################################################'
97
98# Required packages
99apt-get -y update
100#apt-get -y install grub-common screen virt-manager ethtool build-essential x11-common x11-utils x11-apps libguestfs-tools hwloc libguestfs-tools numactl vlan nfs-common nfs-kernel-server
101apt-get -y install grub-common screen virt-manager ethtool build-essential x11-common x11-utils libguestfs-tools hwloc libguestfs-tools numactl vlan nfs-common nfs-kernel-server
102
103echo "Remove unneeded packages....."
104apt-get -y autoremove
105# Selinux management
106#yum install -y policycoreutils-python
107
108
109
110echo '
111#################################################################
112##### INSTALL USER #####
113#################################################################'
114
115# Add required groups
116groupadd -f admin
117groupadd -f libvirt #for other operating systems may be libvirtd
118
119# Adds user, default password same as name
120if grep -q "^${user_name}:" /etc/passwd
121then
122 #user exist, add to group
123 echo "adding user ${user_name} to groups libvirt,admin"
124 usermod -a -G libvirt,admin -g admin $user_name
125else
126 #create user if it does not exist
127 [ -z "$FORCE" ] && read -p "user '${user_name}' does not exist, create (Y/n)" kk
128 if ! [ -z "$kk" -o "$kk"="y" -o "$kk"="Y" ]
129 then
130 exit
131 fi
132 echo "creating and configuring user ${user_name}"
133 useradd -m -G libvirt,admin -g admin $user_name
134 #Password
135 if [ -z "$FORCE" ]
136 then
137 echo "Provide a password for $user_name"
138 passwd $user_name
139 else
140 echo -e "$user_name\n$user_name" | passwd --stdin $user_name
141 fi
142fi
143
144# Allow admin users to access without password
145if ! grep -q "#openmano" /etc/sudoers
146then
147 cat >> /home/${user_name}/script_visudo.sh << EOL
148#!/bin/bash
149cat \$1 | awk '(\$0~"requiretty"){print "#"\$0}(\$0!~"requiretty"){print \$0}' > tmp
150cat tmp > \$1
151rm tmp
152echo "" >> \$1
153echo "#openmano allow to group admin to grant root privileges without password" >> \$1
154echo "%admin ALL=(ALL) NOPASSWD: ALL" >> \$1
155EOL
156 chmod +x /home/${user_name}/script_visudo.sh
157 echo "allowing admin user to get root privileges withut password"
158 export EDITOR=/home/${user_name}/script_visudo.sh && sudo -E visudo
159 rm -f /home/${user_name}/script_visudo.sh
160fi
161
162
163echo '
164#################################################################
165##### INSTALL HUGEPAGES ISOLCPUS GRUB #####
166#################################################################'
167
168# Huge pages 1G auto mount
169mkdir -p /mnt/huge
170if ! grep -q "Huge pages" /etc/fstab
171then
172 echo "" >> /etc/fstab
173 echo "# Huge pages" >> /etc/fstab
174 echo "nodev /mnt/huge hugetlbfs pagesize=1GB 0 0" >> /etc/fstab
175 echo "" >> /etc/fstab
176fi
177
178# Grub virtualization options:
179
180# Get isolcpus
181isolcpus=`gawk 'BEGIN{pre=-2;}
182 ($1=="processor"){pro=$3;}
183 ($1=="core" && $4!=0){
184 if (pre+1==pro){endrange="-" pro}
185 else{cpus=cpus endrange sep pro; sep=","; endrange="";};
186 pre=pro;}
187 END{printf("%s",cpus endrange);}' /proc/cpuinfo`
188
189
190echo "CPUS: $isolcpus"
191
192# Huge pages reservation file: reserving all memory apart from 4GB per NUMA node
193# Get the number of hugepages: all memory but 8GB reserved for the OS
194#totalmem=`dmidecode --type 17|grep Size |grep MB |gawk '{suma+=$2} END {print suma/1024}'`
195#hugepages=$(($totalmem-8))
196
197if ! [ -f /usr/lib/systemd/hugetlb-reserve-pages ]
198then
199 cat > /usr/lib/systemd/hugetlb-reserve-pages << EOL
200#!/bin/bash
201nodes_path=/sys/devices/system/node/
202if [ ! -d \$nodes_path ]; then
203 echo "ERROR: \$nodes_path does not exist"
204 exit 1
205fi
206
207reserve_pages()
208{
209 echo \$1 > \$nodes_path/\$2/hugepages/hugepages-1048576kB/nr_hugepages
210}
211
212# This example reserves all available memory apart from 4 GB for linux
213# using 1GB size. You can modify it to your needs or comment the lines
214# to avoid reserve memory in a numa node
215EOL
216 for f in /sys/devices/system/node/node?/meminfo
217 do
218 node=`head -n1 $f | gawk '($5=="kB"){print $2}'`
219 memory=`head -n1 $f | gawk '($5=="kB"){print $4}'`
220 memory=$((memory+1048576-1)) #memory must be ceiled
221 memory=$((memory/1048576)) #from `kB to GB
222 #if memory
223 [ $memory -gt 4 ] && echo "reserve_pages $((memory-4)) node$node" >> /usr/lib/systemd/hugetlb-reserve-pages
224 done
225
226 # Run the following commands to enable huge pages early boot reservation:
227 chmod +x /usr/lib/systemd/hugetlb-reserve-pages
228 systemctl enable hugetlb-gigantic-pages
229fi
230
231# Prepares the text to add at the end of the grub line, including blacklisting ixgbevf driver in the host
232memtotal=`grep MemTotal /proc/meminfo | awk '{ print $2 }' `
233hpages=$(( ($memtotal/(1024*1024))-8 ))
234
235memtotal=$((memtotal+1048576-1)) #memory must be ceiled
236memtotal=$((memtotal/1048576)) #from `kB to GBa
237hpages=$((memtotal-8))
238[[ $hpages -lt 0 ]] $$ hpages=0
239
240
241echo "------> memtotal: $memtotal"
242
243textokernel="intel_iommu=on default_hugepagesz=1G hugepagesz=1G hugepages=$hpages isolcpus=$isolcpus modprobe.blacklist=ixgbevf modprobe.blacklist=i40evf"
244
245echo "Text to kernel: $textokernel"
246
247
248# Add text to the kernel line
249if ! grep -q "intel_iommu=on default_hugepagesz=1G hugepagesz=1G" /etc/default/grub
250then
251 echo ">>>>>>> adding cmdline ${textokernel}"
252 sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT=/s/\"\$/${textokernel}\"/" /etc/default/grub
253 # grub2 upgrade
254 #grub2-mkconfig -o /boot/grub2/grub.cfg
255 update-grub
256fi
257
258echo '
259#################################################################
260##### OTHER CONFIGURATION #####
261#################################################################'
262
263# Links the OpenMANO required folder /opt/VNF/images to /var/lib/libvirt/images. The OS installation
264# should have only a / partition with all possible space available
265
266echo " link /opt/VNF/images to /var/lib/libvirt/images"
267if [ "$user_name" != "" ]
268then
269 #mkdir -p /home/${user_name}/VNF_images
270 #chown -R ${user_name}:admin /home/${user_name}/VNF_images
271 #chmod go+x $HOME
272
273 # The orchestator needs to link the images folder
274 rm -f /opt/VNF/images
275 mkdir -p /opt/VNF/
276 ln -s /var/lib/libvirt/images /opt/VNF/images
277 chown -R ${user_name}:admin /opt/VNF
278 chown -R root:admin /var/lib/libvirt/images
279 chmod g+rwx /var/lib/libvirt/images
280
281 # Selinux management
282 #echo "configure Selinux management"
283 #semanage fcontext -a -t virt_image_t "/home/${user_name}/VNF_images(/.*)?"
284 #cat /etc/selinux/targeted/contexts/files/file_contexts.local |grep virt_image
285 #restorecon -R -v /home/${user_name}/VNF_images
286else
287 mkdir -p /opt/VNF/images
288 chmod o+rx /opt/VNF/images
289fi
290
291echo "creating local information /opt/VNF/images/hostinfo.yaml"
292echo "#By default openvim assumes control plane interface naming as em1,em2,em3,em4 " > /opt/VNF/images/hostinfo.yaml
293echo "#and bridge ifaces as virbrMan1, virbrMan2, ..." >> /opt/VNF/images/hostinfo.yaml
294echo "#if compute node contain a different name it must be indicated in this file" >> /opt/VNF/images/hostinfo.yaml
295echo "#with the format extandard-name: compute-name" >> /opt/VNF/images/hostinfo.yaml
296if [ "$interface" != "" -a "$interface" != "em1" ]
297then
298 echo "iface_names:" >> /opt/VNF/images/hostinfo.yaml
299 echo " em1: ${interface}" >> /opt/VNF/images/hostinfo.yaml
300fi
301chmod o+r /opt/VNF/images/hostinfo.yaml
302
303# deactivate memory overcommit
304#echo "deactivate memory overcommit"
305#service ksmtuned stop
306#service ksm stop
307#chkconfig ksmtuned off
308#chkconfig ksm off
309
310
311# Libvirt options (uncomment the following)
312echo "configure Libvirt options"
313sed -i 's/#unix_sock_group = "libvirt"/unix_sock_group = "libvirt"/' /etc/libvirt/libvirtd.conf
314sed -i 's/#unix_sock_rw_perms = "0770"/unix_sock_rw_perms = "0770"/' /etc/libvirt/libvirtd.conf
315sed -i 's/#unix_sock_dir = "\/var\/run\/libvirt"/unix_sock_dir = "\/var\/run\/libvirt"/' /etc/libvirt/libvirtd.conf
316sed -i 's/#auth_unix_rw = "none"/auth_unix_rw = "none"/' /etc/libvirt/libvirtd.conf
317
318
319echo '
320#################################################################
321##### NETWORK CONFIGURATION #####
322#################################################################'
323# Network config (if the second parameter is net)
324echo "Interface ==> $interface"
325if [ -n "$interface" ]
326then
327
328
329 # For management and data interfaces
330 rm -f /etc/udev/rules.d/pci_config.rules # it will be created to define VFs
331
332
333 # Set ONBOOT=on and MTU=9000 on the interface used for the bridges
334 echo "configuring iface $interface"
335
336#MTU for interfaces and bridges
337MTU=9000
338
339cp /etc/network/interfaces interfaces.tmp
340
341
342 #Create infrastructure bridge, normally used for connecting to compute nodes, openflow controller, ...
343
344
345 #Create VLAN for infrastructure bridge
346
347 echo "
348######### CUTLINE #########
349
350auto ${interface}
351iface ${interface} inet static
352 mtu $MTU
353
354auto ${interface}.1001
355iface ${interface}.1001 inet static
356 mtu $MTU
357" >> interfaces.tmp
358
359 echo "ifconfig ${interface} mtu $MTU
360 ifconfig ${interface} up
361" > mtu.tmp
362
363
364 #Create bridge interfaces
365 echo "Creating bridge ifaces: "
366 for ((i=1;i<=20;i++))
367 do
368 i2digits=$i
369 [ $i -lt 10 ] && i2digits="0$i"
370 echo " virbrMan$i vlan 20$i2digits"
371
372 j=$i
373
374 echo "
375auto ${interface}.20$i2digits
376iface ${interface}.20$i2digits inet static
377 mtu $MTU
378
379auto virbrMan$j
380iface virbrMan$j inet static
381 bridge_ports ${interface}.20$i2digits
382 mtu $MTU
383" >> interfaces.tmp
384
385 echo "ifconfig ${interface}.20$i2digits mtu $MTU
386ifconfig virbrMan$j mtu $MTU
387ifconfig virbrMan$j up
388" >> mtu.tmp
389
390 done
391
392 echo "
393auto em2.1001
394iface em2.1001 inet static
395
396auto virbrInf
397iface virbrInf inet static
398 bridge_ports em2.1001
399" >> interfaces.tmp
400
401 echo "ifconfig em2.1001 mtu $MTU
402ifconfig virbrInf mtu $MTU
403ifconfig virbrInf up
404" >> mtu.tmp
405
406if ! grep -q "#### CUTLINE ####" /etc/network/interfaces
407then
408 echo "====== Copying interfaces.tmp to /etc/network/interfaces"
409 cp interfaces.tmp /etc/network/interfaces
410fi
411
412
413 #popd
414fi
415
416
417# Activate 8 Virtual Functions per PF on Niantic cards (ixgbe driver)
418if [[ `lsmod | cut -d" " -f1 | grep "ixgbe" | grep -v vf` ]]
419then
420 if ! grep -q "ixgbe" /etc/modprobe.d/ixgbe.conf
421 then
422 echo "options ixgbe max_vfs=8" >> /etc/modprobe.d/ixgbe.conf
423 fi
424
425fi
426
427# Set dataplane MTU
428
429echo "sleep 10" >> mtu.tmp
430
431interfaces=`ifconfig -a | grep ^p | cut -d " " -f 1`
432for ph in $interfaces
433do
434 echo "ifconfig $ph mtu $MTU" >> mtu.tmp
435 echo "ifconfig $ph up" >> mtu.tmp
436done
437
438
439
440cp mtu.tmp /etc/setmtu.sh
441chmod +x /etc/setmtu.sh
442
443# To define 8 VFs per PF we do it on rc.local, because the driver needs to be unloaded and loaded again
444#if ! grep -q "NFV" /etc/rc.local
445#then
446 echo "#!/bin/sh -e
447" > /etc/rc.local
448 echo "# NFV" >> /etc/rc.local
449 echo "modprobe -r ixgbe" >> /etc/rc.local
450 echo "modprobe ixgbe max_vfs=8" >> /etc/rc.local
451 echo "/etc/setmtu.sh" >> /etc/rc.local
452 echo "
453exit 0" >> /etc/rc.local
454 echo "" >> /etc/rc.local
455
456 chmod +x /etc/rc.d/rc.local
457
458#fi
459
460chmod a+rwx /var/lib/libvirt/images
461mkdir /usr/libexec/
462pushd /usr/libexec/
463ln -s /usr/bin/qemu-system-x86_64 qemu-kvm
464popd
465
466#Deactivating apparmor while looking for a better solution
467/etc/init.d/apparmor stop
468update-rc.d -f apparmor remove
469
470echo
471echo "Do not forget to create a shared (NFS, Samba, ...) where original virtual machine images are allocated"
472echo
473echo "Do not forget to copy the public ssh key into /home/${user_name}/.ssh/authorized_keys for authomatic login from openvim controller"
474echo
475
476echo "Reboot the system to make the changes effective"
477
478