#!/sbin/runscript
# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/checkfs,v 1.18 2002/11/18 13:07:02 azarah Exp $


depend() {
	need checkroot modules
}

start() {
	# LVM support for /usr, /home, /opt ....
	# This should be done *before* checking local
	# volumes, or they never get checked.
	
	# NOTE: Add needed modules for LVM or RAID, etc
	#       to /etc/modules.autoload if needed
	
	if [ -x /sbin/vgscan -a -d /proc/lvm ]
	then
		ebegin "Setting up the Logical Volume Manager"
		#still echo stderr for debugging
		/sbin/vgscan >/dev/null 
		if [ "$?" -eq 0 ] && [ -x /sbin/vgchange -a -f /etc/lvmtab ]
		then
			/sbin/vgchange -a y >/dev/null
		fi
		eend $? "Failed to setup the LVM"
	fi

	# Start software raid
	if [ -x /sbin/raidstart -a -f /etc/raidtab -a -f /proc/mdstat ]
	then
		ebegin "Starting software RAID"
		ACTIVE_RAID="`grep -E "md[0-9]+[[:space:]]?: active raid" /proc/mdstat | awk -F ':' '{print $1}'`"
		if [ ! -z "${ACTIVE_RAID}" ]
		then
			for x in $(grep -E "raiddev /dev/md[0-9]+" /etc/raidtab | \
			           awk '{print $2}' | grep -v "`echo ${ACTIVE_RAID} | sed s'/[[:space:]]//g'`")
			do
				/sbin/raidstart /dev/${x}
			done
		else
			/sbin/raidstart --all
		fi
		eend $? "Failed to start software RAID"
	fi

	if [ -f /fastboot ]
	then
		rm -f /fastboot
	else
		ebegin "Checking all filesystems"
		if [ -f /forcefsck ]
		then
			ewarn "A full fsck has been forced"
			fsck -C -R -A -a -f
			rm -f /forcefsck
		else
			fsck -R -A -a
		fi
		if [ "$?" -eq 0 ]
		then
			eend 0
		elif [ "$?" -eq 1 ]
		then
			ewend 1 "Filesystem errors corrected."
			#everything should be ok, so return a pass
			return 0
		else
			eend 2 "Fsck could not correct all errors, manual repair needed"
			/sbin/sulogin ${CONSOLE}
		fi
	fi
}


# vim:ts=4
