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