add chaos_calmer branch
[15.05/openwrt.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/,/,' \
119                 -e '\,/META_[a-zA-Z0-9]*$,d' \
120                 -e '\,/functions.sh$,d' \
121                 -e '\,/[^/]*-opkg$,d' \
122         > "$file"
123         return 0
124 }
125
126 # hooks
127 sysupgrade_image_check="platform_check_image"
128 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
129         sysupgrade_init_conffiles="add_uci_conffiles" || \
130         sysupgrade_init_conffiles="add_overlayfiles"
131
132 include /lib/upgrade
133
134 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
135
136 do_save_conffiles() {
137         local conf_tar="${1:-$CONF_TAR}"
138
139         [ -z "$(rootfs_type)" ] && {
140                 echo "Cannot save config while running from ramdisk."
141                 ask_bool 0 "Abort" && exit
142                 return 0
143         }
144         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
145         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
146
147         v "Saving config files..."
148         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
149         tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
150
151         rm -f "$CONFFILES"
152 }
153
154 if [ $CONF_BACKUP_LIST -eq 1 ]; then
155         add_uci_conffiles "$CONFFILES"
156         cat "$CONFFILES"
157         rm -f "$CONFFILES"
158         exit 0
159 fi
160
161 if [ -n "$CONF_BACKUP" ]; then
162         do_save_conffiles "$CONF_BACKUP"
163         exit $?
164 fi
165
166 if [ -n "$CONF_RESTORE" ]; then
167         if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
168                 echo "Backup archive '$CONF_RESTORE' not found."
169                 exit 1
170         fi
171
172         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
173         tar -C / -x${TAR_V}zf "$CONF_RESTORE"
174         exit $?
175 fi
176
177 type platform_check_image >/dev/null 2>/dev/null || {
178         echo "Firmware upgrade is not implemented for this platform."
179         exit 1
180 }
181
182 for check in $sysupgrade_image_check; do
183         ( eval "$check \"\$ARGV\"" ) || {
184                 if [ $FORCE -eq 1 ]; then
185                         echo "Image check '$check' failed but --force given - will update anyway!"
186                         break
187                 else
188                         echo "Image check '$check' failed."
189                         exit 1
190                 fi
191         }
192 done
193
194 if [ -n "$CONF_IMAGE" ]; then
195         case "$(get_magic_word $CONF_IMAGE cat)" in
196                 # .gz files
197                 1f8b) ;;
198                 *)
199                         echo "Invalid config file. Please use only .tar.gz files"
200                         exit 1
201                 ;;
202         esac
203         get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
204         export SAVE_CONFIG=1
205 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
206         [ $TEST -eq 1 ] || do_save_conffiles
207         export SAVE_CONFIG=1
208 else
209         export SAVE_CONFIG=0
210 fi
211
212 if [ $TEST -eq 1 ]; then
213         exit 0
214 fi
215
216 run_hooks "" $sysupgrade_pre_upgrade
217
218 # Some platforms/devices may want different sysupgrade process, e.g. without
219 # killing processes yet or calling ubus system upgrade method.
220 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
221 # this point.
222 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
223         platform_pre_upgrade "$ARGV"
224 fi
225
226 ubus call system upgrade
227 touch /tmp/sysupgrade
228
229 if [ ! -f /tmp/failsafe ] ; then
230         kill_remaining TERM
231         sleep 3
232         kill_remaining KILL
233 fi
234
235 if [ -n "$(rootfs_type)" ]; then
236         v "Switching to ramdisk..."
237         run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
238 else
239         do_upgrade
240 fi