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