initramfs: start one hotplug2 for coldplugging and one for handling events between...
[openwrt-10.03/.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 <> /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: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         if [ -n "$INITRAMFS" ]; then
36                 /sbin/hotplug2 --coldplug --no-persistent --max_children 1
37                 /sbin/hotplug2 --persistent --max_children 1 &
38         else
39                 /sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules
40                 /sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2-init.rules &
41         fi
42         M0=/dev/ptmx
43         M1=/dev/ptmx
44         HOTPLUG=
45
46 elif [ -x /sbin/udevd ]; then
47         mount -n -t tmpfs -o mode=0755 udev /dev
48         /sbin/udevd --daemon
49         /sbin/udevtrigger
50         /sbin/udevsettle
51         M0=/dev/pty/ptmx
52         M1=/dev/pty/ptmx
53         HOTPLUG=
54 fi
55
56 mkdir -p /dev/pts /dev/shm
57 mount devpts /dev/pts -t devpts
58
59 # the shell really doesn't like having stdin/out closed
60 # that's why we use /dev/pty/m0 and m1 as replacement
61 # for /dev/console if there's no serial console available
62 dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
63         M0=/dev/console
64         M1=/dev/console
65 }
66
67 exec <$M0 >$M1 2>&0
68
69 echo "- preinit -"
70 trap 'FAILSAFE=true' USR1
71 [ -e /etc/preinit.arch ] && . /etc/preinit.arch
72 set_state preinit
73 echo "$HOTPLUG" > /proc/sys/kernel/hotplug
74 eval ${FAILSAFE:+failsafe}
75 lock -w /tmp/.failsafe
76
77 if [ -z "$INITRAMFS" ]; then
78         mount_root
79         [ -f /sysupgrade.tgz ] && {
80                 echo "- config restore -"
81                 cd /
82                 mv sysupgrade.tgz /tmp
83                 tar xzf /tmp/sysupgrade.tgz
84                 rm -f /tmp/sysupgrade.tgz
85                 sync
86         }
87
88         echo "- init -"
89         
90         killall hotplug2
91         exec /sbin/init
92 fi