base-files/sysupgrade: add more busybox applet symlinks to ramdisk
[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 run_hooks() {
81         local arg="$1"; shift
82         for func in "$@"; do
83                 eval "$func $arg"
84         done
85 }
86
87 ask_bool() {
88         local default="$1"; shift;
89         local answer="$default"
90
91         [ "$INTERACTIVE" -eq 1 ] && {
92                 case "$default" in
93                         0) echo -n "$* (y/N): ";;
94                         *) echo -n "$* (Y/n): ";;
95                 esac
96                 read answer
97                 case "$answer" in
98                         y*) answer=1;;
99                         n*) answer=0;;
100                         *) answer="$default";;
101                 esac
102         }
103         [ "$answer" -gt 0 ]
104 }
105
106 v() {
107         [ "$VERBOSE" -ge 1 ] && echo "$@"
108 }
109
110 rootfs_type() {
111         mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
112 }
113
114 get_image() { # <source> [ <command> ]
115         local from="$1"
116         local conc="$2"
117         local cmd
118
119         case "$from" in
120                 http://*|ftp://*) cmd="wget -O- -q";;
121                 *) cmd="cat";;
122         esac
123         if [ -z "$conc" ]; then
124                 local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
125                 case "$magic" in
126                         1f8b) conc="zcat";;
127                         425a) conc="bzcat";;
128                 esac
129         fi
130
131         eval "$cmd $from ${conc:+| $conc}"
132 }
133
134 get_magic_word() {
135         get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
136 }
137
138 get_magic_long() {
139         get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
140 }
141
142 refresh_mtd_partitions() {
143         mtd refresh rootfs
144 }
145
146 jffs2_copy_config() {
147         if grep rootfs_data /proc/mtd >/dev/null; then
148                 # squashfs+jffs2
149                 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
150         else
151                 # jffs2
152                 mtd jffs2write "$CONF_TAR" rootfs
153         fi
154 }
155
156 default_do_upgrade() {
157         sync
158         if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
159                 get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
160         else
161                 get_image "$1" | mtd write - "${PART_NAME:-image}"
162         fi
163 }
164
165 do_upgrade() {
166         v "Performing system upgrade..."
167         if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
168                 platform_do_upgrade "$ARGV"
169         else
170                 default_do_upgrade "$ARGV"
171         fi
172
173         [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
174                 v "Refreshing partitions"
175                 if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
176                         platform_refresh_partitions
177                 else
178                         refresh_mtd_partitions
179                 fi
180                 if type 'platform_copy_config' >/dev/null 2>/dev/null; then
181                         platform_copy_config
182                 else
183                         jffs2_copy_config
184                 fi
185         }
186         v "Upgrade completed"
187         [ -n "$DELAY" ] && sleep "$DELAY"
188         ask_bool 1 "Reboot" && {
189                 v "Rebooting system..."
190                 reboot -f
191                 sleep 5
192                 echo b 2>/dev/null >/proc/sysrq-trigger
193         }
194 }