#!/sbin/runscript
# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

depend() {
	need checkfs
}

start() {
	# Mount local filesystems in /etc/fstab.
	ebegin "Mounting local filesystems"
	mount -at noproc,noshm${NET_FS_LIST:+,no}${NET_FS_LIST// /,no} \
		-O no_netdev >/dev/null
	eend $? "Some local filesystem failed to mount"

	# Make sure we insert usbcore if its a module
	if [[ -f /proc/modules && ! -d /proc/bus/usb ]] ; then
		# >/dev/null to hide errors from non-USB users
		modprobe usbcore &> /dev/null
	fi
	
	# Check what USB fs the kernel support.  Currently
	# 2.5+ kernels, and later 2.4 kernels have 'usbfs',
	# while older kernels have 'usbdevfs'.
	if [[ -d /proc/bus/usb && ! -e /proc/bus/usb/devices ]] ; then
		local usbfs=$(grep -Fow usbfs /proc/filesystems ||
			grep -Fow usbdevfs /proc/filesystems)
		if [[ -n ${usbfs} ]] ; then
			ebegin "Mounting USB device filesystem (${usbfs})"
			usbgid=$(echo $(getent group usb) | awk -F: '{print $3}')
			mount -t ${usbfs} usbfs /proc/bus/usb \
				-o ${usbgid:+devmode=0664,devgid=${usbgid},}noexec,nosuid
			eend $?
		fi
	fi

	# Setup Kernel Support for miscellaneous Binary Formats
	if [[ -d /proc/sys/fs/binfmt_misc ]] ; then
		local binfmt=$(grep -Fow binfmt_misc /proc/filesystems)
		if [[ -n ${binfmt} ]] ; then
			ebegin $"Mounting misc binary format filesystem"
			mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc \
				-o nodev,noexec,nosuid
			eend $?
		fi
	fi

	# Setup Kernel Support for the security filesystem
	if [[ -d /sys/kernel/security ]] ; then
		local secfs=$(grep -Fow securityfs /proc/filesystems)
		if [[ -n ${secfs} ]] ; then
			ebegin $"Mounting security filesystem"
			mount -t securityfs securityfs /sys/kernel/security \
				-o nodev,noexec,nosuid
			eend $?
		fi
	fi

	# We do our swapping here instead of rc so we can get urandom started
	# before us for people that like an encrypted swap.
	ebegin "Activating (possible) swap"
	/sbin/swapon -a
	eend $?

	# Start dm-crypt mappings, if any
	start_addon dm-crypt
}

# vim:ts=4
