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