0383d253a3230423163be013401134817e066377
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
1 #!/bin/sh
2
3 RAM_ROOT=/tmp/root
4
5 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
7
8 install_file() { # <file> [ <file> ... ]
9 for file in "$@"; do
10 dest="$RAM_ROOT/$file"
11 [ -f $file -a ! -f $dest ] && {
12 dir="$(dirname $dest)"
13 mkdir -p "$dir"
14 cp $file $dest
15 }
16 done
17 }
18
19 install_bin() { # <file> [ <symlink> ... ]
20 src=$1
21 files=$1
22 [ -x "$src" ] && files="$src $(libs $src)"
23 install_file $files
24 shift
25 for link in "$@"; do {
26 dest="$RAM_ROOT/$link"
27 dir="$(dirname $dest)"
28 mkdir -p "$dir"
29 [ -f "$dest" ] || ln -s $src $dest
30 }; done
31 }
32
33 supivot() { # <new_root> <old_root>
34 /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
35 mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
36 /bin/mount -o noatime,move /proc $1/proc && \
37 pivot_root $1 $1$2 || {
38 /bin/umount -l $1 $1
39 return 1
40 }
41
42 /bin/mount -o noatime,move $2/sys /sys
43 /bin/mount -o noatime,move $2/dev /dev
44 /bin/mount -o noatime,move $2/tmp /tmp
45 /bin/mount -o noatime,move $2/overlay /overlay 2>&-
46 return 0
47 }
48
49 run_ramfs() { # <command> [...]
50 install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
51 /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep \
52 /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd \
53 /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
54 /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
55 /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
56 /bin/rm /usr/bin/basename /bin/kill /bin/chmod
57
58 install_bin /bin/uclient-fetch /bin/wget
59 install_bin /sbin/mtd
60 install_bin /sbin/mount_root
61 install_bin /sbin/snapshot
62 install_bin /sbin/snapshot_tool
63 install_bin /usr/sbin/ubiupdatevol
64 install_bin /usr/sbin/ubiattach
65 install_bin /usr/sbin/ubiblock
66 install_bin /usr/sbin/ubiformat
67 install_bin /usr/sbin/ubidetach
68 install_bin /usr/sbin/ubirsvol
69 install_bin /usr/sbin/ubirmvol
70 install_bin /usr/sbin/ubimkvol
71 install_bin /usr/sbin/partx
72 for file in $RAMFS_COPY_BIN; do
73 install_bin ${file//:/ }
74 done
75 install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
76
77 [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
78
79 supivot $RAM_ROOT /mnt || {
80 echo "Failed to switch over to ramfs. Please reboot."
81 exit 1
82 }
83
84 /bin/mount -o remount,ro /mnt
85 /bin/umount -l /mnt
86
87 grep /overlay /proc/mounts > /dev/null && {
88 /bin/mount -o noatime,remount,ro /overlay
89 /bin/umount -l /overlay
90 }
91
92 # spawn a new shell from ramdisk to reduce the probability of cache issues
93 exec /bin/busybox ash -c "$*"
94 }
95
96 kill_remaining() { # [ <signal> ]
97 local sig="${1:-TERM}"
98 echo -n "Sending $sig to remaining processes ... "
99
100 local my_pid=$$
101 local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
102 local my_ppisupgraded=
103 grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
104 local my_ppisupgraded=1
105 }
106
107 local stat
108 for stat in /proc/[0-9]*/stat; do
109 [ -f "$stat" ] || continue
110
111 local pid name state ppid rest
112 read pid name state ppid rest < $stat
113 name="${name#(}"; name="${name%)}"
114
115 local cmdline
116 read cmdline < /proc/$pid/cmdline
117
118 # Skip kernel threads
119 [ -n "$cmdline" ] || continue
120
121 if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
122 # Running as init process, kill everything except me
123 if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
124 echo -n "$name "
125 kill -$sig $pid 2>/dev/null
126 fi
127 else
128 case "$name" in
129 # Skip essential services
130 *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;;
131
132 # Killable process
133 *)
134 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
135 echo -n "$name "
136 kill -$sig $pid 2>/dev/null
137 fi
138 ;;
139 esac
140 fi
141 done
142 echo ""
143 }
144
145 run_hooks() {
146 local arg="$1"; shift
147 for func in "$@"; do
148 eval "$func $arg"
149 done
150 }
151
152 ask_bool() {
153 local default="$1"; shift;
154 local answer="$default"
155
156 [ "$INTERACTIVE" -eq 1 ] && {
157 case "$default" in
158 0) echo -n "$* (y/N): ";;
159 *) echo -n "$* (Y/n): ";;
160 esac
161 read answer
162 case "$answer" in
163 y*) answer=1;;
164 n*) answer=0;;
165 *) answer="$default";;
166 esac
167 }
168 [ "$answer" -gt 0 ]
169 }
170
171 v() {
172 [ "$VERBOSE" -ge 1 ] && echo "$@"
173 }
174
175 rootfs_type() {
176 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
177 }
178
179 get_image() { # <source> [ <command> ]
180 local from="$1"
181 local conc="$2"
182 local cmd
183
184 case "$from" in
185 http://*|ftp://*) cmd="wget -O- -q";;
186 *) cmd="cat";;
187 esac
188 if [ -z "$conc" ]; then
189 local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
190 case "$magic" in
191 1f8b) conc="zcat";;
192 425a) conc="bzcat";;
193 esac
194 fi
195
196 eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
197 }
198
199 get_magic_word() {
200 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
201 }
202
203 get_magic_long() {
204 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
205 }
206
207 jffs2_copy_config() {
208 if grep rootfs_data /proc/mtd >/dev/null; then
209 # squashfs+jffs2
210 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
211 else
212 # jffs2
213 mtd jffs2write "$CONF_TAR" rootfs
214 fi
215 }
216
217 # Flash firmware to MTD partition
218 #
219 # $(1): path to image
220 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
221 default_do_upgrade() {
222 sync
223 if [ "$SAVE_CONFIG" -eq 1 ]; then
224 get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
225 else
226 get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
227 fi
228 }
229
230 do_upgrade() {
231 v "Performing system upgrade..."
232 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
233 platform_do_upgrade "$ARGV"
234 else
235 default_do_upgrade "$ARGV"
236 fi
237
238 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
239 platform_copy_config
240 fi
241
242 v "Upgrade completed"
243 [ -n "$DELAY" ] && sleep "$DELAY"
244 ask_bool 1 "Reboot" && {
245 v "Rebooting system..."
246 reboot -f
247 sleep 5
248 echo b 2>/dev/null >/proc/sysrq-trigger
249 }
250 }