From 6552727e03622ed04b4943bd0859e7343a8cc985 Mon Sep 17 00:00:00 2001 From: Huang Jianan Date: Wed, 8 Sep 2021 18:28:32 +0800 Subject: [PATCH] Avoid too much reserved_size for erofs image After the erofs image is built, it won't be modified again. The reserve room is only used for the hash footer. There is no need to set an excessive margin. Bug: 199248596 Test: erofs image size is smaller than before Signed-off-by: Huang Jianan Change-Id: If2535b9ad2c2d60bea74feeca576a168387ff341 --- tools/releasetools/build_image.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py index 1fe468e0b1..02f66b4a7a 100755 --- a/tools/releasetools/build_image.py +++ b/tools/releasetools/build_image.py @@ -231,6 +231,22 @@ def CheckHeadroom(ext4fs_output, prop_dict): mount_point, total_blocks, used_blocks, headroom_blocks, adjusted_blocks)) +def CalculateSizeAndReserved(prop_dict, size): + fs_type = prop_dict.get("fs_type", "") + partition_headroom = int(prop_dict.get("partition_headroom", 0)) + # If not specified, give us 16MB margin for GetDiskUsage error ... + reserved_size = int(prop_dict.get("partition_reserved_size", BYTES_IN_MB * 16)) + + if fs_type == "erofs": + reserved_size = int(prop_dict.get("partition_reserved_size", 0)) + if reserved_size == 0: + # give .3% margin or a minimum size for AVB footer + return max(size * 1003 // 1000, 256 * 1024) + + if fs_type.startswith("ext4") and partition_headroom > reserved_size: + reserved_size = partition_headroom + + return size + reserved_size def BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config): """Builds a pure image for the files under in_dir and writes it to out_file. @@ -468,12 +484,7 @@ def BuildImage(in_dir, prop_dict, out_file, target_out=None): size = GetDiskUsage(in_dir) logger.info( "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB) - # If not specified, give us 16MB margin for GetDiskUsage error ... - reserved_size = int(prop_dict.get("partition_reserved_size", BYTES_IN_MB * 16)) - partition_headroom = int(prop_dict.get("partition_headroom", 0)) - if fs_type.startswith("ext4") and partition_headroom > reserved_size: - reserved_size = partition_headroom - size += reserved_size + size = CalculateSizeAndReserved(prop_dict, size) # Round this up to a multiple of 4K so that avbtool works size = common.RoundUpTo4K(size) if fs_type.startswith("ext"):