init 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. INITRAMFS=1
  4. . /etc/preinit
  5. set_state init
  6. echo "- init -"
  7. [ -n "$SWITCH_ROOT_TMPFS" ] && {
  8. mount -o noatime none /mnt -t tmpfs
  9. ( cd /; find -xdev -type d ) | ( cd /mnt; xargs mkdir -p )
  10. find / \! -type d -xdev | tar -cT /proc/self/fd/0 | tar -xC /mnt
  11. mkdir /mnt/proc /mnt/dev /mnt/tmp /mnt/sys
  12. mount -o noatime,move /proc /mnt/proc
  13. mount -o noatime,move /dev /mnt/dev
  14. mount -o noatime,move /tmp /mnt/tmp
  15. mount -o noatime,move /sys /mnt/sys
  16. rm -rf /proc /dev /tmp /sys
  17. exec switch_root -c /dev/console /mnt /sbin/init
  18. }
  19. # if we have no root parameter, just go to running from ramfs
  20. [ -z $rootfs ] && {
  21. export NOMOUNT="No Root"
  22. exec /sbin/init
  23. }
  24. #if we have a failsafe boot selected, dont bother
  25. #trying to find or wait for a root mount point
  26. [ -z "$FAILSAFE" ] || {
  27. exec /bin/busybox init
  28. }
  29. # Load the modules we have in initramfs, this should
  30. # make the media accessible, but, it may take some time
  31. . /lib/functions.sh
  32. load_modules /etc/modules /etc/modules.d/*
  33. #wait 10 seconds for the disc to show up
  34. #usb stick typically takes 4 to 6 seconds
  35. #till it's readable
  36. #it's quite possible the disc never shows up
  37. #if we netbooted this kernel
  38. COUNTER=0
  39. while [ $COUNTER -lt 10 ]; do
  40. sleep 1
  41. [ -e $rootfs ] && let COUNTER=10;
  42. let COUNTER=COUNTER+1
  43. done
  44. [ -e $rootfs ] || {
  45. export FAILSAFE="NoDisc"
  46. exec /bin/busybox init
  47. }
  48. # now we'll try mount it, again with a timeout
  49. # This will fail if the inserted stick is formatted
  50. # in a manner we dont understand
  51. COUNTER=0
  52. while [ $COUNTER -lt 10 ]; do
  53. sleep 1
  54. mount -o noatime $rootfs /mnt
  55. [ $? -eq "0" ] && let COUNTER=100;
  56. let COUNTER=COUNTER+1
  57. done
  58. [ $? -ne "0" ] && {
  59. export FAILSAFE="MountFail"
  60. exec /bin/busybox init
  61. }
  62. #It mounted, lets look for a postinit file, again, give it time
  63. #I've seen this take 6 seconds to actually complete
  64. COUNTER=0
  65. while [ $COUNTER -lt 10 ]; do
  66. sleep 1
  67. [ -e /mnt/etc/banner ] && let COUNTER=10;
  68. let COUNTER=COUNTER+1
  69. done
  70. [ -e /mnt/etc/banner ] || {
  71. export FAILSAFE="No Openwrt FS"
  72. exec /bin/busybox init
  73. }
  74. unset rootfs
  75. mount -o noatime,move /proc /mnt/proc
  76. mount -o noatime,move /dev /mnt/dev
  77. mount -o noatime,move /dev/pts /mnt/dev/pts
  78. mount -o noatime,move /tmp /mnt/tmp
  79. mount -o noatime,move /sys /mnt/sys
  80. mount -o noatime none /tmp -t tmpfs
  81. killall -q hotplug2
  82. exec switch_root -c /dev/console /mnt /sbin/init
  83. set_state done