[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] |
---|
| 12 | cpu_flag="no" |
---|
| 13 | Linux_bit="" |
---|
| 14 | Linux_Distribution="" |
---|
| 15 | Linux_Version="" |
---|
| 16 | VMM_select="" |
---|
| 17 | |
---|
| 18 | # [Check Root] |
---|
| 19 | function check_root(){ |
---|
| 20 | if [ $USER != "root" ]; then |
---|
[171] | 21 | echo -e "Please change root to run it!" |
---|
| 22 | exit |
---|
[156] | 23 | fi |
---|
| 24 | } |
---|
| 25 | |
---|
[171] | 26 | function check_root_run(){ |
---|
| 27 | if [ $USER != "root" ]; then |
---|
| 28 | echo -e "Please change root to run it!" |
---|
| 29 | sudo su -c ~/"$0" "$@" |
---|
| 30 | exit |
---|
| 31 | fi |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
[156] | 35 | # [Check CPU support] |
---|
| 36 | function cpu_check(){ |
---|
| 37 | egrep '(vmx|svm)' --color=always /proc/cpuinfo > /dev/null |
---|
| 38 | if [ $? == 0 ]; then |
---|
| 39 | cpu_flag="yes" |
---|
| 40 | fi |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | # [Check System Version] |
---|
| 44 | function check_systemInfo(){ |
---|
| 45 | Linux_bit=$(uname -m) |
---|
| 46 | Linux_Distribution=$(lsb_release -i | awk '{print $3}') |
---|
| 47 | Linux_Version=$(lsb_release -r | awk '{print $2}') |
---|
| 48 | } |
---|
| 49 | |
---|
[171] | 50 | # [Select Hypervisior(Xen/KVM)] |
---|
[156] | 51 | function VMM_select(){ |
---|
[180] | 52 | echo "" |
---|
[156] | 53 | if [ $cpu_flag == "yes" ]; then |
---|
| 54 | while [ "$VMM_select" != "1" -a "$VMM_select" != "2" ] |
---|
| 55 | do |
---|
[157] | 56 | read -p "Which VMM/Hypervisior will be used in DRBL (1)Xen (2)KVM: " VMM_select |
---|
[156] | 57 | done |
---|
[171] | 58 | if [ $VMM_select == "1" ]; then |
---|
[180] | 59 | echo "Xen" > $Work_Home/etc/hypervisior |
---|
[171] | 60 | else |
---|
[180] | 61 | echo "KVM" > $Work_Home/etc/hypervisior |
---|
[171] | 62 | fi |
---|
[156] | 63 | else |
---|
| 64 | echo "Xen will be the only Hypervisior in your DRBL" |
---|
[171] | 65 | echo -e "Because CPU don't support virtualization, it can't choose KVM to be the Hypervisior" |
---|
| 66 | echo "" |
---|
| 67 | VMM_select=1 |
---|
[180] | 68 | echo "Xen" > $Work_Home/etc/hypervisior |
---|
[156] | 69 | fi |
---|
| 70 | } |
---|
| 71 | |
---|
[171] | 72 | # [Check Hypervisior] |
---|
[161] | 73 | function check_hypervisior(){ |
---|
[180] | 74 | hypervisior=$(cat $Work_Home/etc/hypervisior) |
---|
[161] | 75 | } |
---|
| 76 | |
---|
[171] | 77 | |
---|
| 78 | # [Check Debian Xen package] |
---|
| 79 | function check_debian_xen(){ |
---|
| 80 | aptitude update |
---|
| 81 | check_debian_xen_pkg=$(aptitude search xen-linux-system) |
---|
| 82 | if [ -n $check_debian_xen_pkg ]; then |
---|
| 83 | check_debian_xen_pkg="yes" |
---|
| 84 | else |
---|
| 85 | check_debian_xen_pkg="no" |
---|
| 86 | fi |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | # [Check Xen kernel] |
---|
| 91 | function check_xen_nu(){ |
---|
| 92 | Kernels=$(ls /boot | grep vmlinuz) |
---|
[172] | 93 | declare -i Kernels_nu=$(echo $Kernels | wc -w) |
---|
[171] | 94 | Kernel_choose="" |
---|
[172] | 95 | |
---|
| 96 | Xens=$(ls /boot | grep -i ^xen) |
---|
| 97 | declare -i Xen_nu=$(echo $Xen | wc -w) |
---|
| 98 | Xen_choose="" |
---|
| 99 | |
---|
[171] | 100 | jude="no" |
---|
| 101 | |
---|
| 102 | while [ $jude != "yes" -a $jude != "y" ] |
---|
| 103 | do |
---|
| 104 | declare -i i=1 |
---|
[172] | 105 | declare -i j=1 |
---|
[171] | 106 | echo "" |
---|
| 107 | |
---|
[172] | 108 | if [ $Kernels_nu -gt 1 ]; then |
---|
| 109 | for Kernel in $Kernels |
---|
| 110 | do |
---|
| 111 | echo "($i) $Kernel" |
---|
| 112 | i=i+1 |
---|
| 113 | done |
---|
[171] | 114 | |
---|
[172] | 115 | read -p "Xen Kenrel is (1/2/...): " Kernel_choose |
---|
| 116 | Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose}) |
---|
| 117 | |
---|
| 118 | else |
---|
| 119 | Xen_Kernel=$Kernels |
---|
| 120 | fi |
---|
| 121 | |
---|
| 122 | if [ $Xen_nu -gt 1 ]; then |
---|
| 123 | for Xen in $Xens |
---|
| 124 | do |
---|
| 125 | echo "($i) $Xen" |
---|
| 126 | j=j+1 |
---|
| 127 | done |
---|
| 128 | |
---|
| 129 | read -p "Xen is (1/2/...): " Xen_choose |
---|
| 130 | Xen=$(echo $Xens | cut -d " " -f${Xen_choose}) |
---|
| 131 | else |
---|
| 132 | Xen=$Xens |
---|
| 133 | fi |
---|
| 134 | |
---|
[171] | 135 | read -p "Are you sure (yes/no): " jude |
---|
| 136 | done |
---|
| 137 | |
---|
| 138 | #echo "debug: Xen_Kernel=$Xen_Kernel" |
---|
| 139 | } |
---|
| 140 | |
---|
[178] | 141 | |
---|
[179] | 142 | function debain-lenny_xen_patch(){ |
---|
| 143 | echo xen.independent_wallclock=1 >> /etc/sysctl.conf |
---|
| 144 | echo loop max_loop=255 >> /etc/modules |
---|
| 145 | echo xenblktap >> /etc/modules |
---|
| 146 | ln -s /usr/lib/$/bin/tapdisk /usr/sbin |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
[180] | 150 | function get_DRBL_eth(){ |
---|
[181] | 151 | eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1) |
---|
[182] | 152 | eths_nu=$(echo $eths | wc -w) |
---|
[180] | 153 | |
---|
[182] | 154 | if [ $eths_nu -gt 1 ]; then |
---|
| 155 | echo "" |
---|
| 156 | echo "There are multiple eth for DRBL environment: " |
---|
| 157 | echo "$eths" |
---|
| 158 | fi |
---|
[180] | 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
[178] | 162 | function get_Host_IP_range(){ |
---|
[180] | 163 | if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then |
---|
| 164 | echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd" |
---|
| 165 | exit 0 |
---|
| 166 | fi |
---|
| 167 | |
---|
[184] | 168 | # get eth network X.X.X |
---|
[180] | 169 | eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3) |
---|
| 170 | |
---|
| 171 | IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l) |
---|
[181] | 172 | Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}') |
---|
[180] | 173 | Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}') |
---|
| 174 | |
---|
[178] | 175 | } |
---|
| 176 | |
---|
[182] | 177 | function get_VM_IP_range_and_prefix_name(){ |
---|
| 178 | |
---|
[180] | 179 | echo "" |
---|
[182] | 180 | echo "DRBL client IP range -> $eth: [$Host_first_IP ~ $Host_last_IP]" |
---|
[178] | 181 | |
---|
[182] | 182 | Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4) |
---|
[180] | 183 | Host_last_IP_4=$(($Host_last_IP_4+1)) |
---|
| 184 | VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3) |
---|
| 185 | VM_first_IP="${VM_first_IP}.${Host_last_IP_4}" |
---|
| 186 | |
---|
| 187 | echo "The VM IP Range will start from -> [$VM_first_IP]" |
---|
| 188 | jude="no" |
---|
| 189 | while [ $jude != "yes" -a $jude != "y" ]; |
---|
| 190 | do |
---|
| 191 | echo "" |
---|
[182] | 192 | echo "Input VM number/IP range for $eth (ex. 40)." |
---|
| 193 | |
---|
| 194 | jude_2="no" |
---|
| 195 | while [ $jude_2 != "yes" -a $jude_2 != "y" ] |
---|
| 196 | do |
---|
| 197 | jude_2="yes" |
---|
| 198 | read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range |
---|
| 199 | if [ $((${VM_IP_range}+${Host_last_IP_4})) -ge 254 ]; then |
---|
| 200 | echo "range can't greater than 254" |
---|
| 201 | jude_2="no" |
---|
| 202 | fi |
---|
| 203 | done |
---|
| 204 | |
---|
[184] | 205 | #read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name |
---|
| 206 | VM_prefix_name=$(cat /etc/drbl/drblpush.conf | grep hostname= | sed 's/hostname=//') |
---|
[180] | 207 | read -p "Is it correct? (yes/no): " jude |
---|
| 208 | done |
---|
| 209 | |
---|
| 210 | #echo "VM_first_IP=$VM_first_IP" |
---|
| 211 | #echo "VM_IP_range=$VM_IP_range" |
---|
[178] | 212 | } |
---|
| 213 | |
---|
[185] | 214 | |
---|
[182] | 215 | #function get_VM_prefix_name(){ |
---|
| 216 | #jude="no" |
---|
| 217 | #while [ $jude != "yes" -a $jude != "y" ]; |
---|
| 218 | #do |
---|
| 219 | # echo "" |
---|
| 220 | # read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name |
---|
| 221 | # read -p "Is it correct? (yes/no): " jude |
---|
| 222 | #done |
---|
[180] | 223 | #echo "VM_prefix_name=$VM_prefix_name" |
---|
[182] | 224 | #} |
---|
[156] | 225 | |
---|
[180] | 226 | function create_VM_IP_table(){ |
---|
| 227 | #/etc/drbl-virt/etc/IP_VM_eth |
---|
[181] | 228 | VM_prefix_eth=$(echo $eth | sed 's/eth//g') |
---|
[183] | 229 | VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range})) |
---|
[181] | 230 | if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then |
---|
[184] | 231 | 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] | 232 | fi |
---|
| 233 | declare -i VM_IP_0=$VM_prefix_eth |
---|
| 234 | declare -i VM_IP_1="" |
---|
| 235 | declare -i VM_IP_2="" |
---|
| 236 | declare -i VM_IP_3="" |
---|
[171] | 237 | |
---|
[181] | 238 | # 2 bit |
---|
| 239 | if [ $VM_Host_totoal_nu -lt 100 ]; then |
---|
[182] | 240 | for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ )) |
---|
[181] | 241 | do |
---|
| 242 | if [ $i -lt 10 ]; then |
---|
| 243 | VM_IP_1=0 |
---|
| 244 | VM_IP_2=$i |
---|
| 245 | 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 |
---|
| 246 | else |
---|
| 247 | VM_IP_1=$i |
---|
[182] | 248 | echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth |
---|
[181] | 249 | fi |
---|
| 250 | done |
---|
| 251 | # 3 bit |
---|
| 252 | elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then |
---|
[182] | 253 | for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ )) |
---|
[181] | 254 | do |
---|
| 255 | if [ $i -lt 10 ]; then |
---|
| 256 | VM_IP_1=0 |
---|
| 257 | VM_IP_2=0 |
---|
| 258 | VM_IP_3=$i |
---|
| 259 | 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] | 260 | elif [ $i -ge 10 ] && [ $i -lt 100 ]; then |
---|
[181] | 261 | VM_IP_1=0 |
---|
| 262 | VM_IP_2=$i |
---|
| 263 | 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 |
---|
| 264 | else |
---|
| 265 | VM_IP_1=$i |
---|
| 266 | echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth |
---|
| 267 | fi |
---|
| 268 | done |
---|
| 269 | |
---|
| 270 | fi |
---|
[180] | 271 | } |
---|
[171] | 272 | |
---|
[183] | 273 | function generate_Xen_MAC_address(){ |
---|
| 274 | echo "eth $eth" |
---|
| 275 | echo "VM_Host_totoal_nu $VM_Host_totoal_nu" |
---|
| 276 | echo "Host_last_IP_4 $Host_last_IP_4" |
---|
| 277 | echo "VM_prefix_eth $VM_prefix_eth" |
---|
[180] | 278 | |
---|
[183] | 279 | # /opt/drbl-virt/etc/macadr-VM-ethX.txt |
---|
| 280 | # MAC address [00:16:3e:xx:xx:xx] is for Xen use |
---|
| 281 | declare -i Xen_MAC_1="0" |
---|
| 282 | declare -i Xen_MAC_2="$VM_prefix_eth" |
---|
| 283 | declare -i Xen_MAC_3="0" |
---|
| 284 | declare -i Xen_MAC_4="0" |
---|
| 285 | Xen_MAC_5="" |
---|
| 286 | Xen_MAC_6="" |
---|
[180] | 287 | |
---|
[183] | 288 | # backup |
---|
[184] | 289 | mv -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 |
---|
[180] | 290 | |
---|
[183] | 291 | # generate MAC |
---|
| 292 | for (( i=0 ; i<="$VM_Host_totoal_nu" ; i++ )) |
---|
| 293 | do |
---|
| 294 | if [ $i -lt 10 ]; then |
---|
| 295 | Xen_MAC_5=0 |
---|
| 296 | Xen_MAC_6=$i |
---|
| 297 | 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 |
---|
| 298 | elif [ $i -ge 10 ] && [ $i -le 15 ] ; then |
---|
| 299 | Xen_MAC_5=0 |
---|
| 300 | # translate 10(dec) to 16(hex) |
---|
| 301 | printf '%X\n' $i > /tmp/drbl-virt_MAC |
---|
| 302 | Xen_MAC_6=$(cat /tmp/drbl-virt_MAC) |
---|
| 303 | 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 |
---|
| 304 | else |
---|
| 305 | printf '%X\n' $i > /tmp/drbl-virt_MAC |
---|
| 306 | Xen_MAC_5=$(cat /tmp/drbl-virt_MAC) |
---|
| 307 | 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 |
---|
| 308 | fi |
---|
| 309 | done |
---|
[184] | 310 | } |
---|
[183] | 311 | |
---|
[185] | 312 | #function add_VM_dhcpd_conf(){ |
---|
| 313 | ## backup dhcpd.conf |
---|
| 314 | #if [ -e /etc/dhcp3/dhcpd.conf ]; then |
---|
| 315 | #cp -f /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.drbl-virt_bak |
---|
| 316 | #fi |
---|
| 317 | #subnet_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep $eth_network | awk '{print $1}') |
---|
| 318 | #host_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep 'host ' | grep '{' | awk '{print $1}') |
---|
| 319 | # |
---|
| 320 | #for subnet_line in $subnet_lines |
---|
| 321 | #do |
---|
| 322 | # host_lines cat -n /etc/dhcp3/dhcpd.conf | sed -n "$subnet_line,\$p" | grep 'host .* {' | awk '{print $1}' |
---|
| 323 | #done |
---|
| 324 | #} |
---|