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