X-OpenWrt/Scripts/AutoUpdate.sh

310 lines
9.5 KiB
Bash
Raw Normal View History

2020-09-19 07:37:55 +00:00
#!/bin/bash
# https://github.com/Hyy2001X/AutoBuild-Actions
# AutoBuild Module by Hyy2001
# AutoUpdate for Openwrt
2020-09-19 07:37:55 +00:00
Version=V5.5
2020-09-23 09:43:46 +00:00
Shell_Helper() {
echo -e "\n使用方法: bash /bin/AutoUpdate.sh [参数1] [参数2]"
echo -e "\n支持下列参数:\n"
echo " -q 更新固件,不打印备份信息日志 [保留配置]"
echo " -n 更新固件 [不保留配置]"
echo " -f 强制更新固件,即跳过版本号验证,自动下载以及安装必要软件包 [保留配置]"
echo " -u 适用于定时更新 LUCI 的参数 [保留配置]"
echo " -c [参数2:<Github 地址>] 更换 Github 检查更新以及固件下载地址"
echo " -b [参数2:<引导方式 UEFI/Legacy>] 指定 x86 设备下载使用 UEFI/Legacy 引导的固件 [危险]"
echo " -l 列出所有信息"
echo " -d 清除固件下载缓存"
echo -e " -h 打印帮助信息\n"
exit
}
List_Info() {
echo -e "\n/overlay 可用: ${Overlay_Available}"
echo "/tmp 可用: ${TMP_Available}M"
echo "固件下载位置: /tmp/Downloads"
echo "当前设备: ${CURRENT_Device}"
echo "默认设备: ${DEFAULT_Device}"
echo "当前固件版本: ${CURRENT_Version}"
echo "固件名称: AutoBuild-${CURRENT_Device}-${CURRENT_Version}${Firmware_SFX}"
echo "Github 地址: ${Github}"
echo "解析 API 地址: ${Github_Tags}"
echo "固件下载地址: ${Github_Download}"
echo "作者/仓库: ${Author}"
if [[ ${DEFAULT_Device} == "x86_64" ]];then
echo "EFI 引导: ${EFI_Boot}"
echo "固件压缩: ${Compressed_x86}"
fi
echo "固件格式: ${Firmware_SFX}"
exit
2020-09-25 01:51:26 +00:00
}
2021-02-19 08:17:31 +00:00
Install_Pkg() {
PKG_NAME=${1}
grep "${PKG_NAME}" /tmp/Package_list > /dev/null 2>&1
if [[ $? -ne 0 ]];then
if [[ "${Force_Update}" == "1" ]] || [[ "${AutoUpdate_Mode}" == "1" ]];then
Choose="Y"
else
TIME && read -p "未安装[${PKG_NAME}],是否执行安装?[Y/n]:" Choose
fi
if [[ "${Choose}" == Y ]] || [[ "${Choose}" == y ]];then
TIME && echo -e "开始安装[${PKG_NAME}],请耐心等待...\n"
opkg update > /dev/null 2>&1
opkg install ${PKG_NAME}
if [[ $? -ne 0 ]];then
TIME && echo "[${PKG_NAME}] 安装失败,请尝试手动安装!"
exit
else
TIME && echo "[${PKG_NAME}] 安装成功!"
fi
else
TIME && echo "用户已取消安装,即将退出更新脚本..."
sleep 2
exit
fi
fi
}
TIME() {
echo -ne "\n[$(date "+%H:%M:%S")] "
}
2021-02-19 08:17:31 +00:00
opkg list | awk '{print $1}' > /tmp/Package_list
Input_Option="$1"
Input_Other="$2"
2021-01-11 13:45:30 +00:00
CURRENT_Version="$(awk 'NR==1' /etc/openwrt_info)"
Github="$(awk 'NR==2' /etc/openwrt_info)"
Github_Download="${Github}/releases/download/AutoUpdate"
Author="${Github##*com/}"
Github_Tags="https://api.github.com/repos/${Author}/releases/latest"
2021-01-11 13:45:30 +00:00
DEFAULT_Device="$(awk 'NR==3' /etc/openwrt_info)"
2021-02-19 08:17:31 +00:00
Firmware_Type="$(awk 'NR==4' /etc/openwrt_info)"
TMP_Available="$(df -m | grep "/tmp" | awk '{print $4}' | awk 'NR==1' | awk -F. '{print $1}')"
Overlay_Available="$(df -h | grep ":/overlay" | awk '{print $4}' | awk 'NR==1')"
2021-02-19 08:17:31 +00:00
case ${DEFAULT_Device} in
x86_64)
[[ -z ${Firmware_Type} ]] && Firmware_Type="img"
if [[ "${Firmware_Type}" == "img.gz" ]];then
Compressed_x86="1"
else
Compressed_x86="0"
fi
if [ -f /etc/openwrt_boot ];then
BOOT_Type="-$(cat /etc/openwrt_boot)"
2021-02-19 08:17:31 +00:00
else
if [ -d /sys/firmware/efi ];then
BOOT_Type="-UEFI"
else
BOOT_Type="-Legacy"
fi
2021-02-19 08:17:31 +00:00
fi
case "${BOOT_Type}" in
-Legacy)
EFI_Boot=0
;;
-UEFI)
EFI_Boot=1
;;
esac
2021-02-19 08:17:31 +00:00
Firmware_SFX="${BOOT_Type}.${Firmware_Type}"
Detail_SFX="${BOOT_Type}.detail"
2021-02-19 08:17:31 +00:00
CURRENT_Device="x86_64"
Space_Req=480
2021-02-19 08:17:31 +00:00
;;
*)
CURRENT_Device="$(jsonfilter -e '@.model.id' < /etc/board.json | tr ',' '_')"
Firmware_SFX=".${Firmware_Type}"
[[ -z ${Firmware_SFX} ]] && Firmware_SFX=".bin"
Detail_SFX=".detail"
Space_Req=0
2021-02-19 08:17:31 +00:00
esac
2020-09-19 07:37:55 +00:00
cd /etc
2020-11-17 06:14:17 +00:00
clear && echo "Openwrt-AutoUpdate Script ${Version}"
2020-12-23 14:28:57 +00:00
if [[ -z "${Input_Option}" ]];then
2020-12-21 14:16:18 +00:00
Upgrade_Options="-q" && TIME && echo "执行: 保留配置更新固件[静默模式]"
2020-09-28 14:28:40 +00:00
else
2020-12-23 14:28:57 +00:00
case ${Input_Option} in
-n | -f | -u)
case ${Input_Option} in
-n)
TIME && echo "执行: 更新固件(不保留配置)"
Upgrade_Options="-n"
;;
-f)
Force_Update=1
Upgrade_Options="-q"
TIME && echo "执行: 强制更新固件(保留配置)"
;;
-u)
AutoUpdate_Mode=1
Upgrade_Options="-q"
;;
esac
2020-12-23 14:28:57 +00:00
;;
-c)
if [[ ! -z "${Input_Other}" ]];then
sed -i "s?${Github}?${Input_Other}?g" /etc/openwrt_info > /dev/null 2>&1
echo -e "\nGithub 地址已更换为: ${Input_Other}"
unset Input_Other
else
Shell_Helper
fi
exit
;;
-l | -L)
List_Info
;;
-d)
rm -f /tmp/Downloads/* /tmp/Github_Tags
TIME && echo "固件下载缓存清理完成!"
sleep 1
exit
;;
-h | -H | --help)
Shell_Helper
;;
-b)
[[ -z "${Input_Other}" ]] && Shell_Helper
case "${Input_Other}" in
UEFI | Legacy)
echo "${Input_Other}" > openwrt_boot
sed -i '/openwrt_boot/d' /etc/sysupgrade.conf
echo -e "\n/etc/openwrt_boot" >> /etc/sysupgrade.conf
TIME && echo "固件引导方式已指定为: ${Input_Other}!"
;;
*)
echo -e "\n错误的参数: [${Input_Other}],当前支持的选项: [UEFI/Legacy] !"
;;
esac
exit
;;
2020-12-21 14:16:18 +00:00
*)
echo -e "\nERROR INPUT: [$*]"
Shell_Helper
2020-12-21 14:16:18 +00:00
;;
esac
fi
if [[ ! "${Force_Update}" == "1" ]];then
2020-12-21 14:16:18 +00:00
grep "curl" /tmp/Package_list > /dev/null 2>&1
if [[ ! $? -ne 0 ]];then
Google_Check=$(curl -I -s --connect-timeout 3 google.com -w %{http_code} | tail -n1)
if [ ! "$Google_Check" == 301 ];then
TIME && echo "Google 连接失败,尝试使用 [FastGit] 镜像加速!"
PROXY_URL="https://download.fastgit.org"
fi
2020-12-21 14:16:18 +00:00
fi
if [[ "${TMP_Available}" -lt "${Space_Req}" ]];then
TIME && echo "/tmp 空间不足: [${Space_Req}M],无法执行更新!"
exit
fi
2020-10-02 18:06:37 +00:00
fi
2021-02-19 08:17:31 +00:00
Install_Pkg wget
2021-01-11 13:45:30 +00:00
if [[ -z "${CURRENT_Version}" ]];then
2020-12-03 11:25:21 +00:00
TIME && echo "警告: 当前固件版本获取失败!"
2021-01-11 13:45:30 +00:00
CURRENT_Version="未知"
2020-09-19 07:37:55 +00:00
fi
2021-01-11 13:45:30 +00:00
if [[ -z "${CURRENT_Device}" ]];then
[[ "${Force_Update}" == "1" ]] && exit
2021-01-11 13:45:30 +00:00
TIME && echo "警告: 当前设备名称获取失败,使用预设名称[$DEFAULT_Device]"
CURRENT_Device="${DEFAULT_Device}"
2020-09-26 17:14:39 +00:00
fi
2020-12-03 11:25:21 +00:00
TIME && echo "正在检查版本更新..."
2020-11-17 06:14:17 +00:00
wget -q ${Github_Tags} -O - > /tmp/Github_Tags
2021-02-19 08:17:31 +00:00
if [[ ! "$?" == 0 ]];then
TIME && echo "检查更新失败,请稍后重试!"
exit
fi
TIME && echo "正在获取云端固件版本..."
GET_Firmware=$(cat /tmp/Github_Tags | egrep -o "AutoBuild-${CURRENT_Device}-R[0-9].+-[0-9]+${Firmware_SFX}" | awk 'END {print}')
GET_Version=$(echo ${GET_Firmware} | egrep -o "R[0-9].+-[0-9]+")
if [[ -z "${GET_Firmware}" ]] || [[ -z "${GET_Version}" ]];then
TIME && echo "云端固件版本获取失败!"
2020-09-19 07:37:55 +00:00
exit
fi
Firmware_Info="$(echo ${GET_Firmware} | egrep -o "AutoBuild-${CURRENT_Device}-R[0-9].+-[0-9]+")"
Firmware="${GET_Firmware}"
Firmware_Detail="${Firmware_Info}${Detail_SFX}"
let X=$(grep -n "${Firmware}" /tmp/Github_Tags | tail -1 | cut -d : -f 1)-4
let Cloud_Firmware_Size=$(sed -n "${X}p" /tmp/Github_Tags | egrep -o "[0-9]+" | awk '{print ($1)/1048576}' | awk -F. '{print $1}')+1
2020-10-05 11:01:23 +00:00
echo -e "\n固件作者: ${Author%/*}"
echo "设备名称: ${CURRENT_Device}"
2021-03-16 11:42:00 +00:00
echo "固件格式: ${Firmware_SFX}"
2021-01-11 13:45:30 +00:00
echo -e "\n当前固件版本: ${CURRENT_Version}"
2020-12-23 14:28:57 +00:00
echo "云端固件版本: ${GET_Version}"
echo "当前可用空间: ${TMP_Available}M"
echo "云端固件大小: ${Cloud_Firmware_Size}M"
2020-11-17 06:14:17 +00:00
if [[ ! ${Force_Update} == 1 ]];then
if [[ "${TMP_Available}" -lt "${Cloud_Firmware_Size}" ]];then
TIME && echo "/tmp 空间不足: [${Cloud_Firmware_Size}M],无法执行更新!"
exit
fi
if [[ ${CURRENT_Version} == ${GET_Version} ]];then
2020-12-23 14:28:57 +00:00
[[ "${AutoUpdate_Mode}" == "1" ]] && exit
TIME && read -p "已是最新版本,是否强制更新固件?[Y/n]:" Choose
2020-12-05 17:45:06 +00:00
if [[ "${Choose}" == Y ]] || [[ "${Choose}" == y ]];then
2020-12-23 14:28:57 +00:00
TIME && echo "开始强制更新固件..."
2020-10-02 18:04:28 +00:00
else
TIME && echo "已取消强制更新,即将退出更新程序..."
sleep 2
2020-10-02 18:04:28 +00:00
exit
fi
2020-09-28 14:28:40 +00:00
fi
2020-10-02 18:07:56 +00:00
fi
if [ ! -z "${PROXY_URL}" ];then
Github_Download=${PROXY_URL}/${Author}/releases/download/AutoUpdate
fi
2020-12-23 14:28:57 +00:00
echo -e "\n云端固件名称: ${Firmware}"
echo "固件下载地址: ${Github_Download}"
echo "固件保存位置: /tmp/Downloads"
[ ! -d "/tmp/Downloads" ] && mkdir -p /tmp/Downloads
rm -f /tmp/Downloads/*
2020-09-25 01:51:26 +00:00
TIME && echo "正在下载固件,请耐心等待..."
cd /tmp/Downloads
wget -q "${Github_Download}/${Firmware}" -O ${Firmware}
if [[ ! "$?" == 0 ]];then
TIME && echo "固件下载失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
fi
TIME && echo "固件下载成功!"
TIME && echo "正在获取云端固件MD5,请耐心等待..."
2020-11-17 06:14:17 +00:00
wget -q ${Github_Download}/${Firmware_Detail} -O ${Firmware_Detail}
if [[ ! "$?" == 0 ]];then
TIME && echo "MD5 获取失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
fi
GET_MD5=$(awk -F '[ :]' '/MD5/ {print $2;exit}' ${Firmware_Detail})
2020-11-17 06:14:17 +00:00
CURRENT_MD5=$(md5sum ${Firmware} | cut -d ' ' -f1)
echo -e "\n本地固件MD5:${CURRENT_MD5}"
2020-11-17 06:14:17 +00:00
echo "云端固件MD5:${GET_MD5}"
if [[ -z "${GET_MD5}" ]] || [[ -z "${CURRENT_MD5}" ]];then
2021-03-16 11:42:00 +00:00
TIME && echo "MD5 获取失败!"
2020-09-19 07:37:55 +00:00
exit
fi
2020-11-17 06:14:17 +00:00
if [[ ! "${GET_MD5}" == "${CURRENT_MD5}" ]];then
2021-03-16 11:42:00 +00:00
TIME && echo "MD5 对比失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
else
2021-03-16 11:42:00 +00:00
TIME && echo "MD5 对比通过!"
2020-09-19 07:37:55 +00:00
fi
if [[ ${Compressed_x86} == 1 ]];then
2021-02-19 08:17:31 +00:00
TIME && echo "检测到固件为 [.gz] 压缩格式,开始解压固件..."
Install_Pkg gzip
gzip -dk ${Firmware} > /dev/null 2>&1
Firmware="${Firmware_Info}${BOOT_Type}.img"
2021-02-19 08:17:31 +00:00
if [ -f "${Firmware}" ];then
TIME && echo "固件解压成功,名称: ${Firmware}"
2021-02-19 08:17:31 +00:00
else
TIME && echo "固件解压失败!"
2021-02-19 08:17:31 +00:00
exit
fi
fi
TIME && echo -e "一切准备就绪,3s 后开始更新固件..."
sleep 3
2021-03-16 11:42:00 +00:00
TIME && echo "正在更新固件,期间请耐心等待..."
2021-02-19 08:17:31 +00:00
sysupgrade ${Upgrade_Options} ${Firmware}
if [[ $? -ne 0 ]];then
TIME && echo "固件刷写失败,请尝试手动下载更新固件!"
2021-02-19 08:17:31 +00:00
exit
fi