base-files: regenerate the /lib64 symlink when switching to a 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.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 /bin/mkdir /bin/rmdir        \
59                 /bin/rm /usr/bin/basename /bin/kill /bin/chmod
60
61         install_bin /sbin/mtd
62         install_bin /sbin/ubi
63         install_bin /sbin/mount_root
64         install_bin /sbin/snapshot
65         install_bin /sbin/snapshot_tool
66         install_bin /usr/sbin/ubiupdatevol
67         install_bin /usr/sbin/ubiattach
68         install_bin /usr/sbin/ubiblock
69         install_bin /usr/sbin/ubiformat
70         install_bin /usr/sbin/ubidetach
71         install_bin /usr/sbin/ubirsvol
72         install_bin /usr/sbin/ubirmvol
73         install_bin /usr/sbin/ubimkvol
74         for file in $RAMFS_COPY_BIN; do
75                 install_bin ${file//:/ }
76         done
77         install_file /etc/resolv.conf /lib/functions.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 stat
103         for stat in /proc/[0-9]*/stat; do
104                 [ -f "$stat" ] || continue
105
106                 local pid name state ppid rest
107                 read pid name state ppid rest < $stat
108                 name="${name#(}"; name="${name%)}"
109
110                 local cmdline
111                 read cmdline < /proc/$pid/cmdline
112
113                 # Skip kernel threads
114                 [ -n "$cmdline" ] || continue
115
116                 case "$name" in
117                         # Skip essential services
118                         *procd*|*upgraded*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;;
119
120                         # Killable process
121                         *)
122                                 if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
123                                         echo -n "$name "
124                                         kill -$sig $pid 2>/dev/null
125                                 fi
126                         ;;
127                 esac
128         done
129         echo ""
130 }
131
132 run_hooks() {
133         local arg="$1"; shift
134         for func in "$@"; do
135                 eval "$func $arg"
136         done
137 }
138
139 ask_bool() {
140         local default="$1"; shift;
141         local answer="$default"
142
143         [ "$INTERACTIVE" -eq 1 ] && {
144                 case "$default" in
145                         0) echo -n "$* (y/N): ";;
146                         *) echo -n "$* (Y/n): ";;
147                 esac
148                 read answer
149                 case "$answer" in
150                         y*) answer=1;;
151                         n*) answer=0;;
152                         *) answer="$default";;
153                 esac
154         }
155         [ "$answer" -gt 0 ]
156 }
157
158 v() {
159         [ "$VERBOSE" -ge 1 ] && echo "$@"
160 }
161
162 rootfs_type() {
163         /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
164 }
165
166 get_image() { # <source> [ <command> ]
167         local from="$1"
168         local conc="$2"
169         local cmd
170
171         case "$from" in
172                 http://*|ftp://*) cmd="wget -O- -q";;
173                 *) cmd="cat";;
174         esac
175         if [ -z "$conc" ]; then
176                 local magic="$(eval $cmd $from 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
177                 case "$magic" in
178                         1f8b) conc="zcat";;
179                         425a) conc="bzcat";;
180                 esac
181         fi
182
183         eval "$cmd $from 2>/dev/null ${conc:+| $conc}"
184 }
185
186 get_magic_word() {
187         (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
188 }
189
190 get_magic_long() {
191         (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
192 }
193
194 jffs2_copy_config() {
195         if grep rootfs_data /proc/mtd >/dev/null; then
196                 # squashfs+jffs2
197                 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
198         else
199                 # jffs2
200                 mtd jffs2write "$CONF_TAR" rootfs
201         fi
202 }
203
204 default_do_upgrade() {
205         sync
206         if [ "$SAVE_CONFIG" -eq 1 ]; then
207                 get_image "$1" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
208         else
209                 get_image "$1" | mtd write - "${PART_NAME:-image}"
210         fi
211 }
212
213 do_upgrade() {
214         v "Performing system upgrade..."
215         if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
216                 platform_do_upgrade "$ARGV"
217         else
218                 default_do_upgrade "$ARGV"
219         fi
220
221         if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
222                 platform_copy_config
223         fi
224
225         v "Upgrade completed"
226         [ -n "$DELAY" ] && sleep "$DELAY"
227         ask_bool 1 "Reboot" && {
228                 v "Rebooting system..."
229                 reboot -f
230                 sleep 5
231                 echo b 2>/dev/null >/proc/sysrq-trigger
232         }
233 }