#!/bin/sh

LOGFILE=/var/log/pv_vm_fsck.log

PVDIR=/home/xen/PARAVIRT
EXTRAS="/dev/VM/U704-ROOT-PV"

function check_fs() {
   filesystem=${1}

	echo -n "*** Checking $filesystem:" >> $LOGFILE
	fsck -p $filesystem 1> $$ 2> $$
	status=`echo $?`
	if [ $status -gt 3 ] ; then
		mail -s "VM FSCK FAILURE" root <<EOF
PV VM Filesystem $filesystem could not be automatically repaired.
EOF
        echo " problem detected" >>  $LOGFILE
		cat $$ >>  $LOGFILE
		echo "*** Mail sent to root about this problem"  >> $LOGFILE
    else
		echo " no problems" >>  $LOGFILE
#		cat $$ >>  $LOGFILE	
    fi
	rm $$
}

stamp=`date`
echo "Fscking VM Images during reboot: Start $stamp" >> $LOGFILE

find $PVDIR -name "*.img" | while read imagename ; do
	check_fs $imagename
done

if [ "x$EXTRAS" != "x" ] ; then
	echo $EXTRAS | while read partition ; do
  	  check_fs $partition
	done
fi

stamp=`date`
echo "Fscking VM Images during reboot: Finished $stamp" >> $LOGFILE
echo ""  >> $LOGFILE
