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