3 . /lib/functions/system.sh
6 RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
7 RAMFS_COPY_DATA="" # extra data files
8 export MTD_CONFIG_ARGS=""
15 export CONF_BACKUP_LIST=0
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;;
40 echo "Invalid option: $1"
48 export CONFFILES=/tmp/sysupgrade.conffiles
49 export CONF_TAR=/tmp/sysupgrade.tgz
54 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
56 Usage: $0 [<upgrade-option>...] <image file or URL>
57 $0 [-q] [-i] <backup-command> <file>
60 -d <delay> add a delay before rebooting
61 -f <config> restore configuration from .tar.gz (file or url)
63 -c attempt to preserve all changed files in /etc/
64 -n do not save configuration over reflash
66 Verify image and config .tar.gz but do not actually flash.
68 Flash image even if image checks fail, this is dangerous!
71 -h | --help display this help
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.
83 list the files that would be backed up when calling
84 sysupgrade -b. Does not create a backup file.
90 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
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.
98 # prevent messages from clobbering the tarball when using stdout
99 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
101 add_uci_conffiles() {
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"
112 if [ -d /overlay/upper ]; then
113 local overlaydir="/overlay/upper"
115 local overlaydir="/overlay"
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' \
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"
135 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
137 do_save_conffiles() {
138 local conf_tar="${1:-$CONF_TAR}"
140 [ -z "$(rootfs_type)" ] && {
141 echo "Cannot save config while running from ramdisk."
142 ask_bool 0 "Abort" && exit
145 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
146 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
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
155 if [ $CONF_BACKUP_LIST -eq 1 ]; then
156 add_uci_conffiles "$CONFFILES"
162 if [ -n "$CONF_BACKUP" ]; then
163 do_save_conffiles "$CONF_BACKUP"
167 if [ -n "$CONF_RESTORE" ]; then
168 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
169 echo "Backup archive '$CONF_RESTORE' not found."
173 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
174 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
178 type platform_check_image >/dev/null 2>/dev/null || {
179 echo "Firmware upgrade is not implemented for this platform."
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!"
189 echo "Image check '$check' failed."
195 if [ -n "$CONF_IMAGE" ]; then
196 case "$(get_magic_word $CONF_IMAGE cat)" in
200 echo "Invalid config file. Please use only .tar.gz files"
204 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
206 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
207 [ $TEST -eq 1 ] || do_save_conffiles
213 if [ $TEST -eq 1 ]; then
217 run_hooks "" $sysupgrade_pre_upgrade
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
223 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
224 platform_pre_upgrade "$ARGV"
227 ubus call system upgrade
228 touch /tmp/sysupgrade
230 if [ ! -f /tmp/failsafe ] ; then
236 if [ -n "$(rootfs_type)" ]; then
237 v "Switching to ramdisk..."
238 run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'