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