#! /bin/sh
#
# Copyright (c) 1996 S.u.S.E. GmbH Fuerth, Germany.  All rights reserved.
#
# Author: Koblinger Egmont <egmont@fazekas.hu>
#
# /sbin/init.d/filesystem
#

. /etc/rc.config

#
# start: mount everything according to /etc/fstab
#
# stop: unmount everything, leave '/' read-only, and '/proc' read-write.
#

HAVE_VAR=no
grep -v '^#' /etc/fstab | \
while read des fs rest; do
    if [ "$fs" = "/var" ]; then
        HAVE_VAR=yes
    fi
done


if [ "$1" = start ]; then


    if [ $HAVE_VAR = yes -a -e /var/log ]; then
        echo "Unmounting /var..."
        umount -n -v /var
    fi

    echo "Mounting /proc device..."
    if [ ! -f /proc/version ]; then
        mount -n -v /proc
    fi

    # possibly there are file systems on devices, which need a kernel 
    # module to be loaded.  So start kerneld as early as possible.
    if [ "$START_KERNELD" = yes -a \
            -x /sbin/kerneld -a ! -e /proc/sys/kernel/modprobe ]; then
        /sbin/kerneld
    fi

    # maybe we use "Multiple devices"
    if [ -f /etc/mdtab -a -x /sbin/mdadd ]; then
        echo "Initializing Multiple Devices..."
           /sbin/mdadd -ar
    fi

    echo "Activating swap-devices in /etc/fstab..."
    swapon -a -v

    echo "Running update (bdflush) daemon."
    /sbin/update

    if [ ! -f /fastboot ]; then
        mount -n -o remount,ro /
        if [ $? = 0 ]; then
            sync
            echo "Checking file systems..."
            fsck -A -a
            # A return code of 1 indicates that file system errors
            # were corrected, but that the boot may proceed.
            # A return code of 2 or larger indicates failure.
            if [ $? -gt 1 ]; then
                if [ -f /etc/default.keytab -a -x /bin/loadkeys ]; then
                    /bin/loadkeys /etc/default.keytab
                fi
                echo
                echo "fsck failed.  Please repair manually and reboot. The root"
                echo "file system is currently mounted read-only. To remount it"
                echo "read-write do:"
                echo
                echo "   bash# mount -n -o remount,rw /"
                echo
                echo "Attention: Only CONTROL-D will reboot the system in this"
                echo "maintanance mode. shutdown or reboot will not work."
                echo
                PS1="(repair filesystem) # "
                export PS1
                # XXX /sbin/sulogin /dev/console
                /sbin/sulogin /dev/tty1

                # if the user has mounted something rw, this should be umounted
                echo "Unmounting file systems (ignore error messages)"
                umount -a -n -v
                mount -n -o remount,ro /

                sync
                reboot
            fi
            sync
            mount -n -o remount,rw /
        else
            echo "Failed to remount root file system read-only,"
            echo "file systems are NOT being checked."
        fi
    else
        echo "File systems are NOT being checked."
        mount -n -o remount,rw /
    fi
    rm -f /etc/mtab* /etc/nologin /nologin /fastboot
    #echo "Booting, try again in a few minutes" >/etc/nologin
    sync

    # Mount local filesystems in '/etc/fstab' (and create an entry for /).
    #
    # if the /etc/fstab contains file systems, which are loadable
    # modules, then it causes an error message, if they are placed
    # in front of /proc.  So mount /proc first
    #
    echo "Mounting local file systems..."
    mount -f /proc
    mount -a -v -t nonfs,noproc

    exit 0

fi


if [ "$1" = stop ]; then

    echo "Unmounting file systems."
    mount -f -o remount,ro /
    umount -a -v -t noproc
    mount -n -o remount,ro /

    if [ "$?" != "0" ]; then
        echo "Oops: umount failed :-(  --  trying to remount readonly..."
        mounts=/etc/fstab
        test -r /proc/mounts && mounts=/proc/mounts
        grep -v '^#' $mounts | \
        while read des fs rest; do
            if [ "$fs" != "" ]; then
                mount -v -n -o remount,ro $fs
            fi
        done

        echo "extra sync..."
        sync; sync
        echo "... hope now it's ok to reboot."
    fi

    # maybe we use Multiple devices
    if [ -f /etc/mdtab -a -x /sbin/mdadd ]; then
        echo -n "Disable Multiple Devices"
        /sbin/mdstop -a
        echo "."
    fi

    echo "Turning off swap."
    sync; sync
    swapoff -a -v

    sync; sync

    exit 0

fi


echo "Usage: $0 {start|stop}"
exit 1

