build_image: Factor out CheckHeadroom() and add tests.
The test is mostly trivial, but it ensures the result parsing from
actual calls to mke2fs.
Test: python -m unittest test_build_image
Test: `m dist` on aosp_marlin-userdebug (w/ and w/o defining
PRODUCT_SYSTEM_HEADROOM respectively).
Change-Id: I8b9964213950e76f6d7d5518414a1bab888b4706
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 49d1f0a..13201ef 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -376,6 +376,40 @@
return None
return base_fs_file
+
+def CheckHeadroom(ext4fs_output, prop_dict):
+ """Checks if there's enough headroom space available.
+
+ Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
+ which is useful for devices with low disk space that have system image
+ variation between builds. The 'partition_headroom' in prop_dict is the size
+ in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
+
+ Args:
+ ext4fs_output: The output string from mke2fs command.
+ prop_dict: The property dict.
+
+ Returns:
+ The check result.
+ """
+ ext4fs_stats = re.compile(
+ r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
+ r'(?P<total_blocks>[0-9]+) blocks')
+ m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
+ used_blocks = int(m.groupdict().get('used_blocks'))
+ total_blocks = int(m.groupdict().get('total_blocks'))
+ headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
+ adjusted_blocks = total_blocks - headroom_blocks
+ if used_blocks > adjusted_blocks:
+ mount_point = prop_dict.get("mount_point")
+ print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
+ "headroom: %d blocks, available: %d blocks)" % (
+ mount_point, total_blocks, used_blocks, headroom_blocks,
+ adjusted_blocks))
+ return False
+ return True
+
+
def BuildImage(in_dir, prop_dict, out_file, target_out=None):
"""Build an image to out_file from in_dir with property prop_dict.
@@ -532,7 +566,6 @@
shutil.copytree(origin_in, staging_system, symlinks=True)
ext4fs_output = None
-
try:
if fs_type.startswith("ext4"):
(ext4fs_output, exit_code) = RunCommand(build_command)
@@ -550,24 +583,10 @@
print("Error: '%s' failed with exit code %d" % (build_command, exit_code))
return False
- # Check if there's enough headroom space available. This is useful for devices
- # with low disk space that have system image variation between builds.
+ # Check if there's enough headroom space available for ext4 image.
if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
assert ext4fs_output is not None
- ext4fs_stats = re.compile(
- r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
- r'(?P<total_blocks>[0-9]+) blocks')
- m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
- used_blocks = int(m.groupdict().get('used_blocks'))
- total_blocks = int(m.groupdict().get('total_blocks'))
- headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
- adjusted_blocks = total_blocks - headroom_blocks
- if used_blocks > adjusted_blocks:
- mount_point = prop_dict.get("mount_point")
- print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
- "headroom: %d blocks, available: %d blocks)" % (
- mount_point, total_blocks, used_blocks,
- headroom_blocks, adjusted_blocks))
+ if not CheckHeadroom(ext4fs_output, prop_dict):
return False
if not fs_spans_partition: