base-files: board.d: support dsl modems, atm bridges and pppoe protocol
[openwrt.git] / package / base-files / files / lib / functions / preinit.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2013 OpenWrt.org
3 # Copyright (C) 2010 Vertical Communications
4
5 boot_hook_splice_start() {
6         export -n PI_HOOK_SPLICE=1
7 }
8
9 boot_hook_splice_finish() {
10         local hook
11         for hook in $PI_STACK_LIST; do
12                 local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
13                 export -n "${hook}=${v% }"
14                 export -n "${hook}_splice="
15         done
16         export -n PI_HOOK_SPLICE=
17 }
18
19 boot_hook_init() {
20         local hook="${1}_hook"
21         export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
22         export -n "$hook="
23 }
24
25 boot_hook_add() {
26         local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
27         local func="${2}"
28
29         [ -n "$func" ] && {
30                 local v; eval "v=\$$hook"
31                 export -n "$hook=${v:+$v }$func"
32         }
33 }
34
35 boot_hook_shift() {
36         local hook="${1}_hook"
37         local rvar="${2}"
38
39         local v; eval "v=\$$hook"
40         [ -n "$v" ] && {
41                 local first="${v%% *}"
42
43                 [ "$v" != "${v#* }" ] && \
44                         export -n "$hook=${v#* }" || \
45                         export -n "$hook="
46
47                 export -n "$rvar=$first"
48                 return 0
49         }
50
51         return 1
52 }
53
54 boot_run_hook() {
55         local hook="$1"
56         local func
57
58         while boot_hook_shift "$hook" func; do
59                 local ran; eval "ran=\$PI_RAN_$func"
60                 [ -n "$ran" ] || {
61                         export -n "PI_RAN_$func=1"
62                         $func "$1" "$2"
63                 }
64         done
65 }
66
67 pivot() { # <new_root> <old_root>
68         /bin/mount -o noatime,move /proc $1/proc && \
69         pivot_root $1 $1$2 && {
70                 /bin/mount -o noatime,move $2/dev /dev
71                 /bin/mount -o noatime,move $2/tmp /tmp
72                 /bin/mount -o noatime,move $2/sys /sys 2>&-
73                 /bin/mount -o noatime,move $2/overlay /overlay 2>&-
74                 return 0
75         }
76 }
77
78 fopivot() { # <rw_root> <work_dir> <ro_root> <dupe?>
79         /bin/mount -o noatime,lowerdir=/,upperdir=$1,workdir=$2 -t overlay "overlayfs:$1" /mnt
80         pivot /mnt $3
81 }
82
83 ramoverlay() {
84         mkdir -p /tmp/root
85         /bin/mount -t tmpfs -o noatime,mode=0755 root /tmp/root
86         mkdir -p /tmp/root/root /tmp/root/work
87         fopivot /tmp/root/root /tmp/root/work /rom 1
88 }