add sysupgrade script for config preserving system upgrades. only implemented for...
[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 <> /dev/null 2>&1
15 }
16 lock /tmp/.failsafe
17 ash --login
18 }
19
20 mount none /proc -t proc
21 mount none /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 none /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
25
26 if grep devfs /proc/filesystems > /dev/null; then
27 mount none /dev -t devfs
28 M0=/dev/pty/m0
29 M1=/dev/pty/m1
30 HOTPLUG=/sbin/hotplug-call
31 else
32 mount -t tmpfs tmpfs /dev -o size=512K
33 mknod /dev/console c 5 1
34 mkdir /dev/shm
35 /sbin/hotplug2 --coldplug --set-rules-file /etc/hotplug2-init.rules
36 /sbin/hotplug2 --no-coldplug --persistent --set-rules-file /etc/hotplug2-init.rules &
37 M0=/dev/ptmx
38 M1=/dev/ptmx
39 HOTPLUG=
40 fi
41
42 mkdir -p /dev/pts /dev/shm
43 mount none /dev/pts -t devpts
44
45 # the shell really doesn't like having stdin/out closed
46 # that's why we use /dev/pty/m0 and m1 as replacement
47 # for /dev/console if there's no serial console available
48 dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && {
49 M0=/dev/console
50 M1=/dev/console
51 }
52
53 exec <$M0 >$M1 2>&0
54
55 echo "- preinit -"
56 trap 'FAILSAFE=true' USR1
57 [ -e /etc/preinit.arch ] && . /etc/preinit.arch
58 set_state preinit
59 echo "$HOTPLUG" > /proc/sys/kernel/hotplug
60 eval ${FAILSAFE:+failsafe}
61 lock -w /tmp/.failsafe
62 mount_root
63 [ -f /sysupgrade.tgz ] && {
64 echo "- config restore -"
65 cd /
66 mv sysupgrade.tgz /tmp
67 tar xzf /tmp/sysupgrade.tgz
68 rm -f /tmp/sysupgrade.tgz
69 sync
70 }
71
72 echo "- init -"
73
74 killall hotplug2
75 exec /sbin/init