
1 概述将多个 ipk 安装包、安装脚本、校验脚本、版本元信息打包后经 AES-256-CBC 加密输出设备端可识别的 pkg升级文件。设备端升级脚本执行升级包解密、校验和安装。2 升级包打包脚本#!/bin/sh # 验证参数个数 if [ $# -lt 3 ]; then echo 参数:参数顺序 程序名称 版本号 架构类型 exit 1 fi PKG_NAME$1 # 程序名称 VERSION$2 # 版本号 ARCH$3 # 架构类型 BUILD_DATE$(date %Y-%m-%d %H:%M:%S) # 生成当前日期时间 # 显示参数值调试用 echo 程序名称: ${PKG_NAME} echo 版本号: ${VERSION} echo 架构类型: ${ARCH} echo 打包时间: ${BUILD_DATE} # 当前脚本路径 cur_dir$(dirname $(readlink -f ${0})) # 引入校验脚本 . ${cur_dir}/checktool # ipk安装包目录 ipk_dir${cur_dir}/ipk_list # 脚本文件列表 sh_listchecktool install # 保存校验值文件名 check_file${ipk_dir}/checkval # info.ini 路径 info_ini${ipk_dir}/version.ini # 切换到当前目录 cd ${cur_dir} # 删除其他安装包 rm -f ${PKG_NAME}_*.tar.gz ${PKG_NAME}_*.pkg # 检查ipk目录是否存在 if [ ! -d ${ipk_dir} ]; then echo 错误ipk目录不存在 2 exit 1 fi # ipk_list目录删除非ipk文件 find ${ipk_dir} -mindepth 1 ! -name *.ipk -exec rm -rf {} # 检测存在ipk文件是否存在 if ! find ${ipk_dir} -type f -name *.ipk | grep -q .; then echo 错误ipk安装包不存在 2 exit 1 fi # 检查脚本文件是否存在 for file in ${sh_list}; do if [ ! -f ${file} ]; then echo 错误: 脚本文件[${file}]不存在 2 exit 1 fi done # 直接 覆盖写入每次都重新生成覆盖旧文件 cat ${info_ini} EOF [version] name${PKG_NAME} version${VERSION} arch${ARCH} date${BUILD_DATE} EOF # 拷贝脚本文件 cp ${sh_list} ${ipk_dir} # 计算校验值并保存 check_val$(get_check_val ${ipk_dir}) if [ $? -eq 0 ]; then echo ${check_val} ${check_file} echo 文件校验值: ${check_val} else echo 错误: 计算校验值异常 2 exit 1 fi # 打包 pkg_name${PKG_NAME}_${VERSION}_${ARCH}.tar.gz if ! tar -czf ${pkg_name} -C ${ipk_dir} --xforms,^\./,,r .; then echo 错误tar打包失败 2 exit 1 fi # 加密 if ! openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -pass pass:123456 -in ${pkg_name} -out ${pkg_name}.pkg; then echo 错误加密失败 2 rm -f ${pkg_name} exit 1 else echo 成功${pkg_name}.pkg fi # 删除原始tar.gz只保留pkg文件 rm -f ${pkg_name} # 清理非ipk文件 find ${ipk_dir} -mindepth 1 ! -name *.ipk -exec rm -rf {} 3、升级校验脚本3.1 sha256校验#!/bin/sh get_check_val() { local target_dir$1 local check_listinstall checktool version.ini # 检查目录是否存在 if [ ! -d ${target_dir} ]; then echo 错误: 目录 ${target_dir} 不存在 2 return 1 fi # 检查ipk文件是否存在 if ! find ${target_dir} -maxdepth 1 -type f -name *.ipk | grep -q .; then echo 错误: 未找到任何.ipk文件 2 return 1 fi # 检查必需文件是否存在 for file in ${check_list}; do if [ ! -f ${target_dir}/${file} ]; then echo 错误: 必需文件 ${target_dir}/${file} 不存在 2 return 1 fi done # 从check_list动态构建find表达式 local check_files for name in ${check_list}; do check_files${check_files} -o -name ${name} done # 收集文件并排序计算校验值 find ${target_dir} -maxdepth 1 -type f \( -name *.ipk ${check_files} \) | sort | xargs cat 2/dev/null | sha256sum | awk {print $1} }3.2 md5校验#!/bin/sh get_check_val() { local target_dir$1 local check_listinstall checktool version.ini # 检查目录是否存在 if [ ! -d ${target_dir} ]; then echo 错误: 目录 ${target_dir} 不存在 2 return 1 fi # 检查ipk文件是否存在 if ! find ${target_dir} -maxdepth 1 -type f -name *.ipk | grep -q .; then echo 错误: 未找到任何.ipk文件 2 return 1 fi # 检查必需文件是否存在 for file in ${check_list}; do if [ ! -f ${target_dir}/${file} ]; then echo 错误: 必需文件 ${target_dir}/${file} 不存在 2 return 1 fi done # 从check_list动态构建find表达式 local check_files for name in ${check_list}; do check_files${check_files} -o -name ${name} done # 收集文件并排序计算MD5 find ${target_dir} -maxdepth 1 -type f \( -name *.ipk ${check_files} \) | sort | xargs cat 2/dev/null | md5sum | awk {print $1} }安装脚本待完善