一、稀疏镜像升级背景
常用系统镜像格式为原始镜像,即RAW格式。镜像体积比较大,在烧录固件或者升级固件时比较耗时,而且在移动设备升级过程时比较耗费流量。为此,将原始镜像用稀疏描述,可以大大地缩减镜像体积,省时省流量。
二、稀疏镜像原理
1、稀疏镜像的概念
原始镜像:即raw image,完整的ext4分区镜像,包含很多全零的无效填充区
稀疏镜像:即sparse image,将raw ext4进行稀疏描述,因此尺寸比较小,制作目录有多少文件就计算多少,没有全零填充
2、稀疏镜像格式
稀疏镜像数据格式:首先是sparse_header占用28byte,然后是12byte的chunk_header,同样这chunk_header的类型决定了后面跟着的数据,如果读到数据是0xCAC1意味着后面是本身的raw_data,如果是0xCAC3,则后面num为0,接着再0xCAC2意味着后面填充4byte的内容。
三、实现稀疏镜像升级方案
版本基线:
OpenAtom OpenHarmony(以下简称“OpenHarmony”) 3.1 Release
代码路径:
https://gitee.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v3.1-release.md
1、稀疏镜像烧录
(1)生成稀疏格式镜像
有2种方法可以生成稀疏镜像:
1)修改文件build/ohos_var.gni中,sparse_image=true
2)编译命令增加–sparse-image字段,
如./build.sh --product-name=xxx --sparse-image
(2)增加稀疏格式转换工具
在目录build/ohos/images/mkimage中增加文件img2simg,该工具用于编译完成后将raw镜像转换为sparse格式,并设置权限为777。
(3)编译后的镜像对比
编译出的镜像格式为sparse格式,镜像大小相比raw格式明显变小。
(4)烧录稀疏镜像
烧录稀疏镜像方法和烧录原始镜像方法一致。
稀疏镜像本身是不能直接挂载的,在烧录过程中通过uboot将稀疏格式镜像还原为原始镜像,然后写到磁盘中,系统启动后可挂载对应的镜像。
2、稀疏镜像升级
升级包采用稀疏镜像制作。
(1)修改升级包制作工具
官方升级包工具不支持生成稀疏镜像的升级包,修改升级包工具,生成稀疏格式的升级包。
.\base\update\packaging_tools\image_class.py
按照上图所示注释代码
(2)生成稀疏镜像升级包
和全量镜像升级包制作方法一致。
参考:
https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/subsystems/subsys-ota-guide.md#%E6%A0%87%E5%87%86%E7%B3%BB%E7%BB%9F%E5%8D%87%E7%BA%A7%E5%8C%85%E5%88%B6%E4%BD%9C
(3)适配updater组件中稀疏镜像功能
● 增加写稀疏镜像分支
.\base\update\updater\services\applypatch\data_writer.cpp
写数据函数CreateDataWriter增加写稀疏镜像分支
case WRITE_SPARSE:
{
std::unique_ptr<SparseWriter> writer(std::make_unique<SparseWriter>(partitionName));
return std::move(writer);
}
● 增加稀疏镜像类声明
.\base\update\updater\services\applypatch\raw_writer.h
增加稀疏镜像类声明及相关变量定义
typedef struct sparse_header {
uint32_t magic; /* 0xed26ff3a */
uint16_t major_version; /* (0x1) - reject images with higher major versions */
uint16_t minor_version; /* (0x0) - allow images with higer minor versions */
uint16_t file_hdr_sz; /* 28 bytes for first revision of the file format */
uint16_t chunk_hdr_sz; /* 12 bytes for first revision of the file format */
uint32_t blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */
uint32_t total_blks; /* total blocks in the non-sparse output image */
uint32_t total_chunks; /* total chunks in the sparse input image */