preinit 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  4. . /etc/diag.sh
  5. failsafe_ip() {
  6. ifconfig $ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255 up
  7. }
  8. failsafe() {
  9. [ -n "$ifname" ] && grep "$ifname" /proc/net/dev >/dev/null && {
  10. failsafe_ip
  11. netmsg 192.168.1.255 "Entering Failsafe!"
  12. telnetd -l /bin/login <> /dev/null 2>&1
  13. }
  14. lock /tmp/.failsafe
  15. ash --login
  16. }
  17. mount proc /proc -t proc
  18. mount sysfs /sys -t sysfs
  19. size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo)
  20. mount tmpfs /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
  21. if grep devfs /proc/filesystems > /dev/null; then
  22. mount devfs /dev -t devfs
  23. M0=/dev/pty/m0
  24. M1=/dev/pty/m1
  25. HOTPLUG=/sbin/hotplug-call
  26. elif [ -x /sbin/hotplug2 ]; then
  27. mount -t tmpfs tmpfs /dev -o size=512K
  28. mknod /dev/console c 5 1
  29. /sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules
  30. /sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2-init.rules &
  31. M0=/dev/ptmx
  32. M1=/dev/ptmx
  33. HOTPLUG=
  34. elif [ -x /sbin/udevd ]; then
  35. mount -n -t tmpfs -o mode=0755 udev /dev
  36. /sbin/udevd --daemon
  37. /sbin/udevtrigger
  38. /sbin/udevsettle
  39. M0=/dev/pty/ptmx
  40. M1=/dev/pty/ptmx
  41. HOTPLUG=
  42. fi
  43. mkdir -p /dev/pts /dev/shm
  44. mount devpts /dev/pts -t devpts
  45. # the shell really doesn't like having stdin/out closed
  46. # that's why we use /dev/pty/m0 and m1 as replacement
  47. # for /dev/console if there's no serial console available
  48. dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
  49. M0=/dev/console
  50. M1=/dev/console
  51. }
  52. exec <$M0 >$M1 2>&0
  53. echo "- preinit -"
  54. echo "Press CTRL-C for failsafe"
  55. trap 'FAILSAFE=true' INT
  56. trap 'FAILSAFE=true' USR1
  57. [ -e /etc/preinit.arch ] && . /etc/preinit.arch
  58. set_state preinit
  59. echo "$HOTPLUG" > /proc/sys/kernel/hotplug
  60. eval ${FAILSAFE:+failsafe}
  61. lock -w /tmp/.failsafe
  62. if [ -z "$INITRAMFS" ]; then
  63. mount_root
  64. [ -f /sysupgrade.tgz ] && {
  65. echo "- config restore -"
  66. cd /
  67. mv sysupgrade.tgz /tmp
  68. tar xzf /tmp/sysupgrade.tgz
  69. rm -f /tmp/sysupgrade.tgz
  70. sync
  71. }
  72. echo "- init -"
  73. exec /sbin/init
  74. fi