source: drbl-virt/sbin/functions_drbl_virt @ 207

Last change on this file since 207 was 205, checked in by rock, 14 years ago

Modify: default VM numbers (the same numbers of drbl clients)

File size: 12.6 KB
Line 
1#!/bin/bash
2# Program:
3#   DRBL virt module
4# Author:
5#   Jazz, Rock {jazz, rock}@nchc.org.tw
6# Version:
7#    1.0
8# History:
9#   2010/07/20  Rock    First release (1.0)
10
11# [Variable Declation]
12cpu_flag="no"
13Linux_bit=""
14Linux_Distribution=""
15Linux_Version=""
16VMM_select=""
17
18
19# [Check Root]
20function check_root(){     
21  if [ $USER != "root" ]; then
22    echo -e "Please change root to run it!"
23    exit
24  fi
25} 
26
27# [Change root to run]
28function check_root_run(){
29    if [ $USER != "root" ]; then
30    echo -e "Please change root to run it!"
31    sudo su -c ~/"$0" "$@"
32    exit
33fi
34}
35
36
37# [Check CPU support]
38function cpu_check(){
39egrep '(vmx|svm)' --color=always /proc/cpuinfo > /dev/null
40if [ $? == 0 ]; then
41    cpu_flag="yes"
42fi
43}
44
45
46# [Check System Version]
47function check_systemInfo(){
48Linux_bit=$(uname -m)
49Linux_Distribution=$(lsb_release -i | awk '{print $3}')
50Linux_Version=$(lsb_release -r | awk '{print $2}')
51}
52
53
54# [Select Hypervisior(Xen/KVM)]
55function VMM_select(){
56echo ""
57if [ $cpu_flag == "yes" ]; then
58    while [ "$VMM_select" != "1" -a "$VMM_select" != "2"  ]
59    do
60       read -p "Which VMM/Hypervisior will be used in DRBL (1)Xen (2)KVM: " VMM_select
61    done
62  if [ $VMM_select == "1"  ]; then
63    echo "Xen" > $Work_Home/etc/hypervisior
64  else
65    echo "KVM" > $Work_Home/etc/hypervisior
66  fi 
67else
68    echo "Xen will be the only Hypervisior in your DRBL"
69    echo -e "Because CPU don't support virtualization, it can't choose KVM to be the Hypervisior"
70    read -p "push [enter] to contiunte..."
71    VMM_select=1
72    echo "Xen" > $Work_Home/etc/hypervisior
73fi 
74}   
75
76
77# [Check Hypervisior]
78function check_hypervisior(){
79    hypervisior=$(cat $Work_Home/etc/hypervisior)   
80}
81
82
83# [Check Debian Xen package]
84function check_debian_xen(){
85echo "[Check Xen package in apt server]"
86echo ""
87aptitude update
88check_debian_xen_pkg=$(aptitude search xen-linux-system)
89if [ -n "$check_debian_xen_pkg"  ]; then
90    check_debian_xen_pkg="yes"
91else
92    check_debian_xen_pkg="no"
93fi
94}
95
96
97# [Check Xen kernel]
98function check_xen_nu(){
99Kernels=$(ls /boot | grep vmlinuz)
100declare -i Kernels_nu=$(echo $Kernels | wc -w)
101Kernel_choose=""
102
103Xens=$(ls /boot | grep -i ^xen)
104declare -i Xen_nu=$(echo $Xen | wc -w)
105Xen_choose=""
106
107jude="no"
108
109while [ $jude != "yes" -a $jude != "y" ]
110do
111    declare -i i=1
112    declare -i j=1
113    echo ""
114    echo "[Choose Xen-enabled Kernel]"
115    if [ $Kernels_nu -gt 1  ]; then
116        for Kernel in $Kernels
117        do
118            echo "($i) $Kernel" 
119            i=i+1
120        done
121
122        read -p "Xen Kenrel is (1/2/...): " Kernel_choose
123        Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose})
124
125    else
126        Xen_Kernel=$Kernels
127    fi
128
129    if [ $Xen_nu -gt 1  ]; then
130        for Xen in $Xens
131        do
132            echo "($i) $Xen"
133            j=j+1
134        done
135
136        read -p "Xen is (1/2/...): " Xen_choose
137        Xen=$(echo $Xens | cut -d " " -f${Xen_choose})
138    else
139        Xen=$Xens
140    fi
141
142    read -p "Are you sure (yes/no): " jude
143done
144
145#echo "debug: Xen_Kernel=$Xen_Kernel"
146}
147
148
149# [Tunning Debain env for Xen]
150function debain-lenny_xen_patch(){
151echo xen.independent_wallclock=1 >> /etc/sysctl.conf
152echo loop max_loop=255 >> /etc/modules
153echo xenblktap >> /etc/modules
154ln -s /usr/lib/$/bin/tapdisk /usr/sbin
155}
156
157
158# [Get ethX for DRBL environment usage ]
159function get_DRBL_eth(){
160eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1)
161eths_nu=$(echo $eths | wc -w)
162
163if [ $eths_nu -gt 1 ]; then
164    echo ""
165    echo "There are multiple eth for DRBL environment: "
166    echo "$eths"
167fi
168
169}
170
171
172# [Get DRBL client IP range]
173function get_Host_IP_range(){
174if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then
175    echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd"
176    exit 0
177fi
178
179# get eth network X.X.X
180eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3)
181
182IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l)
183Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}')
184Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}')
185
186}
187
188
189# [Input VM IP range]
190function get_VM_IP_range_and_prefix_name(){
191echo ""
192echo "[Setup VM IP range]"
193echo "DRBL client IP range -> $eth: [$Host_first_IP ~ $Host_last_IP]"
194
195Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4)
196Host_last_IP_4=$(($Host_last_IP_4+1))
197VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3)
198VM_first_IP="${VM_first_IP}.${Host_last_IP_4}"
199drbl_original_clients_nu=$(sed -n '/total_client_no/p' /etc/drbl/drblpush.conf | sed 's/total_client_no=//')
200
201
202echo "The VM IP Range will start from -> [$VM_first_IP]"
203jude="no"
204while [ $jude != "yes" -a $jude != "y" ];
205do
206    echo ""
207    echo "Input VM number/IP range for $eth (ex. 40)."
208    echo "default is the same numbers of drbl clients"
209   
210
211    jude_2="no"
212    while [ $jude_2 != "yes" -a $jude_2 != "y" ]
213    do
214        jude_2="yes"
215        echo "(If you have 10 PCs(4-core), suggestion is 30): "
216        read -p "[$drbl_original_clients_nu] " VM_IP_range
217        if [ $((${VM_IP_range}+${Host_last_IP_4})) -ge 254 ]; then
218            echo "range can't greater than 254"
219            jude_2="no"
220        fi
221    done
222
223    #read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
224    VM_prefix_name=$(cat /etc/drbl/drblpush.conf  | grep hostname= | sed 's/hostname=//')
225    read -p "Is it correct? (yes/no): " jude
226done
227
228#echo "VM_first_IP=$VM_first_IP"
229#echo "VM_IP_range=$VM_IP_range"
230}
231
232
233# [Get VM prefix name]
234#function get_VM_prefix_name(){
235#jude="no"
236#while [ $jude != "yes" -a $jude != "y" ];
237#do
238#    echo ""
239#    read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
240#    read -p "Is it correct? (yes/no): " jude
241#done
242#echo "VM_prefix_name=$VM_prefix_name"
243#}
244
245
246# [Create VM IP table]
247function create_VM_IP_table(){
248#/etc/drbl-virt/etc/IP_VM_eth
249VM_prefix_eth=$(echo $eth |  sed 's/eth//g')
250VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range}))
251if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then
252    mv -f /opt/drbl-virt/etc/IP_VM_$eth /opt/drbl-virt/etc/IP_VM_${eth}.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
253fi
254declare -i VM_IP_0=$VM_prefix_eth
255declare -i VM_IP_1=""
256declare -i VM_IP_2=""
257declare -i VM_IP_3=""
258
259# 2 bit
260if [ $VM_Host_totoal_nu -lt 100 ]; then
261    for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ ))
262    do
263        if [ $i -lt 10 ]; then
264            VM_IP_1=0
265            VM_IP_2=$i
266            echo "${eth_network_f3}.$VM_IP_2 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}${VM_IP_2}" >> /opt/drbl-virt/etc/IP_VM_$eth
267        else
268            VM_IP_1=$i
269            echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
270        fi
271    done
272# 3 bit
273elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then
274    for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ ))
275    do
276        if [ $i -lt 10 ]; then
277            VM_IP_1=0
278            VM_IP_2=0
279            VM_IP_3=$i
280            echo "${eth_network_f3}.${VM_IP_3} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}${VM_IP_2}${VM_IP_3}" >> /opt/drbl-virt/etc/IP_VM_$eth
281        elif [ $i -ge 10 ] && [ $i -lt 100 ]; then
282            VM_IP_1=0
283            VM_IP_2=$i
284            echo "${eth_network_f3}.${VM_IP_2} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}${VM_IP_2}" >> /opt/drbl-virt/etc/IP_VM_$eth
285        else
286            VM_IP_1=$i
287            echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
288        fi
289    done
290
291fi
292}
293
294
295# [Creat VM Mac table]
296function generate_Xen_MAC_address(){
297#echo "eth $eth"
298#echo "VM_Host_totoal_nu $VM_Host_totoal_nu"
299#echo "Host_last_IP_4 $Host_last_IP_4"
300#echo "VM_prefix_eth $VM_prefix_eth"
301
302# /opt/drbl-virt/etc/macadr-VM-ethX.txt
303# MAC address [00:16:3e:xx:xx:xx] is for Xen use
304declare -i Xen_MAC_1="0"
305declare -i Xen_MAC_2="$VM_prefix_eth"
306declare -i Xen_MAC_3="0"
307declare -i Xen_MAC_4="0"
308Xen_MAC_5=""
309Xen_MAC_6=""
310VM_Host_totoal_nu=$(cat $Work_Home/etc/IP_VM_$eth | wc -l)
311
312# backup
313if [ -e /opt/drbl-virt/etc/macadr-VM-$eth.txt ]; then
314mv -f /opt/drbl-virt/etc/macadr-VM-$eth.txt /opt/drbl-virt/etc/macadr-VM-$eth.txt.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
315fi
316
317# generate MAC
318for (( i=1 ; i<="$VM_Host_totoal_nu" ; i++ ))
319do
320    if [ $i -lt 10 ]; then
321        Xen_MAC_5=0
322        Xen_MAC_6=$i
323        echo "00:16:3e:${Xen_MAC_1}${Xen_MAC_2}:${Xen_MAC_3}${Xen_MAC_4}:${Xen_MAC_5}${Xen_MAC_6}" >> /opt/drbl-virt/etc/macadr-VM-$eth.txt
324    elif [ $i -ge 10 ] && [ $i -le 15 ] ; then
325        Xen_MAC_5=0
326        # translate 10(dec) to 16(hex)
327        printf '%X\n' $i > /tmp/drbl-virt_MAC
328        Xen_MAC_6=$(cat /tmp/drbl-virt_MAC)
329        echo "00:16:3e:${Xen_MAC_1}${Xen_MAC_2}:${Xen_MAC_3}${Xen_MAC_4}:${Xen_MAC_5}${Xen_MAC_6}" >> /opt/drbl-virt/etc/macadr-VM-$eth.txt
330    else
331        printf '%X\n' $i > /tmp/drbl-virt_MAC
332        Xen_MAC_5=$(cat /tmp/drbl-virt_MAC)
333        echo "00:16:3e:${Xen_MAC_1}${Xen_MAC_2}:${Xen_MAC_3}${Xen_MAC_4}:${Xen_MAC_5}" >> /opt/drbl-virt/etc/macadr-VM-$eth.txt
334    fi
335done
336
337# delete tmp file
338rm /tmp/drbl-virt_MAC
339}
340
341
342#function add_VM_dhcpd_conf(){
343## backup dhcpd.conf
344#if [ -e /etc/dhcp3/dhcpd.conf ]; then
345#cp -f /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.drbl-virt_bak
346#fi
347#subnet_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep $eth_network | awk '{print $1}')
348#host_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep 'host ' | grep '{' | awk '{print $1}')
349#
350#for subnet_line in $subnet_lines
351#do
352#    host_lines cat -n /etc/dhcp3/dhcpd.conf | sed -n "$subnet_line,\$p" | grep 'host .* {' | awk '{print $1}'
353#done
354#}
355
356function delete_duplicating_MAC(){
357drbl_mac_file="/etc/drbl/macadr-$eth.txt"
358# if macadr-ethX.txt.drbl-virt_bak already have, backup it
359# else cp default backup
360if [ -e $drbl_mac_file.drbl-virt_bak ]; then
361    cp $drbl_mac_file $drbl_mac_file.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
362    cp -f $drbl_mac_file.drbl-virt_bak $drbl_mac_file
363else 
364    cp $drbl_mac_file $drbl_mac_file.drbl-virt_bak
365fi
366
367    # Delete duplating VM MAC
368    for vm_mac in $(echo $VM_MACs)
369    do
370        vm_mac_lines=$(cat -n $drbl_mac_file | grep $vm_mac | awk '{print $1}' )
371        # no duplicated vlaue, doesn't do it
372        if [ -n "$vm_mac_lines" ]; then
373            vm_mac_count=$(echo $vm_mac_lines | wc -w)
374            for (( i=1 ; i<=${vm_mac_count} ; i++ ))
375            do
376                del_line=$(echo $vm_mac_lines | cut -d " " -f${i})
377                sed -i "${del_line}d" $drbl_mac_file
378            done
379        fi
380    done
381} 
382
383
384# [create ssh key amd cpoy to client]
385function drbl_sshkey(){
386# The reasone we do not to use $HOME is that sudo will not change
387# environmental variable $HOME, but it will change the $USER
388# we need to know who is really running this after applying sudo.
389# say, sudo echo "$HOME", it will show user's home, instead of root's home,
390REALHOME=$(LC_ALL=C grep -Ew "^$USER" /etc/passwd | cut -d":" -f6)
391drblroot="/tftpboot/nodes"
392
393if [ ! -f $REALHOME/.ssh/id_rsa ]; then
394  ssh-keygen -t rsa -q -f $REALHOME/.ssh/id_rsa -N ""
395fi
396
397# Put authorized_keys for the user. 2 cases:
398# (1). It is root running drbl-doit.
399# (2). It is normal user running drbl-doit.
400# For root, we will NOT copy authorized_keys in server, since the root in the client should not share the same authorized_keys with that in server. We only put them in the client. (Note! Every client has its own root directory in $ihost/root/)
401# For normal user, we let user can ssh login back to server and other machine without password (Note! This is NFS-based home, so we just have to copy id_rsa.pub as authorized_keys in user's home, then no matter which machine user logins, it will use this key).
402if [ "$UID" = "0" ]; then
403  # for root, copy id_rsa.pub as authorized_keys in clients.
404  for ihost in $drblroot/*; do
405    if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
406      mkdir -p $ihost/root/.ssh
407      cp -af $REALHOME/.ssh/id_rsa.pub $ihost/root/.ssh/authorized_keys
408    fi
409  done
410else
411  if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
412    cp -af $REALHOME/.ssh/id_rsa.pub $REALHOME/.ssh/authorized_keys
413  fi
414fi
415
416}
417
418
419# [Creat drbl-virter user]
420function creat_drbl-virter(){
421useradd -m -s /bin/bash drbl-virter
422}
423
424
425# [Download tomcat and mv drbl-virt.wat to wepapps]                           
426function install_tomcat_and_mv_drbl-virt(){
427if [ ! -e $Work_Home/tomcat-7.0.2.tar.gz ]; then
428wget http://drbl-virt.googlecode.com/files/tomcat-7.0.2.tar.gz -P $Work_Home
429fi                         
430tar zxvf $Work_Home/tomcat-7.0.2.tar.gz -C $Work_Home/
431mv $Work_Home/drbl-virt.war $Work_Home/tomcat-7.0.2/webapps/
432chown -R drbl-virter.drbl-virter $Work_Home/tomcat-7.0.2
433su  drbl-virter -c "$Work_Home/tomcat-7.0.2/bin/startup.sh"
434echo ""
435echo "[Go to http://{server_IP}:8080/drbl-virt/ to check !]"
436}
Note: See TracBrowser for help on using the repository browser.