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

Archive Download this file



interactive