全部博文(685)
分类: LINUX
2014-11-27 18:35:58
sysupgrade [<升级选项>...]
sysupgrade [-q] [-i] <备份选项>
更新openwrt.bin固件
sysupgrade openwrt.bin
强制更新openwrt.bin固件
sysupgrade会检查支持板子的固件头信息,如果一个model没有在sysupgrade的支持列表里,使用-F来忽略检查失败,强制烧写。
sysupgrade -F openwrt.bin
更新后不保存之前的配置
sysupgrade烧写时默认会备份配置文件,在烧写后把配置文件覆盖到新系统中。-n参数指定不做这个动作。
sysupgrade -n openwrt.bin
备份配置文件到/tmp/backup.tgz
sysupgrade -b /tmp/backup.tgz
恢复之前备份的/tmp/backup.tgz
sysupgrade -r /tmp/backup.tgz
列出会被备份的文件
sysupgrade -l
列出的文件会在-b备份时或系统升级时被保存。
# parse options while [ -n "$1" ]; do case "$1" in -i) export INTERACTIVE=1;; -d) export DELAY="$2"; shift;;
-v) export VERBOSE="$(($VERBOSE + 1))";;
-q) export VERBOSE="$(($VERBOSE - 1))";;
-n) export SAVE_CONFIG=0;;
-c) export SAVE_OVERLAY=1;;
-b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
-r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;; -l|--list-backup) export CONF_BACKUP_LIST=1; break;; -f) export CONF_IMAGE="$2"; shift;;
-F|--force) export FORCE=1;;
-T|--test) export TEST=1;;
-h|--help) export HELP=1; break;;
-*) echo "Invalid option: $1" exit 1 ;;
*) break;; esac shift; done export CONFFILES=/tmp/sysupgrade.conffiles export CONF_TAR=/tmp/sysupgrade.tgz export ARGV="$*" export ARGC="$#"
解析选项。ARGV是参数列表,ARGC是参数个数。
选项中有-d, -b, -r, -f时,由于这些选项都要带一个参数,所以使用了shift去减少ARGV和ARGC的值。
CONFFILES 和 CONF_TAR 是两个临时文件,后面会用到。
sysupgrade openwrt.bin --> ARGV="openwrt.bin", ARGC=1
sysupgrade -b config.backup --> ARGV为空,ARGC=0
[ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
cat <<-EOF
-b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
Do not specify both -b|-r and a firmware image.
EOF exit 1 }
如果sysupgrade附带参数-b或-r时,则$NEED_IMAGE=1,否则为空
当$NEED_IMAGE=1时,我们希望ARGV是空的,否则就是出错,则输出帮助信息,并退出。
例如:
# hooks sysupgrade_image_check="platform_check_image" [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \ sysupgrade_init_conffiles="add_uci_conffiles" || \ sysupgrade_init_conffiles="add_overlayfiles"
带-c参数,且"/overlay/etc"目录存在 --> sysupgrade_init_conffiles="add_overlayfiles"
否则 --> sysupgrade_init_conffiles="add_uci_conffiles"
这里会影响要备份的配置文件
include /lib/upgrade
包含lib/upgrade目录下的所有文件
[ "$1" = "nand" ] && nand_upgrade_stage2 $@
命令指定nand时,则调用nand_upgrade_stage2函数,例如sysupgrade nand openwrt.bin。 暂时使用spi flash,不讨论这里。
add_uci_conffiles() {
local file="$1" ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
/etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
-type f 2>/dev/null;
opkg list-changed-conffiles ) | sort -u > "$file" return 0 } if [ $CONF_BACKUP_LIST -eq 1 ]; then add_uci_conffiles "$CONFFILES" cat "$CONFFILES" rm -f "$CONFFILES" exit 0 fi
列出一份文件列表,放入/tmp/sysupgrade.conffiles,打印出来,然后删掉。文件列表:
find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null opkg list-changed-conffiles
do_save_conffiles() {
local conf_tar="${1:-$CONF_TAR}" [ -z "$(rootfs_type)" ] && { echo "Cannot save config while running from ramdisk." ask_bool 0 "Abort" && exit return 0 }
run_hooks "$CONFFILES" $sysupgrade_init_conffiles ask_bool 0 "Edit config file list" && vi "$CONFFILES" v "Saving config files..." [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V="" tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
rm -f "$CONFFILES" } if [ -n "$CONF_BACKUP" ]; then do_save_conffiles "$CONF_BACKUP" exit $? fi
-b 如果指定打包文件时,$CONF_BACKUP 为那个文件名。则此时按如下流程来生成备份文件:
do_save_conffiles
-> sysupgrade_init_conffiles -> add_uci_conffiles -> tar czf /tmp/backup.tgz -T /tmp/sysupgrade.conffiles -> rm -f /tmp/sysupgrade.conffiles
if [ -n "$CONF_RESTORE" ]; then if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then echo "Backup archive '$CONF_RESTORE' not found." exit 1 fi [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V="" tar -C / -x${TAR_V}zf "$CONF_RESTORE" exit $? fi
sysupgrade -r config.tgz --> tar -C / -xzf config.tgz
解压,覆盖到/目录下
for check in $sysupgrade_image_check; do ( eval "$check \"\$ARGV\"" ) || { if [ $FORCE -eq 1 ]; then echo "Image check '$check' failed but --force given - will update anyway!" break else echo "Image check '$check' failed." exit 1 fi } done
sysupgrade openwrt.bin --> 检查bin文件:platform_check_image openwrt.bin
lib/upgrade/platform.sh 中定义platform_check_image函数,取.bin文件的头部,检查magic number
if [ -n "$CONF_IMAGE" ]; then case "$(get_magic_word $CONF_IMAGE cat)" in # .gz files 1f8b) ;;
*) echo "Invalid config file. Please use only .tar.gz files" exit 1 ;; esac get_image "$CONF_IMAGE" "cat" > "$CONF_TAR" export SAVE_CONFIG=1 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then [ $TEST -eq 1 ] || do_save_conffiles export SAVE_CONFIG=1 else export SAVE_CONFIG=0 fi
在升级时会先保存配置文件到/tmp/sysupgrade.tgz
if [ -n "$(rootfs_type)" ]; then v "Switching to ramdisk..." run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade' else do_upgrade fi
mtd工具在写入时,会把$CONF_TAR文件整合进入jffs2分区,可以看到打印信息:
Appending jffs2 data from /tmp/sysupgrade.tgz to firmware...