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