init 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. . /etc/diag.sh
  4. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  5. mount none /proc -t proc
  6. mount none /sys -t sysfs
  7. size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)?mt-l:s}' /proc/meminfo)
  8. mount none /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
  9. if grep devfs /proc/filesystems > /dev/null; then
  10. mount none /dev -t devfs
  11. elif [ -x /sbin/hotplug2 ]; then
  12. mount -t tmpfs tmpfs /dev -o size=512K
  13. mknod /dev/console c 5 1
  14. mkdir /dev/pts
  15. mkdir /dev/shm
  16. /sbin/hotplug2 --no-persistent --coldplug --max_children 1
  17. elif [ -x /sbin/udevd ]; then
  18. mount -n -t tmpfs -o mode=0755 udev /dev
  19. mkdir /dev/pts
  20. mkdir /dev/shm
  21. if [ -e /proc/sys/kernel/hotplug ]; then
  22. echo "" > /proc/sys/kernel/hotplug
  23. fi
  24. /sbin/udevd --daemon
  25. /sbin/udevtrigger
  26. /sbin/udevsettle
  27. fi
  28. mount none /dev/pts -t devpts
  29. # the root mount point can come from the kernel command line
  30. # or it can be overridden in the preinit.arch file
  31. trap 'FAILSAFE=true' USR1
  32. [ -e /etc/preinit.arch ] && . /etc/preinit.arch
  33. set_state preinit
  34. echo "$HOTPLUG" > /proc/sys/kernel/hotplug
  35. eval ${FAILSAFE:+failsafe}
  36. lock -w /tmp/.failsafe
  37. # if we have no root parameter, just go to running from ramfs
  38. [ -z $rootfs ] && {
  39. export NOMOUNT="No Root"
  40. exec /sbin/init
  41. }
  42. #if we have a failsafe boot selected, dont bother
  43. #trying to find or wait for a root mount point
  44. [ -z "$FAILSAFE" ] || {
  45. exec /bin/busybox init
  46. }
  47. # Load the modules we have in initramfs, this should
  48. # make the media accessible, but, it may take some time
  49. . /etc/functions.sh
  50. load_modules /etc/modules /etc/modules.d/*
  51. #wait 10 seconds for the disc to show up
  52. #usb stick typically takes 4 to 6 seconds
  53. #till it's readable
  54. #it's quite possible the disc never shows up
  55. #if we netbooted this kernel
  56. COUNTER=0
  57. while [ $COUNTER -lt 10 ]; do
  58. sleep 1
  59. [ -e $rootfs ] && let COUNTER=10;
  60. let COUNTER=COUNTER+1
  61. done
  62. [ -e $rootfs ] || {
  63. export FAILSAFE="NoDisc"
  64. exec /bin/busybox init
  65. }
  66. # now we'll try mount it, again with a timeout
  67. # This will fail if the inserted stick is formatted
  68. # in a manner we dont understand
  69. COUNTER=0
  70. while [ $COUNTER -lt 10 ]; do
  71. sleep 1
  72. mount $rootfs /mnt
  73. [ $? -eq "0" ] && let COUNTER=100;
  74. let COUNTER=COUNTER+1
  75. done
  76. [ $? -ne "0" ] && {
  77. export FAILSAFE="MountFail"
  78. exec /bin/busybox init
  79. }
  80. #It mounted, lets look for a postinit file, again, give it time
  81. #I've seen this take 6 seconds to actually complete
  82. COUNTER=0
  83. while [ $COUNTER -lt 10 ]; do
  84. sleep 1
  85. [ -e /mnt/etc/banner ] && let COUNTER=10;
  86. let COUNTER=COUNTER+1
  87. done
  88. [ -e /mnt/etc/banner ] || {
  89. export FAILSAFE="No Openwrt FS"
  90. exec /bin/busybox init
  91. }
  92. unset rootfs
  93. mount -o move /proc /mnt/proc
  94. mount -o move /dev /mnt/dev
  95. mount -o move /dev/pts /mnt/dev/pts
  96. mount -o move /tmp /mnt/tmp
  97. mount -o move /sys /mnt/sys
  98. mount none /tmp -t tmpfs
  99. exec switch_root -c /dev/console /mnt /sbin/init