base-files: properly fix copying ld*.so with eglibc
[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 $* | 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 /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \
52 /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
53 /bin/dd /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 /sbin/mtd
59 install_bin /sbin/ubi
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 for file in $RAMFS_COPY_BIN; do
72 install_bin ${file//:/ }
73 done
74 install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
75
76 [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
77
78 supivot $RAM_ROOT /mnt || {
79 echo "Failed to switch over to ramfs. Please reboot."
80 exit 1
81 }
82
83 /bin/mount -o remount,ro /mnt
84 /bin/umount -l /mnt
85
86 grep /overlay /proc/mounts > /dev/null && {
87 /bin/mount -o noatime,remount,ro /overlay
88 /bin/umount -l /overlay
89 }
90
91 # spawn a new shell from ramdisk to reduce the probability of cache issues
92 exec /bin/busybox ash -c "$*"
93 }
94
95 kill_remaining() { # [ <signal> ]
96 local sig="${1:-TERM}"
97 echo -n "Sending $sig to remaining processes ... "
98
99 local my_pid=$$
100 local my_ppid=$(cut -d' ' -f4 /proc/$my_pid/stat)
101 local my_ppisupgraded=
102 grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
103 local my_ppisupgraded=1
104 }
105
106 local stat
107 for stat in /proc/[0-9]*/stat; do
108 [ -f "$stat" ] || continue
109
110 local pid name state ppid rest
111 read pid name state ppid rest < $stat
112 name="${name#(}"; name="${name%)}"
113
114 local cmdline
115 read cmdline < /proc/$pid/cmdline
116
117 # Skip kernel threads
118 [ -n "$cmdline" ] || continue
119
120 if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
121 # Running as init process, kill everything except me
122 if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
123 echo -n "$name "
124 kill -$sig $pid 2>/dev/null
125 fi
126 else
127 case "$name" in
128 # Skip essential services
129 *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;;
130
131 # Killable process
132 *)
133 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
134 echo -n "$name "
135 kill -$sig $pid 2>/dev/null
136 fi
137 ;;
138 esac
139 fi
140 done
141 echo ""
142 }
143
144 run_hooks() {
145 local arg="$1"; shift
146 for func in "$@"; do
147 eval "$func $arg"
148 done
149 }
150
151 ask_bool() {
152 local default="$1"; shift;
153 local answer="$default"
154
155 [ "$INTERACTIVE" -eq 1 ] && {
156 case "$default" in
157 0) echo -n "$* (y/N): ";;
158 *) echo -n "$* (Y/n): ";;
159 esac
160 read answer
161 case "$answer" in
162 y*) answer=1;;
163 n*) answer=0;;
164 *) answer="$default";;
165 esac
166 }
167 [ "$answer" -gt 0 ]
168 }
169
170 v() {
171 [ "$VERBOSE" -ge 1 ] && echo "$@"
172 }
173
174 rootfs_type() {
175 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
176 }
177
178 get_image() { # <source> [ <command> ]
179 local from="$1"
180 local conc="$2"
181 local cmd
182
183 case "$from" in
184 http://*|ftp://*) cmd="wget -O- -q";;
185 *) cmd="cat";;
186 esac
187 if [ -z "$conc" ]; then
188 local magic="$(eval $cmd $from 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
189 case "$magic" in
190 1f8b) conc="zcat";;
191 425a) conc="bzcat";;
192 esac
193 fi
194
195 eval "$cmd $from 2>/dev/null ${conc:+| $conc}"
196 }
197
198 get_magic_word() {
199 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
200 }
201
202 get_magic_long() {
203 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
204 }
205
206 jffs2_copy_config() {
207 if grep rootfs_data /proc/mtd >/dev/null; then
208 # squashfs+jffs2
209 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
210 else
211 # jffs2
212 mtd jffs2write "$CONF_TAR" rootfs
213 fi
214 }
215
216 default_do_upgrade() {
217 sync
218 if [ "$SAVE_CONFIG" -eq 1 ]; then
219 get_image "$1" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
220 else
221 get_image "$1" | mtd write - "${PART_NAME:-image}"
222 fi
223 }
224
225 do_upgrade() {
226 v "Performing system upgrade..."
227 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
228 platform_do_upgrade "$ARGV"
229 else
230 default_do_upgrade "$ARGV"
231 fi
232
233 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
234 platform_copy_config
235 fi
236
237 v "Upgrade completed"
238 [ -n "$DELAY" ] && sleep "$DELAY"
239 ask_bool 1 "Reboot" && {
240 v "Rebooting system..."
241 reboot -f
242 sleep 5
243 echo b 2>/dev/null >/proc/sysrq-trigger
244 }
245 }