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