#! /bin/sh
# Description:       Mount all networked filesystems defined in /etc/fstab
#                    Start the portmapper before mounting (this is needed for
#                    Linux 2.1.x and up) if necessary.

PATH=/sbin:/bin
. /lib/init/vars.sh

. /lib/lsb/init-functions
. /lib/init/mount-functions.sh

# Some simple locking to ensure we never have two parallel instances
# running, but that each one called will eventually execute serially.

LOCKNAME=/var/run/mountall-net-fs.lock
trap "rm -rf $LOCKNAME" 0
while [ -d $LOCKNAME ] || [ -d /dev/shm/var.run ] || ! mkdir $LOCKNAME 2>/dev/null; do
  sleep 1
done

do_start() {
	[ -f /etc/fstab ] || return
	#
	# Read through fstab line by line. If it is a networked file system,
	# set the flag for mounting networked file systems; 
	# if any NFS are found and are not mounted with the
	# nolock option, we start the portmapper.
	# 
	# If any sec={krb5,krb5i,krb5p} option is given, or any of the file
	# systems are nfs4, we'll need to start rpc.gssd and/or rpc.idmapd 
	# too; we'll leave that to nfs-common.
	#

	exec 9<&0 </etc/fstab

	start_nfs=no
	while read DEV MTPT FSTYPE OPTS REST
	do
		case "$DEV" in
		  ""|\#*)
			continue
			;;
		esac
		case "$OPTS" in
		  noauto|*,noauto|noauto,*|*,noauto,*)
			continue
			;;
		esac
		case "$FSTYPE" in
		  nfs)
		  	# NFS filsystems normally require statd and portmap. 
			# However, if nolock is set, portmap and statd are not 
			# required for this file system.
			case "$OPTS" in
			  nolock|*,nolock|nolock,*|*,nolock,*)
			  	# no action
				;;
			  *)
				start_nfs=yes
				;;
			esac

			# However, Kerberos requires gssd, so start nfs-common 
			# anyway.
			case "$OPTS" in
			  sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
			  	start_nfs=yes
				;;
			esac
			;;
		  nfs4)
			# NFSv4 requires idmapd, so start nfs-common no matter 
			# what the options are.
			start_nfs=yes
			;;
		  smbfs|cifs|coda|ncp|ncpfs|ocfs2|gfs)
			;;
		  *)
			FSTYPE=
			;;
		esac
		if [ "$FSTYPE" ]
		then
			case "$NETFS" in
			  $FSTYPE|*,$FSTYPE|$FSTYPE,*|*,$FSTYPE,*)
				;;
			  *)
				NETFS="$NETFS${NETFS:+,}$FSTYPE"
				;;
			esac
		fi
	done

	exec 0<&9 9<&-

	#
	# Initialize nfs-common (which starts rpc.statd, rpc.gssd
	# and/or rpc.idmapd, and loads the right kernel modules if
 	# applicable) if we use Kerberos and/or NFSv4 mounts.
	#
	if [ "$start_nfs" = yes ] && [ -x /etc/init.d/portmap ] && [ -x /etc/init.d/nfs-common ]
	then
		/etc/init.d/portmap start
		/etc/init.d/nfs-common start
	fi

	if [ "$NETFS" ]
	then
		pre_mountall
		mount -a -t$NETFS 
		post_mountall
	fi
}

do_start

:
