base-files: provide a sysupgrade -r (--restore-backup) option as convenience wrapper...
[openwrt/openwrt.git] / package / base-files / files / sbin / sysupgrade
1 #!/bin/sh
2 . /lib/functions.sh
3
4 # initialize defaults
5 RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
6 RAMFS_COPY_DATA="" # extra data files
7 export INTERACTIVE=0
8 export VERBOSE=1
9 export SAVE_CONFIG=1
10 export SAVE_OVERLAY=0
11 export DELAY=
12 export CONF_IMAGE=
13 export CONF_BACKUP=
14 export CONF_RESTORE=
15 export HELP=0
16 export FORCE=0
17
18 # parse options
19 while [ -n "$1" ]; do
20 case "$1" in
21 -i) export INTERACTIVE=1;;
22 -d) export DELAY="$2"; shift;;
23 -v) export VERBOSE="$(($VERBOSE + 1))";;
24 -q) export VERBOSE="$(($VERBOSE - 1))";;
25 -n) export SAVE_CONFIG=0;;
26 -c) export SAVE_OVERLAY=1;;
27 -b|--create-backup) export CONF_BACKUP="$2"; shift;;
28 -r|--restore-backup) export CONF_RESTORE="$2"; shift;;
29 -f) export CONF_IMAGE="$2"; shift;;
30 -F|--force) export FORCE=1;;
31 -h|--help) export HELP=1; break;;
32 -*)
33 echo "Invalid option: $1"
34 exit 1
35 ;;
36 *) break;;
37 esac
38 shift;
39 done
40
41 export CONFFILES=/tmp/sysupgrade.conffiles
42 export CONF_TAR=/tmp/sysupgrade.tgz
43
44 export ARGV="$*"
45 export ARGC="$#"
46
47 [ -z "$ARGV" -a -z "$CONF_BACKUP" -o $HELP -gt 0 ] && {
48 cat <<EOF
49 Usage: $0 [options] <image file or URL>
50
51 Options:
52 -d <delay> add a delay before rebooting
53 -f <config> restore configuration from .tar.gz (file or url)
54 -i interactive mode
55 -c attempt to preserve all changed files in /etc/
56 -b / --create-backup <file>
57 create .tar.gz of files specified in sysupgrade.conf
58 then exit. Does not flash an image. If file is '-',
59 i.e. stdout, verbosity is set to 0 (i.e. quiet).
60 -r / --restore-backup <file>
61 restore a .tar.gz created with sysupgrade -b
62 then exit. Does not flash an image. If file is '-',
63 the archive is read from stdin.
64 -n do not save configuration over reflash
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 EOF
72 exit 1
73 }
74
75 [ -n "$ARGV" -a -n "$CONF_BACKUP" ] && {
76 cat <<-EOF
77 -b/--create-backup does not perform a firmware upgrade. Do not
78 specify both -b and a firmware image.
79 EOF
80 exit 1
81 }
82
83 # prevent messages from clobbering the tarball when using stdout
84 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
85
86 add_uci_conffiles() {
87 local file="$1"
88 ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
89 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
90 -type f 2>/dev/null;
91 opkg list-changed-conffiles ) | sort -u > "$file"
92 return 0
93 }
94
95 add_overlayfiles() {
96 local file="$1"
97 find /overlay/etc/ -type f | sed \
98 -e 's,^/overlay/,/,' \
99 -e '\,/META_[a-zA-Z0-9]*$,d' \
100 -e '\,/functions.sh$,d' \
101 -e '\,/[^/]*-opkg$,d' \
102 > "$file"
103 return 0
104 }
105
106 # hooks
107 sysupgrade_image_check="platform_check_image"
108 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
109 sysupgrade_init_conffiles="add_uci_conffiles" || \
110 sysupgrade_init_conffiles="add_overlayfiles"
111
112 include /lib/upgrade
113
114 do_save_conffiles() {
115 local conf_tar="${1:-$CONF_TAR}"
116
117 [ -z "$(rootfs_type)" ] && {
118 echo "Cannot save config while running from ramdisk."
119 ask_bool 0 "Abort" && exit
120 return 0
121 }
122 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
123 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
124
125 v "Saving config files..."
126 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
127 tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
128 }
129
130 if [ -n "$CONF_BACKUP" ]; then
131 do_save_conffiles "$CONF_BACKUP"
132 exit $?
133 fi
134
135 if [ -f "$CONF_RESTORE" ] || [ "$CONF_RESTORE" = "-" ]; then
136 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
137 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
138 exit $?
139 fi
140
141 type platform_check_image >/dev/null 2>/dev/null || {
142 echo "Firmware upgrade is not implemented for this platform."
143 exit 1
144 }
145
146 for check in $sysupgrade_image_check; do
147 ( eval "$check \"\$ARGV\"" ) || {
148 if [ $FORCE -eq 1 ]; then
149 echo "Image check '$check' failed but --force given - will update anyway!"
150 break
151 else
152 echo "Image check '$check' failed."
153 exit 1
154 fi
155 }
156 done
157
158 if [ -n "$CONF_IMAGE" ]; then
159 case "$(get_magic_word $CONF_IMAGE cat)" in
160 # .gz files
161 1f8b) ;;
162 *)
163 echo "Invalid config file. Please use only .tar.gz files"
164 exit 1
165 ;;
166 esac
167 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
168 export SAVE_CONFIG=1
169 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
170 do_save_conffiles
171 export SAVE_CONFIG=1
172 else
173 export SAVE_CONFIG=0
174 fi
175
176 run_hooks "" $sysupgrade_pre_upgrade
177
178 kill_remaining TERM
179 sleep 3
180 kill_remaining KILL
181
182 if [ -n "$(rootfs_type)" ]; then
183 v "Switching to ramdisk..."
184 run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
185 else
186 do_upgrade
187 fi