#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          udev
# Required-Start:    mountkernfs 
# Required-Stop:     
# Default-Start:     S
# Default-Stop:
# X-Interactive:     true
# Short-Description: Start udevd, populate /dev and load drivers.
### END INIT INFO

# we need to unmount /dev/pts/ and remount it later over the tmpfs
unmount_devpts() {
  if mountpoint -q /dev/pts/; then
    umount -n -l /dev/pts/
  fi

  if mountpoint -q /dev/shm/; then
    umount -n -l /dev/shm/
  fi
}

# mount a tmpfs over /dev, if somebody did not already do it
mount_tmpfs() {
  if grep -E -q "^[^[:space:]]+ /dev (dev)?tmpfs" /proc/mounts; then
    mount -n -o remount,${dev_mount_options} -t tmpfs tmpfs /dev
    return
  fi

  if ! mount -n -o $dev_mount_options -t tmpfs tmpfs /dev; then
    log_failure_msg "udev requires tmpfs support, not started."
    log_end_msg 1
  fi

  return 0
}

create_dev_makedev() {
  if [ -e /sbin/MAKEDEV ]; then
    ln -sf /sbin/MAKEDEV /dev/MAKEDEV
  else
    ln -sf /bin/true /dev/MAKEDEV
  fi
}

supported_kernel() {
  case "$(uname -r)" in
    2.[012345].*|2.6.[0-9]|2.6.[0-9][!0-9]*) return 1 ;;
    2.6.1[0-9]|2.6.1[0-9][!0-9]*) return 1 ;;
    2.6.2[0-5]|2.6.2[0-5][!0-9]*) return 1 ;;
  esac
  return 0
}

# shell version of /usr/bin/tty
my_tty() {
  [ -x /bin/readlink ] || return 0
  [ -e /proc/self/fd/0 ] || return 0
  readlink --silent /proc/self/fd/0 || true
}

warn_if_interactive() {
  if [ "$RUNLEVEL" = "S" -a "$PREVLEVEL" = "N" ]; then
    return
  fi

  TTY=$(my_tty)
  if [ -z "$TTY" -o "$TTY" = "/dev/console" -o "$TTY" = "/dev/null" ]; then
    return
  fi

  printf "\n\n\nIt has been detected that the command\n\n\t$0 $*\n\n"
  printf "has been run from an interactive shell.\n"
  printf "It will probably not do what you expect, so this script will wait\n"
  printf "60 seconds before continuing. Press ^C to stop it.\n"
  printf "RUNNING THIS COMMAND IS HIGHLY DISCOURAGED!\n\n\n\n"
  sleep 60
}

create_dev_root_rule() {
  local udevroot="$1"
  [ -e $udevroot/rules.d/61-dev-root-link.rules ] && return 0

  eval $(udevadm info --export --export-prefix=ROOT_ --device-id-of-file=/ \
    || true)
  [ "$ROOT_MAJOR" -a "$ROOT_MINOR" ] || return 0

  echo 'ACTION=="add|change", SUBSYSTEM=="block", ENV{MAJOR}=="'$ROOT_MAJOR'", ENV{MINOR}=="'$ROOT_MINOR'", SYMLINK+="root"' \
    > $udevroot/root-link-rule
  mv $udevroot/root-link-rule $udevroot/rules.d/61-dev-root-link.rules
}

##############################################################################

[ -x /sbin/udevd ] || exit 0

PATH="/sbin:/bin"

# defaults
tmpfs_size="10M"
udev_root="/dev"

if [ -e /etc/udev/udev.conf ]; then
  . /etc/udev/udev.conf
fi

. /lib/lsb/init-functions

if ! supported_kernel; then
  log_failure_msg "udev requires a kernel >= 2.6.26, not started."
  log_end_msg 1
fi

if [ ! -e /proc/filesystems ]; then
  log_failure_msg "udev requires a mounted procfs, not started."
  log_end_msg 1
fi

if ! grep -q '[[:space:]]tmpfs$' /proc/filesystems; then
  log_failure_msg "udev requires tmpfs support, not started."
  log_end_msg 1
fi

if [ ! -d /sys/class/ ]; then
  log_failure_msg "udev requires a mounted sysfs, not started."
  log_end_msg 1
fi

if [ ! -e /sys/kernel/uevent_helper ]; then
  log_failure_msg "udev requires hotplug support, not started."
  log_end_msg 1
fi

##############################################################################

# this is experimental and may not work well
if [ "$UDEV_DISABLED" = "yes" ]; then
  udev_root=/etc/udev/.dev
  export UDEV_ROOT=$udev_root
