preinit: fix failsafe mode through ctrl+c on devices that do not provide a preinit...
[openwrt/svn-archive/archive.git] / package / base-files / files / etc / preinit
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
4 . /etc/diag.sh
5
6 failsafe_ip() {
7 ifconfig $ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255 up
8 }
9
10 failsafe() {
11 [ -n "$ifname" ] && grep "$ifname" /proc/net/dev >/dev/null && {
12 failsafe_ip
13 netmsg 192.168.1.255 "Entering Failsafe!"
14 telnetd -l /bin/login.sh <> /dev/null 2>&1
15 }
16 lock /tmp/.failsafe
17 ash --login
18 }
19
20 mount proc /proc -t proc
21 mount sysfs /sys -t sysfs
22
23 size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo)
24 mount tmpfs /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
25
26 if grep devfs /proc/filesystems > /dev/null; then
27 mount devfs /dev -t devfs
28 M0=/dev/pty/m0
29 M1=/dev/pty/m1
30 HOTPLUG=/sbin/hotplug-call
31
32 elif [ -x /sbin/hotplug2 ]; then
33 mount -t tmpfs tmpfs /dev -o size=512K
34 mknod /dev/console c 5 1
35 /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --no-persistent --set-coldplug-cmd /sbin/udevtrigger
36 /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --persistent &
37 M0=/dev/ptmx
38 M1=/dev/ptmx
39 HOTPLUG=
40
41 elif [ -x /sbin/udevd ]; then
42 mount -n -t tmpfs -o mode=0755 udev /dev
43 [ -d /lib/udev/devices ] && cp -af /lib/udev/devices/* /dev/
44 [ -c /dev/console ] || mknod -m 0600 /dev/console c 5 1
45 [ -c /dev/null ] || mknod -m 0666 /dev/null c 1 3
46 /sbin/udevd --daemon --resolve-names=never
47 /sbin/udevadm trigger
48 /sbin/udevadm settle
49 M0=/dev/pty/ptmx
50 M1=/dev/pty/ptmx
51 HOTPLUG=
52 fi
53
54 [ -d /dev/pts ] || mkdir -p /dev/pts
55 [ -d /dev/shm ] || mkdir -p /dev/shm
56 mount devpts /dev/pts -t devpts
57
58 # the shell really doesn't like having stdin/out closed
59 # that's why we use /dev/pty/m0 and m1 as replacement
60 # for /dev/console if there's no serial console available
61 dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
62 M0=/dev/console
63 M1=/dev/console
64 }
65
66 exec <$M0 >$M1 2>&0
67
68 echo "- preinit -"
69 echo "Press CTRL-C for failsafe"
70 trap 'FAILSAFE=true' INT
71 trap 'FAILSAFE=true' USR1
72 if [ -e /etc/preinit.arch ]; then
73 . /etc/preinit.arch
74 else
75 sleep 2
76 fi
77 set_state preinit
78 echo "$HOTPLUG" > /proc/sys/kernel/hotplug
79 export FAILSAFE
80 eval ${FAILSAFE:+failsafe}
81 lock -w /tmp/.failsafe
82
83 if [ -z "$INITRAMFS" ]; then
84 mount_root
85 [ -f /sysupgrade.tgz ] && {
86 echo "- config restore -"
87 cd /
88 mv sysupgrade.tgz /tmp
89 tar xzf /tmp/sysupgrade.tgz
90 rm -f /tmp/sysupgrade.tgz
91 sync
92 }
93
94 echo "- init -"
95
96 exec /sbin/init
97 fi