source: drbl-virt/sbin/functions_drbl_virt @ 199

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

Add function Code: check_drbl_client.sh (Detect DRBL clinet status)

File size: 11.5 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    echo ""
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(){
85aptitude update
86check_debian_xen_pkg=$(aptitude search xen-linux-system)
87if [ -n "$check_debian_xen_pkg"  ]; then
88    check_debian_xen_pkg="yes"
89else
90    check_debian_xen_pkg="no"
91fi
92}
93
94
95# [Check Xen kernel]
96function check_xen_nu(){
97Kernels=$(ls /boot | grep vmlinuz)
98declare -i Kernels_nu=$(echo $Kernels | wc -w)
99Kernel_choose=""
100
101Xens=$(ls /boot | grep -i ^xen)
102declare -i Xen_nu=$(echo $Xen | wc -w)
103Xen_choose=""
104
105jude="no"
106
107while [ $jude != "yes" -a $jude != "y" ]
108do
109    declare -i i=1
110    declare -i j=1
111    echo ""
112    echo "Choose Xen-enabled Kernel"
113    if [ $Kernels_nu -gt 1  ]; then
114        for Kernel in $Kernels
115        do
116            echo "($i) $Kernel" 
117            i=i+1
118        done
119
120        read -p "Xen Kenrel is (1/2/...): " Kernel_choose
121        Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose})
122
123    else
124        Xen_Kernel=$Kernels
125    fi
126
127    if [ $Xen_nu -gt 1  ]; then
128        for Xen in $Xens
129        do
130            echo "($i) $Xen"
131            j=j+1
132        done
133
134        read -p "Xen is (1/2/...): " Xen_choose
135        Xen=$(echo $Xens | cut -d " " -f${Xen_choose})
136    else
137        Xen=$Xens
138    fi
139
140    read -p "Are you sure (yes/no): " jude
141done
142
143#echo "debug: Xen_Kernel=$Xen_Kernel"
144}
145
146
147# [Tunning Debain env for Xen]
148function debain-lenny_xen_patch(){
149echo xen.independent_wallclock=1 >> /etc/sysctl.conf
150echo loop max_loop=255 >> /etc/modules
151echo xenblktap >> /etc/modules
152ln -s /usr/lib/$/bin/tapdisk /usr/sbin
153}
154
155
156# [Get ethX for DRBL environment usage ]
157function get_DRBL_eth(){
158eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1)
159eths_nu=$(echo $eths | wc -w)
160
161if [ $eths_nu -gt 1 ]; then
162    echo ""
163    echo "There are multiple eth for DRBL environment: "
164    echo "$eths"
165fi
166
167}
168
169
170# [Get DRBL client IP range]
171function get_Host_IP_range(){
172if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then
173    echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd"
174    exit 0
175fi
176
177# get eth network X.X.X
178eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3)
179
180IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l)
181Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}')
182Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}')
183
184}
185
186
187# [Input VM IP range]
188function get_VM_IP_range_and_prefix_name(){
189echo ""
190echo "DRBL client IP range -> $eth: [$Host_first_IP ~ $Host_last_IP]"
191
192Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4)
193Host_last_IP_4=$(($Host_last_IP_4+1))
194VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3)
195VM_first_IP="${VM_first_IP}.${Host_last_IP_4}"
196
197echo "The VM IP Range will start from -> [$VM_first_IP]"
198jude="no"
199while [ $jude != "yes" -a $jude != "y" ];
200do
201    echo ""
202    echo "Input VM number/IP range for $eth (ex. 40)."
203
204    jude_2="no"
205    while [ $jude_2 != "yes" -a $jude_2 != "y" ]
206    do
207        jude_2="yes"
208        read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range
209        if [ $((${VM_IP_range}+${Host_last_IP_4})) -ge 254 ]; then
210            echo "range can't greater than 254"
211            jude_2="no"
212        fi
213    done
214
215    #read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
216    VM_prefix_name=$(cat /etc/drbl/drblpush.conf  | grep hostname= | sed 's/hostname=//')
217    read -p "Is it correct? (yes/no): " jude
218done
219
220#echo "VM_first_IP=$VM_first_IP"
221#echo "VM_IP_range=$VM_IP_range"
222}
223
224
225# [Get VM prefix name]
226#function get_VM_prefix_name(){
227#jude="no"
228#while [ $jude != "yes" -a $jude != "y" ];
229#do
230#    echo ""
231#    read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name
232#    read -p "Is it correct? (yes/no): " jude
233#done
234#echo "VM_prefix_name=$VM_prefix_name"
235#}
236
237
238# [Create VM IP table]
239function create_VM_IP_table(){
240#/etc/drbl-virt/etc/IP_VM_eth
241VM_prefix_eth=$(echo $eth |  sed 's/eth//g')
242VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range}))
243if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then
244    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
245fi
246declare -i VM_IP_0=$VM_prefix_eth
247declare -i VM_IP_1=""
248declare -i VM_IP_2=""
249declare -i VM_IP_3=""
250
251# 2 bit
252if [ $VM_Host_totoal_nu -lt 100 ]; then
253    for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ ))
254    do
255        if [ $i -lt 10 ]; then
256            VM_IP_1=0
257            VM_IP_2=$i
258            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
259        else
260            VM_IP_1=$i
261            echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
262        fi
263    done
264# 3 bit
265elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then
266    for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ ))
267    do
268        if [ $i -lt 10 ]; then
269            VM_IP_1=0
270            VM_IP_2=0
271            VM_IP_3=$i
272            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
273        elif [ $i -ge 10 ] && [ $i -lt 100 ]; then
274            VM_IP_1=0
275            VM_IP_2=$i
276            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
277        else
278            VM_IP_1=$i
279            echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
280        fi
281    done
282
283fi
284}
285
286
287# [Creat VM Mac table]
288function generate_Xen_MAC_address(){
289#echo "eth $eth"
290#echo "VM_Host_totoal_nu $VM_Host_totoal_nu"
291#echo "Host_last_IP_4 $Host_last_IP_4"
292#echo "VM_prefix_eth $VM_prefix_eth"
293
294# /opt/drbl-virt/etc/macadr-VM-ethX.txt
295# MAC address [00:16:3e:xx:xx:xx] is for Xen use
296declare -i Xen_MAC_1="0"
297declare -i Xen_MAC_2="$VM_prefix_eth"
298declare -i Xen_MAC_3="0"
299declare -i Xen_MAC_4="0"
300Xen_MAC_5=""
301Xen_MAC_6=""
302
303# backup
304if [ -e /opt/drbl-virt/etc/macadr-VM-$eth.txt ]; then
305mv -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
306fi
307
308# generate MAC
309for (( i=1 ; i<"$VM_Host_totoal_nu" ; i++ ))
310do
311    if [ $i -lt 10 ]; then
312        Xen_MAC_5=0
313        Xen_MAC_6=$i
314        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
315    elif [ $i -ge 10 ] && [ $i -le 15 ] ; then
316        Xen_MAC_5=0
317        # translate 10(dec) to 16(hex)
318        printf '%X\n' $i > /tmp/drbl-virt_MAC
319        Xen_MAC_6=$(cat /tmp/drbl-virt_MAC)
320        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
321    else
322        printf '%X\n' $i > /tmp/drbl-virt_MAC
323        Xen_MAC_5=$(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}" >> /opt/drbl-virt/etc/macadr-VM-$eth.txt
325    fi
326done
327}
328
329
330#function add_VM_dhcpd_conf(){
331## backup dhcpd.conf
332#if [ -e /etc/dhcp3/dhcpd.conf ]; then
333#cp -f /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.drbl-virt_bak
334#fi
335#subnet_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep $eth_network | awk '{print $1}')
336#host_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep 'host ' | grep '{' | awk '{print $1}')
337#
338#for subnet_line in $subnet_lines
339#do
340#    host_lines cat -n /etc/dhcp3/dhcpd.conf | sed -n "$subnet_line,\$p" | grep 'host .* {' | awk '{print $1}'
341#done
342#}
343
344function delete_duplicating_MAC(){
345drbl_mac_file="/etc/drbl/macadr-$eth.txt"
346# if macadr-ethX.txt.drbl-virt_bak already have, backup it
347# else cp default backup
348if [ -e $drbl_mac_file.drbl-virt_bak ]; then
349    cp $drbl_mac_file $drbl_mac_file.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
350    cp -f $drbl_mac_file.drbl-virt_bak $drbl_mac_file
351else 
352    cp $drbl_mac_file $drbl_mac_file.drbl-virt_bak
353fi
354
355    # Delete duplating VM MAC
356    for vm_mac in $(echo $VM_MACs)
357    do
358        vm_mac_lines=$(cat -n $drbl_mac_file | grep $vm_mac | awk '{print $1}' )
359        # no duplicated vlaue, doesn't do it
360        if [ -n "$vm_mac_lines" ]; then
361            vm_mac_count=$(echo $vm_mac_lines | wc -w)
362            for (( i=1 ; i<=${vm_mac_count} ; i++ ))
363            do
364                del_line=$(echo $vm_mac_lines | cut -d " " -f${i})
365                sed -i "${del_line}d" $drbl_mac_file
366            done
367        fi
368    done
369} 
370
371
372# [create ssh key amd cpoy to client]
373function drbl_sshkey(){
374# The reasone we do not to use $HOME is that sudo will not change
375# environmental variable $HOME, but it will change the $USER
376# we need to know who is really running this after applying sudo.
377# say, sudo echo "$HOME", it will show user's home, instead of root's home,
378REALHOME=$(LC_ALL=C grep -Ew "^$USER" /etc/passwd | cut -d":" -f6)
379drblroot="/tftpboot/nodes"
380
381if [ ! -f $REALHOME/.ssh/id_rsa ]; then
382  ssh-keygen -t rsa -q -f $REALHOME/.ssh/id_rsa -N ""
383fi
384
385# Put authorized_keys for the user. 2 cases:
386# (1). It is root running drbl-doit.
387# (2). It is normal user running drbl-doit.
388# 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/)
389# 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).
390if [ "$UID" = "0" ]; then
391  # for root, copy id_rsa.pub as authorized_keys in clients.
392  for ihost in $drblroot/*; do
393    if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
394      mkdir -p $ihost/root/.ssh
395      cp -af $REALHOME/.ssh/id_rsa.pub $ihost/root/.ssh/authorized_keys
396    fi
397  done
398else
399  if [ -f "$REALHOME/.ssh/id_rsa.pub" ]; then
400    cp -af $REALHOME/.ssh/id_rsa.pub $REALHOME/.ssh/authorized_keys
401  fi
402fi
403
404}
Note: See TracBrowser for help on using the repository browser.