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