Fix set_state preinit stuff
[openwrt.git] / package / base-files / files / sbin / mount_root
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 . /etc/functions.sh
4
5 size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)?mt-l:s}' /proc/meminfo)
6 mount none /tmp -t tmpfs -o size=$size,nosuid,nodev,mode=1777
7
8 if grep devfs /proc/filesystems > /dev/null; then
9         mount none /dev -t devfs
10         M0=/dev/pty/m0
11         M1=/dev/pty/m1
12         HOTPLUG=/sbin/hotplug-call
13 else
14         mount -t tmpfs tmpfs /dev -o size=512K
15         mknod /dev/console c 5 1
16         mkdir /dev/shm
17         /sbin/hotplug2 --no-persistent --coldplug --set-rules-file /etc/hotplug2-init.rules
18         M0=/dev/ptmx
19         M1=/dev/ptmx
20         HOTPLUG=
21 fi
22
23 mkdir -p /dev/pts /dev/shm
24 mount none /dev/pts -t devpts
25
26 # the shell really doesn't like having stdin/out closed
27 # that's why we use /dev/pty/m0 and m1 as replacement
28 # for /dev/console if there's no serial console available
29 dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null || \
30         exec <$M0 >$M1 2>&0
31
32 echo "$HOTPLUG" > /proc/sys/kernel/hotplug
33
34 jffs2_ready () {
35         mtdpart="$(find_mtd_part rootfs_data)"
36         magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
37         [ "$magic" != "deadc0de" ]
38 }
39
40 [ failsafe != "$1" ] && {
41         grep rootfs_data /proc/mtd >/dev/null 2>/dev/null && {
42                 . /bin/firstboot
43                 mtd unlock rootfs_data
44                 jffs2_ready && {
45                         echo "switching to jffs2"
46                         mount "$(find_mtd_part rootfs_data)" /jffs -t jffs2 && \
47                                 fopivot /jffs /rom
48                 } || {
49                         echo "jffs2 not ready yet; using ramdisk"
50                         ramoverlay
51                 }
52         } || {
53                 mtd unlock rootfs
54                 mount -o remount,rw /dev/root /
55         }
56 }