[backfire] backport r27630
[openwrt-10.03/.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 export HELP=0
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                 -f) export CONF_IMAGE="$2"; shift;;
23                 -h|--help) export HELP=1; break;;
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" -o $HELP -gt 0 ] && {
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         -n           do not save configuration over reflash
48         -q           less verbose
49         -v           more verbose
50         -h / --help  display this help
51
52 EOF
53         exit 1
54 }
55
56 add_uci_conffiles() {
57         local file="$1"
58         ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
59                 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
60                 -type f 2>/dev/null;
61           opkg list-changed-conffiles ) | sort -u > "$file"
62         return 0
63 }
64
65 # hooks
66 sysupgrade_image_check="platform_check_image"
67 sysupgrade_init_conffiles="add_uci_conffiles"
68
69 include /lib/upgrade
70
71 do_save_conffiles() {
72         [ -z "$(rootfs_type)" ] && {
73                 echo "Cannot save config while running from ramdisk."
74                 ask_bool 0 "Abort" && exit
75                 return 0
76         }
77         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
78         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
79
80         v "Saving config files..."
81         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
82         tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
83 }
84
85 type platform_check_image >/dev/null 2>/dev/null || {
86         echo "Firmware upgrade is not implemented for this platform."
87         exit 1
88 }
89
90 for check in $sysupgrade_image_check; do
91         ( eval "$check \"\$ARGV\"" ) || {
92                 echo "Image check '$check' failed."
93                 exit 1
94         }
95 done
96
97 if [ -n "$CONF_IMAGE" ]; then
98         case "$(get_magic_word $CONF_IMAGE cat)" in
99                 # .gz files
100                 1f8b) ;;
101                 *)
102                         echo "Invalid config file. Please use only .tar.gz files"
103                         exit 1
104                 ;;
105         esac
106         get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
107         export SAVE_CONFIG=1
108 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
109         do_save_conffiles
110         export SAVE_CONFIG=1
111 else
112         export SAVE_CONFIG=0
113 fi
114 run_hooks "" $sysupgrade_pre_upgrade
115
116 if [ -n "$(rootfs_type)" ]; then
117         v "Switching to ramdisk..."
118         run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
119 else
120         do_upgrade
121 fi