base-files: sysupgrade: include uci-defaults script disabling services
[openwrt/staging/stintel.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 mkdir -p $dir/etc/uci-defaults/
264 touch $dir/etc/uci-defaults/10_disable_services
265 for service in /etc/init.d/*; do
266 if ! $service enabled; then
267 echo "$service disable" >> $dir/etc/uci-defaults/10_disable_services
268 fi
269 done
270 echo "exit 0" >> $dir/etc/uci-defaults/10_disable_services
271 echo "/etc/uci-defaults/10_disable_services" >> "$CONFFILES"
272
273 v "Saving config files..."
274 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
275 sed -i -e 's,^/,,' "$CONFFILES"
276 tar c${TAR_V}zf "$conf_tar" -C "${dir:-/}" -T "$CONFFILES"
277 local err=$?
278 if [ "$err" -ne 0 ]; then
279 echo "Failed to create the configuration backup."
280 rm -f "$conf_tar"
281 fi
282
283 umount "$dir"
284 rm -rf "$tmp"
285
286 rm -f "$CONFFILES"
287
288 return "$err"
289 }
290
291 if [ $CONF_BACKUP_LIST -eq 1 ]; then
292 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
293 [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
294 cat "$CONFFILES"
295 rm -f "$CONFFILES"
296 exit 0
297 fi
298
299 if [ -n "$CONF_BACKUP" ]; then
300 do_save_conffiles "$CONF_BACKUP"
301 exit
302 fi
303
304 if [ -n "$CONF_RESTORE" ]; then
305 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
306 echo "Backup archive '$CONF_RESTORE' not found." >&2
307 exit 1
308 fi
309
310 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
311 v "Restoring config files..."
312 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
313 exit $?
314 fi
315
316 type platform_check_image >/dev/null 2>/dev/null || {
317 echo "Firmware upgrade is not implemented for this platform." >&2
318 exit 1
319 }
320
321 case "$IMAGE" in
322 http://*|\
323 https://*)
324 wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
325 IMAGE=/tmp/sysupgrade.img
326 ;;
327 esac
328
329 IMAGE="$(readlink -f "$IMAGE")"
330
331 case "$IMAGE" in
332 '')
333 echo "Image file not found." >&2
334 exit 1
335 ;;
336 /tmp/*) ;;
337 *)
338 v "Image not in /tmp, copying..."
339 cp -f "$IMAGE" /tmp/sysupgrade.img
340 IMAGE=/tmp/sysupgrade.img
341 ;;
342 esac
343
344 json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
345 echo "Failed to check image"
346 exit 1
347 }
348 json_get_var valid "valid"
349 [ "$valid" -eq 0 ] && {
350 if [ $FORCE -eq 1 ]; then
351 echo "Image check failed but --force given - will update anyway!" >&2
352 else
353 echo "Image check failed." >&2
354 exit 1
355 fi
356 }
357
358 if [ -n "$CONF_IMAGE" ]; then
359 case "$(get_magic_word $CONF_IMAGE cat)" in
360 # .gz files
361 1f8b) ;;
362 *)
363 echo "Invalid config file. Please use only .tar.gz files" >&2
364 exit 1
365 ;;
366 esac
367 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
368 export SAVE_CONFIG=1
369 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
370 [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR" || exit
371 export SAVE_CONFIG=1
372 else
373 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
374 export SAVE_CONFIG=0
375 fi
376
377 if [ $TEST -eq 1 ]; then
378 exit 0
379 fi
380
381 install_bin /sbin/upgraded
382 v "Commencing upgrade. Closing all shell sessions."
383
384 COMMAND='/lib/upgrade/do_stage2'
385
386 if [ -n "$FAILSAFE" ]; then
387 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
388 lock -u /tmp/.failsafe
389 else
390 json_init
391 json_add_string prefix "$RAM_ROOT"
392 json_add_string path "$IMAGE"
393 [ $FORCE -eq 1 ] && json_add_boolean force 1
394 [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
395 json_add_string command "$COMMAND"
396 json_add_object options
397 json_add_int save_partitions "$SAVE_PARTITIONS"
398 json_close_object
399
400 ubus call system sysupgrade "$(json_dump)"
401 fi