#!/bin/sh
# Begin $RC_BASE/init.d/udev

### BEGIN INIT INFO
# Provides:            udev
# Required-Start:
# Should-Start:        modules
# Required-Stop:
# Should-Stop:
# Default-Start:       S
# Default-Stop:
# Short-Description:   Populated /dev with device nodes.
# Description:         Mounts a tempfs on /dev and starts the udevd daemon.
#                      Device nodes are created as defined by udev.
# X-LFS-Provided-By:   LFS
### END INIT INFO

. /lib/lsb/init-functions

MESSAGE="Populating /dev with device nodes..."

case "${1}" in
	start)
		if ! grep -q '[[:space:]]sysfs' /proc/mounts; then
			echo_failure
			boot_mesg -n "FAILURE:\n\nUnable to create" ${FAILURE}
			boot_mesg -n " devices without a SysFS filesystem"
			boot_mesg -n "\n\nAfter you press Enter, this system"
			boot_mesg -n " will be halted and powered off."
			boot_mesg -n "\n\nPress Enter to continue..." ${INFO}
			boot_mesg "" ${NORMAL}
			$FAILURE_ACTION
			/etc/rc.d/init.d/halt stop
		fi

		# If not using DEVTMPFS 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').
		if ! mountpoint /dev > /dev/null; then
			mount -n -t tmpfs tmpfs /dev -o mode=755
		fi
		if [ ${?} -ne 0 ]; then
			echo_failure
			boot_mesg -n "FAILURE:\n\nCannot mount a tmpfs" ${FAILURE}
			boot_mesg -n " onto /dev, this system will be halted."
			boot_mesg -n "\n\nAfter you press Enter, this system"
			boot_mesg -n " will be halted and powered off."
			boot_mesg -n "\n\nPress Enter to continue..." ${INFO}
			boot_mesg "" ${NORMAL}
			$FAILURE_ACTION
			/etc/rc.d/init.d/halt stop
		fi

                # Create a symlink for POSIX shared memory
                ln -s /run/shm /dev/shm

		# Udev handles uevents itself, so we don't need to have
		# the kernel call out to any binary in response to them
		# This is a failsafe and should be done in kernel config
		echo > /proc/sys/kernel/hotplug

		# Copy the only static device node that Udev >= 155 doesn't
		# handle to /dev (handled by default with DEVTMPFS)
		if [ ! -f /dev/null ]; then
			cp -a /lib/udev/devices/null /dev
		fi

		# Start the udev daemon to continually watch for, and act on,
		# uevents
		/sbin/udevd --daemon

		# Now traverse /sys in order to "coldplug" devices that have
		# already been discovered
		/sbin/udevadm trigger --action=add --type=subsystems
		/sbin/udevadm trigger --action=add --type=devices

		# Now wait for udevd to process the uevents we triggered
		/sbin/udevadm settle
		evaluate_retval standard
		;;

	*)
		echo "Usage ${0} {start}"
		exit 1
		;;
esac

# End $RC_BASE/init.d/udev