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