source: drbl-virt/sbin/functions_drbl_virt @ 219

Last change on this file since 219 was 215, checked in by rock, 14 years ago

Add: KVM support (check KVM kernel)

File size: 12.9 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        VM_IP_range=$drbl_original_clients_nu
217        read -p "[$drbl_original_clients_nu] " VM_IP_range
218        if [ $((${VM_IP_range}+${Host_last_IP_4})) -ge 254 ]; then
219            echo "range can't greater than 254"
220            jude_2="no"
221        fi
222    done
223
224    #read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
225    VM_prefix_name=$(cat /etc/drbl/drblpush.conf  | grep hostname= | sed 's/hostname=//')
226    read -p "Is it correct? (yes/no): " jude
227done
228
229#echo "VM_first_IP=$VM_first_IP"
230#echo "VM_IP_range=$VM_IP_range"
231}
232
233
234# [Get VM prefix name]
235#function get_VM_prefix_name(){
236#jude="no"
237#while [ $jude != "yes" -a $jude != "y" ];
238#do
239#    echo ""
240#    read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
241#    read -p "Is it correct? (yes/no): " jude
242#done
243#echo "VM_prefix_name=$VM_prefix_name"
244#}
245
246
247# [Create VM IP table]
248function create_VM_IP_table(){
249#/etc/drbl-virt/etc/IP_VM_eth
250VM_prefix_eth=$(echo $eth |  sed 's/eth//g')
251VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range}))
252if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then
253    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
254fi
255declare -i VM_IP_0=$VM_prefix_eth
256declare -i VM_IP_1=""
257declare -i VM_IP_2=""
258declare -i VM_IP_3=""
259
260# 2 bit
261if [ $VM_Host_totoal_nu -lt 100 ]; then
262    for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ ))
263    do
264        if [ $i -lt 10 ]; then
265            VM_IP_1=0
266            VM_IP_2=$i
267            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
268        else
269            VM_IP_1=$i
270            echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
271        fi
272    done
273# 3 bit
274elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then
275    for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ ))
276    do
277        if [ $i -lt 10 ]; then
278            VM_IP_1=0
279            VM_IP_2=0
280            VM_IP_3=$i
281            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
282        elif [ $i -ge 10 ] && [ $i -lt 100 ]; then
283            VM_IP_1=0
284            VM_IP_2=$i
285            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
286        else
287            VM_IP_1=$i
288            echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
289        fi
290    done
291
292fi
293}
294
295
296# [Creat VM Mac table]
297function generate_Xen_MAC_address(){
298#echo "eth $eth"
299#echo "VM_Host_totoal_nu $VM_Host_totoal_nu"
300#echo "Host_last_IP_4 $Host_last_IP_4"
301#echo "VM_prefix_eth $VM_prefix_eth"
302
303# /opt/drbl-virt/etc/macadr-VM-ethX.txt
304# MAC address [00:16:3e:xx:xx:xx] is for Xen use
305declare -i Xen_MAC_1="0"
306declare -i Xen_MAC_2="$VM_prefix_eth"
307declare -i Xen_MAC_3="0"
308declare -i Xen_MAC_4="0"
309Xen_MAC_5=""
310Xen_MAC_6=""
311VM_Host_totoal_nu=$(cat $Work_Home/etc/IP_VM_$eth | wc -l)
312
313# backup
314if [ -e /opt/drbl-virt/etc/macadr-VM-$eth.txt ]; then
315mv -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
316fi
317
318# generate MAC
319for (( i=1 ; i<="$VM_Host_totoal_nu" ; i++ ))
320do
321    if [ $i -lt 10 ]; then
322        Xen_MAC_5=0
323        Xen_MAC_6=$i
324        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
325    elif [ $i -ge 10 ] && [ $i -le 15 ] ; then
326        Xen_MAC_5=0
327        # translate 10(dec) to 16(hex)
328        printf '%X\n' $i > /tmp/drbl-virt_MAC
329        Xen_MAC_6=$(cat /tmp/drbl-virt_MAC)
330        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
331    else
332        printf '%X\n' $i > /tmp/drbl-virt_MAC
333        Xen_MAC_5=$(cat /tmp/drbl-virt_MAC)
334        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
335    fi
336done
337
338# delete tmp file
339rm /tmp/drbl-virt_MAC
340}
341
342
343#function add_VM_dhcpd_conf(){
344## backup dhcpd.conf
345#if [ -e /etc/dhcp3/dhcpd.conf ]; then
346#cp -f /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.drbl-virt_bak
347#fi
348#subnet_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep $eth_network | awk '{print $1}')
349#host_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep 'host ' | grep '{' | awk '{print $1}')
350#
351#for subnet_line in $subnet_lines
352#do
353#    host_lines cat -n /etc/dhcp3/dhcpd.conf | sed -n "$subnet_line,\$p" | grep 'host .* {' | awk '{print $1}'
354#done
355#}
356
357function delete_duplicating_MAC(){
358drbl_mac_file="/etc/drbl/macadr-$eth.txt"
359# if macadr-ethX.txt.drbl-virt_bak already have, backup it
360# else cp default backup
361if [ -e $drbl_mac_file.drbl-virt_bak ]; then
362    cp $drbl_mac_file $drbl_mac_file.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
363    cp -f $drbl_mac_file.drbl-virt_bak $drbl_mac_file
364else
365    cp $drbl_mac_file $drbl_mac_file.drbl-virt_bak
366fi
367
368    # Delete duplating VM MAC
369    for vm_mac in $(echo $VM_MACs)
370    do
371        vm_mac_lines=$(cat -n $drbl_mac_file | grep $vm_mac | awk '{print $1}' )
372        # no duplicated vlaue, doesn't do it
373        if [ -n "$vm_mac_lines" ]; then
374            vm_mac_count=$(echo $vm_mac_lines | wc -w)
375            for (( i=1 ; i<=${vm_mac_count} ; i++ ))
376            do
377                del_line=$(echo $vm_mac_lines | cut -d " " -f${i})
378                sed -i "${del_line}d" $drbl_mac_file
379            done
380        fi
381    done
382}
383
384
385# [create ssh key amd cpoy to client]
386function drbl_sshkey(){
387# The reasone we do not to use $HOME is that sudo will not change
388# environmental variable $HOME, but it will change the $USER
389# we need to know who is really running this after applying sudo.
390# say, sudo echo "$HOME", it will show user's home, instead of root's home,
391REALHOME=$(LC_ALL=C grep -Ew "^$USER" /etc/passwd | cut -d":" -f6)
392drblroot="/tftpboot/nodes"
393
394if [ ! -f $REALHOME/.ssh/id_rsa ]; then
395  ssh-keygen -t rsa -q -f $REALHOME/.ssh/id_rsa -N ""
396fi
397
398# Put authorized_keys for the user. 2 cases:
399# (1). It is root running drbl-doit.
400# (2). It is normal user running drbl-doit.
401# 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/)
402# 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).
403if [ "$UID" = "0" ]; then
404  # for root, copy id_rsa.pub as authorized_keys in clients.
405  for ihost in $drblroot/*; do
406    if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
407      mkdir -p $ihost/root/.ssh
408      cp -af $REALHOME/.ssh/id_rsa.pub $ihost/root/.ssh/authorized_keys
409    fi
410  done
411else
412  if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
413    cp -af $REALHOME/.ssh/id_rsa.pub $REALHOME/.ssh/authorized_keys
414  fi
415fi
416
417}
418
419
420# [Creat drbl-virter user]
421function creat_drbl-virter(){
422useradd -m -s /bin/bash drbl-virter
423}
424
425
426# [Download tomcat and mv drbl-virt.wat to wepapps]
427function install_tomcat_and_mv_drbl-virt(){
428if [ ! -e $Work_Home/tomcat-7.0.2.tar.gz ]; then
429wget http://drbl-virt.googlecode.com/files/tomcat-7.0.2.tar.gz -P $Work_Home
430fi
431tar zxvf $Work_Home/tomcat-7.0.2.tar.gz -C $Work_Home/
432mv $Work_Home/drbl-virt.war $Work_Home/tomcat-7.0.2/webapps/
433chown -R drbl-virter.drbl-virter $Work_Home/tomcat-7.0.2
434su  drbl-virter -c "$Work_Home/tomcat-7.0.2/bin/startup.sh"
435echo ""
436echo "[Go to http://{server_IP}:8080/drbl-virt/ to check !]"
437}
438
439
440# [Cehck KVM kernel & version]
441function check_kvm(){
442echo -e "\nCheck this kernel..."
443KVM_Kernel=$(uname -r)
444
445grep -E '(CONFIG_KVM=m|CONFIG_KVM=y)' /boot/config-${KVM_Kernel} >> /dev/null
446if [ $? -eq 0 ]; then
447    echo "This Kernel supportes KVM !!!"
448else
449    echo "This Kernel doesn't support KVM !!!"
450    echo "Please change Kernel."
451    exit
452fi
453}
454
Note: See TracBrowser for help on using the repository browser.