#!/bin/bash
#
# by: Brian Butler (20100902)
#
# Grade the Central Storage 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=centralstoreGrade
DEBUG=false
ERROR_MESSAGE="FAILED."
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
userInput="no"
set -o pipefail
mnt="/coldstorage"

# 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"

# Check for required packages
PACKAGES=( iscsi-initiator-utils )
check_packages

# Check to see if the iscsi target is logged in
echo
echo -n "Checking for active iscsi target sessions... "
tgt="$(iscsiadm -m node 2> log)"
sess="$(iscsiadm -m session 2> log)"
[ "$tgt" != "" -a "$sess" != "" ] && echo "PASSED." || exit 1

# Check to see if the filesystem is accessible/mounted and mounts at boot
echo
echo -n "Checking for the mounted filesystem... "
(mount | grep -q "^/dev/sda1 on /coldstorage type ext") || exit 1
(echo 'testing' > $mnt/testing 2> /dev/null) && echo "PASSED." || exit 1
echo
echo -n "Checking that the filesystem will mount at boot... "
(fuser -km $mnt 2>&1 | log)
(umount $mnt 2>&1 | log) || exit
(mount -a 2>&1 | log) || exit

if [ -f $mnt/testing ] && chkconfig iscsi
then
  echo "PASSED."
else
  exit 1
fi

exit 0
