preinit 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: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. if [ -n "$INITRAMFS" ]; then
  30. /sbin/hotplug2 --coldplug --no-persistent --max_children 1
  31. /sbin/hotplug2 --persistent --max_children 1 &
  32. else
  33. /sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules
  34. /sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2-init.rules &
  35. fi
  36. M0=/dev/ptmx
  37. M1=/dev/ptmx
  38. HOTPLUG=
  39. elif [ -x /sbin/udevd ]; then
  40. mount -n -t tmpfs -o mode=0755 udev /dev
  41. /sbin/udevd --daemon
  42. /sbin/udevtrigger
  43. /sbin/udevsettle
  44. M0=/dev/pty/ptmx
  45. M1=/dev/pty/ptmx
  46. HOTPLUG=
  47. fi
  48. mkdir -p /dev/pts /dev/shm
  49. mount devpts /dev/pts -t devpts
  50. # the shell really doesn't like having stdin/out closed
  51. # that's why we use /dev/pty/m0 and m1 as replacement
  52. # for /dev/console if there's no serial console available
  53. dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
  54. M0=/dev/console
  55. M1=/dev/console
  56. }
  57. exec <$M0 >$M1 2>&0
  58. echo "- preinit -"
  59. trap 'FAILSAFE=true' USR1
  60. [ -e /etc/preinit.arch ] && . /etc/preinit.arch
  61. set_state preinit
  62. echo "$HOTPLUG" > /proc/sys/kernel/hotplug
  63. eval ${FAILSAFE:+failsafe}
  64. lock -w /tmp/.failsafe
  65. if [ -z "$INITRAMFS" ]; then
  66. mount_root
  67. [ -f /sysupgrade.tgz ] && {
  68. echo "- config restore -"
  69. cd /
  70. mv sysupgrade.tgz /tmp
  71. tar xzf /tmp/sysupgrade.tgz
  72. rm -f /tmp/sysupgrade.tgz
  73. sync
  74. }
  75. echo "- init -"
  76. killall hotplug2
  77. exec /sbin/init
  78. fi