init 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/sh
  2. UDEVSTART=/sbin/udevstart
  3. # defaults
  4. tmpfs_size="2M"
  5. udev_root="/dev"
  6. [ -x $UDEVSTART ] || exit 0
  7. . /etc/udev/udev.conf
  8. ##############################################################################
  9. # we need to unmount /dev/pts/ and remount it later over the tmpfs
  10. unmount_devpts() {
  11. if mountpoint -q /dev/pts/; then
  12. umount -l /dev/pts/
  13. fi
  14. if mountpoint -q /dev/shm/; then
  15. umount -l /dev/shm/
  16. fi
  17. }
  18. # mount a tmpfs over /dev, if somebody did not already do it
  19. mount_tmpfs() {
  20. if grep -E -q "^[^[:space:]]+ /dev tmpfs" /proc/mounts; then
  21. return 0
  22. fi
  23. # /dev/.static/dev/ is used by MAKEDEV to access the real /dev/ directory.
  24. # /etc/udev/ is recycled as a temporary mount point because it's the only
  25. # directory which is guaranteed to be available.
  26. mount -n -o bind /dev /etc/udev
  27. if ! mount -n -o size=$tmpfs_size,mode=0755 -t tmpfs tmpfs /dev; then
  28. umount /etc/udev
  29. echo "udev requires tmpfs support, not started."
  30. exit 1
  31. fi
  32. # using ln to test if /dev works, because touch is in /usr/bin/
  33. if ln -s test /dev/test-file; then
  34. rm /dev/test-file
  35. else
  36. echo "udev requires tmpfs support, not started."
  37. umount /etc/udev
  38. umount /dev
  39. exit 1
  40. fi
  41. mkdir -p /dev/.static/dev
  42. chmod 700 /dev/.static/
  43. # The mount options in busybox are non-standard...
  44. if test -x /bin/mount.util-linux
  45. then
  46. /bin/mount.util-linux --move /etc/udev /dev/.static/dev
  47. elif test -x /bin/busybox
  48. then
  49. busybox mount -n -o move /etc/udev /dev/.static/dev
  50. else
  51. echo "udev requires an identifiable mount command, not started."
  52. umount /etc/udev
  53. umount /dev
  54. exit 1
  55. fi
  56. }
  57. # I hate this hack. -- Md
  58. make_extra_nodes() {
  59. [ -e /etc/udev/links.conf ] || return 0
  60. grep '^[^#]' /etc/udev/links.conf | \
  61. while read type name arg1; do
  62. [ "$type" -a "$name" -a ! -e "/dev/$name" -a ! -L "/dev/$name" ] ||continue
  63. case "$type" in
  64. L) ln -s $arg1 /dev/$name ;;
  65. D) mkdir -p /dev/$name ;;
  66. M) mknod -m 600 /dev/$name $arg1 ;;
  67. *) echo "links.conf: unparseable line ($type $name $arg1)" ;;
  68. esac
  69. done
  70. }
  71. # this function is duplicated in preinst, postinst and d-i
  72. supported_kernel() {
  73. case "$(uname -r)" in
  74. 2.[012345].*|2.6.[0-9]|2.6.[0-9][!0-9]*) return 1 ;;
  75. 2.6.1[01]|2.6.1[01][!0-9]*) return 1 ;;
  76. esac
  77. return 0
  78. }
  79. # shell version of /usr/bin/tty
  80. my_tty() {
  81. [ -x /bin/readlink ] || return 0
  82. [ -e /proc/self/fd/0 ] || return 0
  83. readlink --silent /proc/self/fd/0 || true
  84. }
  85. warn_if_interactive() {
  86. if [ "$RUNLEVEL" = "S" -a "$PREVLEVEL" = "N" ]; then
  87. return 0
  88. fi
  89. TTY=$(my_tty)
  90. if [ -z "$TTY" -o "$TTY" = "/dev/console" ]; then
  91. return 0
  92. fi
  93. printf "\n\n\nIt has been detected that the command\n\n\t$0 $*\n\n"
  94. printf "has been run from an interactive shell.\n"
  95. printf "It will probably not do what you expect, so this script will wait\n"
  96. printf "60 seconds before continuing. Press ^C to stop it.\n"
  97. printf "RUNNING THIS COMMAND IS HIGHLY DISCOURAGED!\n\n\n\n"
  98. sleep 60
  99. }
  100. ##############################################################################
  101. if ! supported_kernel; then
  102. echo "udev requires a kernel >= 2.6.12, not started."
  103. exit 1
  104. fi
  105. if [ ! -e /proc/filesystems ]; then
  106. echo "udev requires a mounted procfs, not started."
  107. exit 1
  108. fi
  109. if ! grep -q '[[:space:]]tmpfs$' /proc/filesystems; then
  110. echo "udev requires tmpfs support, not started."
  111. exit 1
  112. fi
  113. if [ ! -d /sys/class/ ]; then
  114. echo "udev requires a mounted sysfs, not started."
  115. exit 1
  116. fi
  117. if [ ! -e /proc/sys/kernel/hotplug ] && [ ! -e /sys/kernel/uevent_helper ]; then
  118. echo "udev requires hotplug support, not started."
  119. exit 1
  120. fi
  121. ##############################################################################
  122. # When modifying this script, do not forget that between the time that
  123. # the new /dev has been mounted and udevstart has been run there will be
  124. # no /dev/null. This also means that you cannot use the "&" shell command.
  125. case "$1" in
  126. start)
  127. if [ -e "$udev_root/.udevdb" ]; then
  128. if mountpoint -q /dev/; then
  129. TMPFS_MOUNTED=1
  130. else
  131. echo ".udevdb already exists on the old $udev_root!"
  132. fi
  133. fi
  134. warn_if_interactive
  135. #echo /sbin/udevsend > /proc/sys/kernel/hotplug
  136. if [ -e /sys/kernel/uevent_helper ] ; then
  137. echo "" > /sys/kernel/uevent_helper
  138. else
  139. echo "" > /proc/sys/kernel/hotplug
  140. fi
  141. udevsend
  142. if [ "$UDEV_DISABLED" = "yes" ]; then
  143. echo "udev disabled on the kernel command line, not started."
  144. exit 0
  145. fi
  146. if [ ! "$TMPFS_MOUNTED" ]; then
  147. unmount_devpts
  148. mount_tmpfs
  149. [ -d /proc/1 ] || mount -n /proc
  150. # if this directory is not present /dev will not be updated by udev
  151. mkdir /dev/.udevdb/
  152. echo "Creating initial device nodes..."
  153. udevstart
  154. fi
  155. make_extra_nodes
  156. ;;
  157. stop)
  158. warn_if_interactive
  159. start-stop-daemon --stop --exec /sbin/udevd --quiet
  160. unmount_devpts
  161. if [ -d /dev/.static/dev/ ]; then
  162. umount -l /dev/.static/dev/ || true
  163. fi
  164. echo "Unmounting /dev..."
  165. # unmounting with -l should never fail
  166. if ! umount -l /dev; then
  167. exit 1
  168. fi
  169. ;;
  170. restart|force-reload)
  171. start-stop-daemon --stop --exec /sbin/udevd --quiet
  172. log_begin_msg "Recreating device nodes..."
  173. udevstart
  174. make_extra_nodes
  175. log_end_msg 0
  176. ;;
  177. *)
  178. echo "Usage: /etc/init.d/udev {start|stop|restart|force-reload}"
  179. exit 1
  180. ;;
  181. esac
  182. exit 0