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