add switch to not save configuration over the reflash in noninteractive mode
[openwrt.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 DELAY=
11
12 # parse options
13 while [ -n "$1" ]; do 
14         case "$1" in
15                 -i) export INTERACTIVE=1;;
16                 -d) export DELAY="$2"; shift;;
17                 -v) export VERBOSE="$(($VERBOSE + 1))";;
18                 -q) export VERBOSE="$(($VERBOSE - 1))";;
19                 -n) export SAVE_CONFIG=0
20                 -*)
21                         echo "Invalid option: $1"
22                         exit 1
23                 ;;
24                 *) break;;
25         esac
26         shift;
27 done
28
29 export CONFFILES=/tmp/sysupgrade.conffiles
30 export CONF_TAR=/tmp/sysupgrade.tgz
31
32 export ARGV="$*"
33 export ARGC="$#"
34
35 [ -z "$ARGV" ] && {
36         cat <<EOF
37 Usage: $0 [options] <image file or URL>
38
39 Options:
40         -d <delay>   add a delay before rebooting
41         -i           interactive mode
42         -n           do not save configuration over reflash
43         -q           less verbose
44         -v           more verbose
45
46 EOF
47         exit 1
48 }
49
50 add_uci_conffiles() {
51         local file="$1"
52         find /etc/config /etc/passwd /etc/group /etc/dropbear /etc/firewall.user > "$file"
53         return 0
54 }
55
56 # hooks
57 sysupgrade_image_check="platform_check_image"
58 sysupgrade_init_conffiles="add_uci_conffiles"
59
60 include /lib/upgrade
61
62 do_save_conffiles() {
63         [ -z "$(rootfs_type)" ] && {
64                 echo "Cannot save config while running from ramdisk."
65                 ask_bool 0 "Abort" && exit
66                 return 0
67         }
68         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
69         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
70
71         v "Saving config files..."
72         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
73         tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
74 }
75
76 type platform_check_image >/dev/null 2>/dev/null || {
77         echo "Firmware upgrade is not implemented for this platform."
78         exit 1
79 }
80
81 for check in $sysupgrade_image_check; do
82         ( eval "$check \"\$ARGV\"" ) || {
83                 echo "Image check '$check' failed."
84                 exit 1
85         }
86 done
87
88 if ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
89         do_save_conffiles
90         export SAVE_CONFIG=1
91 else
92         export SAVE_CONFIG=0
93 fi
94 run_hooks "" $sysupgrade_pre_upgrade
95
96 v "Switching to ramdisk..."
97 run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'