#!/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(){
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/.tmp/hypervisior
	else
	  echo "KVM" > $Work_Home/.tmp/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/.tmp/hypervisior
fi  
}   


# [Create drbl-virt path]
function install_drbl-virt_dir(){
    mkdir -p $Work_Home/.tmp
}


# [Check Hypervisior]
function check_hypervisior(){
    hypervisior=$(cat $Work_Home/.tmp/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_Host_IP_range(){
cat /etc/drbl/IP_HOST_TABLE | sed "${Host_first_Line}p" 
cat /etc/drbl/IP_HOST_TABLE | sed "${Host_final_Line}p"                    
declare -i Host_first_Line=$(cat -n /etc/drbl/IP_HOST_TABLE  | grep "# Created by DRBL (drblpush), begin" | awk '{print $1}')
Host_first_Line=$((${Host_first_Line}+1))
                        
declare -i Host_final_Line=$(cat -n /etc/drbl/IP_HOST_TABLE  | grep "# Created by DRBL (drblpush), end" | awk '{print $1}')
Host_final_Line=$((${Host_final_Line}-1))
#echo "$Host_first_Line"
#echo "$Host_final_Line"
                                                                           
Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | sed -n "${Host_first_Line}p"| awk '{print $1}')
Host_final_IP=$(cat /etc/drbl/IP_HOST_TABLE | sed -n "${Host_final_Line}p"| awk '{print $1}')
#echo $Host_first_IP                                                                                                                                                 
#echo $Host_final_IP
}

function get_VM_IP_range(){
echo ""        
echo "Your drbl client IP range $Host_first_IP ~ $Host_final_IP"
declare -i Host_final_IP_4=$(echo $Host_final_IP | cut -d "." -f4)
Host_final_IP_4=$(($Host_final_IP_4+1))                       
VM_first_IP=$(echo $Host_final_IP | cut -d "." -f 1-3)
VM_first_IP="${VM_first_IP}.${Host_final_IP_4}"       
echo "The VM IP Range will start from $VM_first_IP"   
while [ $jude != "yes" -a $jude != "y" ];             
do             
    read "Input your IP range for VM: " VM_IP_range   
done           
echo "VM_first_IP=$VM_first_IP" > $Work_Home/.tmp/IP_VM_TABLE
echo "VM_IP_range=$VM_IP_range" >> $Work_Home/.tmp/IP_VM_TABLE

}

# [DRBL Check]



