base-files: add an experimental "-c" flag which tries to preserve *all* changed files...
[openwrt/staging/mkresin.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 > "$file"
60 return 0
61 }
62
63 add_overlayfiles() {
64 local file="$1"
65 find /overlay/etc/ -type f | sed \
66 -e 's,^/overlay/,/,' \
67 -e '\,/META_[a-zA-Z0-9]*$,d' \
68 -e '\,/functions.sh$,d' \
69 -e '\,/[^/]*-opkg$,d' \
70 > "$file"
71 return 0
72 }
73
74 # hooks
75 sysupgrade_image_check="platform_check_image"
76 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
77 sysupgrade_init_conffiles="add_uci_conffiles" || \
78 sysupgrade_init_conffiles="add_overlayfiles"
79
80 include /lib/upgrade
81
82 do_save_conffiles() {
83 [ -z "$(rootfs_type)" ] && {
84 echo "Cannot save config while running from ramdisk."
85 ask_bool 0 "Abort" && exit
86 return 0
87 }
88 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
89 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
90
91 v "Saving config files..."
92 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
93 tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
94 }
95
96 type platform_check_image >/dev/null 2>/dev/null || {
97 echo "Firmware upgrade is not implemented for this platform."
98 exit 1
99 }
100
101 for check in $sysupgrade_image_check; do
102 ( eval "$check \"\$ARGV\"" ) || {
103 echo "Image check '$check' failed."
104 exit 1
105 }
106 done
107
108 if [ -n "$CONF_IMAGE" ]; then
109 case "$(get_magic_word $CONF_IMAGE cat)" in
110 # .gz files
111 1f8b) ;;
112 *)
113 echo "Invalid config file. Please use only .tar.gz files"
114 exit 1
115 ;;
116 esac
117 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
118 export SAVE_CONFIG=1
119 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
120 do_save_conffiles
121 export SAVE_CONFIG=1
122 else
123 export SAVE_CONFIG=0
124 fi
125 run_hooks "" $sysupgrade_pre_upgrade
126
127 if [ -n "$(rootfs_type)" ]; then
128 v "Switching to ramdisk..."
129 run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
130 else
131 do_upgrade
132 fi