base-files: add sysupgrade -u to skip unchanged files
[openwrt/openwrt.git] / package / base-files / files / sbin / sysupgrade
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/system.sh
5
6 # initialize defaults
7 export MTD_CONFIG_ARGS=""
8 export INTERACTIVE=0
9 export VERBOSE=1
10 export SAVE_CONFIG=1
11 export SAVE_OVERLAY=0
12 export SAVE_PARTITIONS=1
13 export SKIP_UNCHANGED=0
14 export CONF_IMAGE=
15 export CONF_BACKUP_LIST=0
16 export CONF_BACKUP=
17 export CONF_RESTORE=
18 export NEED_IMAGE=
19 export HELP=0
20 export FORCE=0
21 export TEST=0
22
23 # parse options
24 while [ -n "$1" ]; do
25 case "$1" in
26 -i) export INTERACTIVE=1;;
27 -v) export VERBOSE="$(($VERBOSE + 1))";;
28 -q) export VERBOSE="$(($VERBOSE - 1))";;
29 -n) export SAVE_CONFIG=0;;
30 -c) export SAVE_OVERLAY=1;;
31 -p) export SAVE_PARTITIONS=0;;
32 -u) export SKIP_UNCHANGED=1;;
33 -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
34 -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
35 -l|--list-backup) export CONF_BACKUP_LIST=1;;
36 -f) export CONF_IMAGE="$2"; shift;;
37 -F|--force) export FORCE=1;;
38 -T|--test) export TEST=1;;
39 -h|--help) export HELP=1; break;;
40 -*)
41 echo "Invalid option: $1" >&2
42 exit 1
43 ;;
44 *) break;;
45 esac
46 shift;
47 done
48
49 export CONFFILES=/tmp/sysupgrade.conffiles
50 export CONF_TAR=/tmp/sysupgrade.tgz
51
52 IMAGE="$1"
53
54 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
55 cat <<EOF
56 Usage: $0 [<upgrade-option>...] <image file or URL>
57 $0 [-q] [-i] [-c] [-u] <backup-command> <file>
58
59 upgrade-option:
60 -f <config> restore configuration from .tar.gz (file or url)
61 -i interactive mode
62 -c attempt to preserve all changed files in /etc/
63 -u skip from backup files that are equal to those in /rom
64 -n do not save configuration over reflash
65 -p do not attempt to restore the partition table after flash.
66 -T | --test
67 Verify image and config .tar.gz but do not actually flash.
68 -F | --force
69 Flash image even if image checks fail, this is dangerous!
70 -q less verbose
71 -v more verbose
72 -h | --help display this help
73
74 backup-command:
75 -b | --create-backup <file>
76 create .tar.gz of files specified in sysupgrade.conf
77 then exit. Does not flash an image. If file is '-',
78 i.e. stdout, verbosity is set to 0 (i.e. quiet).
79 -r | --restore-backup <file>
80 restore a .tar.gz created with sysupgrade -b
81 then exit. Does not flash an image. If file is '-',
82 the archive is read from stdin.
83 -l | --list-backup
84 list the files that would be backed up when calling
85 sysupgrade -b. Does not create a backup file.
86
87 EOF
88 exit 1
89 }
90
91 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
92 cat <<-EOF
93 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
94 Do not specify both -b|-r and a firmware image.
95 EOF
96 exit 1
97 }
98
99 # prevent messages from clobbering the tarball when using stdout
100 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
101
102
103 list_conffiles() {
104 awk '
105 BEGIN { conffiles = 0 }
106 /^Conffiles:/ { conffiles = 1; next }
107 !/^ / { conffiles = 0; next }
108 conffiles == 1 { print }
109 ' /usr/lib/opkg/status
110 }
111
112 list_changed_conffiles() {
113 # Cannot handle spaces in filenames - but opkg cannot either...
114 list_conffiles | while read file csum; do
115 [ -r "$file" ] || continue
116
117 echo "${csum} ${file}" | sha256sum -sc - || echo "$file"
118 done
119 }
120
121 add_conffiles() {
122 local file="$1"
123 ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
124 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
125 \( -type f -o -type l \) $find_filter 2>/dev/null;
126 list_changed_conffiles ) | sort -u > "$file"
127 return 0
128 }
129
130 add_overlayfiles() {
131 local file="$1"
132 ( cd /overlay/upper/; find ./etc \( -type f -o -type l \) $find_filter | sed \
133 -e 's,^\.,,' \
134 -e '\,^/etc/board.json$,d' \
135 -e '\,/[^/]*-opkg$,d' \
136 -e '\,^/etc/urandom.seed$,d' \
137 )> "$file"
138 return 0
139 }
140
141 # hooks
142 sysupgrade_image_check="fwtool_check_signature fwtool_check_image platform_check_image"
143
144 if [ $SAVE_OVERLAY = 1 ]; then
145 [ ! -d /overlay/upper/etc ] && {
146 echo "Cannot find '/overlay/upper/etc', required for '-c'" >&2
147 exit 1
148 }
149 sysupgrade_init_conffiles="add_overlayfiles"
150 else
151 sysupgrade_init_conffiles="add_conffiles"
152 fi
153
154 find_filter=""
155 if [ $SKIP_UNCHANGED = 1 ]; then
156 [ ! -d /rom/ ] && {
157 echo "'/rom/' is required by '-u'"
158 exit 1
159 }
160 find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
161 fi
162
163 include /lib/upgrade
164
165 do_save_conffiles() {
166 local conf_tar="$1"
167
168 [ -z "$(rootfs_type)" ] && {
169 echo "Cannot save config while running from ramdisk." >&2
170 ask_bool 0 "Abort" && exit
171 rm -f "$conf_tar"
172 return 0
173 }
174 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
175 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
176
177 v "Saving config files..."
178 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
179 tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
180 if [ "$?" -ne 0 ]; then
181 echo "Failed to create the configuration backup."
182 rm -f "$conf_tar"
183 exit 1
184 fi
185
186 rm -f "$CONFFILES"
187 }
188
189 if [ $CONF_BACKUP_LIST -eq 1 ]; then
190 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
191 cat "$CONFFILES"
192 rm -f "$CONFFILES"
193 exit 0
194 fi
195
196 if [ -n "$CONF_BACKUP" ]; then
197 do_save_conffiles "$CONF_BACKUP"
198 exit $?
199 fi
200
201 if [ -n "$CONF_RESTORE" ]; then
202 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
203 echo "Backup archive '$CONF_RESTORE' not found." >&2
204 exit 1
205 fi
206
207 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
208 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
209 exit $?
210 fi
211
212 type platform_check_image >/dev/null 2>/dev/null || {
213 echo "Firmware upgrade is not implemented for this platform." >&2
214 exit 1
215 }
216
217 case "$IMAGE" in
218 http://*|\
219 https://*)
220 wget -O/tmp/sysupgrade.img "$IMAGE"
221 IMAGE=/tmp/sysupgrade.img
222 ;;
223 esac
224
225 IMAGE="$(readlink -f "$IMAGE")"
226
227 case "$IMAGE" in
228 '')
229 echo "Image file not found." >&2
230 exit 1
231 ;;
232 /tmp/*) ;;
233 *)
234 v "Image not in /tmp, copying..."
235 cp -f "$IMAGE" /tmp/sysupgrade.img
236 IMAGE=/tmp/sysupgrade.img
237 ;;
238 esac
239
240 export ARGV="$IMAGE"
241 export ARGC=1
242
243 for check in $sysupgrade_image_check; do
244 ( $check "$IMAGE" ) || {
245 if [ $FORCE -eq 1 ]; then
246 echo "Image check '$check' failed but --force given - will update anyway!" >&2
247 break
248 else
249 echo "Image check '$check' failed." >&2
250 exit 1
251 fi
252 }
253 done
254
255 if [ -n "$CONF_IMAGE" ]; then
256 case "$(get_magic_word $CONF_IMAGE cat)" in
257 # .gz files
258 1f8b) ;;
259 *)
260 echo "Invalid config file. Please use only .tar.gz files" >&2
261 exit 1
262 ;;
263 esac
264 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
265 export SAVE_CONFIG=1
266 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
267 [ $TEST -eq 1 ] || do_save_conffiles "$CONF_TAR"
268 export SAVE_CONFIG=1
269 else
270 [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
271 export SAVE_CONFIG=0
272 fi
273
274 if [ $TEST -eq 1 ]; then
275 exit 0
276 fi
277
278 if [ $SAVE_PARTITIONS -eq 0 ]; then
279 touch /tmp/sysupgrade.always.overwrite.bootdisk.partmap
280 else
281 rm -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap
282 fi
283
284 install_bin /sbin/upgraded
285 v "Commencing upgrade. Closing all shell sessions."
286
287 COMMAND='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
288
289 if [ -n "$FAILSAFE" ]; then
290 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
291 lock -u /tmp/.failsafe
292 else
293 ubus call system sysupgrade "{
294 \"prefix\": $(json_string "$RAM_ROOT"),
295 \"path\": $(json_string "$IMAGE"),
296 \"command\": $(json_string "$COMMAND")
297 }"
298 fi