#!/bin/bash
#
# by: Robert Locke (20101130)
# based on: Brian Butler
#
# Grade the LVM lab
# this script is to be installed and run on serverX.example.com
# output is logged to syslog

# Set environment and declare global variables
. /usr/local/lib/labtool.shlib
trap on_exit EXIT
LOG_FACILITY=local0
LOG_PRIORITY=info
LOG_TAG=lvmGrade
DEBUG=false
ERROR_MESSAGE="FAILED."
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
userInput="no"
set -o pipefail

# Check to make sure we are running as root
check_root

# Check to make sure we are running on the correct host
check_host "server"

newvg=extra
newlv=iso
HOME="vgsrv/home"

# Check size of $HOME LV
echo
echo "Checking size of $HOME LV"
SIZE=`lvs --noheadings -o lv_size $HOME | awk -F . '{print $1}'`
if [[ $SIZE -lt 400 ]];then
	echo $HOME LV is too small
elif [[ $SIZE -gt 600 ]];then
	echo $HOME LV is too large
else
	echo PASS
fi

# Check size of $HOME FS
echo
echo "Checking size of $HOME filesystem"
SIZE=`df -m /home | grep /home | awk '{print $1}'`
if [[ $SIZE -lt 400 ]];then
	echo $HOME filesystem is too small
elif [[ $SIZE -gt 600 ]];then
	echo $HOME filesystem is too large
else
	echo PASS
fi

# Check that all remaining disk space was used.
# TODO

# Check $newvg exists
echo
echo "Checking that $newvg exists"
if vgs $newvg |grep $newvg &> /dev/null;then
	echo PASS
else
	echo $newvg does not exist
fi

# Check $newvg/$newlv exists
echo
echo "Checking that $newvg/$newlv exists"
if lvs $newvg/$newlv &> /dev/null;then
	echo PASS
else
	echo $newvg/$newlv does not exist
fi

# Check that we still have space left in the $newvg VG
VGLEFT=`vgs --noheadings -o vg_free_count $newvg`
echo
echo "Checking that $newvg has space left"
if [[ $VGLEFT -gt 0 ]];then
	echo PASS
else
        echo $newvg does not have space left
fi

ISO=/iso
# Check that $ISO is mounted
echo
echo "Checking that $ISO is mounted"
if df | grep -q $ISO ; then
	echo PASS
else
        echo $ISO is not mounted
fi

# Check that $ISO iso is mounted persistently
echo
echo "Checking that $ISO iso is mounted persistently"
if grep -q $ISO /etc/fstab ; then
	echo PASS
else
        echo $ISO will not be mounted at boot time
fi

# Check that $ISO is ext4
echo
echo "Checking that $ISO is ext4"
if df -T | grep $ISO | grep -q ext4; then
	echo PASS
else
        echo $ISO is not formated with ext4
fi

exit 0
