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