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