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