base-files: sysupgrade: replace UMOUNT_ETCBACKUP_DIR with a local variable
[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 # 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' or '-o'" >&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
227 local umount_etcbackup_dir=0
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 if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
239 echo "${INSTALLED_PACKAGES}" >> "$CONFFILES"
240 mkdir -p "$ETCBACKUP_DIR"
241 # Avoid touching filesystem on each backup
242 RAMFS="$(mktemp -d -t sysupgrade.XXXXXX)"
243 mkdir -p "$RAMFS/upper" "$RAMFS/work"
244 mount -t overlay overlay -o lowerdir=$ETCBACKUP_DIR,upperdir=$RAMFS/upper,workdir=$RAMFS/work $ETCBACKUP_DIR &&
245 umount_etcbackup_dir=1 || {
246 echo "Cannot mount '$ETCBACKUP_DIR' as tmpfs to avoid touching disk while saving the list of installed packages." >&2
247 ask_bool 0 "Abort" && exit
248 }
249
250 # Format: pkg-name<TAB>{rom,overlay,unknown}
251 # rom is used for pkgs in /rom, even if updated later
252 find /usr/lib/opkg/info -name "*.control" \( \
253 \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
254 \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
255 \( -exec echo {} unknown \; \) \
256 \) | sed -e 's,.*/,,;s/\.control /\t/' > ${INSTALLED_PACKAGES}
257 fi
258
259 v "Saving config files..."
260 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
261 sed -i -e 's,^/,,' "$CONFFILES"
262 tar c${TAR_V}zf "$conf_tar" -C / -T "$CONFFILES"
263 local err=$?
264 if [ "$err" -ne 0 ]; then
265 echo "Failed to create the configuration backup."
266 rm -f "$conf_tar"
267 fi
268
269 [ "$umount_etcbackup_dir" -eq 1 ] && {
270 umount "$ETCBACKUP_DIR"
271 rm -rf "$RAMFS"
272 }
273 rm -f "$CONFFILES"
274
275 return "$err"
276 }
277
278 if [ $CONF_BACKUP_LIST -eq 1 ]; then
279 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
280 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
281 cat "$CONFFILES"
282 rm -f "$CONFFILES"
283 exit 0
284 fi
285
286 if [ -n "$CONF_BACKUP" ]; then
287 do_save_conffiles "$CONF_BACKUP"
288 exit
289 fi
290
291 if [ -n "$CONF_RESTORE" ]; then
292 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
293 echo "Backup archive '$CONF_RESTORE' not found." >&2
294 exit 1
295 fi
296
297 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
298 v "Restoring config files..."
299 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
300 exit $?
301 fi
302
303 type platform_check_image >/dev/null 2>/dev/null || {
304 echo "Firmware upgrade is not implemented for this platform." >&2
305 exit 1
306 }
307
308 case "$IMAGE" in
309 http://*|\
310 https://*)
311 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
312 IMAGE=/tmp/sysupgrade.img
313 ;;
314 esac
315
316 IMAGE="$(readlink -f "$IMAGE")"
317
318 case "$IMAGE" in
319 '')
320 echo "Image file not found." >&2
321 exit 1
322 ;;
323 /tmp/*) ;;
324 *)
325 v "Image not in /tmp, copying..."
326 cp -f "$IMAGE" /tmp/sysupgrade.img
327 IMAGE=/tmp/sysupgrade.img
328 ;;
329 esac
330
331 json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
332 echo "Failed to check image"
333 exit 1
334 }
335 json_get_var valid "valid"
336 [ "$valid" -eq 0 ] && {
337 if [ $FORCE -eq 1 ]; then
338 echo "Image check failed but --force given - will update anyway!" >&2
339 else
340 echo "Image check failed." >&2
341 exit 1
342 fi
343 }
344
345 if [ -n "$CONF_IMAGE" ]; then
346 case "$(get_magic_word $CONF_IMAGE cat)" in
347 # .gz files
348 1f8b) ;;
349 *)
350 echo "Invalid config file. Please use only .tar.gz files" >&2
351 exit 1
352 ;;
353 esac
354 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
355 export SAVE_CONFIG=1
356 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
357 [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR" || exit
358 export SAVE_CONFIG=1
359 else
360 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
361 export SAVE_CONFIG=0
362 fi
363
364 if [ $TEST -eq 1 ]; then
365 exit 0
366 fi
367
368 install_bin /sbin/upgraded
369 v "Commencing upgrade. Closing all shell sessions."
370
371 COMMAND='/lib/upgrade/do_stage2'
372
373 if [ -n "$FAILSAFE" ]; then
374 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
375 lock -u /tmp/.failsafe
376 else
377 json_init
378 json_add_string prefix "$RAM_ROOT"
379 json_add_string path "$IMAGE"
380 [ $FORCE -eq 1 ] && json_add_boolean force 1
381 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
382 json_add_string command "$COMMAND"
383 json_add_object options
384 json_add_int save_partitions "$SAVE_PARTITIONS"
385 json_close_object
386
387 ubus call system sysupgrade "$(json_dump)"
388 fi