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