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