4abe3c778738b0cd54cea52b4571937e9dc143e9
[osm/openvim.git] / scripts / configure-compute-node-develop-UBUNTU16.04.sh
1 #!/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
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-develope-UBUNTU16.04.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 interfaced_path='/etc/network/interfaces.d/'
44 #interfaced_path='/home/ubuntu/openvim_install/openvim/test-inter/'
45 set_mtu_path='/etc/'
46 VLAN_INDEX=20
47 function _usage(){
48
49 echo -e "Usage: sudo $0 [-y] <user-name> <iface-name>"
50 echo -e " Configure compute host for VIM usage. (version 0.4). Params:"
51 echo -e " -y do not prompt for confirmation. If a new user is created, the user name is set as password"
52 echo -e " <user-name> Create if not exist and configure this user for openvim to connect"
53 echo -e " <iface-name> if suplied creates bridge interfaces on this interface, needed for openvim"
54 }
55
56 function _interface_cfg_generator(){
57 #$1 interface name | $2 MTU | $3 type
58
59 echo "
60 auto ${1}
61 iface ${1} inet ${3}
62 mtu ${2}
63 ${bridge_ports}
64 " >> ${interfaced_path}${1}."cfg"
65 }
66
67
68 function _interface_cfg_generator(){
69 #$1 interface name | $2 vlan | $3 virbrMan | $4 MTU
70
71 echo "
72 auto ${1}.${2}
73 iface ${1}.${2} inet manual
74 mtu ${4}
75 post-up vconfig add ${1} ${2}
76 post-down vconfig rem ${1}.${2}
77
78 auto ${3}
79 iface ${3} inet manual
80 bridge_ports ${1}.${2}
81 mtu ${4}
82 vlan-raw-device $1
83 " >> ${interfaced_path}${1}.${2}."cfg"
84 }
85
86 function _install_user() {
87 # create user given by the user and add to groups need it.
88 # Add required groups
89 groupadd -f admin
90 groupadd -f libvirt #for other operating systems may be libvirtd
91
92 # Adds user, default password same as name
93 if grep -q "^${user_name}:" /etc/passwd
94 then
95 #user exist, add to group
96 echo "adding user ${user_name} to groups libvirt,admin"
97 usermod -a -G libvirt,admin -g admin ${user_name}
98 else
99 #create user if it does not exist
100 [ -z "$FORCE" ] && read -p "user '${user_name}' does not exist, create (Y/n)" kk
101 if ! [ -z "$kk" -o "$kk"="y" -o "$kk"="Y" ]
102 then
103 exit
104 fi
105 echo "creating and configuring user ${user_name}"
106 useradd -m -G libvirt,admin -g admin ${user_name}
107 #Password
108 if [ -z "$FORCE" ]
109 then
110 echo "Provide a password for ${user_name}"
111 passwd ${user_name}
112 else
113 echo -e "$user_name\n$user_name" | passwd --stdin ${user_name}
114 fi
115 fi
116
117 }
118
119 function _openmano_img_2_libvirt_img(){
120 # Links the OpenMANO required folder /opt/VNF/images to /var/lib/libvirt/images.
121 # The OS installation
122 # should have only a / partition with all possible space available
123
124 echo " link /opt/VNF/images to /var/lib/libvirt/images"
125 if [ "$user_name" != "" ]
126 then
127 # The orchestator needs to link the images folder
128 rm -f /opt/VNF/images
129 mkdir -p /opt/VNF/
130 ln -s /var/lib/libvirt/images /opt/VNF/images
131 chown -R ${user_name}:admin /opt/VNF
132 chown -R root:admin /var/lib/libvirt/images
133 chmod g+rwx /var/lib/libvirt/images
134 else
135 mkdir -p /opt/VNF/images
136 chmod o+rx /opt/VNF/images
137 fi
138 }
139
140 function _install_pacckags_dependences()
141 {
142 # Required packages by openvim
143 apt-get -y update
144 apt-get -y install grub-common screen virt-manager ethtool build-essential \
145 x11-common x11-utils libguestfs-tools hwloc libguestfs-tools \
146 numactl vlan nfs-common nfs-kernel-server
147 echo "Remove unneeded packages....."
148 apt-get -y autoremove
149 }
150
151 function _network_configuration(){
152 # adding vlan support
153 grep -q '8021q' '/etc/modules'; [ $? -eq 1 ] && sudo su -c 'echo "8021q" >> /etc/modules'
154
155 #grep -q ${interface} '/etc/network/interfaces.d/50-cloud-init.cfg'; [ $? -eq 0 ] && sed -e '/'${interface}'/ s/^#*/#/' -i '/etc/network/interfaces.d/50-cloud-init.cfg'
156
157 # Network interfaces static configuration
158 echo "Interface ==> $interface"
159 if [ -n "$interface" ]
160 then
161 # For management and data interfaces
162 rm -f /etc/udev/rules.d/pci_config.rules # it will be created to define VFs
163 # Set ONBOOT=on and MTU=9000 on the interface used for the bridges
164 echo "configuring iface $interface"
165
166 # Static network interface configuration and MTU
167 MTU=9000
168 virbrMan_interface_number=20
169
170 #Create bridge interfaces
171 echo "Creating bridge ifaces: "
172 for ((i =1; i <= ${virbrMan_interface_number}; i++))
173 do
174 i2digits=${i}
175 [ ${i} -lt 10 ] && i2digits="0${i}"
176 echo " ${interface} ${VLAN_INDEX}${i2digits}"
177 echo " virbrMan${i} vlan ${VLAN_INDEX}${i2digits}"
178 j=${i}
179 #$1 interface name | $2 vlan | $3 MTU | $3 virbrMan | $4 bridge_ports
180 _interface_cfg_generator ${interface} ${VLAN_INDEX}${i2digits} 'virbrMan'${i} ${MTU}
181 done
182
183 fi
184 }
185
186 function _disable_aaparmor(){
187 #Deactivating apparmor while looking for a better solution
188 /etc/init.d/apparmor stop
189 update-rc.d -f apparmor remove
190 }
191
192 function _check_interface(){
193 #check if interface given as an argument exits
194 if [ -n "$1" ] && ! ifconfig $1 &> /dev/null
195 then
196 echo "Error: interface '$1' is not present in the system"\n
197 _usage
198 exit 1
199 fi
200 }
201
202 function _user_remainder_pront()
203 {
204 echo
205 echo "Do not forget to create a shared (NFS, Samba, ...) where original virtual machine images are allocated"
206 echo
207 echo "Do not forget to copy the public ssh key into /home/${user_name}/.ssh/authorized_keys for authomatic login from openvim controller"
208 echo
209 echo "Reboot the system to make the changes effective"
210 }
211
212 function _libvirt_configuration(){
213 # Libvirt options for openvim
214 echo "configure Libvirt options"
215 sed -i 's/#unix_sock_group = "libvirt"/unix_sock_group = "libvirt"/' /etc/libvirt/libvirtd.conf
216 sed -i 's/#unix_sock_rw_perms = "0770"/unix_sock_rw_perms = "0770"/' /etc/libvirt/libvirtd.conf
217 sed -i 's/#unix_sock_dir = "\/var\/run\/libvirt"/unix_sock_dir = "\/var\/run\/libvirt"/' /etc/libvirt/libvirtd.conf
218 sed -i 's/#auth_unix_rw = "none"/auth_unix_rw = "none"/' /etc/libvirt/libvirtd.conf
219
220 chmod a+rwx /var/lib/libvirt/images
221 mkdir /usr/libexec/
222 pushd /usr/libexec/
223 ln -s /usr/bin/qemu-system-x86_64 qemu-kvm
224 popd
225 }
226
227 function _hostinfo_config()
228 {
229
230 echo "#By default openvim assumes control plane interface naming as em1,em2,em3,em4 " > /opt/VNF/images/hostinfo.yaml
231 echo "creating local information /opt/VNF/images/hostinfo.yaml"
232 echo "#and bridge ifaces as virbrMan1, virbrMan2, ..." >> /opt/VNF/images/hostinfo.yaml
233 echo "#if compute node contain a different name it must be indicated in this file" >> /opt/VNF/images/hostinfo.yaml
234 echo "#with the format extandard-name: compute-name" >> /opt/VNF/images/hostinfo.yaml
235 chmod o+r /opt/VNF/images/hostinfo.yaml
236 }
237
238 function _get_opts()
239 {
240 [[ ${BASH_SOURCE[0]} != $0 ]] && ___exit="return" || ___exit="exit"
241
242 options="$1"
243 shift
244
245 get_argument=""
246 #reset variables
247 params=""
248 for option_group in $options
249 do
250 _name=${option_group%%:*}
251 _name=${_name%=}
252 _name=${_name//-/_}
253 eval option_${_name}='""'
254 done
255
256 while [[ $# -gt 0 ]]
257 do
258 argument="$1"
259 shift
260 if [[ -n $get_argument ]]
261 then
262 [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
263 eval ${get_argument}='"$argument"'
264 #echo option $get_argument with argument
265 get_argument=""
266 continue
267 fi
268
269
270 #short options
271 if [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
272 then
273 index=0
274 while index=$((index+1)) && [[ $index -lt ${#argument} ]]
275 do
276 option=${argument:$index:1}
277 bad_option=y
278 for option_group in $options
279 do
280 _name=""
281 for o in $(echo $option_group | tr ":=" " ")
282 do
283 [[ -z "$_name" ]] && _name=${o//-/_}
284 #echo option $option versus $o
285 if [[ "$option" == "${o}" ]]
286 then
287 eval option_${_name}='${option_'${_name}'}-'
288 bad_option=n
289 if [[ ${option_group:${#option_group}-1} != "=" ]]
290 then
291 continue
292 fi
293 if [[ ${#argument} -gt $((index+1)) ]]
294 then
295 eval option_${_name}='"${argument:$((index+1))}"'
296 index=${#argument}
297 else
298 get_argument=option_${_name}
299 #echo next should be argument $argument
300 fi
301
302 break
303 fi
304 done
305 done
306 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
307 done
308 elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
309 then
310 option=${argument:2}
311 option_argument=${option#*=}
312 option_name=${option%%=*}
313 [[ "$option_name" == "$option" ]] && option_argument=""
314 bad_option=y
315 for option_group in $options
316 do
317 _name=""
318 for o in $(echo $option_group | tr ":=" " ")
319 do
320 [[ -z "$_name" ]] && _name=${o//-/_}
321 #echo option $option versus $o
322 if [[ "$option_name" == "${o}" ]]
323 then
324 bad_option=n
325 if [[ ${option_group:${#option_group}-1} != "=" ]]
326 then #not an argument
327 [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && $___exit 1
328 eval option_${_name}='"${option_'${_name}'}-"'
329 elif [[ -n "${option_argument}" ]]
330 then
331 eval option_${_name}='"${option_argument}"'
332 else
333 get_argument=option_${_name}
334 #echo next should be argument $argument
335 fi
336 break
337 fi
338 done
339 done
340 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
341 elif [[ ${argument:0:2} == "--" ]]
342 then
343 option__="$*"
344 bad_option=y
345 for o in $options
346 do
347 if [[ "$o" == "--" ]]
348 then
349 bad_option=n
350 option__=" $*"
351 break
352 fi
353 done
354 [[ $bad_option == y ]] && echo "invalid argument '--'? Type -h for help" >&2 && $___exit 1
355 break
356 else
357 params="$params ${argument}"
358 fi
359
360 done
361
362 [[ -n "$get_argument" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
363 $___exit 0
364
365 }
366
367 function _parse_opts()
368 {
369 FORCE=""
370 if [ -n "$option_help" ];
371 then
372 _usage
373 exit -1
374 fi
375
376 if [ -n "$option_force" ];
377 then
378 FORCE="yes"
379 echo "force = yes"
380 fi
381
382 [ -z "$option_user" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && usage
383 USER=${option_user}
384 echo "user_name = "$option_user
385
386 [ -z "$option_iface_name" ] && echo -e "ERROR: User argument is mandatory, --user=<user>\n" && usage
387 interface=$option_iface_name
388
389 }
390 #1 CHECK input parameters
391 #1.1 root privileges
392 [ "${USER}" != "root" ] && echo "Needed root privileges" && _usage && exit -1
393
394
395 #Parse opts
396 _get_opts "help:h force:f user:u= iface-name:in= " $*
397 _parse_opts
398
399 echo "checking interface "$interface
400
401 _check_interface $interface
402
403 echo '
404 #################################################################
405 ##### INSTALL USER #####
406 #################################################################'
407 _install_user
408
409 echo '
410 #################################################################
411 ##### INSTALL NEEDED PACKETS #####
412 #################################################################'
413 _install_pacckags_dependences
414
415 echo '
416 #################################################################
417 ##### OTHER CONFIGURATION #####
418 #################################################################'
419 _openmano_img_2_libvirt_img
420 _hostinfo_config
421 _libvirt_configuration
422
423 echo '
424 #################################################################
425 ##### NETWORK CONFIGURATION #####
426 #################################################################'
427 _network_configuration
428 _disable_aaparmor
429 _user_remainder_pront
430
431
432