[base system & package/block-extroot] Fixed duplicated content in file in commit...
[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_splice_start() {
10 export -n PI_HOOK_SPLICE=1
11 }
12
13 boot_hook_splice_finish() {
14 local hook
15 for hook in $PI_STACK_LIST; do
16 local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
17 export -n "${hook}=${v% }"
18 export -n "${hook}_splice="
19 done
20 export -n PI_HOOK_SPLICE=
21 }
22
23 boot_hook_init() {
24 local hook="${1}_hook"
25 export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
26 export -n "$hook="
27 }
28
29 boot_hook_add() {
30 local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
31 local func="${2}"
32
33 [ -n "$func" ] && {
34 local v; eval "v=\$$hook"
35 export -n "$hook=${v:+$v }$func"
36 }
37 }
38
39 boot_hook_shift() {
40 local hook="${1}_hook"
41 local rvar="${2}"
42
43 local v; eval "v=\$$hook"
44 [ -n "$v" ] && {
45 local first="${v%% *}"
46
47 [ "$v" != "${v#* }" ] && \
48 export -n "$hook=${v#* }" || \
49 export -n "$hook="
50
51 export -n "$rvar=$first"
52 return 0
53 }
54
55 return 1
56 }
57
58 boot_run_hook() {
59 local hook="$1"
60 local func
61
62 while boot_hook_shift "$hook" func; do
63 local ran; eval "ran=\$PI_RAN_$func"
64 [ -n "$ran" ] || {
65 export -n "PI_RAN_$func=1"
66 $func "$1" "$2"
67 }
68 done
69 }
70
71 find_mtd_part() {
72 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
73 local PREFIX=/dev/mtdblock
74
75 PART="${PART##mtd}"
76 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
77 echo "${PART:+$PREFIX$PART}"
78 }
79
80 jffs2_ready () {
81 mtdpart="$(find_mtd_part rootfs_data)"
82 [ -z "$mtdpart" ] && return 1
83 magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
84 [ "$magic" != "deadc0de" ]
85 }
86
87 dupe() { # <new_root> <old_root>
88 cd $1
89 echo -n "creating directories... "
90 {
91 cd $2
92 find . -xdev -type d
93 echo "./dev ./overlay ./mnt ./proc ./tmp"
94 # xdev skips mounted directories
95 cd $1
96 } | xargs mkdir -p
97 echo "done"
98
99 echo -n "setting up symlinks... "
100 for file in $(cd $2; find . -xdev -type f;); do
101 case "$file" in
102 ./rom/note) ;; #nothing
103 ./etc/config*|\
104 ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
105 *) ln -sf /rom/${file#./*} $file;;
106 esac
107 done
108 for file in $(cd $2; find . -xdev -type l;); do
109 cp -af $2/${file#./*} $file
110 done
111 echo "done"
112 }
113
114 pivot() { # <new_root> <old_root>
115 mount -o move /proc $1/proc && \
116 pivot_root $1 $1$2 && {
117 mount -o move $2/dev /dev
118 mount -o move $2/tmp /tmp
119 mount -o move $2/sys /sys 2>&-
120 mount -o move $2/overlay /overlay 2>&-
121 return 0
122 }
123 }
124
125 fopivot() { # <rw_root> <ro_root> <dupe?>
126 root=$1
127 {
128 if grep -q mini_fo /proc/filesystems; then
129 mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
130 else
131 mount --bind / /mnt
132 mount --bind -o union "$1" /mnt && root=/mnt
133 fi
134 } || {
135 [ "$3" = "1" ] && {
136 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
137 dupe $1 $rom
138 }
139 }
140 pivot $root $2
141 }
142
143 ramoverlay() {
144 mkdir -p /tmp/root
145 mount -t tmpfs root /tmp/root
146 fopivot /tmp/root /rom 1
147 }
148
This page took 0.063183 seconds and 5 git commands to generate.