Skip to content

Commit 30f61a3

Browse files
committed
base-files: always use staged sysupgrade
Support for the -d and -p options is dropped; it may be added again at some point by adding these flags to the ubus sysupgrade call. A downside of this is that we get a lot less information about the progress of the upgrade: as soon as the actual upgrade starts, all shell sessions are killed to allow unmounting the root filesystem. Signed-off-by: Matthias Schiffer <[email protected]>
1 parent 393817d commit 30f61a3

File tree

4 files changed

+162
-148
lines changed

4 files changed

+162
-148
lines changed

package/base-files/files/lib/upgrade/common.sh

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ run_ramfs() { # <command> [...]
5656
/bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find \
5757
/bin/mknod
5858

59-
install_bin /bin/uclient-fetch /bin/wget
6059
install_bin /sbin/mtd
6160
install_bin /sbin/mount_root
6261
install_bin /sbin/snapshot
@@ -96,51 +95,37 @@ run_ramfs() { # <command> [...]
9695
exec /bin/busybox ash -c "$*"
9796
}
9897

99-
kill_remaining() { # [ <signal> ]
98+
kill_remaining() { # [ <signal> [ <loop> ] ]
10099
local sig="${1:-TERM}"
100+
local loop="${2:-0}"
101+
local run=true
102+
local stat
103+
101104
echo -n "Sending $sig to remaining processes ... "
102105

103-
local my_pid=$$
104-
local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
105-
local my_ppisupgraded=
106-
grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
107-
local my_ppisupgraded=1
108-
}
109-
110-
local stat
111-
for stat in /proc/[0-9]*/stat; do
112-
[ -f "$stat" ] || continue
113-
114-
local pid name state ppid rest
115-
read pid name state ppid rest < $stat
116-
name="${name#(}"; name="${name%)}"
117-
118-
local cmdline
119-
read cmdline < /proc/$pid/cmdline
120-
121-
# Skip kernel threads
122-
[ -n "$cmdline" ] || continue
123-
124-
if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
125-
# Running as init process, kill everything except me
126-
if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
127-
echo -n "$name "
128-
kill -$sig $pid 2>/dev/null
129-
fi
130-
else
131-
case "$name" in
132-
# Skip essential services
133-
*procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*|*relayd*) : ;;
134-
135-
# Killable process
136-
*)
137-
if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
138-
echo -n "$name "
139-
kill -$sig $pid 2>/dev/null
140-
fi
141-
;;
142-
esac
143-
fi
106+
while $run; do
107+
run=false
108+
for stat in /proc/[0-9]*/stat; do
109+
[ -f "$stat" ] || continue
110+
111+
local pid name state ppid rest
112+
read pid name state ppid rest < $stat
113+
name="${name#(}"; name="${name%)}"
114+
115+
# Skip PID1, ourself and our children
116+
[ $pid -ne 1 -a $pid -ne $$ -a $ppid -ne $$ ] || continue
117+
118+
local cmdline
119+
read cmdline < /proc/$pid/cmdline
120+
121+
# Skip kernel threads
122+
[ -n "$cmdline" ] || continue
123+
124+
echo -n "$name "
125+
kill -$sig $pid 2>/dev/null
126+
127+
[ $loop -eq 1 ] && run=true
128+
done
144129
done
145130
echo ""
146131
}
@@ -175,28 +160,31 @@ v() {
175160
[ "$VERBOSE" -ge 1 ] && echo "$@"
176161
}
177162

163+
json_string() {
164+
local v="$1"
165+
v="${v//\\/\\\\}"
166+
v="${v//\"/\\\"}"
167+
echo "\"$v\""
168+
}
169+
178170
rootfs_type() {
179171
/bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
180172
}
181173

182174
get_image() { # <source> [ <command> ]
183175
local from="$1"
184-
local conc="$2"
185-
local cmd
186-
187-
case "$from" in
188-
http://*|ftp://*) cmd="wget -O- -q";;
189-
*) cmd="cat";;
190-
esac
191-
if [ -z "$conc" ]; then
192-
local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
176+
local cat="$2"
177+
178+
if [ -z "$cat" ]; then
179+
local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
193180
case "$magic" in
194-
1f8b) conc="zcat";;
195-
425a) conc="bzcat";;
181+
1f8b) cat="zcat";;
182+
425a) cat="bzcat";;
183+
*) cat="cat";;
196184
esac
197185
fi
198186

199-
eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
187+
$cat "$from" 2>/dev/null
200188
}
201189

