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