[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 | |
---|
| 73 | # [Create drbl-virt path] |
---|
[161] | 74 | function install_drbl-virt_dir(){ |
---|
| 75 | mkdir -p $Work_Home/.tmp |
---|
[180] | 76 | mkdir -p $Work_Home/etc |
---|
[161] | 77 | } |
---|
| 78 | |
---|
[171] | 79 | |
---|
| 80 | # [Check Hypervisior] |
---|
[161] | 81 | function check_hypervisior(){ |
---|
[180] | 82 | hypervisior=$(cat $Work_Home/etc/hypervisior) |
---|
[161] | 83 | } |
---|
| 84 | |
---|
[171] | 85 | |
---|
| 86 | # [Check Debian Xen package] |
---|
| 87 | function check_debian_xen(){ |
---|
| 88 | aptitude update |
---|
| 89 | check_debian_xen_pkg=$(aptitude search xen-linux-system) |
---|
| 90 | if [ -n $check_debian_xen_pkg ]; then |
---|
| 91 | check_debian_xen_pkg="yes" |
---|
| 92 | else |
---|
| 93 | check_debian_xen_pkg="no" |
---|
| 94 | fi |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | # [Check Xen kernel] |
---|
| 99 | function check_xen_nu(){ |
---|
| 100 | Kernels=$(ls /boot | grep vmlinuz) |
---|
[172] | 101 | declare -i Kernels_nu=$(echo $Kernels | wc -w) |
---|
[171] | 102 | Kernel_choose="" |
---|
[172] | 103 | |
---|
| 104 | Xens=$(ls /boot | grep -i ^xen) |
---|
| 105 | declare -i Xen_nu=$(echo $Xen | wc -w) |
---|
| 106 | Xen_choose="" |
---|
| 107 | |
---|
[171] | 108 | jude="no" |
---|
| 109 | |
---|
| 110 | while [ $jude != "yes" -a $jude != "y" ] |
---|
| 111 | do |
---|
| 112 | declare -i i=1 |
---|
[172] | 113 | declare -i j=1 |
---|
[171] | 114 | echo "" |
---|
| 115 | |
---|
[172] | 116 | if [ $Kernels_nu -gt 1 ]; then |
---|
| 117 | for Kernel in $Kernels |
---|
| 118 | do |
---|
| 119 | echo "($i) $Kernel" |
---|
| 120 | i=i+1 |
---|
| 121 | done |
---|
[171] | 122 | |
---|
[172] | 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 | |
---|
[171] | 143 | read -p "Are you sure (yes/no): " jude |
---|
| 144 | done |
---|
| 145 | |
---|
| 146 | #echo "debug: Xen_Kernel=$Xen_Kernel" |
---|
| 147 | } |
---|
| 148 | |
---|
[178] | 149 | |
---|
[179] | 150 | function debain-lenny_xen_patch(){ |
---|
| 151 | echo xen.independent_wallclock=1 >> /etc/sysctl.conf |
---|
| 152 | echo loop max_loop=255 >> /etc/modules |
---|
| 153 | echo xenblktap >> /etc/modules |
---|
| 154 | ln -s /usr/lib/$/bin/tapdisk /usr/sbin |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
[180] | 158 | function get_DRBL_eth(){ |
---|
[181] | 159 | eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1) |
---|
[182] | 160 | eths_nu=$(echo $eths | wc -w) |
---|
[180] | 161 | |
---|
[182] | 162 | if [ $eths_nu -gt 1 ]; then |
---|
| 163 | echo "" |
---|
| 164 | echo "There are multiple eth for DRBL environment: " |
---|
| 165 | echo "$eths" |
---|
| 166 | fi |
---|
[180] | 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
[178] | 170 | function get_Host_IP_range(){ |
---|
[180] | 171 | if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then |
---|
| 172 | echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd" |
---|
| 173 | exit 0 |
---|
| 174 | fi |
---|
| 175 | |
---|
[184] | 176 | # get eth network X.X.X |
---|
[180] | 177 | eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3) |
---|
| 178 | |
---|
| 179 | IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l) |
---|
[181] | 180 | Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}') |
---|
[180] | 181 | Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}') |
---|
| 182 | |
---|
[178] | 183 | } |
---|
| 184 | |
---|
[182] | 185 | function get_VM_IP_range_and_prefix_name(){ |
---|
| 186 | |
---|
[180] | 187 | echo "" |
---|
[182] | 188 | echo "DRBL client IP range -> $eth: [$Host_first_IP ~ $Host_last_IP]" |
---|
[178] | 189 | |
---|
[182] | 190 | Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4) |
---|
[180] | 191 | Host_last_IP_4=$(($Host_last_IP_4+1)) |
---|
| 192 | VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3) |
---|
| 193 | VM_first_IP="${VM_first_IP}.${Host_last_IP_4}" |
---|
| 194 | |
---|
| 195 | echo "The VM IP Range will start from -> [$VM_first_IP]" |
---|
| 196 | jude="no" |
---|
| 197 | while [ $jude != "yes" -a $jude != "y" ]; |
---|
| 198 | do |
---|
| 199 | echo "" |
---|
[182] | 200 | echo "Input VM number/IP range for $eth (ex. 40)." |
---|
| 201 | |
---|
| 202 | jude_2="no" |
---|
| 203 | while [ $jude_2 != "yes" -a $jude_2 != "y" ] |
---|
| 204 | do |
---|
| 205 | jude_2="yes" |
---|
| 206 | read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range |
---|
| 207 | if [ $((${VM_IP_range}+${Host_last_IP_4})) -ge 254 ]; then |
---|
| 208 | echo "range can't greater than 254" |
---|
| 209 | jude_2="no" |
---|
| 210 | fi |
---|
| 211 | done |
---|
| 212 | |
---|
[184] | 213 | #read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name |
---|
| 214 | VM_prefix_name=$(cat /etc/drbl/drblpush.conf | grep hostname= | sed 's/hostname=//') |
---|
[180] | 215 | read -p "Is it correct? (yes/no): " jude |
---|
| 216 | done |
---|
| 217 | |
---|
| 218 | #echo "VM_first_IP=$VM_first_IP" |
---|
| 219 | #echo "VM_IP_range=$VM_IP_range" |
---|
[178] | 220 | } |
---|
| 221 | |
---|
[182] | 222 | #function get_VM_prefix_name(){ |
---|
| 223 | #jude="no" |
---|
| 224 | #while [ $jude != "yes" -a $jude != "y" ]; |
---|
| 225 | #do |
---|
| 226 | # echo "" |
---|
| 227 | # read -p "Input Prefix name for VM for $eth (ex. drblvm): " VM_prefix_name |
---|
| 228 | # read -p "Is it correct? (yes/no): " jude |
---|
| 229 | #done |
---|
[180] | 230 | #echo "VM_prefix_name=$VM_prefix_name" |
---|
[182] | 231 | #} |
---|
[156] | 232 | |
---|
[180] | 233 | function create_VM_IP_table(){ |
---|
| 234 | #/etc/drbl-virt/etc/IP_VM_eth |
---|
[181] | 235 | VM_prefix_eth=$(echo $eth | sed 's/eth//g') |
---|
[183] | 236 | VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range})) |
---|
[181] | 237 | if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then |
---|
[184] | 238 | 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] | 239 | fi |
---|
| 240 | declare -i VM_IP_0=$VM_prefix_eth |
---|
| 241 | declare -i VM_IP_1="" |
---|
| 242 | declare -i VM_IP_2="" |
---|
| 243 | declare -i VM_IP_3="" |
---|
[171] | 244 | |
---|
[181] | 245 | # 2 bit |
---|
| 246 | if [ $VM_Host_totoal_nu -lt 100 ]; then |
---|
[182] | 247 | for (( i=$Host_last_IP_4 ; i<="$VM_Host_totoal_nu" ; i++ )) |
---|
[181] | 248 | do |
---|
| 249 | if [ $i -lt 10 ]; then |
---|
| 250 | VM_IP_1=0 |
---|
| 251 | VM_IP_2=$i |
---|
| 252 | 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 |
---|
| 253 | else |
---|
| 254 | VM_IP_1=$i |
---|
[182] | 255 | echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth |
---|
[181] | 256 | fi |
---|
| 257 | done |
---|
| 258 | # 3 bit |
---|
| 259 | elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then |
---|
[182] | 260 | for (( i=$Host_last_IP_4 ; i<=$VM_Host_totoal_nu ; i++ )) |
---|
[181] | 261 | do |
---|
| 262 | if [ $i -lt 10 ]; then |
---|
| 263 | VM_IP_1=0 |
---|
| 264 | VM_IP_2=0 |
---|
| 265 | VM_IP_3=$i |
---|
| 266 | 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] | 267 | elif [ $i -ge 10 ] && [ $i -lt 100 ]; then |
---|
[181] | 268 | VM_IP_1=0 |
---|
| 269 | VM_IP_2=$i |
---|
| 270 | 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 |
---|
| 271 | else |
---|
| 272 | VM_IP_1=$i |
---|
| 273 | echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth |
---|
| 274 | fi |
---|
| 275 | done |
---|
| 276 | |
---|
| 277 | fi |
---|
[180] | 278 | } |
---|
[171] | 279 | |
---|
[183] | 280 | function generate_Xen_MAC_address(){ |
---|
| 281 | echo "eth $eth" |
---|
| 282 | echo "VM_Host_totoal_nu $VM_Host_totoal_nu" |
---|
| 283 | echo "Host_last_IP_4 $Host_last_IP_4" |
---|
| 284 | echo "VM_prefix_eth $VM_prefix_eth" |
---|
[180] | 285 | |
---|
[183] | 286 | # /opt/drbl-virt/etc/macadr-VM-ethX.txt |
---|
| 287 | # MAC address [00:16:3e:xx:xx:xx] is for Xen use |
---|
| 288 | declare -i Xen_MAC_1="0" |
---|
| 289 | declare -i Xen_MAC_2="$VM_prefix_eth" |
---|
| 290 | declare -i Xen_MAC_3="0" |
---|
| 291 | declare -i Xen_MAC_4="0" |
---|
| 292 | Xen_MAC_5="" |
---|
| 293 | Xen_MAC_6="" |
---|
[180] | 294 | |
---|
[183] | 295 | # backup |
---|
[184] | 296 | 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] | 297 | |
---|
[183] | 298 | # generate MAC |
---|
| 299 | for (( i=0 ; i<="$VM_Host_totoal_nu" ; i++ )) |
---|
| 300 | do |
---|
| 301 | if [ $i -lt 10 ]; then |
---|
| 302 | Xen_MAC_5=0 |
---|
| 303 | Xen_MAC_6=$i |
---|
| 304 | 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 |
---|
| 305 | elif [ $i -ge 10 ] && [ $i -le 15 ] ; then |
---|
| 306 | Xen_MAC_5=0 |
---|
| 307 | # translate 10(dec) to 16(hex) |
---|
| 308 | printf '%X\n' $i > /tmp/drbl-virt_MAC |
---|
| 309 | Xen_MAC_6=$(cat /tmp/drbl-virt_MAC) |
---|
| 310 | 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 |
---|
| 311 | else |
---|
| 312 | printf '%X\n' $i > /tmp/drbl-virt_MAC |
---|
| 313 | Xen_MAC_5=$(cat /tmp/drbl-virt_MAC) |
---|
| 314 | 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 |
---|
| 315 | fi |
---|
| 316 | done |
---|
[184] | 317 | } |
---|
[183] | 318 | |
---|
[184] | 319 | function add_VM_dhcpd_conf(){ |
---|
| 320 | # backup dhcpd.conf |
---|
| 321 | if [ -e /etc/dhcp3/dhcpd.conf ]; then |
---|
| 322 | cp -f /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.drbl-virt_bak |
---|
| 323 | fi |
---|
[183] | 324 | |
---|
[184] | 325 | subnet_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep $eth_network | awk '{print $1}') |
---|
| 326 | host_lines=$(cat -n /etc/dhcp3/dhcpd.conf | grep 'host ' | grep '{' | awk '{print $1}') |
---|
[183] | 327 | |
---|
[184] | 328 | for subnet_line in $subnet_lines |
---|
| 329 | do |
---|
| 330 | host_lines cat -n /etc/dhcp3/dhcpd.conf | sed -n "$subnet_line,\$p" | grep 'host .* {' | awk '{print $1}' |
---|
[183] | 331 | |
---|
[184] | 332 | done |
---|
| 333 | |
---|
| 334 | } |
---|