#!/bin/bash # Program: # DRBL virt module # Author: # Jazz, Rock {jazz, rock}@nchc.org.tw # Version: # 1.0 # History: # 2010/07/20 Rock First release (1.0) # [Variable Declation] cpu_flag="no" Linux_bit="" Linux_Distribution="" Linux_Version="" VMM_select="" # [Check Root] function check_root(){ if [ $USER != "root" ]; then echo -e "Please change root to run it!" exit fi } function check_root_run(){ if [ $USER != "root" ]; then echo -e "Please change root to run it!" sudo su -c ~/"$0" "$@" exit fi } # [Check CPU support] function cpu_check(){ egrep '(vmx|svm)' --color=always /proc/cpuinfo > /dev/null if [ $? == 0 ]; then cpu_flag="yes" fi } # [Check System Version] function check_systemInfo(){ Linux_bit=$(uname -m) Linux_Distribution=$(lsb_release -i | awk '{print $3}') Linux_Version=$(lsb_release -r | awk '{print $2}') } # [Select Hypervisior(Xen/KVM)] function VMM_select(){ echo "" if [ $cpu_flag == "yes" ]; then while [ "$VMM_select" != "1" -a "$VMM_select" != "2" ] do read -p "Which VMM/Hypervisior will be used in DRBL (1)Xen (2)KVM: " VMM_select done if [ $VMM_select == "1" ]; then echo "Xen" > $Work_Home/etc/hypervisior else echo "KVM" > $Work_Home/etc/hypervisior fi else echo "Xen will be the only Hypervisior in your DRBL" echo -e "Because CPU don't support virtualization, it can't choose KVM to be the Hypervisior" echo "" VMM_select=1 echo "Xen" > $Work_Home/etc/hypervisior fi } # [Create drbl-virt path] function install_drbl-virt_dir(){ mkdir -p $Work_Home/.tmp mkdir -p $Work_Home/etc } # [Check Hypervisior] function check_hypervisior(){ hypervisior=$(cat $Work_Home/etc/hypervisior) } # [Check Debian Xen package] function check_debian_xen(){ aptitude update check_debian_xen_pkg=$(aptitude search xen-linux-system) if [ -n $check_debian_xen_pkg ]; then check_debian_xen_pkg="yes" else check_debian_xen_pkg="no" fi } # [Check Xen kernel] function check_xen_nu(){ Kernels=$(ls /boot | grep vmlinuz) declare -i Kernels_nu=$(echo $Kernels | wc -w) Kernel_choose="" Xens=$(ls /boot | grep -i ^xen) declare -i Xen_nu=$(echo $Xen | wc -w) Xen_choose="" jude="no" while [ $jude != "yes" -a $jude != "y" ] do declare -i i=1 declare -i j=1 echo "" if [ $Kernels_nu -gt 1 ]; then for Kernel in $Kernels do echo "($i) $Kernel" i=i+1 done read -p "Xen Kenrel is (1/2/...): " Kernel_choose Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose}) else Xen_Kernel=$Kernels fi if [ $Xen_nu -gt 1 ]; then for Xen in $Xens do echo "($i) $Xen" j=j+1 done read -p "Xen is (1/2/...): " Xen_choose Xen=$(echo $Xens | cut -d " " -f${Xen_choose}) else Xen=$Xens fi read -p "Are you sure (yes/no): " jude done #echo "debug: Xen_Kernel=$Xen_Kernel" } function debain-lenny_xen_patch(){ echo xen.independent_wallclock=1 >> /etc/sysctl.conf echo loop max_loop=255 >> /etc/modules echo xenblktap >> /etc/modules ln -s /usr/lib/$/bin/tapdisk /usr/sbin } function get_DRBL_eth(){ eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt$ | cut -d "-" -f2 | cut -d "." -f1) eths_nu=$(echo $eths | wc -l) } function get_Host_IP_range(){ if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd" exit 0 fi # get eth network eth_address=$(/opt/drbl/bin/drbl-get-ipadd $eth) eth_netmask=$(/opt/drbl/bin/drbl-get-netmask $eth) eth_network=$(/opt/drbl/bin/drbl-get-network $eth_address $eth_netmask) eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3) IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l) Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print $1}') Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}') } function get_VM_IP_range(){ echo "" echo "DRBL client IP range -> $eth [$Host_first_IP ~ $Host_last_IP]" declare -i Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4) Host_last_IP_4=$(($Host_last_IP_4+1)) VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3) VM_first_IP="${VM_first_IP}.${Host_last_IP_4}" echo "The VM IP Range will start from -> [$VM_first_IP]" jude="no" while [ $jude != "yes" -a $jude != "y" ]; do echo "" echo "Input VM number/IP range in $eth (ex. 40)." read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range read -p "Is it correct? (yes/no): " jude done #echo "VM_first_IP=$VM_first_IP" #echo "VM_IP_range=$VM_IP_range" } function get_VM_prefix_name(){ jude="no" while [ $jude != "yes" -a $jude != "y" ]; do echo "" read -p "Input Prefix name for VM in $eth (ex. drblvm): " VM_prefix_name read -p "Is it correct? (yes/no): " jude done #echo "VM_prefix_name=$VM_prefix_name" } function create_VM_IP_table(){ #/etc/drbl-virt/etc/IP_VM_eth VM_prefix_eth=$(echo $eth | sed 's/eth//g') declare -i VM_Host_totoal_nu=$((${Host_last_IP_4}-1+${VM_IP_range})) if [ -e /opt/drbl-virt/etc/IP_VM_$eth ]; then 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 fi declare -i VM_IP_0=$VM_prefix_eth declare -i VM_IP_1="" declare -i VM_IP_2="" declare -i VM_IP_3="" # 2 bit if [ $VM_Host_totoal_nu -lt 100 ]; then for (( i=$Host_last_IP_4 ; i<="$VM_IP_range" ; i++ )) do if [ $i -lt 10 ]; then VM_IP_1=0 VM_IP_2=$i 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 else VM_IP_1=$i echo "${eth_network_f3}.$VM_IP_1 ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$et fi done # 3 bit elif [ $VM_Host_totoal_nu -ge 100 ] && [ $VM_Host_totoal_nu -lt 254 ]; then for (( i=$Host_last_IP_4 ; i<=$VM_IP_range ; i++ )) do if [ $i -lt 10 ]; then VM_IP_1=0 VM_IP_2=0 VM_IP_3=$i 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 elif [ $i -lt 100 ]; then VM_IP_1=0 VM_IP_2=$i 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 else VM_IP_1=$i echo "${eth_network_f3}.${VM_IP_1} ${VM_prefix_name}${VM_IP_0}${VM_IP_1}" >> /opt/drbl-virt/etc/IP_VM_$eth fi done fi }