base-files: Fix sysupgrade overlay saving
[lede.git] / package / base-files / files / sbin / sysupgrade
1 #!/bin/sh
2 . /lib/functions.sh
3 . /lib/functions/system.sh
4
5 # initialize defaults
6 RAMFS_COPY_BIN=""       # extra programs for temporary ramfs root
7 RAMFS_COPY_DATA=""      # extra data files
8 export MTD_CONFIG_ARGS=""
9 export INTERACTIVE=0
10 export VERBOSE=1
11 export SAVE_CONFIG=1
12 export SAVE_OVERLAY=0
13 export DELAY=
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                 -d) export DELAY="$2"; shift;;
28                 -v) export VERBOSE="$(($VERBOSE + 1))";;
29                 -q) export VERBOSE="$(($VERBOSE - 1))";;
30                 -n) export SAVE_CONFIG=0;;
31                 -c) export SAVE_OVERLAY=1;;
32                 -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
33                 -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
34                 -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
35                 -f) export CONF_IMAGE="$2"; shift;;
36                 -F|--force) export FORCE=1;;
37                 -T|--test) export TEST=1;;
38                 -h|--help) export HELP=1; break;;
39                 -*)
40                         echo "Invalid option: $1"
41                         exit 1
42                 ;;
43                 *) break;;
44         esac
45         shift;
46 done
47
48 export CONFFILES=/tmp/sysupgrade.conffiles
49 export CONF_TAR=/tmp/sysupgrade.tgz
50
51 export ARGV="$*"
52 export ARGC="$#"
53
54 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
55         cat <<EOF
56 Usage: $0 [<upgrade-option>...] <image file or URL>
57        $0 [-q] [-i] <backup-command> <file>
58
59 upgrade-option:
60         -d <delay>   add a delay before rebooting
61         -f <config>  restore configuration from .tar.gz (file or url)
62         -i           interactive mode
63         -c           attempt to preserve all changed files in /etc/
64         -n           do not save configuration over reflash
65         -T | --test
66                      Verify image and config .tar.gz but do not actually flash.
67         -F | --force
68                      Flash image even if image checks fail, this is dangerous!
69         -q           less verbose
70         -v           more verbose
71         -h | --help  display this help
72
73 backup-command:
74         -b | --create-backup <file>
75                      create .tar.gz of files specified in sysupgrade.conf
76                      then exit. Does not flash an image. If file is '-',
77                      i.e. stdout, verbosity is set to 0 (i.e. quiet).
78         -r | --restore-backup <file>
79                      restore a .tar.gz created with sysupgrade -b
80                      then exit. Does not flash an image. If file is '-',
81                      the archive is read from stdin.
82         -l | --list-backup
83                      list the files that would be backed up when calling
84                      sysupgrade -b. Does not create a backup file.
85
86 EOF
87         exit 1
88 }
89
90 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
91         cat <<-EOF
92                 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
93                 Do not specify both -b|-r and a firmware image.
94         EOF
95         exit 1
96 }
97
98 # prevent messages from clobbering the tarball when using stdout
99 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
100
101 add_uci_conffiles() {
102         local file="$1"
103         ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
104                 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
105                 -type f -o -type l 2>/dev/null;
106           opkg list-changed-conffiles ) | sort -u > "$file"
107         return 0
108 }
109
110 add_overlayfiles() {
111         local file="$1"
112         if [ -d /overlay/upper ]; then
113                 local overlaydir="/overlay/upper"
114         else
115                 local overlaydir="/overlay"
116         fi
117         find $overlaydir/etc/ -type f -o -type l | sed \
118                 -e 's,^/overlay\/upper/,/,' \
119                 -e 's,^/overlay/,/,' \
120                 -e '\,/META_[a-zA-Z0-9]*$,d' \
121                 -e '\,/functions.sh$,d' \
122                 -e '\,/[^/]*-opkg$,d' \
123         > "$file"
124         return 0
125 }
126
127 # hooks
128 sysupgrade_image_check="platform_check_image"
129 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
130         sysupgrade_init_conffiles="add_uci_conffiles" || \
131         sysupgrade_init_conffiles="add_overlayfiles"
132
133 include /lib/upgrade
134
135 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
136
137 do_save_conffiles() {
138         local conf_tar="${1:-$CONF_TAR}"
139
140         [ -z "$(rootfs_type)" ] && {
141                 echo "Cannot save config while running from ramdisk."
142                 ask_bool 0 "Abort" && exit
143                 return 0
144         }
145         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
146         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
147
148         v "Saving config files..."
149         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
150         tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
151
152         rm -f "$CONFFILES"
153 }
154
155 if [ $CONF_BACKUP_LIST -eq 1 ]; then
156         add_uci_conffiles "$CONFFILES"
157         cat "$CONFFILES"
158         rm -f "$CONFFILES"
159         exit 0
160 fi
161
162 if [ -n "$CONF_BACKUP" ]; then
163         do_save_conffiles "$CONF_BACKUP"
164         exit $?
165 fi
166
167 if [ -n "$CONF_RESTORE" ]; then
168         if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
169                 echo "Backup archive '$CONF_RESTORE' not found."
170                 exit 1
171         fi
172
173         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
174         tar -C / -x${TAR_V}zf "$CONF_RESTORE"
175         exit $?
176 fi
177
178 type platform_check_image >/dev/null 2>/dev/null || {
179         echo "Firmware upgrade is not implemented for this platform."
180         exit 1
181 }
182
183 for check in $sysupgrade_image_check; do
184         ( eval "$check \"\$ARGV\"" ) || {
185                 if [ $FORCE -eq 1 ]; then
186                         echo "Image check '$check' failed but --force given - will update anyway!"
187                         break
188                 else
189                         echo "Image check '$check' failed."
190                         exit 1
191                 fi
192         }
193 done
194
195 if [ -n "$CONF_IMAGE" ]; then
196         case "$(get_magic_word $CONF_IMAGE cat)" in
197                 # .gz files
198                 1f8b) ;;
199                 *)
200                         echo "Invalid config file. Please use only .tar.gz files"
201                         exit 1
202                 ;;
203         esac
204         get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
205         export SAVE_CONFIG=1
206 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
207         [ $TEST -eq 1 ] || do_save_conffiles
208         export SAVE_CONFIG=1
209 else
210         export SAVE_CONFIG=0
211 fi
212
213 if [ $TEST -eq 1 ]; then
214         exit 0
215 fi
216
217 run_hooks "" $sysupgrade_pre_upgrade
218
219 # Some platforms/devices may want different sysupgrade process, e.g. without
220 # killing processes yet or calling ubus system upgrade method.
221 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
222 # this point.
223 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
224         platform_pre_upgrade "$ARGV"
225 fi
226
227 ubus call system upgrade
228 touch /tmp/sysupgrade
229
230 if [ ! -f /tmp/failsafe ] ; then
231         kill_remaining TERM
232         sleep 3
233         kill_remaining KILL
234 fi
235
236 if [ -n "$(rootfs_type)" ]; then
237         v "Switching to ramdisk..."
238         run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
239 else
240         do_upgrade
241 fi