cc8047d988e39ca9ba27d2588744aad469d1d978
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / stage2
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . /lib/functions/system.sh
5
6 export IMAGE="$1"
7 COMMAND="$2"
8
9 export ARGV="$IMAGE"
10 export ARGC=1
11
12 export SAVE_CONFIG=1
13 export SAVE_PARTITIONS=1
14
15 export INTERACTIVE=0
16 export VERBOSE=1
17 export CONFFILES=/tmp/sysupgrade.conffiles
18 export CONF_TAR=/tmp/sysupgrade.tgz
19
20
21 [ -f "$CONF_TAR" ] || export SAVE_CONFIG=0
22 [ -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap ] && export SAVE_PARTITIONS=0
23
24 include /lib/upgrade
25
26
27 supivot() { # <new_root> <old_root>
28 /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
29 mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
30 /bin/mount -o noatime,move /proc $1/proc && \
31 pivot_root $1 $1$2 || {
32 /bin/umount -l $1 $1
33 return 1
34 }
35
36 /bin/mount -o noatime,move $2/sys /sys
37 /bin/mount -o noatime,move $2/dev /dev
38 /bin/mount -o noatime,move $2/tmp /tmp
39 /bin/mount -o noatime,move $2/overlay /overlay 2>&-
40 return 0
41 }
42
43 switch_to_ramfs() {
44 install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
45 /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep \
46 /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd \
47 /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
48 /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
49 /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
50 /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find \
51 /bin/mknod
52
53 install_bin /sbin/mtd
54 install_bin /sbin/mount_root
55 install_bin /sbin/snapshot
56 install_bin /sbin/snapshot_tool
57 install_bin /usr/sbin/ubiupdatevol
58 install_bin /usr/sbin/ubiattach
59 install_bin /usr/sbin/ubiblock
60 install_bin /usr/sbin/ubiformat
61 install_bin /usr/sbin/ubidetach
62 install_bin /usr/sbin/ubirsvol
63 install_bin /usr/sbin/ubirmvol
64 install_bin /usr/sbin/ubimkvol
65 install_bin /usr/sbin/partx
66 install_bin /usr/sbin/losetup
67 install_bin /usr/sbin/mkfs.ext4
68 for file in $RAMFS_COPY_BIN; do
69 install_bin ${file//:/ }
70 done
71 install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
72
73 [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
74
75 supivot $RAM_ROOT /mnt || {
76 echo "Failed to switch over to ramfs. Please reboot."
77 exit 1
78 }
79
80 /bin/mount -o remount,ro /mnt
81 /bin/umount -l /mnt
82
83 grep /overlay /proc/mounts > /dev/null && {
84 /bin/mount -o noatime,remount,ro /overlay
85 /bin/umount -l /overlay
86 }
87 }
88
89 kill_remaining() { # [ <signal> [ <loop> ] ]
90 local sig="${1:-TERM}"
91 local loop="${2:-0}"
92 local run=true
93 local stat
94
95 echo -n "Sending $sig to remaining processes ... "
96
97 while $run; do
98 run=false
99 for stat in /proc/[0-9]*/stat; do
100 [ -f "$stat" ] || continue
101
102 local pid name state ppid rest
103 read pid name state ppid rest < $stat
104 name="${name#(}"; name="${name%)}"
105
106 # Skip PID1, ourself and our children
107 [ $pid -ne 1 -a $pid -ne $$ -a $ppid -ne $$ ] || continue
108
109 local cmdline
110 read cmdline < /proc/$pid/cmdline
111
112 # Skip kernel threads
113 [ -n "$cmdline" ] || continue
114
115 echo -n "$name "
116 kill -$sig $pid 2>/dev/null
117
118 [ $loop -eq 1 ] && run=true
119 done
120 done
121 echo ""
122 }
123
124
125 killall -9 telnetd
126 killall -9 dropbear
127 killall -9 ash
128
129 kill_remaining TERM
130 sleep 3
131 kill_remaining KILL 1
132
133 sleep 1
134
135
136 if [ -n "$IMAGE" ] && type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
137 IS_PRE_UPGRADE=1 platform_pre_upgrade "$IMAGE"
138
139 # Needs to be unset again because of busybox weirdness ...
140 IS_PRE_UPGRADE=
141 fi
142
143 if [ -n "$(rootfs_type)" ]; then
144 echo "Switching to ramdisk..."
145 switch_to_ramfs
146 fi
147
148 # Exec new shell from ramfs
149 exec /bin/busybox ash -c "$COMMAND"