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