base-files: sysupgrade: group & cleanup global variables
[openwrt/staging/981213.git] / package / base-files / files / sbin / sysupgrade
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/system.sh
5 . /usr/share/libubox/jshn.sh
6
7 # File-local constants
8 CONF_TAR=/tmp/sysupgrade.tgz
9 ETCBACKUP_DIR=/etc/backup
10 INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
11 COMMAND=/lib/upgrade/do_stage2
12
13 # File-local globals
14 SAVE_OVERLAY=0
15 SAVE_OVERLAY_PATH=
16 SAVE_PARTITIONS=1
17 SAVE_INSTALLED_PKGS=0
18 SKIP_UNCHANGED=0
19 CONF_IMAGE=
20 CONF_BACKUP_LIST=0
21 CONF_BACKUP=
22 CONF_RESTORE=
23 NEED_IMAGE=
24 HELP=0
25 TEST=0
26
27 # Globals accessed in other files
28 export MTD_ARGS=""
29 export MTD_CONFIG_ARGS=""
30 export INTERACTIVE=0
31 export VERBOSE=1
32 export SAVE_CONFIG=1
33 export IGNORE_MINOR_COMPAT=0
34 export FORCE=0
35 export CONFFILES=/tmp/sysupgrade.conffiles
36
37 # parse options
38 while [ -n "$1" ]; do
39 case "$1" in
40 -i) export INTERACTIVE=1;;
41 -v) export VERBOSE="$(($VERBOSE + 1))";;
42 -q) export VERBOSE="$(($VERBOSE - 1))";;
43 -n) export SAVE_CONFIG=0;;
44 -c) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
45 -o) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
46 -p) SAVE_PARTITIONS=0;;
47 -k) SAVE_INSTALLED_PKGS=1;;
48 -u) SKIP_UNCHANGED=1;;
49 -b|--create-backup) CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
50 -r|--restore-backup) CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
51 -l|--list-backup) CONF_BACKUP_LIST=1;;
52 -f) CONF_IMAGE="$2"; shift;;
53 -F|--force) export FORCE=1;;
54 -T|--test) TEST=1;;
55 -h|--help) HELP=1; break;;
56 --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT=1;;
57 -*)
58 echo "Invalid option: $1" >&2
59 exit 1
60 ;;
61 *) break;;
62 esac
63 shift;
64 done
65
66 IMAGE="$1"
67
68 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
69 cat <<EOF
70 Usage: $0 [<upgrade-option>...] <image file or URL>
71 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
72
73 upgrade-option:
74 -f <config> restore configuration from .tar.gz (file or url)
75 -i interactive mode
76 -c attempt to preserve all changed files in /etc/
77 -o attempt to preserve all changed files in /, except those
78 from packages but including changed confs.
79 -u skip from backup files that are equal to those in /rom
80 -n do not save configuration over reflash
81 -p do not attempt to restore the partition table after flash.
82 -k include in backup a list of current installed packages at
83 $INSTALLED_PACKAGES
84 -T | --test
85 Verify image and config .tar.gz but do not actually flash.
86 -F | --force
87 Flash image even if image checks fail, this is dangerous!
88 --ignore-minor-compat-version
89 Flash image even if the minor compat version is incompatible.
90 -q less verbose
91 -v more verbose
92 -h | --help display this help
93
94 backup-command:
95 -b | --create-backup <file>
96 create .tar.gz of files specified in sysupgrade.conf
97 then exit. Does not flash an image. If file is '-',
98 i.e. stdout, verbosity is set to 0 (i.e. quiet).
99 -r | --restore-backup <file>
100 restore a .tar.gz created with sysupgrade -b
101 then exit. Does not flash an image. If file is '-',
102 the archive is read from stdin.
103 -l | --list-backup
104 list the files that would be backed up when calling
105 sysupgrade -b. Does not create a backup file.
106
107 EOF
108 exit 1
109 }
110
111 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
112 cat <<-EOF
113 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
114 Do not specify both -b|-r and a firmware image.
115 EOF
116 exit 1
117 }
118
119 # prevent messages from clobbering the tarball when using stdout
120 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
121
122
123 list_conffiles() {
124 awk '
125 BEGIN { conffiles = 0 }
126 /^Conffiles:/ { conffiles = 1; next }
127 !/^ / { conffiles = 0; next }
128 conffiles == 1 { print }
129 ' /usr/lib/opkg/status
130 }
131
132 list_changed_conffiles() {
133 # Cannot handle spaces in filenames - but opkg cannot either...
134 list_conffiles | while read file csum; do
135 [ -r "$file" ] || continue
136
137 echo "${csum} ${file}" | busybox sha256sum -sc - || echo "$file"
138 done
139 }
140
141 list_static_conffiles() {
142 local filter=$1
143
144 find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
145 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
146 \( -type f -o -type l \) $filter 2>/dev/null
147 }
148
149 add_conffiles() {
150 local file="$1"
151
152 ( list_static_conffiles "$find_filter"; list_changed_conffiles ) |
153 sort -u > "$file"
154 return 0
155 }
156
157 add_overlayfiles() {
158 local file="$1"
159
160 local packagesfiles=$1.packagesfiles
161 touch "$packagesfiles"
162
163 if [ "$SAVE_OVERLAY_PATH" = / ]; then
164 local conffiles=$1.conffiles
165 local keepfiles=$1.keepfiles
166
167 list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
168
169 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
170 # ignore those aready controlled by opkg conffiles
171 list_static_conffiles | sort -u |
172 grep -h -v -x -F -f $conffiles > "$keepfiles"
173
174 # backup conffiles, but only those changed if '-u'
175 [ $SKIP_UNCHANGED = 1 ] &&
176 list_changed_conffiles | sort -u > "$conffiles"
177
178 # do not backup files from packages, except those listed
179 # in conffiles and keep.d
180 {
181 find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
182 find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
183 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
184 cut -f2 -d:
185 } | grep -v -x -F -f $conffiles |
186 grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
187 rm -f "$keepfiles" "$conffiles"
188 fi
189
190 # busybox grep bug when file is empty
191 [ -s "$packagesfiles" ] || echo > $packagesfiles
192
193 ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
194 -e 's,^\.,,' \
195 -e '\,^/etc/board.json$,d' \
196 -e '\,/[^/]*-opkg$,d' \
197 -e '\,^/etc/urandom.seed$,d' \
198 -e "\,^$INSTALLED_PACKAGES$,d" \
199 -e '\,^/usr/lib/opkg/.*,d' \
200 ) | grep -v -x -F -f $packagesfiles > "$file"
201
202 rm -f "$packagesfiles"
203
204 return 0
205 }
206
207 if [ $SAVE_OVERLAY = 1 ]; then
208 [ ! -d /overlay/upper/etc ] && {
209 echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
210 exit 1
211 }
212 sysupgrade_init_conffiles="add_overlayfiles"
213 else
214 sysupgrade_init_conffiles="add_conffiles"
215 fi
216
217 find_filter=""
218 if [ $SKIP_UNCHANGED = 1 ]; then
219 [ ! -d /rom/ ] && {
220 echo "'/rom/' is required by '-u'"
221 exit 1
222 }
223 find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
224 fi
225
226 include /lib/upgrade
227
228 do_save_conffiles() {
229 local conf_tar="$1"
230
231 local umount_etcbackup_dir=0
232
233 [ "$(rootfs_type)" = "tmpfs" ] && {
234 echo "Cannot save config while running from ramdisk." >&2
235 ask_bool 0 "Abort" && exit
236 rm -f "$conf_tar"
237 return 0
238 }
239 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
240 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
241
242 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
243 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
244 mkdir -p "$ETCBACKUP_DIR"
245 # Avoid touching filesystem on each backup
246 RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
247 mkdir -p "$RAMFS/upper" "$RAMFS/work"
248 mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
249 umount_etcbackup_dir=1 || {
250 echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
251 ask_bool 0 "Abort" && exit
252 }
253
254 # Format: pkg-name<TAB>{rom,overlay,unknown}
255 # rom is used for pkgs in /rom, even if updated later
256 find /usr/lib/opkg/info -name "*.control" \( \
257 \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
258 \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
259 \( -exec echo {} unknown \; \) \
260 \) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
261 fi
262
263 v "Saving config files..."
264 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
265 sed -i -e 's,^/,,' "$CONFFILES"
266 tar c${TAR_V}zf "$conf_tar" -C / -T "$CONFFILES"
267 local err=$?
268 if [ "$err" -ne 0 ]; then
269 echo "Failed to create the configuration backup."
270 rm -f "$conf_tar"
271 fi
272
273 [ "$umount_etcbackup_dir" -eq 1 ] && {
274 umount "$ETCBACKUP_DIR"
275 rm -rf "$RAMFS"
276 }
277 rm -f "$CONFFILES"
278
279 return "$err"
280 }
281
282 if [ $CONF_BACKUP_LIST -eq 1 ]; then
283 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
284 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
285 cat "$CONFFILES"
286 rm -f "$CONFFILES"
287 exit 0
288 fi
289
290 if [ -n "$CONF_BACKUP" ]; then
291 do_save_conffiles "$CONF_BACKUP"
292 exit
293 fi
294
295 if [ -n "$CONF_RESTORE" ]; then
296 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
297 echo "Backup archive '$CONF_RESTORE' not found." >&2
298 exit 1
299 fi
300
301 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
302 v "Restoring config files..."
303 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
304 exit $?
305 fi
306
307 type platform_check_image >/dev/null 2>/dev/null || {
308 echo "Firmware upgrade is not implemented for this platform." >&2
309 exit 1
310 }
311
312 case "$IMAGE" in
313 http://*|\
314 https://*)
315 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
316 IMAGE=/tmp/sysupgrade.img
317 ;;
318 esac
319
320 IMAGE="$(readlink -f "$IMAGE")"
321
322 case "$IMAGE" in
323 '')
324 echo "Image file not found." >&2
325 exit 1
326 ;;
327 /tmp/*) ;;
328 *)
329 v "Image not in /tmp, copying..."
330 cp -f "$IMAGE" /tmp/sysupgrade.img
331 IMAGE=/tmp/sysupgrade.img
332 ;;
333 esac
334
335 json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
336 echo "Failed to check image"
337 exit 1
338 }
339 json_get_var valid "valid"
340 [ "$valid" -eq 0 ] && {
341 if [ $FORCE -eq 1 ]; then
342 echo "Image check failed but --force given - will update anyway!" >&2
343 else
344 echo "Image check failed." >&2
345 exit 1
346 fi
347 }
348
349 if [ -n "$CONF_IMAGE" ]; then
350 case "$(get_magic_word $CONF_IMAGE cat)" in
351 # .gz files
352 1f8b) ;;
353 *)
354 echo "Invalid config file. Please use only .tar.gz files" >&2
355 exit 1
356 ;;
357 esac
358 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
359 export SAVE_CONFIG=1
360 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
361 [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR" || exit
362 export SAVE_CONFIG=1
363 else
364 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
365 export SAVE_CONFIG=0
366 fi
367
368 if [ $TEST -eq 1 ]; then
369 exit 0
370 fi
371
372 install_bin /sbin/upgraded
373 v "Commencing upgrade. Closing all shell sessions."
374
375 if [ -n "$FAILSAFE" ]; then
376 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
377 lock -u /tmp/.failsafe
378 else
379 json_init
380 json_add_string prefix "$RAM_ROOT"
381 json_add_string path "$IMAGE"
382 [ $FORCE -eq 1 ] && json_add_boolean force 1
383 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
384 json_add_string command "$COMMAND"
385 json_add_object options
386 json_add_int save_partitions "$SAVE_PARTITIONS"
387 json_close_object
388
389 ubus call system sysupgrade "$(json_dump)"
390 fi