#!/bin/bash

# install-vserver - script to install a virtual machine with the server number +100.  
# For use as a testing server platform.

# Created by by Joshua M. Hoffman
# Modified by Zak Brown, 14-Aug-2007

# Say something so they know it is working

# exit codes:
# 0 probable success
# 1 can't reach instructor
# 2 not running as root
# 3 no virt in kernel
# 4 no $VIRT_COMMAND
# 5 vserver already installed
# 6 /dev/vol0/vserver missing
# 7 vserver installed and running
VIRT_COMMAND=/usr/bin/virsh

echo
echo "* Preparing to install vserver virtual machine"

#Setup environment
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

# Lets get the last octet of the ip address.
LOOIA=$(echo "$(hostname -i | cut -d. -f4 )" | bc)

# Can we reach instructor?
ping -c1 instructor &>/dev/null
PING_STATUS=$?
if [ "$PING_STATUS" -ne "0" ] ; then
	echo
	echo "* There was an error contacting instructor"
	echo "* Please verify your network settings and try again"
	echo "* vserver install was ABORTED"
	echo
	exit 1
fi

# Are we running as root?
if [ "$(id -u)" -ne "0" ] ; then
	echo
	echo "* This command must be run as root"
	echo "* vserver install was ABORTED"
	echo
	exit 2
fi

# Is KVM supported?
if ! egrep -q '^flags.*(vmx|svm)' /proc/cpuinfo; then
	echo
	echo "* You do not appear to be running a machine that supports KVM"
	echo "* Please ensure you have a 64-bit CPU with VT extensions enabled in the BIOS"
	echo "* vserver install was ABORTED"
	echo
	exit 3
fi

# Do we have $VIRT_COMMAND?
if [ ! -x "$VIRT_COMMAND" ] ; then
	echo
	echo "* You are missing a needed package"
	echo "* Please install libvirt-client and try again"
	echo "* HINT: yum -y install libvirt-client"
	echo "* vserver install was ABORTED"
	echo
	exit 4
fi

# Is vserver already installed?
if [ -e /etc/libvirt/qemu/vserver.xml ] ; then
	echo
	echo "* vserver seems to already be installed"
		# Is it running as well?
		$VIRT_COMMAND list | grep -q vserver
		RUNNING=$?
		if [ "$RUNNING" -eq "0" ] ; then
			echo
			echo "* vserver also appears to be running"
			echo
			echo "* To access your vserver, select"
			echo "  Applications -> System Tools -> Virtual Machine Manager"
			echo "  from the menu bar at the top of the screen and then double-click"
			echo "  on then entry for vserver."
			echo
			echo "* To kill it, issue the command: $VIRT_COMMAND destroy vserver"
			echo
			echo "* To remove it and start over, issue the commands:"
			echo "   $VIRT_COMMAND destroy vserver ; $VIRT_COMMAND undefine vserver"
			echo
			
			echo "* Make sure to kill it if you want to start over"
			echo
			echo "* vserver install was ABORTED"
			echo
			exit 7
		fi	
	echo "* To start it, issue the command: $VIRT_COMMAND start vserver"
	echo "* To remove it and start over, run the following:"
	echo "   $VIRT_COMMAND destroy vserver ; $VIRT_COMMAND undefine vserver"
	echo "* vserver install was ABORTED"
	echo
	exit 5
fi

if [ ! -L /dev/vol0/vserver ]; then
     echo "You are missing needed logical volume"
     echo "Please check there are /dev/vol0/vserver and try again"
     echo "vserver install was ABORTED"
     exit 6
fi

# ADD: test for libvirt, libvirtd service, qemu-kvm

# echo a blank line so they don't lose hope!
echo
echo "* Starting kickstart install of vserver virtual machine"

if [ $LOOIA -lt 10 ];then
        LOOIA_PLUSZERO=`echo 0$LOOIA`
elif [ $LOOIA -gt 20 ];then
        # Not regular station
        # 1-20 (decimal!) are taken, so use something else
        # This may cause overlap, but assume we are greater than 32
        LOOIA_PLUSZERO=`echo "obase=16; $LOOIA"|bc`
else
        # Regular station, 10-20
        LOOIA_PLUSZERO=$LOOIA
fi

# Ok, lets give it a shot...  If it completes successfully then add a link into the /etc/libvirt/qemu/autostart/ directory.
/usr/sbin/virt-install --vnc --noautoconsole --name=vserver --ram=512 --arch=x86_64 --vcpus=1 --os-type=linux --os-variant=rhel6 --hvm --accelerate --disk=/dev/vol0/vserver -m 52:54:00:00:00:${LOOIA_PLUSZERO} -w bridge=br0 --location=http://instructor.example.com/pub/rhel6/dvd --extra-args="ks=nfs:instructor.example.com:/kickstart/vserver.cfg ip=192.168.0.$(($LOOIA+100)) netmask=255.255.255.0 gateway=192.168.0.254 dns=192.168.0.254 noipv6 s=$LOOIA" &>/dev/null && $VIRT_COMMAND autostart vserver


# NOTE: In rhel5beta2 virt-install is so buggy that it dies right after kicking
# it off.  When it actually works we will most likely want students to see the
# output as it will be informative. -JMH (Jan 19 15:05 EST 2007)

echo
echo "* The vserver virtual machine is installing"
echo
echo "* To access and monitor vserver, select" 
echo "  Applications -> System Tools -> Virtual Machine Manager"
echo "  from the menu bar at the top of the screen, then click on the"
echo "  entry for vserver and click the Open button"
echo
echo "* Once the install completes, vserver will shutdown.  To start it"
echo "  use the command: $VIRT_COMMAND start vserver"
echo
echo "* Note: Your vserver machine will have the ip address 192.168.0.$(($LOOIA+100))"
echo

exit 0

