Root/package/base-files/files/lib/upgrade/common.sh

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

Archive Download this file



interactive