fi

udev_root=${udev_root%/}

dev_mount_options='mode=0755'
if [ "$tmpfs_size" ]; then
  dev_mount_options="size=${tmpfs_size},${dev_mount_options}"
fi

if [ "$udev_root" != "/dev" ]; then
  log_warning_msg "udev_root != /dev/"

case "$1" in
    start)
    if [ -e "$udev_root/.udev/" ]; then
	if mountpoint -q $udev_root/; then
	    log_failure_msg "udev is already active on $udev_root."
	    log_end_msg 1
	else
	    log_warning_msg ".udev/ already exists on the static $udev_root!"
	fi
    fi

    echo > /sys/kernel/uevent_helper

    mount -n -o $dev_mount_options -t tmpfs tmpfs $udev_root

    log_daemon_msg "Starting the hotplug events dispatcher" "udevd"
    if udevd --daemon; then
	log_end_msg $?
    else
	log_end_msg $?
    fi

    mkdir -p $udev_root/.udev/rules.d/
    create_dev_root_rule $udev_root/.udev/

    log_action_begin_msg "Synthesizing initial hotplug events"
    if udevadm trigger --action=add; then
	log_action_end_msg $?
    else
	log_action_end_msg $?
    fi

    ;;
    stop)
    log_daemon_msg "Stopping the hotplug events dispatcher" "udevd"
    if start-stop-daemon --stop --name udevd --quiet --oknodo --retry 5; then
	log_end_msg $?
    else
	log_end_msg $?
    fi

    log_action_begin_msg "Unmounting $udev_root"
    # unmounting with -l should never fail
    if umount -n -l $udev_root; then
	log_action_end_msg $?
    else
	log_action_end_msg $?
    fi
    ;;

    restart)
    $0 stop
    $0 start
    ;;

    reload|force-reload)
    udevadm control --reload-rules
    ;;

    *)
    echo "Usage: /etc/init.d/udev {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

  exit 0
fi # udev_root != /dev

##############################################################################

# When modifying this script, do not forget that between the time that the
# new /dev has been mounted and udevadm trigger has been run there will be
# no /dev/null. This also means that you cannot use the "&" shell command.

case "$1" in
    start)
    if [ -e "$udev_root/.udev/" ]; then
	if mountpoint -q $udev_root/; then
	    TMPFS_MOUNTED=1
	else
	    log_warning_msg ".udev/ already exists on the static $udev_root!"
	fi
    else
	warn_if_interactive
    fi

    echo > /sys/kernel/uevent_helper

    if [ -z "$TMPFS_MOUNTED" ]; then
	unmount_devpts
	mount_tmpfs
	[ -d /proc/1 ] || mount -n /proc
    else
	# and clean up the database of the initramfs udev
	rm -rf /dev/.udev/
    fi

    # set the SELinux context for devices created in the initramfs
    [ -x /sbin/restorecon ] && /sbin/restorecon -R /dev

    # /dev/null must be created before udevd is started
    /lib/udev/create_static_nodes || true

    log_daemon_msg "Starting the hotplug events dispatcher" "udevd"
    if udevd --daemon; then
	log_end_msg $?
    else
	log_end_msg $?
    fi

    mkdir -p /dev/.udev/rules.d/
    create_dev_root_rule /dev/.udev/

    log_action_begin_msg "Synthesizing the initial hotplug events"
    if udevadm trigger --action=add; then
	log_action_end_msg $?
    else
	log_action_end_msg $?
    fi

    create_dev_makedev

    # wait for the udevd childs to finish
    log_action_begin_msg "Waiting for /dev to be fully populated"
    if udevadm settle; then
	log_action_end_msg 0
    else
	log_action_end_msg 0 'timeout'
    fi
    ;;

    stop)
    log_daemon_msg "Stopping the hotplug events dispatcher" "udevd"
    if start-stop-daemon --stop --name udevd --quiet --oknodo --retry 5; then
	log_end_msg $?
    else
	log_end_msg $?
    fi
    ;;

    restart)
    log_daemon_msg "Stopping the hotplug events dispatcher" "udevd"
    if start-stop-daemon --stop --name udevd --quiet --oknodo --retry 5; then
	log_end_msg $?
    else
	log_end_msg $? || true
    fi

    log_daemon_msg "Starting the hotplug events dispatcher" "udevd"
    if udevd --daemon; then
	log_end_msg $?
    else
	log_end_msg $?
    fi
    ;;

    reload|force-reload)
    udevadm control --reload-rules
    ;;

    *)
    echo "Usage: /etc/init.d/udev {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0

