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