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