linuxOS_D21X/target/d211/demo128_nand/swupdate/update_data.sh
2026-06-03 18:09:21 +08:00

64 lines
1.5 KiB
Bash

#!/bin/sh
# 增量更新 /data 分区
# swupdate 已将 data_update.tar.gz 写到 /tmp/data_update.tar.gz
# 只在 postinst 阶段执行(此时 rawfile 已经复制完成)
if [ "$1" = "preinst" ]; then
echo "[update_data] preinst: skip"
exit 0
fi
TAR="/tmp/data_update.tar.gz"
SRC="/tmp/data_update_src"
DST="/mnt/data_update"
if [ ! -f "$TAR" ]; then
echo "[update_data] ERROR: $TAR not found"
exit 1
fi
# 挂载 data 分区到临时挂载点
mkdir -p "$DST"
if ! mount -t ubifs ubi2:data "$DST"; then
echo "[update_data] ERROR: failed to mount ubi2:data"
exit 1
fi
echo "[update_data] mounted ubi2:data on $DST"
# 解压到临时目录
mkdir -p "$SRC"
tar -xzf "$TAR" -C "$SRC"
# 遍历升级包中的所有文件,只覆盖不一样的
find "$SRC" -type f | while read src_file; do
rel_path="${src_file#$SRC/}"
dst_file="$DST/$rel_path"
# 目标目录不存在则创建
dst_dir=$(dirname "$dst_file")
[ -d "$dst_dir" ] || mkdir -p "$dst_dir"
if [ -f "$dst_file" ]; then
src_md5=$(md5sum "$src_file" | cut -d' ' -f1)
dst_md5=$(md5sum "$dst_file" | cut -d' ' -f1)
if [ "$src_md5" = "$dst_md5" ]; then
echo "[update_data] unchanged: $rel_path"
continue
fi
fi
cp "$src_file" "$dst_file"
echo "[update_data] updated: $rel_path"
done
# 清理临时文件
rm -rf "$SRC" "$TAR"
# 卸载 data 分区
sync
umount "$DST"
echo "[update_data] unmounted $DST"
echo "[update_data] done"
exit 0