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