Root/package/base-files/files/sbin/sysupgrade

1#!/bin/sh
2. /etc/functions.sh
3
4# initialize defaults
5RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
6RAMFS_COPY_DATA="" # extra data files
7export INTERACTIVE=0
8export VERBOSE=1
9export SAVE_CONFIG=1
10export SAVE_OVERLAY=0
11export DELAY=
12export CONF_IMAGE=
13
14# parse options
15while [ -n "$1" ]; do
16    case "$1" in
17        -i) export INTERACTIVE=1;;
18        -d) export DELAY="$2"; shift;;
19        -v) export VERBOSE="$(($VERBOSE + 1))";;
20        -q) export VERBOSE="$(($VERBOSE - 1))";;
21        -n) export SAVE_CONFIG=0;;
22        -c) export SAVE_OVERLAY=1;;
23        -f) export CONF_IMAGE="$2"; shift;;
24        -*)
25            echo "Invalid option: $1"
26            exit 1
27        ;;
28        *) break;;
29    esac
30    shift;
31done
32
33export CONFFILES=/tmp/sysupgrade.conffiles
34export CONF_TAR=/tmp/sysupgrade.tgz
35
36export ARGV="$*"
37export ARGC="$#"
38
39[ -z "$ARGV" ] && {
40    cat <<EOF
41Usage: $0 [options] <image file or URL>
42
43Options:
44    -d <delay> add a delay before rebooting
45    -f <config> restore configuration from .tar.gz (file or url)
46    -i interactive mode
47    -c attempt to preserve all changed files in /etc/
48    -n do not save configuration over reflash
49    -q less verbose
50    -v more verbose
51
52EOF
53    exit 1
54}
55
56add_uci_conffiles() {
57    local file="$1"
58    ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
59        /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
60        -type f 2>/dev/null;
61      opkg list-changed-conffiles ) | sort -u > "$file"
62    return 0
63}
64
65add_overlayfiles() {
66    local file="$1"
67    find /overlay/etc/ -type f | sed \
68        -e 's,^/overlay/,/,' \
69        -e '\,/META_[a-zA-Z0-9]*$,d' \
70        -e '\,/functions.sh$,d' \
71        -e '\,/[^/]*-opkg$,d' \
72    > "$file"
73    return 0
74}
75
76# hooks
77sysupgrade_image_check="platform_check_image"
78[ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
79    sysupgrade_init_conffiles="add_uci_conffiles" || \
80    sysupgrade_init_conffiles="add_overlayfiles"
81
82include /lib/upgrade
83
84do_save_conffiles() {
85    [ -z "$(rootfs_type)" ] && {
86        echo "Cannot save config while running from ramdisk."
87        ask_bool 0 "Abort" && exit
88        return 0
89    }
90    run_hooks "$CONFFILES" $sysupgrade_init_conffiles
91    ask_bool 0 "Edit config file list" && vi "$CONFFILES"
92
93    v "Saving config files..."
94    [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
95    tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
96}
97
98type platform_check_image >/dev/null 2>/dev/null || {
99    echo "Firmware upgrade is not implemented for this platform."
100    exit 1
101}
102
103for check in $sysupgrade_image_check; do
104    ( eval "$check \"\$ARGV\"" ) || {
105        echo "Image check '$check' failed."
106        exit 1
107    }
108done
109
110if [ -n "$CONF_IMAGE" ]; then
111    case "$(get_magic_word $CONF_IMAGE cat)" in
112        # .gz files
113        1f8b) ;;
114        *)
115            echo "Invalid config file. Please use only .tar.gz files"
116            exit 1
117        ;;
118    esac
119    get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
120    export SAVE_CONFIG=1
121elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
122    do_save_conffiles
123    export SAVE_CONFIG=1
124else
125    export SAVE_CONFIG=0
126fi
127run_hooks "" $sysupgrade_pre_upgrade
128
129if [ -n "$(rootfs_type)" ]; then
130    v "Switching to ramdisk..."
131    run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
132else
133    do_upgrade
134fi
135

Archive Download this file



interactive