#!/bin/sh

# unmount lustre
if [ -f /proc/mounts ] ; then
    while read DEV MNTPNT FSTYPE OPTS REST; do
        # Skip blank lines and comments
        [ -z "${DEV##\#*}" ] && continue

        if [ "$FSTYPE" = "lustre" ] ; then
	    fuser -k -m $MNTPNT
	    # lazy unmount to avoid blocking
            umount -l $MNTPNT
        fi

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

# Run shutdown scripts.
# FIXME:
# We don't have a proper SysV init, K* symlinks, or chkconfig to manage deps.
# The best we can do is run stop on all the S* scripts.
for i in /etc/init.d/rc3.d/S[0-9][0-9]* ; do
    test -f $i || continue;

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

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

# enable sysrq
echo 1 > /proc/sys/kernel/sysrq
# goodbye (boot)
echo b > /proc/sysrq-trigger
