[package] base-files: sysupgrade: merge info from "opkg list-changed-conffiles" to...
[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
14 # parse options
15 while [ -n "$1" ]; do
16 case "$1" in
17 -i) export INTERACTIVE=1;;
18 -d) export DELAY="$2"; shift;;
19 -v) export VERBOSE="$(($VERBOSE + 1))";;
20 -q) export VERBOSE="$(($VERBOSE - 1))";;
21 -n) export SAVE_CONFIG=0;;
22 -c) export SAVE_OVERLAY=1;;
23 -f) export CONF_IMAGE="$2"; shift;;
24 -*)
25 echo "Invalid option: $1"
26 exit 1
27 ;;
28 *) break;;
29 esac
30 shift;
31 done
32
33 export CONFFILES=/tmp/sysupgrade.conffiles
34 export CONF_TAR=/tmp/sysupgrade.tgz
35
36 export ARGV="$*"
37 export ARGC="$#"
38
39 [ -z "$ARGV" ] && {
40 cat <<EOF
41 Usage: $0 [options] <image file or URL>
42
43 Options:
44 -d <delay> add a delay before rebooting
45 -f <config> restore configuration from .tar.gz (file or url)
46 -i interactive mode
47 -c attempt to preserve all changed files in /etc/
48 -n do not save configuration over reflash
49 -q less verbose
50 -v more verbose
51
52 EOF
53 exit 1
54 }
55
56 add_uci_conffiles() {
57 local file="$1"
58 ( find /etc/config /etc/passwd /etc/group /etc/dropbear \
59 /etc/firewall.user /etc/rc.local -type f;
60 opkg list-changed-conffiles ) | sort -u > "$file"
61 return 0
62 }
63
64 add_overlayfiles() {
65 local file="$1"
66 find /overlay/etc/ -type f | sed \
67 -e 's,^/overlay/,/,' \
68 -e '\,/META_[a-zA-Z0-9]*$,d' \
69 -e '\,/functions.sh$,d' \
70 -e '\,/[^/]*-opkg$,d' \
71 > "$file"
72 return 0
73 }
74
75 # hooks
76 sysupgrade_image_check="platform_check_image"
77 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
78 sysupgrade_init_conffiles="add_uci_conffiles" || \
79 sysupgrade_init_conffiles="add_overlayfiles"
80
81 include /lib/upgrade
82
83 do_save_conffiles() {
84 [ -z "$(rootfs_type)" ] && {
85 echo "Cannot save config while running from ramdisk."
86 ask_bool 0 "Abort" && exit
87 return 0
88 }
89 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
90 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
91
92 v "Saving config files..."
93 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
94 tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
95 }
96
97 type platform_check_image >/dev/null 2>/dev/null || {
98 echo "Firmware upgrade is not implemented for this platform."
99 exit 1
100 }
101
102 for check in $sysupgrade_image_check; do
103 ( eval "$check \"\$ARGV\"" ) || {
104 echo "Image check '$check' failed."
105 exit 1
106 }
107 done
108
109 if [ -n "$CONF_IMAGE" ]; then
110 case "$(get_magic_word $CONF_IMAGE cat)" in
111 # .gz files
112 1f8b) ;;
113 *)
114 echo "Invalid config file. Please use only .tar.gz files"
115 exit 1
116 ;;
117 esac
118 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
119 export SAVE_CONFIG=1
120 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
121 do_save_conffiles
122 export SAVE_CONFIG=1
123 else
124 export SAVE_CONFIG=0
125 fi
126 run_hooks "" $sysupgrade_pre_upgrade
127
128 if [ -n "$(rootfs_type)" ]; then
129 v "Switching to ramdisk..."
130 run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
131 else
132 do_upgrade
133 fi