ath5k: add an upstream patch to fix a memory leak
[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 /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump /bin/sleep /bin/zcat /usr/bin/bzcat
52 install_bin /sbin/mtd
53 for file in $RAMFS_COPY_BIN; do
54 install_bin $file
55 done
56 install_file /etc/resolv.conf /etc/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
57
58 pivot $RAM_ROOT /mnt || {
59 echo "Failed to switch over to ramfs. Please reboot."
60 exit 1
61 }
62
63 mount -o remount,ro /mnt
64 umount -l /mnt
65
66 grep /overlay /proc/mounts > /dev/null && {
67 mount -o remount,ro /overlay
68 umount -l /overlay
69 }
70
71 # spawn a new shell from ramdisk to reduce the probability of cache issues
72 exec /bin/busybox ash -c "$*"
73 }
74
75 run_hooks() {
76 local arg="$1"; shift
77 for func in "$@"; do
78 eval "$func $arg"
79 done
80 }
81
82 ask_bool() {
83 local default="$1"; shift;
84 local answer="$default"
85
86 [ "$INTERACTIVE" -eq 1 ] && {
87 case "$default" in
88 0) echo -n "$* (y/N): ";;
89 *) echo -n "$* (Y/n): ";;
90 esac
91 read answer
92 case "$answer" in
93 y*) answer=1;;
94 n*) answer=0;;
95 *) answer="$default";;
96 esac
97 }
98 [ "$answer" -gt 0 ]
99 }
100
101 v() {
102 [ "$VERBOSE" -ge 1 ] && echo "$@"
103 }
104
105 rootfs_type() {
106 mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
107 }
108
109 get_image() { # <source> [ <command> ]
110 local from="$1"
111 local conc="$2"
112 local cmd
113
114 case "$from" in
115 http://*|ftp://*) cmd="wget -O- -q";;
116 *) cmd="cat";;
117 esac
118 if [ -z "$conc" ]; then
119 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
120 case "$magic" in
121 1f8b) conc="zcat";;
122 425a) conc="bzcat";;
123 esac
124 fi
125
126 eval "$cmd $from ${conc:+| $conc}"
127 }
128
129 get_magic_word() {
130 get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
131 }
132
133 get_magic_long() {
134 get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
135 }
136
137 refresh_mtd_partitions() {
138 mtd refresh rootfs
139 }
140
141 jffs2_copy_config() {
142 if grep rootfs_data /proc/mtd >/dev/null; then
143 # squashfs+jffs2
144 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
145 else
146 # jffs2
147 mtd jffs2write "$CONF_TAR" rootfs
148 fi
149 }
150
151 default_do_upgrade() {
152 sync
153 if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
154 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
155 else
156 get_image "$1" | mtd write - "${PART_NAME:-image}"
157 fi
158 }
159
160 do_upgrade() {
161 v "Performing system upgrade..."
162 if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
163 platform_do_upgrade "$ARGV"
164 else
165 default_do_upgrade "$ARGV"
166 fi
167
168 [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
169 v "Refreshing partitions"
170 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
171 platform_refresh_partitions
172 else
173 refresh_mtd_partitions
174 fi
175 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
176 platform_copy_config
177 else
178 jffs2_copy_config
179 fi
180 }
181 v "Upgrade completed"
182 [ -n "$DELAY" ] && sleep "$DELAY"
183 ask_bool 1 "Reboot" && {
184 v "Rebooting system..."
185 reboot
186 sleep 5
187 echo b 2>/dev/null >/proc/sysrq-trigger
188 }
189 }
This page took 0.051934 seconds and 5 git commands to generate.