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