package/base-files: copy /lib/functions.sh to the ramfs on sysupgrade
[openwrt/svn-archive/archive.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* | awk '{print $3}'; }
7
8 install_file() { # <file> [ <file> ... ]
9 for file in "$@"; do
10 dest="$RAM_ROOT/$file"
11 [ -f $file -a ! -f $dest ] && {
12 dir="$(dirname $dest)"
13 mkdir -p "$dir"
14 cp $file $dest
15 }
16 done
17 }
18
19 install_bin() { # <file> [ <symlink> ... ]
20 src=$1
21 files=$1
22 [ -x "$src" ] && files="$src $(libs $src)"
23 install_file $files
24 [ -e /lib/ld-linux.so.3 ] && {
25 install_file /lib/ld-linux.so.3
26 }
27 shift
28 for link in "$@"; do {
29 dest="$RAM_ROOT/$link"
30 dir="$(dirname $dest)"
31 mkdir -p "$dir"
32 [ -f "$dest" ] || ln -s $src $dest
33 }; done
34 }
35
36 pivot() { # <new_root> <old_root>
37 mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
38 mkdir -p $1$2 $1/proc $1/dev $1/tmp $1/overlay && \
39 mount -o move /proc $1/proc && \
40 pivot_root $1 $1$2 || {
41 umount $1 $1
42 return 1
43 }
44 mount -o move $2/dev /dev
45 mount -o move $2/tmp /tmp
46 mount -o move $2/overlay /overlay 2>&-
47 return 0
48 }
49
50 run_ramfs() { # <command> [...]
51 install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
52 /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \
53 /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
54 /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
55 /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc
56
57 install_bin /sbin/mtd
58 for file in $RAMFS_COPY_BIN; do
59 install_bin $file
60 done
61 install_file /etc/resolv.conf /etc/functions.sh /lib/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
62
63 pivot $RAM_ROOT /mnt || {
64 echo "Failed to switch over to ramfs. Please reboot."
65 exit 1
66 }
67
68 mount -o remount,ro /mnt
69 umount -l /mnt
70
71 grep /overlay /proc/mounts > /dev/null && {
72 mount -o remount,ro /overlay
73 umount -l /overlay
74 }
75
76 # spawn a new shell from ramdisk to reduce the probability of cache issues
77 exec /bin/busybox ash -c "$*"
78 }
79
80 kill_remaining() { # [ <signal> ]
81 local sig="${1:-TERM}"
82 echo -n "Sending $sig to remaining processes ... "
83
84 local stat
85 for stat in /proc/[0-9]*/stat; do
86 local pid name state ppid rest
87 read pid name state ppid rest < $stat
88 name="${name#(}"; name="${name%)}"
89
90 local cmdline
91 read cmdline < /proc/$pid/cmdline
92
93 # Skip kernel threads
94 [ -n "$cmdline" ] || continue
95
96 case "$name" in
97 # Skip essential services
98 *ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*ubusd*|*netifd*|*hostapd*|*wpa_supplicant*|*udhcpc*) : ;;
99
100 # Killable process
101 *)
102 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
103 echo -n "$name "
104 kill -$sig $pid 2>/dev/null
105 fi
106 ;;
107 esac
108 done
109 echo ""
110 }
111
112 run_hooks() {
113 local arg="$1"; shift
114 for func in "$@"; do
115 eval "$func $arg"
116 done
117 }
118
119 ask_bool() {
120 local default="$1"; shift;
121 local answer="$default"
122
123 [ "$INTERACTIVE" -eq 1 ] && {
124 case "$default" in
125 0) echo -n "$* (y/N): ";;
126 *) echo -n "$* (Y/n): ";;
127 esac
128 read answer
129 case "$answer" in
130 y*) answer=1;;
131 n*) answer=0;;
132 *) answer="$default";;
133 esac
134 }
135 [ "$answer" -gt 0 ]
136 }
137
138 v() {
139 [ "$VERBOSE" -ge 1 ] && echo "$@"
140 }
141
142 rootfs_type() {
143 mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
144 }
145
146 get_image() { # <source> [ <command> ]
147 local from="$1"
148 local conc="$2"
149 local cmd
150
151 case "$from" in
152 http://*|ftp://*) cmd="wget -O- -q";;
153 *) cmd="cat";;
154 esac
155 if [ -z "$conc" ]; then
156 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
157 case "$magic" in
158 1f8b) conc="zcat";;
159 425a) conc="bzcat";;
160 esac
161 fi
162
163 eval "$cmd $from ${conc:+| $conc}"
164 }
165
166 get_magic_word() {
167 get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
168 }
169
170 get_magic_long() {
171 get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
172 }
173
174 refresh_mtd_partitions() {
175 mtd refresh rootfs
176 }
177
178 jffs2_copy_config() {
179 if grep rootfs_data /proc/mtd >/dev/null; then
180 # squashfs+jffs2
181 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
182 else
183 # jffs2
184 mtd jffs2write "$CONF_TAR" rootfs
185 fi
186 }
187
188 default_do_upgrade() {
189 sync
190 if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
191 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
192 else
193 get_image "$1" | mtd write - "${PART_NAME:-image}"
194 fi
195 }
196
197 do_upgrade() {
198 v "Performing system upgrade..."
199 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
200 platform_do_upgrade "$ARGV"
201 else
202 default_do_upgrade "$ARGV"
203 fi
204
205 [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
206 v "Refreshing partitions"
207 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
208 platform_refresh_partitions
209 else
210 refresh_mtd_partitions
211 fi
212 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
213 platform_copy_config
214 else
215 jffs2_copy_config
216 fi
217 }
218 v "Upgrade completed"
219 [ -n "$DELAY" ] && sleep "$DELAY"
220 ask_bool 1 "Reboot" && {
221 v "Rebooting system..."
222 reboot -f
223 sleep 5
224 echo b 2>/dev/null >/proc/sysrq-trigger
225 }
226 }