202190
get_magic_word() {
@@ -320,25 +308,26 @@ default_do_upgrade() {
320308
fi
321309
}
322310

323-
do_upgrade() {
311+
do_upgrade_stage2() {
324312
v "Performing system upgrade..."
325-
if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
326-
platform_do_upgrade "$ARGV"
313+
if [ -n "$do_upgrade" ]; then
314+
$do_upgrade "$IMAGE"
315+
elif type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
316+
platform_do_upgrade "$IMAGE"
327317
else
328-
default_do_upgrade "$ARGV"
318+
default_do_upgrade "$IMAGE"
329319
fi
330320

331321
if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
332322
platform_copy_config
333323
fi
334324

335325
v "Upgrade completed"
336-
[ -n "$DELAY" ] && sleep "$DELAY"
337-
ask_bool 1 "Reboot" && {
338-
v "Rebooting system..."
339-
umount -a
340-
reboot -f
341-
sleep 5
342-
echo b 2>/dev/null >/proc/sysrq-trigger
343-
}
326+
sleep 1
327+
328+
v "Rebooting system..."
329+
umount -a
330+
reboot -f
331+
sleep 5
332+
echo b 2>/dev/null >/proc/sysrq-trigger
344333
}

package/base-files/files/lib/upgrade/nand.sh

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,16 @@ nand_upgrade_tar() {
283283
}
284284

285285
# Recognize type of passed file and start the upgrade process
286-
nand_do_upgrade_stage2() {
286+
nand_do_upgrade() {
287+
if [ -n "$IS_PRE_UPGRADE" ]; then
288+
# Previously, nand_do_upgrade was called from the platform_pre_upgrade
289+
# hook; this piece of code handles scripts that haven't been
290+
# updated. All scripts should gradually move to call nand_do_upgrade
291+
# from platform_do_upgrade instead.
292+
export do_upgrade=nand_do_upgrade
293+
return
294+
fi
295+
287296
local file_type=$(identify $1)
288297

289298
if type 'platform_nand_pre_upgrade' >/dev/null 2>/dev/null; then
@@ -299,45 +308,6 @@ nand_do_upgrade_stage2() {
299308
esac
300309
}
301310

302-
nand_upgrade_stage2() {
303-
[ $1 = "nand" ] && {
304-
[ -f "$2" ] && {
305-
touch /tmp/sysupgrade
306-
307-
killall -9 telnetd
308-
killall -9 dropbear
309-
killall -9 ash
310-
311-
kill_remaining TERM
312-
sleep 3
313-
kill_remaining KILL
314-
315-
sleep 1
316-
317-
if [ -n "$(rootfs_type)" ]; then
318-
v "Switching to ramdisk..."
319-
run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
320-
else
321-
nand_do_upgrade_stage2 $2
322-
fi
323-
return 0
324-
}
325-
echo "Nand upgrade failed"
326-
exit 1
327-
}
328-
}
329-
330-
nand_upgrade_stage1() {
331-
[ -f /tmp/sysupgrade-nand-path ] && {
332-
path="$(cat /tmp/sysupgrade-nand-path)"
333-
[ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
334-
rm $CONF_TAR
335-
336-
ubus call system nandupgrade "{\"prefix\": \"$RAM_ROOT\", \"path\": \"$path\" }"
337-
exit 0
338-
}
339-
}
340-
341311
# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
342312
# 3 types of files:
343313
# 1) UBI - should contain an ubinized image, header is checked for the proper
@@ -364,13 +334,3 @@ nand_do_platform_check() {
364334

365335
return 0
366336
}
367-
368-
# Start NAND upgrade process
369-
#
370-
# $(1): file to be used for upgrade
371-
nand_do_upgrade() {
372-
echo -n $1 > /tmp/sysupgrade-nand-path
373-
install_bin /sbin/upgraded
374-
ln -s "$RAM_ROOT"/sbin/upgraded /tmp/upgraded
375-
nand_upgrade_stage1
376-
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/sh
2+
3+
. /lib/functions.sh
4+
. /lib/functions/system.sh
5+
6+
export IMAGE="$1"
7+
COMMAND="$2"
8+
9+
export ARGV="$IMAGE"
10+
export ARGC=1
11+
12+
export SAVE_CONFIG=1
13+
export SAVE_PARTITIONS=1
14+
15+
export INTERACTIVE=0
16+
export VERBOSE=1
17+
export CONFFILES=/tmp/sysupgrade.conffiles
18+
export CONF_TAR=/tmp/sysupgrade.tgz
19+
20+
21+
[ -f "$CONF_TAR" ] || export SAVE_CONFIG=0
22+
[ -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap ] && export SAVE_PARTITIONS=0
23+
24+
include /lib/upgrade
25+
26+
27+
killall -9 telnetd
28+
killall -9 dropbear
29+
killall -9 ash
30+
31+
kill_remaining TERM
32+
sleep 3
33+
kill_remaining KILL 1
34+
35+
sleep 1
36+
37+
38+
if [ -n "$IMAGE" ] && type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
39+
IS_PRE_UPGRADE=1 platform_pre_upgrade "$IMAGE"
40+
41+
# Needs to be unset again because of busybox weirdness ...
42+
IS_PRE_UPGRADE=
43+
fi
44+
45+
if [ -n "$(rootfs_type)" ]; then
46+
echo "Switching to ramdisk..."
47+
run_ramfs "$COMMAND"
48+
else
49+
exec /bin/busybox ash -c "$COMMAND"
50+
fi

0 commit comments

Comments
 (0)