preinit 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 none /proc -t proc
  18. mount none /sys -t sysfs
  19. size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)?mt-l:s}' /proc/meminfo)
  20. mount none /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
  21. if grep devfs /proc/filesystems > /dev/null; then
  22. mount none /dev -t devfs
  23. M0=/dev/pty/m0
  24. M1=/dev/pty/m1
  25. HOTPLUG=/sbin/hotplug-call
  26. else
  27. mount -t tmpfs tmpfs /dev -o size=512K
  28. mknod /dev/console c 5 1
  29. mkdir /dev/shm
  30. /sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules
  31. /sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2-init.rules &
  32. M0=/dev/ptmx
  33. M1=/dev/ptmx
  34. HOTPLUG=
  35. fi
  36. mkdir -p /dev/pts /dev/shm
  37. mount none /dev/pts -t devpts
  38. # the shell really doesn't like having stdin/out closed
  39. # that's why we use /dev/pty/m0 and m1 as replacement
  40. # for /dev/console if there's no serial console available
  41. dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
  42. M0=/dev/console
  43. M1=/dev/console
  44. }
  45. exec <$M0 >$M1 2>&0
  46. echo "- preinit -"
  47. trap 'FAILSAFE=true' USR1
  48. [ -e /etc/preinit.arch ] && . /etc/preinit.arch
  49. set_state preinit
  50. echo "$HOTPLUG" > /proc/sys/kernel/hotplug
  51. eval ${FAILSAFE:+failsafe}
  52. lock -w /tmp/.failsafe
  53. mount_root
  54. [ -f /sysupgrade.tgz ] && {
  55. echo "- config restore -"
  56. cd /
  57. mv sysupgrade.tgz /tmp
  58. tar xzf /tmp/sysupgrade.tgz
  59. rm -f /tmp/sysupgrade.tgz
  60. sync
  61. }
  62. echo "- init -"
  63. killall hotplug2
  64. exec /sbin/init