#!/bin/sh
# Begin $rc_base/init.d/udev - Udev cold-plugging script

# Written by Zack Winkles  - winkie@linuxfromscratch.org

. /etc/sysconfig/rc
. $rc_functions

# Assure that sysfs is mounted and that udev is present.
[ -d /sys/block -a -x /sbin/udev ] || exit 0

# Create some things that sysfs does not, and should not export for us.  Feel
# free to add devices to this list.
make_extra_nodes() {
	ln -s /proc/self/fd /dev/fd
	ln -s /proc/self/fd/0 /dev/stdin
	ln -s /proc/self/fd/1 /dev/stdout
	ln -s /proc/self/fd/2 /dev/stderr
	ln -s /proc/kcore /dev/core
	mkdir /dev/pts
	mkdir /dev/shm
}

case "$1" in
	start)
		# Don't attempt to populate the /dev directory when something
		# else has already set it up.
		[ -f /dev/.udev.tdb ] && exit 0

		echo "Populating /dev with device nodes..."

		# Mount a temporary file system over /dev, so that any devices
		# made or removed during this boot don't affect the next one.
		# The reason we don't write to mtab is because we don't ever
		# want /dev to be unavailable (such as by `umount -a').
		mount -n -t ramfs ramfs /dev
		if [ $? != 0 ]
		then
			print_status failure
			echo -n -e $FAILURE
			echo
			echo "Cannot mount a ramfs onto /dev, this system will be halted."
			echo
			echo -n "When you press Enter, this system will be halted."
			echo -n -e $NORMAL
			echo
			echo "Press Enter to continue..."
			read ENTER

			halt -f
		fi

		# Assign udev to get hotplug events.  This will be overwritten
		# in the hotplug bootscript.
		echo /sbin/udevsend > /proc/sys/kernel/hotplug

		# Populate /dev with all the devices that are already available,
		# and save it's status so we can report failures.
		udevstart || failed=1

		# Now, create some required files/directories/devices that sysfs
		# doesn't export for us.
		make_extra_nodes

		# When reporting the status, base it on the success or failure
		# of the `udevstart' command, since that's the most important.
		(exit $failed)
		evaluate_retval
		;;
	*)
		echo "Usage $0 {start}"
		exit 1
		;;
esac

# End $rc_base/init.d/udev
