#
# checkroot.sh	Check to root file system.
#
# Version:	@(#)checkroot.sh  2.76  12-Jan-1999  miquels@cistron.nl
# Modified for the Hurd by Marcus Brinkmann.

. /etc/default/rcS

#
# Activate the swap device(s) in /etc/fstab. This needs to be done
# before fsck, since fsck can be quite memory-hungry.
#
if [ -x /sbin/swapon ]
then
  [ "$VERBOSE" != no ] && echo "Activating swap..."
  swapon -a 2>/dev/null
fi

#
# Check the root file system.
#
if [ -f /fastboot ]
then
  echo "Fast boot, no file system check"
else
  #
  # Ensure that root is quiescent and read-only before fsck'ing.
  #
  fsysopts / --readonly
  if [ $? = 0 ]
  then
    if [ -f /forcefsck ]
    then
	force="-f"
    else
	force=""
    fi
    if [ "$FSCKFIX" = yes ]
    then
	fix="-y"
    else
	fix="--preen"
    fi
    echo "Checking root file system..."
    fsck $force $fix /

    case $? in
    # Successful completion
    0)
            ;;
    # Filesystem modified (but ok now)
    1 | 2)
            ;;
    # Fsck couldn't fix it.
    4 | 8)
            echo "Automatic boot failed... help!"
            exit 1
            ;;
    # Signal that really interrupted something
    20 | 130 | 131)
            echo "Boot interrupted"
            exit 1
            ;;
    # Special `let fsck finish' interruption (SIGQUIT)
    12)
            echo "Boot interrupted (filesystem checks complete)"
            exit 1
            ;;
    # Oh dear.
    *)
            echo "Unknown error during fsck"
            exit 1
            ;;
    esac
  else
    echo "*** ERROR!  Cannot fsck root fs because it is not mounted read-only!"
    echo
  fi
fi

#
#	If the root filesystem was not marked as read-only in /etc/fstab,
#	remount the rootfs rw but do not try to change mtab because it
#	is on a ro fs until the remount succeeded. Then clean up old mtabs
#	and finally write the new mtab.
#
(
  mode=writable
  while read fs mnt type opts rest
  do
	[ "$mnt" != / ] && continue
	case "$fs" in
		""|\#*)
			continue;
			;;
	esac
	case "$opts" in
		ro|ro,*|*,ro|*,ro,*)
			mode=readonly
			;;
	esac
  done
  fsysopts / --$mode
  if [ "$mode" = writable ]
  then
	rm -f /etc/mtab~ /etc/nologin
	: > /etc/mtab
  fi
) < /etc/fstab
