source: drbl-virt/sbin/functions_drbl_virt @ 181

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

Modify: the same

File size: 6.7 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# [Check Root]
19function check_root(){     
20  if [ $USER != "root" ]; then
21    echo -e "Please change root to run it!"
22    exit
23  fi
24} 
25
26function check_root_run(){
27    if [ $USER != "root" ]; then
28    echo -e "Please change root to run it!"
29    sudo su -c ~/"$0" "$@"
30    exit
31fi
32}
33
34
35# [Check CPU support]
36function cpu_check(){
37egrep '(vmx|svm)' --color=always /proc/cpuinfo > /dev/null
38if [ $? == 0 ]; then
39    cpu_flag="yes"
40fi
41}
42
43# [Check System Version]
44function check_systemInfo(){
45Linux_bit=$(uname -m)
46Linux_Distribution=$(lsb_release -i | awk '{print $3}')
47Linux_Version=$(lsb_release -r | awk '{print $2}')
48}
49
50# [Select Hypervisior(Xen/KVM)]
51function VMM_select(){
52echo ""
53if [ $cpu_flag == "yes" ]; then
54    while [ "$VMM_select" != "1" -a "$VMM_select" != "2"  ]
55    do
56       read -p "Which VMM/Hypervisior will be used in DRBL (1)Xen (2)KVM: " VMM_select
57    done
58  if [ $VMM_select == "1"  ]; then
59    echo "Xen" > $Work_Home/etc/hypervisior
60  else
61    echo "KVM" > $Work_Home/etc/hypervisior
62  fi 
63else
64    echo "Xen will be the only Hypervisior in your DRBL"
65    echo -e "Because CPU don't support virtualization, it can't choose KVM to be the Hypervisior"
66    echo ""
67    VMM_select=1
68    echo "Xen" > $Work_Home/etc/hypervisior
69fi 
70}   
71
72
73# [Create drbl-virt path]
74function install_drbl-virt_dir(){
75    mkdir -p $Work_Home/.tmp
76    mkdir -p $Work_Home/etc
77}
78
79
80# [Check Hypervisior]
81function check_hypervisior(){
82    hypervisior=$(cat $Work_Home/etc/hypervisior)   
83}
84
85
86# [Check Debian Xen package]
87function check_debian_xen(){
88aptitude update
89check_debian_xen_pkg=$(aptitude search xen-linux-system)
90if [ -n $check_debian_xen_pkg  ]; then
91    check_debian_xen_pkg="yes"
92else
93    check_debian_xen_pkg="no"
94fi
95}
96
97
98# [Check Xen kernel]
99function check_xen_nu(){
100Kernels=$(ls /boot | grep vmlinuz)
101declare -i Kernels_nu=$(echo $Kernels | wc -w)
102Kernel_choose=""
103
104Xens=$(ls /boot | grep -i ^xen)
105declare -i Xen_nu=$(echo $Xen | wc -w)
106Xen_choose=""
107
108jude="no"
109
110while [ $jude != "yes" -a $jude != "y" ]
111do
112    declare -i i=1
113    declare -i j=1
114    echo ""
115   
116    if [ $Kernels_nu -gt 1  ]; then
117        for Kernel in $Kernels
118        do
119            echo "($i) $Kernel" 
120            i=i+1
121        done
122
123        read -p "Xen Kenrel is (1/2/...): " Kernel_choose
124        Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose})
125
126    else
127        Xen_Kernel=$Kernels
128    fi
129
130    if [ $Xen_nu -gt 1  ]; then
131        for Xen in $Xens
132        do
133            echo "($i) $Xen"
134            j=j+1
135        done
136
137        read -p "Xen is (1/2/...): " Xen_choose
138        Xen=$(echo $Xens | cut -d " " -f${Xen_choose})
139    else
140        Xen=$Xens
141    fi
142
143    read -p "Are you sure (yes/no): " jude
144done
145
146#echo "debug: Xen_Kernel=$Xen_Kernel"
147}
148
149
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
158function get_DRBL_eth(){
159eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1)
160eths_nu=$(echo $eths | wc -l)
161
162
163}
164
165function get_Host_IP_range(){
166if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then
167    echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd"
168    exit 0
169fi
170
171# get eth network
172eth_address=$(/opt/drbl/bin/drbl-get-ipadd $eth)
173eth_netmask=$(/opt/drbl/bin/drbl-get-netmask $eth)
174eth_network=$(/opt/drbl/bin/drbl-get-network $eth_address $eth_netmask)
175eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3)
176
177IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l)
178Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}')
179Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}')
180
181}
182
183function get_VM_IP_range(){
184echo ""
185echo "DRBL client IP range -> $eth [$Host_first_IP ~ $Host_last_IP]"
186
187declare -i Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4)
188Host_last_IP_4=$(($Host_last_IP_4+1))
189VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3)
190VM_first_IP="${VM_first_IP}.${Host_last_IP_4}"
191
192echo "The VM IP Range will start from -> [$VM_first_IP]"
193jude="no"
194while [ $jude != "yes" -a $jude != "y" ];
195do
196    echo ""
197    echo "Input VM number/IP range in $eth (ex. 40)."
198    read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range
199    read -p "Is it correct? (yes/no): " jude
200done
201
202#echo "VM_first_IP=$VM_first_IP"
203#echo "VM_IP_range=$VM_IP_range"
204}
205
206function get_VM_prefix_name(){ 
207jude="no"
208while [ $jude != "yes" -a $jude != "y" ];
209do
210    echo ""
211    read -p "Input Prefix name for VM in $eth (ex. drblvm): " VM_prefix_name
212    read -p "Is it correct? (yes/no): " jude
213done
214#echo "VM_prefix_name=$VM_prefix_name"
215}
216
217function create_VM_IP_table(){
218#/etc/drbl-virt/etc/IP_VM_eth
219VM_prefix_eth=$(echo $eth |  sed 's/eth//g')
220declare -i VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range}))
221if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then
222    mv /opt/drbl-virt/etc/IP_VM_$eth /opt/drbl-virt/etc/IP_VM_${eth}.$(date +%Y-%m-%d-%H-%M-%S).drbl-virt_bak
223fi
224declare -i VM_IP_0=$VM_prefix_eth
225declare -i VM_IP_1=""
226declare -i VM_IP_2=""
227declare -i VM_IP_3=""
228
229
230# 2 bit
231if [ $VM_Host_totoal_nu -lt 100 ]; then
232    for (( i=$Host_last_IP_4 ; i<="$VM_IP_range" ; i++ ))
233    do
234        if [ $i -lt 10 ]; then
235            VM_IP_1=0
236            VM_IP_2=$i
237            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
238        else
239            VM_IP_1=$i
240            echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$et
241        fi
242    done
243# 3 bit
244elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then
245    for (( i=$Host_last_IP_4 ; i<=$VM_IP_range ; i++ ))
246    do
247        if [ $i -lt 10 ]; then
248            VM_IP_1=0
249            VM_IP_2=0
250            VM_IP_3=$i
251            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
252        elif [ $i -lt 100 ]; then
253            VM_IP_1=0
254            VM_IP_2=$i
255            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
256        else
257            VM_IP_1=$i
258            echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth
259        fi
260
261    done
262
263fi
264}
265
266
267
268
Note: See TracBrowser for help on using the repository browser.