base-files: fix a preinit hang in jffs2_ready() when no rootfs_data partition is...
[openwrt/openwrt.git] / package / base-files / files / lib / functions / boot.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2010 Vertical Communications
4
5 mount() {
6 /bin/busybox mount "$@"
7 }
8
9 boot_hook_add() {
10 local hook="${1}_hook"
11 local value="$2"
12 local sep=" "
13
14 eval "$hook=\"\${$hook:+\${$hook}\${value:+\$sep}}\$value\""
15 }
16
17 boot_run_hook() {
18 local boot_func
19 for boot_func in $(eval "echo \"\$${1}_hook\""); do
20 $boot_func "$1" "$2"
21 done
22 }
23
24 find_mtd_part() {
25 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
26 local PREFIX=/dev/mtdblock
27
28 PART="${PART##mtd}"
29 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
30 echo "${PART:+$PREFIX$PART}"
31 }
32
33 jffs2_ready () {
34 mtdpart="$(find_mtd_part rootfs_data)"
35 [ -z "$mtdpart" ] && return 1
36 magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
37 [ "$magic" != "deadc0de" ]
38 }
39
40 dupe() { # <new_root> <old_root>
41 cd $1
42 echo -n "creating directories... "
43 {
44 cd $2
45 find . -xdev -type d
46 echo "./dev ./overlay ./mnt ./proc ./tmp"
47 # xdev skips mounted directories
48 cd $1
49 } | xargs mkdir -p
50 echo "done"
51
52 echo -n "setting up symlinks... "
53 for file in $(cd $2; find . -xdev -type f;); do
54 case "$file" in
55 ./rom/note) ;; #nothing
56 ./etc/config*|\
57 ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
58 *) ln -sf /rom/${file#./*} $file;;
59 esac
60 done
61 for file in $(cd $2; find . -xdev -type l;); do
62 cp -af $2/${file#./*} $file
63 done
64 echo "done"
65 }
66
67 pivot() { # <new_root> <old_root>
68 mount -o move /proc $1/proc && \
69 pivot_root $1 $1$2 && {
70 mount -o move $2/dev /dev
71 mount -o move $2/tmp /tmp
72 mount -o move $2/sys /sys 2>&-
73 mount -o move $2/overlay /overlay 2>&-
74 return 0
75 }
76 }
77
78 fopivot() { # <rw_root> <ro_root> <dupe?>
79 root=$1
80 {
81 if grep -q mini_fo /proc/filesystems; then
82 mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
83 else
84 mount --bind / /mnt
85 mount --bind -o union "$1" /mnt && root=/mnt
86 fi
87 } || {
88 [ "$3" = "1" ] && {
89 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
90 dupe $1 $rom
91 }
92 }
93 pivot $root $2
94 }
95
96 ramoverlay() {
97 mkdir -p /tmp/root
98 mount -t tmpfs root /tmp/root
99 fopivot /tmp/root /rom 1
100 }