#! /bin/sh
### BEGIN INIT INFO
# Provides:          umountnfs
# Required-Start:
# Required-Stop:     umountfs
# Should-Stop:       $network $portmap nfs-common
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Unmount all network filesystems except the root fs.
# Description:       Also unmounts all virtual filesystems (proc,
#                    devpts, usbfs, sysfs) that are not mounted at the
#                    top level.
### END INIT INFO

setvar PATH = "/sbin:/usr/sbin:/bin:/usr/bin"
setvar KERNEL = "$(uname -s)"
setvar RELEASE = "$(uname -r)"
source /lib/init/vars.sh

source /lib/lsb/init-functions

case{
  Linux:[01].*|Linux:2.[01].* {
	setvar FLAGS = """"
	}
  Linux:2.[23].*|Linux:2.4.?|Linux:2.4.?-*|Linux:2.4.10|Linux:2.4.10-* {
	setvar FLAGS = ""-f""
	}
  * {
	setvar FLAGS = ""-f -l""
	}
}

proc do_stop {
	# Write a reboot record to /var/log/wtmp before unmounting
	halt -w

	# Remove bootclean flag files (precaution against symlink attacks)
	rm -f /tmp/.clean /run/.clean /run/lock/.clean

	#
	# Make list of points to unmount in reverse order of their creation
	#

	setvar DIRS = """"
	while read -r DEV MTPT FSTYPE OPTS REST
	{
		case (MTPT) {
		  /|/proc|/dev|/dev/pts|/dev/shm|/proc/*|/sys|/run|/run/* {
			continue
			}
		}
		case (FSTYPE) {
		  nfs|nfs4|smbfs|ncp|ncpfs|cifs|coda|ocfs2|gfs|ceph {
			setvar DIRS = ""$MTPT $DIRS""
			}
		  proc|procfs|linprocfs|devpts|usbfs|usbdevfs|sysfs {
			setvar DIRS = ""$MTPT $DIRS""
			}
		}
		case (OPTS) {
		  _netdev|*,_netdev|_netdev,*|*,_netdev,* {
			setvar DIRS = ""$MTPT $DIRS""
			}
		}
	} < /etc/mtab

	if test $DIRS
	{
		test $VERBOSE = no || log_action_begin_msg "Unmounting remote and non-toplevel virtual filesystems"
		fstab-decode umount $FLAGS $DIRS
		setvar ES = ""$?
		test $VERBOSE = no || log_action_end_msg $ES
	}

	# emit unmounted-remote-filesystems hook point so any upstart jobs
	# that support remote filesystems can be stopped
	if test -x /sbin/initctl {
		initctl --quiet emit unmounted-remote-filesystems 2>/dev/null || true
	}
}

case (1) {
  start|status {
	# No-op
	}
  restart|reload|force-reload {
	echo "Error: argument '$1' not supported" >&2
	exit 3
	}
  stop|"" {
	do_stop
	}
  * {
	echo "Usage: umountnfs.sh [start|stop]" >&2
	exit 3
	}
}

: