sysupgrade: run only one instance at a time.
[openwrt/staging/chunkeey.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 SAVE_PARTITIONS=1
14 export DELAY=
15 export CONF_IMAGE=
16 export CONF_BACKUP_LIST=0
17 export CONF_BACKUP=
18 export CONF_RESTORE=
19 export NEED_IMAGE=
20 export HELP=0
21 export FORCE=0
22 export TEST=0
23
24 # parse options
25 while [ -n "$1" ]; do
26 case "$1" in
27 -i) export INTERACTIVE=1;;
28 -d) export DELAY="$2"; shift;;
29 -v) export VERBOSE="$(($VERBOSE + 1))";;
30 -q) export VERBOSE="$(($VERBOSE - 1))";;
31 -n) export SAVE_CONFIG=0;;
32 -c) export SAVE_OVERLAY=1;;
33 -p) export SAVE_PARTITIONS=0;;
34 -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
35 -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
36 -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
37 -f) export CONF_IMAGE="$2"; shift;;
38 -F|--force) export FORCE=1;;
39 -T|--test) export TEST=1;;
40 -h|--help) export HELP=1; break;;
41 -*)
42 echo "Invalid option: $1"
43 exit 1
44 ;;
45 *) break;;
46 esac
47 shift;
48 done
49
50 # just one instance
51 if ! lock -n /tmp/sysupgrade.lock
52 then
53 echo "Another instance of sysupgrade already running"
54 echo "If you are sure this is wrong remove file /tmp/sysupgrade.lock"
55 exit 1
56 fi
57
58 # make sure the lock is removed when done even when killed
59 finish() {
60 lock -u /var/sysupgrade.lock
61 }
62 trap finish EXIT INT TERM
63
64 export CONFFILES=/tmp/sysupgrade.conffiles
65 export CONF_TAR=/tmp/sysupgrade.tgz
66
67 export ARGV="$*"
68 export ARGC="$#"
69
70 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
71 cat <<EOF
72 Usage: $0 [<upgrade-option>...] <image file or URL>
73 $0 [-q] [-i] <backup-command> <file>
74
75 upgrade-option:
76 -d <delay> add a delay before rebooting
77 -f <config> restore configuration from .tar.gz (file or url)
78 -i interactive mode
79 -c attempt to preserve all changed files in /etc/
80 -n do not save configuration over reflash
81 -p do not attempt to restore the partition table after flash.
82 -T | --test
83 Verify image and config .tar.gz but do not actually flash.
84 -F | --force
85 Flash image even if image checks fail, this is dangerous!
86 -q less verbose
87 -v more verbose
88 -h | --help display this help
89
90 backup-command:
91 -b | --create-backup <file>
92 create .tar.gz of files specified in sysupgrade.conf
93 then exit. Does not flash an image. If file is '-',
94 i.e. stdout, verbosity is set to 0 (i.e. quiet).
95 -r | --restore-backup <file>
96 restore a .tar.gz created with sysupgrade -b
97 then exit. Does not flash an image. If file is '-',
98 the archive is read from stdin.
99 -l | --list-backup
100 list the files that would be backed up when calling
101 sysupgrade -b. Does not create a backup file.
102
103 EOF
104 exit 1
105 }
106
107 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
108 cat <<-EOF
109 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
110 Do not specify both -b|-r and a firmware image.
111 EOF
112 exit 1
113 }
114
115 # prevent messages from clobbering the tarball when using stdout
116 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
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 opkg list-changed-conffiles ) | sort -u > "$file"
124 return 0
125 }
126
127 add_overlayfiles() {
128 local file="$1"
129 if [ -d /overlay/upper ]; then
130 local overlaydir="/overlay/upper"
131 else
132 local overlaydir="/overlay"
133 fi
134 find $overlaydir/etc/ -type f -o -type l | sed \
135 -e 's,^/overlay\/upper/,/,' \
136 -e 's,^/overlay/,/,' \
137 -e '\,/META_[a-zA-Z0-9]*$,d' \
138 -e '\,/functions.sh$,d' \
139 -e '\,/[^/]*-opkg$,d' \
140 > "$file"
141 return 0
142 }
143
144 # hooks
145 sysupgrade_image_check="fwtool_check_image platform_check_image"
146 sysupgrade_pre_upgrade="fwtool_pre_upgrade"
147 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
148 sysupgrade_init_conffiles="add_uci_conffiles" || \
149 sysupgrade_init_conffiles="add_overlayfiles"
150
151 include /lib/upgrade
152
153 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
154
155 do_save_conffiles() {
156 local conf_tar="${1:-$CONF_TAR}"
157
158 [ -z "$(rootfs_type)" ] && {
159 echo "Cannot save config while running from ramdisk."
160 ask_bool 0 "Abort" && exit
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 add_uci_conffiles "$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 for check in $sysupgrade_image_check; do
202 ( eval "$check \"\$ARGV\"" ) || {
203 if [ $FORCE -eq 1 ]; then
204 echo "Image check '$check' failed but --force given - will update anyway!"
205 break
206 else
207 echo "Image check '$check' failed."
208 exit 1
209 fi
210 }
211 done
212
213 if [ -n "$CONF_IMAGE" ]; then
214 case "$(get_magic_word $CONF_IMAGE cat)" in
215 # .gz files
216 1f8b) ;;
217 *)
218 echo "Invalid config file. Please use only .tar.gz files"
219 exit 1
220 ;;
221 esac
222 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
223 export SAVE_CONFIG=1
224 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
225 [ $TEST -eq 1 ] || do_save_conffiles
226 export SAVE_CONFIG=1
227 else
228 export SAVE_CONFIG=0
229 fi
230
231 if [ $TEST -eq 1 ]; then
232 exit 0
233 fi
234
235 run_hooks "" $sysupgrade_pre_upgrade
236
237 # Some platforms/devices may want different sysupgrade process, e.g. without
238 # killing processes yet or calling ubus system upgrade method.
239 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
240 # this point.
241 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
242 platform_pre_upgrade "$ARGV"
243 fi
244
245 ubus call system upgrade
246 touch /tmp/sysupgrade
247
248 if [ ! -f /tmp/failsafe ] ; then
249 kill_remaining TERM
250 sleep 3
251 kill_remaining KILL
252 fi
253
254 if [ -n "$(rootfs_type)" ]; then
255 v "Switching to ramdisk..."
256 run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
257 else
258 do_upgrade
259 fi