source: drbl-virt/sbin/functions_drbl_virt @ 208

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

Modify: 精簡化
Add: 一次產生全部DRBL Clinet VM 設定檔(drbl_PXE_PV-VM_create_all)

File size: 12.7 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}"
[205]199drbl_original_clients_nu=$(sed -n '/total_client_no/p' /etc/drbl/drblpush.conf | sed 's/total_client_no=//')
[180]200
[205]201
[180]202echo "The VM IP Range will start from -> [$VM_first_IP]"
203jude="no"
204while [ $jude != "yes" -a $jude != "y" ];
205do
206    echo ""
[182]207    echo "Input VM number/IP range for $eth (ex. 40)."
[205]208    echo "default is the same numbers of drbl clients"
209   
[182]210
211    jude_2="no"
212    while [ $jude_2 != "yes" -a $jude_2 != "y" ]
213    do
214        jude_2="yes"
[205]215        echo "(If you have 10 PCs(4-core), suggestion is 30): "
[208]216        VM_IP_range=$drbl_original_clients_nu
[205]217        read -p "[$drbl_original_clients_nu] " VM_IP_range
[182]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
[184]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=//')
[180]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"
[178]231}
232
[185]233
[195]234# [Get VM prefix name]
[182]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
[180]243#echo "VM_prefix_name=$VM_prefix_name"
[182]244#}
[156]245
[195]246
247# [Create VM IP table]
[180]248function create_VM_IP_table(){
249#/etc/drbl-virt/etc/IP_VM_eth
[181]250VM_prefix_eth=$(echo $eth |  sed 's/eth//g')
[183]251VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range}))
[181]252if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then
[184]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
[181]254fi
255declare -i VM_IP_0=$VM_prefix_eth
256declare -i VM_IP_1=""
257declare -i VM_IP_2=""
258declare -i VM_IP_3=""
[171]259
[181]260# 2 bit
261if [ $VM_Host_totoal_nu -lt 100 ]; then
[182]262    for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ ))
[181]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
[182]270            echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
[181]271        fi
272    done
273# 3 bit
274elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then
[182]275    for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ ))
[181]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
[183]282        elif [ $i -ge 10 ] && [ $i -lt 100 ]; then
[181]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
[180]293}
[171]294
[195]295
296# [Creat VM Mac table]
[183]297function generate_Xen_MAC_address(){
[188]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"
[180]302
[183]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=""
[202]311VM_Host_totoal_nu=$(cat $Work_Home/etc/IP_VM_$eth | wc -l)
[180]312
[183]313# backup
[188]314if [ -e /opt/drbl-virt/etc/macadr-VM-$eth.txt ]; then
[184]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
[188]316fi
[180]317
[183]318# generate MAC
[202]319for (( i=1 ; i<="$VM_Host_totoal_nu" ; i++ ))
[183]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
[202]337
338# delete tmp file
339rm /tmp/drbl-virt_MAC
[184]340}
[183]341
[195]342
[185]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#}
[189]356
357function delete_duplicating_MAC(){
358drbl_mac_file="/etc/drbl/macadr-$eth.txt"
[190]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
[189]367
[190]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
[189]382} 
383
[195]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}
[202]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"
[203]435echo ""
436echo "[Go to http://{server_IP}:8080/drbl-virt/ to check !]"
[202]437}
Note: See TracBrowser for help on using the repository browser.