#!/sbin/runscript
# Copyright 1999-2003 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.23 2003/03/24 15:25:05 azarah Exp $


depend() {
	need checkroot modules
}

start() {
	local retval=0
	
	# 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.
	# Sadly this is based on Mandrake's startup scripts, as nobody seems
	# to want to fix our old one, and I do not use (or able to at present) raid ....
	# NOTE:  You need a properly setup /etc/raidtab, and until sombody come up
	#        with a *working* one, that is tested to work for all setups!
	if [ -f /proc/mdstat -a -f /etc/raidtab ]
	then
		ebegin "Starting up RAID devices: " 

		local rc=0
		local retval=0
        
		for i in $(awk '/^[[:space:]]*raiddev/ { print $2 }' /etc/raidtab)
		do
			local raiddev="${i##*/}"
			local raidstat="$(egrep "^${raiddev} : active" /proc/mdstat)"
			
			if [ -z "${raidstat}" ]
			then
				# First scan the /etc/fstab for the "noauto"-flag
				# for this device. If found, skip the initialization
				# for it to avoid dropping to a shell on errors.
				# If not, try raidstart...if that fails then
				# fall back to raidadd, raidrun.  If that
				# also fails, then we drop to a shell
				local retval=1
				local noauto="$(egrep "^${i}" /etc/fstab | grep -c 'noauto')"

				einfon "  Trying ${raiddev}..."

				raiddev=""
				
				if [ "${noauto}" -gt 0 ]
				then
					retval=0
					raiddev=" (skipped)"
				fi
				if [ "${retval}" -gt 0 -a -x /sbin/raidstart ]
				then
					/sbin/raidstart "${i}"
					retval=$?
				fi
				if [ "${retval}" -gt 0 -a -x /sbin/raid0run ]
				then
					/sbin/raid0run "${i}"
					retval=$?
				fi
				if [ "${retval}" -gt 0 -a -x /sbin/raidadd -a -x /sbin/raidrun ]
				then
					/sbin/raidadd "${i}"
					/sbin/raidrun "${i}"
					retval=$?
				fi

				echo "${raiddev}"
				
				if [ "${retval}" -gt 0 ]
				then
					rc=1
					eend ${retval}
				else
					ewend ${retval}
				fi
			fi
		done
		
		# A non-zero return means there were problems.
		if [ "${rc}" -gt 0 ]
		then
			echo
			eerror "An error occurred during the RAID startup"
			eerror "Dropping you to a shell; the system will reboot"
			eerror "when you leave the shell."
			echo; echo
			/sbin/sulogin ${CONSOLE}
			einfo "Unmounting filesystems"
			/bin/mount -a -o remount,ro &>/dev/null
			einfo "Rebooting"
			/sbin/reboot -f
		fi
	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 -C -R -A -a
		fi
		retval=$?
		if [ "${retval}" -eq 0 ]
		then
			eend 0
		elif [ "${retval}" -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
