#!/bin/bash
# Program:
#   Path DRBL evironment to support KVM
# Author:
#   Jazz, Rock {jazz, rock}@nchc.org.tw
# Version:
#    1.0
# History:
#   2010/10/18  Rock    First release (1.0)

# [Source]
source /opt/drbl-virt/conf/drbl-virt.conf
source $Work_Path/functions_drbl_virt

# [Main]
# = 1. Patch DRBL xen env for client =
# == Shutdown all drbl client ==
echo -e "\nPlease shutdown all drbl client !!!"
echo "You can use /opt/drbl/sbin/dcs to shutdown."
read -p "Push [Enter] to continue......"

# == check KVM Kernel version
check_kvm
echo $KVM_Kernel > $Work_Home/etc/KVM_Kernel

# == Modify PXE booting menu ==
# if menu doesn't has drbl-virt, then add it
cat /opt/drbl/sbin/generate-pxe-menu | grep "label drbl-virt" >> /dev/null
if [  $? != 0 ]; then
# backup generate-pxe-menu
cp /tftpboot/nbi_img/pxelinux.cfg/default /tftpboot/nbi_img/pxelinux.cfg/default.drbl-virt_bak
cp /opt/drbl/sbin/generate-pxe-menu /opt/drbl/sbin/generate-pxe-menu.drbl-virt_bak

generate_PXE_menu="/opt/drbl/sbin/generate-pxe-menu"
declare -i modify_line=$(cat -n $generate_PXE_menu | grep 'label drbl' | awk 'NR==1 {print $1}')
# delete default
sed -i 's/^  MENU DEFAULT/  # MENU DEFAULT/g' $generate_PXE_menu
# modify menu
sed -i "${modify_line}i\label drbl-virt" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  MENU DEFAULT" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  # MENU HIDE" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  MENU LABEL DRBL-Virtualization (KVM $KVM_Kernel)" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  kernel vmlinuz-pxe" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  append  initrd=initrd-pxe.img devfs=nomount drblthincli=off selinux=0 nomodeset" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  TEXT HELP" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\  ENDTEXT" $generate_PXE_menu
modify_line=$modify_line+1
sed -i "${modify_line}i\ " $generate_PXE_menu
fi


# = 2. Patch DRBL initrd to support bridge =
# == Patch /usr/bin/mkpxeinitrd-net (1)Xen network scripts (2)command (3)modules ==
# backup mkpxeinitrd-net. if already has "drbl-virt add", doesn't sed
source $Work_Path/drbl_bridge_kvm_patch


# = 3. Load KVM module when booting =
# for Debian & Ubuntu
if [ -e /etc/modules ]; then
    grep kvm /etc/modules
    if [ $? -eq 1 ]; then
       echo "kvm" >> /etc/modules
    fi
# for CentOS & Fedora
elif [ -e /etc/sysconfig/modules ]; then
    grep -r "modprobe kvm" /etc/sysconfig/modules/ 
    if [ $? -eq 1 ]; then
        cat  > /etc/sysconfig/modules/kvm.modules << EOF
#!/bin/sh                 
echo -n "Loading kvm module..."
if \$(/sbin/modprobe kvm)
    then
        echo "[SUCCESS]:"
    else
        echo "[FAILURE]:"
fi
EOF
    chmod +x /etc/sysconfig/modules/kvm.modules
    fi
fi

# = 4. Copy xen kernel & initrd to /home/domains =
if [ ! -e /home/domains ]; then
    mkdir /home/domains
fi
cp /tftpboot/nbi_img/vmlinuz-pxe /home/domains
cp /tftpboot/nbi_img/initrd-pxe.img /home/domains


