e6f6633adb94123c45e45a652ed753f650e57e89
[openwrt-10.03/.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     magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
36     [ "$magic" != "deadc0de" ]
37 }
38
39 dupe() { # <new_root> <old_root>
40         cd $1
41         echo -n "creating directories... "
42         {
43                 cd $2 
44                 find . -xdev -type d
45                 echo "./dev ./overlay ./mnt ./proc ./tmp"
46                 # xdev skips mounted directories
47                 cd $1 
48         } | xargs mkdir -p
49         echo "done"
50
51         echo -n "setting up symlinks... "
52         for file in $(cd $2; find . -xdev -type f;); do
53                 case "$file" in
54                 ./rom/note) ;; #nothing
55                 ./etc/config*|\
56                 ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
57                 *) ln -sf /rom/${file#./*} $file;;
58                 esac
59         done
60         for file in $(cd $2; find . -xdev -type l;); do
61                 cp -af $2/${file#./*} $file
62         done
63         echo "done"
64 }
65
66 pivot() { # <new_root> <old_root>
67         mount -o move /proc $1/proc && \
68         pivot_root $1 $1$2 && {
69                 mount -o move $2/dev /dev
70                 mount -o move $2/tmp /tmp
71                 mount -o move $2/sys /sys 2>&-
72                 mount -o move $2/overlay /overlay 2>&-
73                 return 0
74         }
75 }
76
77 fopivot() { # <rw_root> <ro_root> <dupe?>
78         root=$1
79         {
80                 if grep -q mini_fo /proc/filesystems; then
81                         mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
82                 else
83                         mount --bind / /mnt
84                         mount --bind -o union "$1" /mnt && root=/mnt 
85                 fi
86         } || {
87                 [ "$3" = "1" ] && {
88                 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
89                 dupe $1 $rom
90                 }
91         }
92         pivot $root $2
93 }
94
95 ramoverlay() {
96         mkdir -p /tmp/root
97         mount -t tmpfs root /tmp/root
98         fopivot /tmp/root /rom 1
99 }