source: drbl-virt/sbin/functions_drbl_virt @ 204

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

Bugfix: correct path & delete unused sh

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