#!/bin/sh

# The new way to do /etc/mtab
test -f /etc/mtab && rm -rf /etc/mtab
ln -s /proc/mounts /etc/mtab

# Only support lustre or filesystems that are part of the kernel
# Other filesystems must do their mounts via initscripts
supported_fs() {
    if [ $# -ne 1 ] ; then
        return -1
    fi

    if [ "$1" = "lustre" ] ; then
        return 0
    fi

    while read FS EXTRA ; do
        # Can't use single arg read and ${FS## } to trim because
        # future kernels use a "\t" to seperate
        if [ "$FS" = "nodev" ] ; then
            FS="$EXTRA"
        fi
        if [ "$FS" = "$1" ] ; then
            return 0
        fi
    done < /proc/filesystems

    return 1
}

if [ -f /etc/fstab ] ; then
    while read DEV MNTPNT FSTYPE OPTS REST; do
	# Skip blank lines and comments	
	[ -z "${DEV##\#*}" ] && continue
	supported_fs $FSTYPE || continue

	OPTIONS=""
	[ ! -d $MNTPNT ] && mkdir -p $MNTPNT
	[ "$OPTS" != "defaults" ] && OPTIONS="-o $OPTS" 

	if [ "$FSTYPE" = "lustre" ] ; then
	    /sbin/mount.lustre $DEV $MNTPNT $OPTIONS
	else	
	    mount -t $FSTYPE $OPTIONS $DEV $MNTPNT 
	fi

	if [ $? -ne 0 ] ; then
	    echo "Error mounting $FSTYPE filesystem, $DEV at $MNTPNT" 1>&2
	fi
    done < /etc/fstab
fi

for i in /etc/init.d/rc3.d/S[0-9][0-9]* ; do
    test -f $i || continue;

    if test -x "$i" ; then
	$i start
    else
	/bin/sh $i start
    fi

    if [ $? -ne 0 ] ; then
	echo "Error starting ${i##*/S[0-9][0-9]}" 1>&2
    fi
done > /dev/null

# Remove modules, modprobe, and tell the kernel not to autoload
echo > /proc/sys/kernel/modprobe
rm -rf /lib/modules
rm /sbin/modprobe
