[package] base-files: kill remaining processes after running user hooks (#10461)
[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 SAVE_OVERLAY=0
11 export DELAY=
12 export CONF_IMAGE=
13 export HELP=0
14
15 # parse options
16 while [ -n "$1" ]; do 
17         case "$1" in
18                 -i) export INTERACTIVE=1;;
19                 -d) export DELAY="$2"; shift;;
20                 -v) export VERBOSE="$(($VERBOSE + 1))";;
21                 -q) export VERBOSE="$(($VERBOSE - 1))";;
22                 -n) export SAVE_CONFIG=0;;
23                 -c) export SAVE_OVERLAY=1;;
24                 -f) export CONF_IMAGE="$2"; shift;;
25                 -h|--help) export HELP=1; break;;
26                 -*)
27                         echo "Invalid option: $1"
28                         exit 1
29                 ;;
30                 *) break;;
31         esac
32         shift;
33 done
34
35 export CONFFILES=/tmp/sysupgrade.conffiles
36 export CONF_TAR=/tmp/sysupgrade.tgz
37
38 export ARGV="$*"
39 export ARGC="$#"
40
41 [ -z "$ARGV" -o $HELP -gt 0 ] && {
42         cat <<EOF
43 Usage: $0 [options] <image file or URL>
44
45 Options:
46         -d <delay>   add a delay before rebooting
47         -f <config>  restore configuration from .tar.gz (file or url)
48         -i           interactive mode
49         -c           attempt to preserve all changed files in /etc/
50         -n           do not save configuration over reflash
51         -q           less verbose
52         -v           more verbose
53         -h / --help  display this help
54
55 EOF
56         exit 1
57 }
58
59 add_uci_conffiles() {
60         local file="$1"
61         ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
62                 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
63                 -type f 2>/dev/null;
64           opkg list-changed-conffiles ) | sort -u > "$file"
65         return 0
66 }
67
68 add_overlayfiles() {
69         local file="$1"
70         find /overlay/etc/ -type f | sed \
71                 -e 's,^/overlay/,/,' \
72                 -e '\,/META_[a-zA-Z0-9]*$,d' \
73                 -e '\,/functions.sh$,d' \
74                 -e '\,/[^/]*-opkg$,d' \
75         > "$file"
76         return 0
77 }
78
79 # hooks
80 sysupgrade_image_check="platform_check_image"
81 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
82         sysupgrade_init_conffiles="add_uci_conffiles" || \
83         sysupgrade_init_conffiles="add_overlayfiles"
84
85 include /lib/upgrade
86
87 do_save_conffiles() {
88         [ -z "$(rootfs_type)" ] && {
89                 echo "Cannot save config while running from ramdisk."
90                 ask_bool 0 "Abort" && exit
91                 return 0
92         }
93         run_hooks "$CONFFILES" $sysupgrade_init_conffiles
94         ask_bool 0 "Edit config file list" && vi "$CONFFILES"
95
96         v "Saving config files..."
97         [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
98         tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
99 }
100
101 type platform_check_image >/dev/null 2>/dev/null || {
102         echo "Firmware upgrade is not implemented for this platform."
103         exit 1
104 }
105
106 for check in $sysupgrade_image_check; do
107         ( eval "$check \"\$ARGV\"" ) || {
108                 echo "Image check '$check' failed."
109                 exit 1
110         }
111 done
112
113 if [ -n "$CONF_IMAGE" ]; then
114         case "$(get_magic_word $CONF_IMAGE cat)" in
115                 # .gz files
116                 1f8b) ;;
117                 *)
118                         echo "Invalid config file. Please use only .tar.gz files"
119                         exit 1
120                 ;;
121         esac
122         get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
123         export SAVE_CONFIG=1
124 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
125         do_save_conffiles
126         export SAVE_CONFIG=1
127 else
128         export SAVE_CONFIG=0
129 fi
130
131 run_hooks "" $sysupgrade_pre_upgrade
132
133 kill_remaining TERM
134 sleep 3
135 kill_remaining KILL
136
137 if [ -n "$(rootfs_type)" ]; then
138         v "Switching to ramdisk..."
139         run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
140 else
141         do_upgrade
142 fi