#! /bin/sh
### BEGIN INIT INFO
# Provides:          umountfs
# Required-Start:    umountnfs urandom
# Required-Stop:
# Default-Start:     6
# Default-Stop:
# Short-Description: Turn off swap and unmount all local file systems.
# Description:
### END INIT INFO
#
# Version:      @(#)umountfs  2.85-16  03-Jun-2004  miquels@cistron.nl
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
umask 022
. /lib/lsb/init-functions

do_stop () {
    # Umount all filesystems except root and the virtual ones
    log_action_begin_msg "Unmounting local filesystems"

    # List all mounts, deepest mount point first
    LANG=C sort -r -k 2 /etc/mtab | 
    (
    DIRS=""
    while read DEV DIR TYPE REST ; do
	case "$DIR" in
	/|/proc|/dev|/dev/pts|/proc/*|/sys)
	    continue # Ignoring virtual file systems needed later
	    ;;
	esac

        case $TYPE in 
        proc|procfs|linprocfs|devfs|sysfs|usbfs|usbdevfs|devpts)
	    continue # Ignoring non-tmpfs virtual file systems
            ;;
        esac
	DIRS="$DIRS $DIR"
    done
    umount -r -d $DIRS
    )
    log_action_end_msg $?

    # Make sure tmpfs file systems are umounted before turning off
    # swap, to avoid running out of memory if the tmpfs filesystems
    # use a lot of space.
    log_action_begin_msg "Deactivating swap"
    swapoff -a
    log_action_end_msg $?
}

case "$1" in
    start)
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        do_stop
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

: exit 0
