base-files: sysupgrade: handle errors when generating backup
[openwrt/staging/mans0n.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 print_help() {
67 cat <<EOF
68 Usage: $0 [<upgrade-option>...] <image file or URL>
69 $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
70
71 upgrade-option:
72 -f <config> restore configuration from .tar.gz (file or url)
73 -i interactive mode
74 -c attempt to preserve all changed files in /etc/
75 -o attempt to preserve all changed files in /, except those
76 from packages but including changed confs.
77 -u skip from backup files that are equal to those in /rom
78 -n do not save configuration over reflash
79 -p do not attempt to restore the partition table after flash.
80 -k include in backup a list of current installed packages at
81 $INSTALLED_PACKAGES
82 -T | --test
83 Verify image and config .tar.gz but do not actually flash.
84 -F | --force
85 Flash image even if image checks fail, this is dangerous!
86 --ignore-minor-compat-version
87 Flash image even if the minor compat version is incompatible.
88 -q less verbose
89 -v more verbose
90 -h | --help display this help
91
92 backup-command:
93 -b | --create-backup <file>
94 create .tar.gz of files specified in sysupgrade.conf
95 then exit. Does not flash an image. If file is '-',
96 i.e. stdout, verbosity is set to 0 (i.e. quiet).
97 -r | --restore-backup <file>
98 restore a .tar.gz created with sysupgrade -b
99 then exit. Does not flash an image. If file is '-',
100 the archive is read from stdin.
101 -l | --list-backup
102 list the files that would be backed up when calling
103 sysupgrade -b. Does not create a backup file.
104
105 EOF
106 }
107
108 IMAGE="$1"
109
110 if [ $HELP -gt 0 ]; then
111 print_help
112 exit 0
113 fi
114
115 if [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 ]; then
116 print_help
117 exit 1
118 fi
119
120 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
121 cat <<-EOF
122 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
123 Do not specify both -b|-r and a firmware image.
124 EOF
125 exit 1
126 }
127
128 # prevent messages from clobbering the tarball when using stdout
129 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
130
131
132 list_conffiles() {
133 awk '
134 BEGIN { conffiles = 0 }
135 /^Conffiles:/ { conffiles = 1; next }
136 !/^ / { conffiles = 0; next }
137 conffiles == 1 { print }
138 ' /usr/lib/opkg/status
139 }
140
141 list_changed_conffiles() {
142 # Cannot handle spaces in filenames - but opkg cannot either...
143 list_conffiles | while read file csum; do
144 [ -r "$file" ] || continue
145
146 echo "${csum} ${file}" | busybox sha256sum -sc - || echo "$file"
147 done
148 }
149
150 list_static_conffiles() {
151 local filter=$1
152
153 find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
154 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
155 \( -type f -o -type l \) $filter 2>/dev/null
156 }
157
158 build_list_of_backup_config_files() {
159 local file="$1"
160
161 ( list_static_conffiles "$find_filter"; list_changed_conffiles ) |
162 sort -u > "$file"
163 return 0
164 }
165
166 build_list_of_backup_overlay_files() {
167 local file="$1"
168
169 local packagesfiles=$1.packagesfiles
170 touch "$packagesfiles"
171
172 if [ "$SAVE_OVERLAY_PATH" = / ]; then
173 local conffiles=$1.conffiles
174 local keepfiles=$1.keepfiles
175
176 list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
177
178 # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
179 # ignore those aready controlled by opkg conffiles
180 list_static_conffiles | sort -u |
181 grep -h -v -x -F -f $conffiles > "$keepfiles"
182
183 # backup conffiles, but only those changed if '-u'
184 [ $SKIP_UNCHANGED = 1 ] &&
185 list_changed_conffiles | sort -u > "$conffiles"
186
187 # do not backup files from packages, except those listed
188 # in conffiles and keep.d
189 {
190 find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
191 find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
192 -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
193 cut -f2 -d:
194 } | grep -v -x -F -f $conffiles |
195 grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
196 rm -f "$keepfiles" "$conffiles"
197 fi
198
199 # busybox grep bug when file is empty
200 [ -s "$packagesfiles" ] || echo > $packagesfiles
201
202 ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
203 -e 's,^\.,,' \
204 -e '\,^/etc/board.json$,d' \
205 -e '\,/[^/]*-opkg$,d' \
206 -e '\,^/etc/urandom.seed$,d' \
207 -e "\,^$INSTALLED_PACKAGES$,d" \
208 -e '\,^/usr/lib/opkg/.*,d' \
209 ) | grep -v -x -F -f $packagesfiles > "$file"
210
211 rm -f "$packagesfiles"
212
213 return 0
214 }
215
216 if [ $SAVE_OVERLAY = 1 ]; then
217 [ ! -d /overlay/upper/etc ] && {
218 echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
219 exit 1
220 }
221 sysupgrade_init_conffiles="build_list_of_backup_overlay_files"
222 else
223 sysupgrade_init_conffiles="build_list_of_backup_config_files"
224 fi
225
226 find_filter=""
227 if [ $SKIP_UNCHANGED = 1 ]; then
228 [ ! -d /rom/ ] && {
229 echo "'/rom/' is required by '-u'"
230 exit 1
231 }
232 find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
233 fi
234
235 include /lib/upgrade
236
237 create_backup_archive() {
238 local conf_tar="$1"
239 local disabled
240 local err
241
242 [ "$(rootfs_type)" = "tmpfs" ] && {
243 echo "Cannot save config while running from ramdisk." >&2
244 ask_bool 0 "Abort" && exit
245 return 0
246 }
247 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
248 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
249
250 [ "$conf_tar" != "-" ] || conf_tar=""
251
252 v "Saving config files..."
253 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
254 sed -i -e 's,^/,,' "$CONFFILES"
255 set -o pipefail
256 {
257 local ret=0
258
259 if [ $ret -eq 0 ]; then
260 for service in /etc/init.d/*; do
261 if ! $service enabled; then
262 disabled="$disabled$service disable\n"
263 fi
264 done
265 disabled="$disabled\nexit 0"
266 tar_print_member "/etc/uci-defaults/10_disable_services" "$(echo -e $disabled)" || ret=1
267 fi
268
269 # Part of archive with installed packages info
270 if [ $ret -eq 0 ]; then
271 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
272 # Format: pkg-name<TAB>{rom,overlay,unknown}
273 # rom is used for pkgs in /rom, even if updated later
274 tar_print_member "$INSTALLED_PACKAGES" "$(find /usr/lib/opkg/info -name "*.control" \( \
275 \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
276 \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
277 \( -exec echo {} unknown \; \) \
278 \) | sed -e 's,.*/,,;s/\.control /\t/')" || ret=1
279 fi
280 fi
281
282 # Rest of archive with config files and ending padding
283 if [ $ret -eq 0 ]; then
284 tar c${TAR_V} -C / -T "$CONFFILES" || ret=1
285 fi
286
287 [ $ret -eq 0 ]
288 } | gzip > "${conf_tar:-/proc/self/fd/1}"
289 err=$?
290 set +o pipefail
291
292 if [ "$err" -ne 0 ]; then
293 echo "Failed to create the configuration backup."
294 [ -f "$conf_tar" ] && rm -f "$conf_tar"
295 fi
296
297 rm -f "$CONFFILES"
298
299 return "$err"
300 }
301
302 if [ $CONF_BACKUP_LIST -eq 1 ]; then
303 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
304 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
305 cat "$CONFFILES"
306 rm -f "$CONFFILES"
307 exit 0
308 fi
309
310 if [ -n "$CONF_BACKUP" ]; then
311 create_backup_archive "$CONF_BACKUP"
312 exit
313 fi
314
315 if [ -n "$CONF_RESTORE" ]; then
316 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
317 echo "Backup archive '$CONF_RESTORE' not found." >&2
318 exit 1
319 fi
320
321 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
322 v "Restoring config files..."
323 if [ "$(type -t platform_restore_backup)" == 'platform_restore_backup' ]; then
324 platform_restore_backup "$TAR_V"
325 else
326 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
327 fi
328 exit $?
329 fi
330
331 type platform_check_image >/dev/null 2>/dev/null || {
332 echo "Firmware upgrade is not implemented for this platform." >&2
333 exit 1
334 }
335
336 case "$IMAGE" in
337 http://*|\
338 https://*)
339 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
340 IMAGE=/tmp/sysupgrade.img
341 ;;
342 esac
343
344 IMAGE="$(readlink -f "$IMAGE")"
345
346 case "$IMAGE" in
347 '')
348 echo "Image file not found." >&2
349 exit 1
350 ;;
351 /tmp/*) ;;
352 *)
353 v "Image not in /tmp, copying..."
354 cp -f "$IMAGE" /tmp/sysupgrade.img
355 IMAGE=/tmp/sysupgrade.img
356 ;;
357 esac
358
359 json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
360 echo "Failed to check image"
361 exit 1
362 }
363 json_get_var valid "valid"
364 [ "$valid" -eq 0 ] && {
365 if [ $FORCE -eq 1 ]; then
366 echo "Image check failed but --force given - will update anyway!" >&2
367 else
368 echo "Image check failed." >&2
369 exit 1
370 fi
371 }
372
373 if [ -n "$CONF_IMAGE" ]; then
374 case "$(get_magic_word $CONF_IMAGE cat)" in
375 # .gz files
376 1f8b) ;;
377 *)
378 echo "Invalid config file. Please use only .tar.gz files" >&2
379 exit 1
380 ;;
381 esac
382 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
383 export SAVE_CONFIG=1
384 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
385 [ $TEST -eq 1 ] || create_backup_archive "$CONF_TAR" || exit
386 export SAVE_CONFIG=1
387 else
388 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
389 export SAVE_CONFIG=0
390 fi
391
392 if [ $TEST -eq 1 ]; then
393 exit 0
394 fi
395
396 install_bin /sbin/upgraded
397 v "Commencing upgrade. Closing all shell sessions."
398
399 if [ -n "$FAILSAFE" ]; then
400 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
401 lock -u /tmp/.failsafe
402 else
403 json_init
404 json_add_string prefix "$RAM_ROOT"
405 json_add_string path "$IMAGE"
406 [ $FORCE -eq 1 ] && json_add_boolean force 1
407 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
408 json_add_string command "$COMMAND"
409 json_add_object options
410 json_add_int save_partitions "$SAVE_PARTITIONS"
411 json_close_object
412
413 ubus call system sysupgrade "$(json_dump)"
414 fi