1#!/usr/bin/env python 2# 3# Copyright (C) 2011 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17""" 18Builds output_image from the given input_directory, properties_file, 19and writes the image to target_output_directory. 20 21Usage: build_image input_directory properties_file output_image \\ 22 target_output_directory 23""" 24 25from __future__ import print_function 26 27import glob 28import logging 29import os 30import os.path 31import re 32import shutil 33import sys 34 35import common 36import verity_utils 37 38logger = logging.getLogger(__name__) 39 40OPTIONS = common.OPTIONS 41BLOCK_SIZE = common.BLOCK_SIZE 42BYTES_IN_MB = 1024 * 1024 43 44 45class BuildImageError(Exception): 46 """An Exception raised during image building.""" 47 48 def __init__(self, message): 49 Exception.__init__(self, message) 50 51 52def GetDiskUsage(path): 53 """Returns the number of bytes that "path" occupies on host. 54 55 Args: 56 path: The directory or file to calculate size on. 57 58 Returns: 59 The number of bytes based on a 1K block_size. 60 """ 61 cmd = ["du", "-b", "-k", "-s", path] 62 output = common.RunAndCheckOutput(cmd, verbose=False) 63 return int(output.split()[0]) * 1024 64 65 66def GetInodeUsage(path): 67 """Returns the number of inodes that "path" occupies on host. 68 69 Args: 70 path: The directory or file to calculate inode number on. 71 72 Returns: 73 The number of inodes used. 74 """ 75 cmd = ["find", path, "-print"] 76 output = common.RunAndCheckOutput(cmd, verbose=False) 77 # increase by > 6% as number of files and directories is not whole picture. 78 inodes = output.count('\n') 79 spare_inodes = inodes * 6 // 100 80 min_spare_inodes = 12 81 if spare_inodes < min_spare_inodes: 82 spare_inodes = min_spare_inodes 83 return inodes + spare_inodes 84 85 86def GetFilesystemCharacteristics(fs_type, image_path, sparse_image=True): 87 """Returns various filesystem characteristics of "image_path". 88 89 Args: 90 image_path: The file to analyze. 91 sparse_image: Image is sparse 92 93 Returns: 94 The characteristics dictionary. 95 """ 96 unsparse_image_path = image_path 97 if sparse_image: 98 unsparse_image_path = UnsparseImage(image_path, replace=False) 99 100 if fs_type.startswith("ext"): 101 cmd = ["tune2fs", "-l", unsparse_image_path] 102 elif fs_type.startswith("f2fs"): 103 cmd = ["fsck.f2fs", "-l", unsparse_image_path] 104 105 try: 106 output = common.RunAndCheckOutput(cmd, verbose=False) 107 finally: 108 if sparse_image: 109 os.remove(unsparse_image_path) 110 fs_dict = {} 111 for line in output.splitlines(): 112 fields = line.split(":") 113 if len(fields) == 2: 114 fs_dict[fields[0].strip()] = fields[1].strip() 115 return fs_dict 116 117 118def UnsparseImage(sparse_image_path, replace=True): 119 img_dir = os.path.dirname(sparse_image_path) 120 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) 121 unsparse_image_path = os.path.join(img_dir, unsparse_image_path) 122 if os.path.exists(unsparse_image_path): 123 if replace: 124 os.unlink(unsparse_image_path) 125 else: 126 return unsparse_image_path 127 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] 128 try: 129 common.RunAndCheckOutput(inflate_command) 130 except: 131 os.remove(unsparse_image_path) 132 raise 133 return unsparse_image_path 134 135 136def ConvertBlockMapToBaseFs(block_map_file): 137 base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs") 138 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file] 139 common.RunAndCheckOutput(convert_command) 140 return base_fs_file 141 142 143def SetUpInDirAndFsConfig(origin_in, prop_dict): 144 """Returns the in_dir and fs_config that should be used for image building. 145 146 When building system.img for all targets, it creates and returns a staged dir 147 that combines the contents of /system (i.e. in the given in_dir) and root. 148 149 Args: 150 origin_in: Path to the input directory. 151 prop_dict: A property dict that contains info like partition size. Values 152 may be updated. 153 154 Returns: 155 A tuple of in_dir and fs_config that should be used to build the image. 156 """ 157 fs_config = prop_dict.get("fs_config") 158 159 if prop_dict["mount_point"] == "system_other": 160 prop_dict["mount_point"] = "system" 161 return origin_in, fs_config 162 163 if prop_dict["mount_point"] != "system": 164 return origin_in, fs_config 165 166 if "first_pass" in prop_dict: 167 prop_dict["mount_point"] = "/" 168 return prop_dict["first_pass"] 169 170 # Construct a staging directory of the root file system. 171 in_dir = common.MakeTempDir() 172 root_dir = prop_dict.get("root_dir") 173 if root_dir: 174 shutil.rmtree(in_dir) 175 shutil.copytree(root_dir, in_dir, symlinks=True) 176 in_dir_system = os.path.join(in_dir, "system") 177 shutil.rmtree(in_dir_system, ignore_errors=True) 178 shutil.copytree(origin_in, in_dir_system, symlinks=True) 179 180 # Change the mount point to "/". 181 prop_dict["mount_point"] = "/" 182 if fs_config: 183 # We need to merge the fs_config files of system and root. 184 merged_fs_config = common.MakeTempFile( 185 prefix="merged_fs_config", suffix=".txt") 186 with open(merged_fs_config, "w") as fw: 187 if "root_fs_config" in prop_dict: 188 with open(prop_dict["root_fs_config"]) as fr: 189 fw.writelines(fr.readlines()) 190 with open(fs_config) as fr: 191 fw.writelines(fr.readlines()) 192 fs_config = merged_fs_config 193 prop_dict["first_pass"] = (in_dir, fs_config) 194 return in_dir, fs_config 195 196 197def CheckHeadroom(ext4fs_output, prop_dict): 198 """Checks if there's enough headroom space available. 199 200 Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM), 201 which is useful for devices with low disk space that have system image 202 variation between builds. The 'partition_headroom' in prop_dict is the size 203 in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks. 204 205 Args: 206 ext4fs_output: The output string from mke2fs command. 207 prop_dict: The property dict. 208 209 Raises: 210 AssertionError: On invalid input. 211 BuildImageError: On check failure. 212 """ 213 assert ext4fs_output is not None 214 assert prop_dict.get('fs_type', '').startswith('ext4') 215 assert 'partition_headroom' in prop_dict 216 assert 'mount_point' in prop_dict 217 218 ext4fs_stats = re.compile( 219 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/' 220 r'(?P<total_blocks>[0-9]+) blocks') 221 last_line = ext4fs_output.strip().split('\n')[-1] 222 m = ext4fs_stats.match(last_line) 223 used_blocks = int(m.groupdict().get('used_blocks')) 224 total_blocks = int(m.groupdict().get('total_blocks')) 225 headroom_blocks = int(prop_dict['partition_headroom']) // BLOCK_SIZE 226 adjusted_blocks = total_blocks - headroom_blocks 227 if used_blocks > adjusted_blocks: 228 mount_point = prop_dict["mount_point"] 229 raise BuildImageError( 230 "Error: Not enough room on {} (total: {} blocks, used: {} blocks, " 231 "headroom: {} blocks, available: {} blocks)".format( 232 mount_point, total_blocks, used_blocks, headroom_blocks, 233 adjusted_blocks)) 234 235def CalculateSizeAndReserved(prop_dict, size): 236 fs_type = prop_dict.get("fs_type", "") 237 partition_headroom = int(prop_dict.get("partition_headroom", 0)) 238 # If not specified, give us 16MB margin for GetDiskUsage error ... 239 reserved_size = int(prop_dict.get("partition_reserved_size", BYTES_IN_MB * 16)) 240 241 if fs_type == "erofs": 242 reserved_size = int(prop_dict.get("partition_reserved_size", 0)) 243 if reserved_size == 0: 244 # give .3% margin or a minimum size for AVB footer 245 return max(size * 1003 // 1000, 256 * 1024) 246 247 if fs_type.startswith("ext4") and partition_headroom > reserved_size: 248 reserved_size = partition_headroom 249 250 return size + reserved_size 251 252def BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config): 253 """Builds a pure image for the files under in_dir and writes it to out_file. 254 255 Args: 256 in_dir: Path to input directory. 257 prop_dict: A property dict that contains info like partition size. Values 258 will be updated with computed values. 259 out_file: The output image file. 260 target_out: Path to the TARGET_OUT directory as in Makefile. It actually 261 points to the /system directory under PRODUCT_OUT. fs_config (the one 262 under system/core/libcutils) reads device specific FS config files from 263 there. 264 fs_config: The fs_config file that drives the prototype 265 266 Raises: 267 BuildImageError: On build image failures. 268 """ 269 build_command = [] 270 fs_type = prop_dict.get("fs_type", "") 271 run_fsck = None 272 needs_projid = prop_dict.get("needs_projid", 0) 273 needs_casefold = prop_dict.get("needs_casefold", 0) 274 needs_compress = prop_dict.get("needs_compress", 0) 275 276 disable_sparse = "disable_sparse" in prop_dict 277 manual_sparse = False 278 279 if fs_type.startswith("ext"): 280 build_command = [prop_dict["ext_mkuserimg"]] 281 if "extfs_sparse_flag" in prop_dict and not disable_sparse: 282 build_command.append(prop_dict["extfs_sparse_flag"]) 283 run_e2fsck = RunE2fsck 284 build_command.extend([in_dir, out_file, fs_type, 285 prop_dict["mount_point"]]) 286 build_command.append(prop_dict["image_size"]) 287 if "journal_size" in prop_dict: 288 build_command.extend(["-j", prop_dict["journal_size"]]) 289 if "timestamp" in prop_dict: 290 build_command.extend(["-T", str(prop_dict["timestamp"])]) 291 if fs_config: 292 build_command.extend(["-C", fs_config]) 293 if target_out: 294 build_command.extend(["-D", target_out]) 295 if "block_list" in prop_dict: 296 build_command.extend(["-B", prop_dict["block_list"]]) 297 if "base_fs_file" in prop_dict: 298 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"]) 299 build_command.extend(["-d", base_fs_file]) 300 build_command.extend(["-L", prop_dict["mount_point"]]) 301 if "extfs_inode_count" in prop_dict: 302 build_command.extend(["-i", prop_dict["extfs_inode_count"]]) 303 if "extfs_rsv_pct" in prop_dict: 304 build_command.extend(["-M", prop_dict["extfs_rsv_pct"]]) 305 if "flash_erase_block_size" in prop_dict: 306 build_command.extend(["-e", prop_dict["flash_erase_block_size"]]) 307 if "flash_logical_block_size" in prop_dict: 308 build_command.extend(["-o", prop_dict["flash_logical_block_size"]]) 309 # Specify UUID and hash_seed if using mke2fs. 310 if os.path.basename(prop_dict["ext_mkuserimg"]) == "mkuserimg_mke2fs": 311 if "uuid" in prop_dict: 312 build_command.extend(["-U", prop_dict["uuid"]]) 313 if "hash_seed" in prop_dict: 314 build_command.extend(["-S", prop_dict["hash_seed"]]) 315 if prop_dict.get("ext4_share_dup_blocks") == "true": 316 build_command.append("-c") 317 if (needs_projid): 318 build_command.extend(["--inode_size", "512"]) 319 else: 320 build_command.extend(["--inode_size", "256"]) 321 if "selinux_fc" in prop_dict: 322 build_command.append(prop_dict["selinux_fc"]) 323 elif fs_type.startswith("erofs"): 324 build_command = ["mkfs.erofs"] 325 326 compressor = None 327 if "erofs_default_compressor" in prop_dict: 328 compressor = prop_dict["erofs_default_compressor"] 329 if "erofs_compressor" in prop_dict: 330 compressor = prop_dict["erofs_compressor"] 331 if compressor: 332 build_command.extend(["-z", compressor]) 333 334 build_command.extend(["--mount-point", prop_dict["mount_point"]]) 335 if target_out: 336 build_command.extend(["--product-out", target_out]) 337 if fs_config: 338 build_command.extend(["--fs-config-file", fs_config]) 339 if "selinux_fc" in prop_dict: 340 build_command.extend(["--file-contexts", prop_dict["selinux_fc"]]) 341 if "timestamp" in prop_dict: 342 build_command.extend(["-T", str(prop_dict["timestamp"])]) 343 if "uuid" in prop_dict: 344 build_command.extend(["-U", prop_dict["uuid"]]) 345 if "block_list" in prop_dict: 346 build_command.extend(["--block-list-file", prop_dict["block_list"]]) 347 if "erofs_pcluster_size" in prop_dict: 348 build_command.extend(["-C", prop_dict["erofs_pcluster_size"]]) 349 if "erofs_share_dup_blocks" in prop_dict: 350 build_command.extend(["--chunksize", "4096"]) 351 if "erofs_use_legacy_compression" in prop_dict: 352 build_command.extend(["-E", "legacy-compress"]) 353 354 build_command.extend([out_file, in_dir]) 355 if "erofs_sparse_flag" in prop_dict and not disable_sparse: 356 manual_sparse = True 357 358 run_fsck = RunErofsFsck 359 elif fs_type.startswith("squash"): 360 build_command = ["mksquashfsimage.sh"] 361 build_command.extend([in_dir, out_file]) 362 if "squashfs_sparse_flag" in prop_dict and not disable_sparse: 363 build_command.extend([prop_dict["squashfs_sparse_flag"]]) 364 build_command.extend(["-m", prop_dict["mount_point"]]) 365 if target_out: 366 build_command.extend(["-d", target_out]) 367 if fs_config: 368 build_command.extend(["-C", fs_config]) 369 if "selinux_fc" in prop_dict: 370 build_command.extend(["-c", prop_dict["selinux_fc"]]) 371 if "block_list" in prop_dict: 372 build_command.extend(["-B", prop_dict["block_list"]]) 373 if "squashfs_block_size" in prop_dict: 374 build_command.extend(["-b", prop_dict["squashfs_block_size"]]) 375 if "squashfs_compressor" in prop_dict: 376 build_command.extend(["-z", prop_dict["squashfs_compressor"]]) 377 if "squashfs_compressor_opt" in prop_dict: 378 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) 379 if prop_dict.get("squashfs_disable_4k_align") == "true": 380 build_command.extend(["-a"]) 381 elif fs_type.startswith("f2fs"): 382 build_command = ["mkf2fsuserimg.sh"] 383 build_command.extend([out_file, prop_dict["image_size"]]) 384 if "f2fs_sparse_flag" in prop_dict and not disable_sparse: 385 build_command.extend([prop_dict["f2fs_sparse_flag"]]) 386 if fs_config: 387 build_command.extend(["-C", fs_config]) 388 build_command.extend(["-f", in_dir]) 389 if target_out: 390 build_command.extend(["-D", target_out]) 391 if "selinux_fc" in prop_dict: 392 build_command.extend(["-s", prop_dict["selinux_fc"]]) 393 build_command.extend(["-t", prop_dict["mount_point"]]) 394 if "timestamp" in prop_dict: 395 build_command.extend(["-T", str(prop_dict["timestamp"])]) 396 if "block_list" in prop_dict: 397 build_command.extend(["-B", prop_dict["block_list"]]) 398 build_command.extend(["-L", prop_dict["mount_point"]]) 399 if (needs_projid): 400 build_command.append("--prjquota") 401 if (needs_casefold): 402 build_command.append("--casefold") 403 if (needs_compress or prop_dict.get("f2fs_compress") == "true"): 404 build_command.append("--compression") 405 if (prop_dict.get("mount_point") != "data"): 406 build_command.append("--readonly") 407 if (prop_dict.get("f2fs_compress") == "true"): 408 build_command.append("--sldc") 409 if (prop_dict.get("f2fs_sldc_flags") == None): 410 build_command.append(str(0)) 411 else: 412 sldc_flags_str = prop_dict.get("f2fs_sldc_flags") 413 sldc_flags = sldc_flags_str.split() 414 build_command.append(str(len(sldc_flags))) 415 build_command.extend(sldc_flags) 416 else: 417 raise BuildImageError( 418 "Error: unknown filesystem type: {}".format(fs_type)) 419 420 try: 421 mkfs_output = common.RunAndCheckOutput(build_command) 422 except: 423 try: 424 du = GetDiskUsage(in_dir) 425 du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB) 426 # Suppress any errors from GetDiskUsage() to avoid hiding the real errors 427 # from common.RunAndCheckOutput(). 428 except Exception: # pylint: disable=broad-except 429 logger.exception("Failed to compute disk usage with du") 430 du_str = "unknown" 431 print( 432 "Out of space? Out of inodes? The tree size of {} is {}, " 433 "with reserved space of {} bytes ({} MB).".format( 434 in_dir, du_str, 435 int(prop_dict.get("partition_reserved_size", 0)), 436 int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB)) 437 if ("image_size" in prop_dict and "partition_size" in prop_dict): 438 print( 439 "The max image size for filesystem files is {} bytes ({} MB), " 440 "out of a total partition size of {} bytes ({} MB).".format( 441 int(prop_dict["image_size"]), 442 int(prop_dict["image_size"]) // BYTES_IN_MB, 443 int(prop_dict["partition_size"]), 444 int(prop_dict["partition_size"]) // BYTES_IN_MB)) 445 raise 446 447 if run_fsck and prop_dict.get("skip_fsck") != "true": 448 run_fsck(out_file) 449 450 if manual_sparse: 451 temp_file = out_file + ".sparse" 452 img2simg_argv = ["img2simg", out_file, temp_file] 453 common.RunAndCheckOutput(img2simg_argv) 454 os.rename(temp_file, out_file) 455 456 return mkfs_output 457 458 459def RunE2fsck(out_file): 460 unsparse_image = UnsparseImage(out_file, replace=False) 461 462 # Run e2fsck on the inflated image file 463 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] 464 try: 465 common.RunAndCheckOutput(e2fsck_command) 466 finally: 467 os.remove(unsparse_image) 468 469 470def RunErofsFsck(out_file): 471 fsck_command = ["fsck.erofs", "--extract", out_file] 472 try: 473 common.RunAndCheckOutput(fsck_command) 474 except: 475 print("Check failed for EROFS image {}".format(out_file)) 476 raise 477 478 479def BuildImage(in_dir, prop_dict, out_file, target_out=None): 480 """Builds an image for the files under in_dir and writes it to out_file. 481 482 Args: 483 in_dir: Path to input directory. 484 prop_dict: A property dict that contains info like partition size. Values 485 will be updated with computed values. 486 out_file: The output image file. 487 target_out: Path to the TARGET_OUT directory as in Makefile. It actually 488 points to the /system directory under PRODUCT_OUT. fs_config (the one 489 under system/core/libcutils) reads device specific FS config files from 490 there. 491 492 Raises: 493 BuildImageError: On build image failures. 494 """ 495 in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict) 496 497 build_command = [] 498 fs_type = prop_dict.get("fs_type", "") 499 500 fs_spans_partition = True 501 if fs_type.startswith("squash") or fs_type.startswith("erofs"): 502 fs_spans_partition = False 503 elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true": 504 fs_spans_partition = False 505 506 # Get a builder for creating an image that's to be verified by Verified Boot, 507 # or None if not applicable. 508 verity_image_builder = verity_utils.CreateVerityImageBuilder(prop_dict) 509 510 disable_sparse = "disable_sparse" in prop_dict 511 mkfs_output = None 512 if (prop_dict.get("use_dynamic_partition_size") == "true" and 513 "partition_size" not in prop_dict): 514 # If partition_size is not defined, use output of `du' + reserved_size. 515 # For compressed file system, it's better to use the compressed size to avoid wasting space. 516 if fs_type.startswith("erofs"): 517 mkfs_output = BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) 518 if "erofs_sparse_flag" in prop_dict and not disable_sparse: 519 image_path = UnsparseImage(out_file, replace=False) 520 size = GetDiskUsage(image_path) 521 os.remove(image_path) 522 else: 523 size = GetDiskUsage(out_file) 524 else: 525 size = GetDiskUsage(in_dir) 526 logger.info( 527 "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB) 528 size = CalculateSizeAndReserved(prop_dict, size) 529 # Round this up to a multiple of 4K so that avbtool works 530 size = common.RoundUpTo4K(size) 531 if fs_type.startswith("ext"): 532 prop_dict["partition_size"] = str(size) 533 prop_dict["image_size"] = str(size) 534 if "extfs_inode_count" not in prop_dict: 535 prop_dict["extfs_inode_count"] = str(GetInodeUsage(in_dir)) 536 logger.info( 537 "First Pass based on estimates of %d MB and %s inodes.", 538 size // BYTES_IN_MB, prop_dict["extfs_inode_count"]) 539 BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) 540 sparse_image = False 541 if "extfs_sparse_flag" in prop_dict and not disable_sparse: 542 sparse_image = True 543 fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image) 544 os.remove(out_file) 545 block_size = int(fs_dict.get("Block size", "4096")) 546 free_size = int(fs_dict.get("Free blocks", "0")) * block_size 547 reserved_size = int(prop_dict.get("partition_reserved_size", 0)) 548 partition_headroom = int(fs_dict.get("partition_headroom", 0)) 549 if fs_type.startswith("ext4") and partition_headroom > reserved_size: 550 reserved_size = partition_headroom 551 if free_size <= reserved_size: 552 logger.info( 553 "Not worth reducing image %d <= %d.", free_size, reserved_size) 554 else: 555 size -= free_size 556 size += reserved_size 557 if reserved_size == 0: 558 # add .3% margin 559 size = size * 1003 // 1000 560 # Use a minimum size, otherwise we will fail to calculate an AVB footer 561 # or fail to construct an ext4 image. 562 size = max(size, 256 * 1024) 563 if block_size <= 4096: 564 size = common.RoundUpTo4K(size) 565 else: 566 size = ((size + block_size - 1) // block_size) * block_size 567 extfs_inode_count = prop_dict["extfs_inode_count"] 568 inodes = int(fs_dict.get("Inode count", extfs_inode_count)) 569 inodes -= int(fs_dict.get("Free inodes", "0")) 570 # add .2% margin or 1 inode, whichever is greater 571 spare_inodes = inodes * 2 // 1000 572 min_spare_inodes = 1 573 if spare_inodes < min_spare_inodes: 574 spare_inodes = min_spare_inodes 575 inodes += spare_inodes 576 prop_dict["extfs_inode_count"] = str(inodes) 577 prop_dict["partition_size"] = str(size) 578 logger.info( 579 "Allocating %d Inodes for %s.", inodes, out_file) 580 elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true": 581 prop_dict["partition_size"] = str(size) 582 prop_dict["image_size"] = str(size) 583 BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) 584 sparse_image = False 585 if "f2fs_sparse_flag" in prop_dict and not disable_sparse: 586 sparse_image = True 587 fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image) 588 os.remove(out_file) 589 block_count = int(fs_dict.get("block_count", "0")) 590 log_blocksize = int(fs_dict.get("log_blocksize", "12")) 591 size = block_count << log_blocksize 592 prop_dict["partition_size"] = str(size) 593 if verity_image_builder: 594 size = verity_image_builder.CalculateDynamicPartitionSize(size) 595 prop_dict["partition_size"] = str(size) 596 logger.info( 597 "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file) 598 599 prop_dict["image_size"] = prop_dict["partition_size"] 600 601 # Adjust the image size to make room for the hashes if this is to be verified. 602 if verity_image_builder: 603 max_image_size = verity_image_builder.CalculateMaxImageSize() 604 prop_dict["image_size"] = str(max_image_size) 605 606 if not mkfs_output: 607 mkfs_output = BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) 608 609 # Update the image (eg filesystem size). This can be different eg if mkfs 610 # rounds the requested size down due to alignment. 611 prop_dict["image_size"] = common.sparse_img.GetImagePartitionSize(out_file) 612 613 # Check if there's enough headroom space available for ext4 image. 614 if "partition_headroom" in prop_dict and fs_type.startswith("ext4"): 615 CheckHeadroom(mkfs_output, prop_dict) 616 617 if not fs_spans_partition and verity_image_builder: 618 verity_image_builder.PadSparseImage(out_file) 619 620 # Create the verified image if this is to be verified. 621 if verity_image_builder: 622 verity_image_builder.Build(out_file) 623 624def ImagePropFromGlobalDict(glob_dict, mount_point): 625 """Build an image property dictionary from the global dictionary. 626 627 Args: 628 glob_dict: the global dictionary from the build system. 629 mount_point: such as "system", "data" etc. 630 """ 631 d = {} 632 633 if "build.prop" in glob_dict: 634 timestamp = glob_dict["build.prop"].GetProp("ro.build.date.utc") 635 if timestamp: 636 d["timestamp"] = timestamp 637 638 def copy_prop(src_p, dest_p): 639 """Copy a property from the global dictionary. 640 641 Args: 642 src_p: The source property in the global dictionary. 643 dest_p: The destination property. 644 Returns: 645 True if property was found and copied, False otherwise. 646 """ 647 if src_p in glob_dict: 648 d[dest_p] = str(glob_dict[src_p]) 649 return True 650 return False 651 652 common_props = ( 653 "extfs_sparse_flag", 654 "erofs_default_compressor", 655 "erofs_pcluster_size", 656 "erofs_share_dup_blocks", 657 "erofs_sparse_flag", 658 "erofs_use_legacy_compression", 659 "squashfs_sparse_flag", 660 "system_f2fs_compress", 661 "system_f2fs_sldc_flags", 662 "f2fs_sparse_flag", 663 "skip_fsck", 664 "ext_mkuserimg", 665 "verity", 666 "verity_key", 667 "verity_signer_cmd", 668 "verity_fec", 669 "verity_disable", 670 "avb_enable", 671 "avb_avbtool", 672 "use_dynamic_partition_size", 673 ) 674 for p in common_props: 675 copy_prop(p, p) 676 677 ro_mount_points = set([ 678 "odm", 679 "odm_dlkm", 680 "oem", 681 "product", 682 "system", 683 "system_dlkm", 684 "system_ext", 685 "system_other", 686 "vendor", 687 "vendor_dlkm", 688 ]) 689 690 # Tuple layout: (readonly, specific prop, general prop) 691 fmt_props = ( 692 # Generic first, then specific file type. 693 (False, "fs_type", "fs_type"), 694 (False, "{}_fs_type", "fs_type"), 695 696 # Ordering for these doesn't matter. 697 (False, "{}_selinux_fc", "selinux_fc"), 698 (False, "{}_size", "partition_size"), 699 (True, "avb_{}_add_hashtree_footer_args", "avb_add_hashtree_footer_args"), 700 (True, "avb_{}_algorithm", "avb_algorithm"), 701 (True, "avb_{}_hashtree_enable", "avb_hashtree_enable"), 702 (True, "avb_{}_key_path", "avb_key_path"), 703 (True, "avb_{}_salt", "avb_salt"), 704 (True, "erofs_use_legacy_compression", "erofs_use_legacy_compression"), 705 (True, "ext4_share_dup_blocks", "ext4_share_dup_blocks"), 706 (True, "{}_base_fs_file", "base_fs_file"), 707 (True, "{}_disable_sparse", "disable_sparse"), 708 (True, "{}_erofs_compressor", "erofs_compressor"), 709 (True, "{}_erofs_pcluster_size", "erofs_pcluster_size"), 710 (True, "{}_erofs_share_dup_blocks", "erofs_share_dup_blocks"), 711 (True, "{}_extfs_inode_count", "extfs_inode_count"), 712 (True, "{}_f2fs_compress", "f2fs_compress"), 713 (True, "{}_f2fs_sldc_flags", "f2fs_sldc_flags"), 714 (True, "{}_reserved_size", "partition_reserved_size"), 715 (True, "{}_squashfs_block_size", "squashfs_block_size"), 716 (True, "{}_squashfs_compressor", "squashfs_compressor"), 717 (True, "{}_squashfs_compressor_opt", "squashfs_compressor_opt"), 718 (True, "{}_squashfs_disable_4k_align", "squashfs_disable_4k_align"), 719 (True, "{}_verity_block_device", "verity_block_device"), 720 ) 721 722 # Translate prefixed properties into generic ones. 723 if mount_point == "data": 724 prefix = "userdata" 725 else: 726 prefix = mount_point 727 728 for readonly, src_prop, dest_prop in fmt_props: 729 if readonly and mount_point not in ro_mount_points: 730 continue 731 732 if src_prop == "fs_type": 733 # This property is legacy and only used on a few partitions. b/202600377 734 allowed_partitions = set(["system", "system_other", "data", "oem"]) 735 if mount_point not in allowed_partitions: 736 continue 737 738 if (mount_point == "system_other") and (dest_prop != "partition_size"): 739 # Propagate system properties to system_other. They'll get overridden 740 # after as needed. 741 copy_prop(src_prop.format("system"), dest_prop) 742 743 copy_prop(src_prop.format(prefix), dest_prop) 744 745 # Set prefixed properties that need a default value. 746 if mount_point in ro_mount_points: 747 prop = "{}_journal_size".format(prefix) 748 if not copy_prop(prop, "journal_size"): 749 d["journal_size"] = "0" 750 751 prop = "{}_extfs_rsv_pct".format(prefix) 752 if not copy_prop(prop, "extfs_rsv_pct"): 753 d["extfs_rsv_pct"] = "0" 754 755 # Copy partition-specific properties. 756 d["mount_point"] = mount_point 757 if mount_point == "system": 758 copy_prop("system_headroom", "partition_headroom") 759 copy_prop("system_root_image", "system_root_image") 760 copy_prop("root_dir", "root_dir") 761 copy_prop("root_fs_config", "root_fs_config") 762 elif mount_point == "data": 763 # Copy the generic fs type first, override with specific one if available. 764 copy_prop("flash_logical_block_size", "flash_logical_block_size") 765 copy_prop("flash_erase_block_size", "flash_erase_block_size") 766 copy_prop("needs_casefold", "needs_casefold") 767 copy_prop("needs_projid", "needs_projid") 768 copy_prop("needs_compress", "needs_compress") 769 d["partition_name"] = mount_point 770 return d 771 772 773def LoadGlobalDict(filename): 774 """Load "name=value" pairs from filename""" 775 d = {} 776 f = open(filename) 777 for line in f: 778 line = line.strip() 779 if not line or line.startswith("#"): 780 continue 781 k, v = line.split("=", 1) 782 d[k] = v 783 f.close() 784 return d 785 786 787def GlobalDictFromImageProp(image_prop, mount_point): 788 d = {} 789 def copy_prop(src_p, dest_p): 790 if src_p in image_prop: 791 d[dest_p] = image_prop[src_p] 792 return True 793 return False 794 795 if mount_point == "system": 796 copy_prop("partition_size", "system_size") 797 elif mount_point == "system_other": 798 copy_prop("partition_size", "system_other_size") 799 elif mount_point == "vendor": 800 copy_prop("partition_size", "vendor_size") 801 elif mount_point == "odm": 802 copy_prop("partition_size", "odm_size") 803 elif mount_point == "vendor_dlkm": 804 copy_prop("partition_size", "vendor_dlkm_size") 805 elif mount_point == "odm_dlkm": 806 copy_prop("partition_size", "odm_dlkm_size") 807 elif mount_point == "system_dlkm": 808 copy_prop("partition_size", "system_dlkm_size") 809 elif mount_point == "product": 810 copy_prop("partition_size", "product_size") 811 elif mount_point == "system_ext": 812 copy_prop("partition_size", "system_ext_size") 813 return d 814 815 816def main(argv): 817 if len(argv) != 4: 818 print(__doc__) 819 sys.exit(1) 820 821 common.InitLogging() 822 823 in_dir = argv[0] 824 glob_dict_file = argv[1] 825 out_file = argv[2] 826 target_out = argv[3] 827 828 glob_dict = LoadGlobalDict(glob_dict_file) 829 if "mount_point" in glob_dict: 830 # The caller knows the mount point and provides a dictionary needed by 831 # BuildImage(). 832 image_properties = glob_dict 833 else: 834 image_filename = os.path.basename(out_file) 835 mount_point = "" 836 if image_filename == "system.img": 837 mount_point = "system" 838 elif image_filename == "system_other.img": 839 mount_point = "system_other" 840 elif image_filename == "userdata.img": 841 mount_point = "data" 842 elif image_filename == "cache.img": 843 mount_point = "cache" 844 elif image_filename == "vendor.img": 845 mount_point = "vendor" 846 elif image_filename == "odm.img": 847 mount_point = "odm" 848 elif image_filename == "vendor_dlkm.img": 849 mount_point = "vendor_dlkm" 850 elif image_filename == "odm_dlkm.img": 851 mount_point = "odm_dlkm" 852 elif image_filename == "system_dlkm.img": 853 mount_point = "system_dlkm" 854 elif image_filename == "oem.img": 855 mount_point = "oem" 856 elif image_filename == "product.img": 857 mount_point = "product" 858 elif image_filename == "system_ext.img": 859 mount_point = "system_ext" 860 else: 861 logger.error("Unknown image file name %s", image_filename) 862 sys.exit(1) 863 864 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) 865 866 try: 867 BuildImage(in_dir, image_properties, out_file, target_out) 868 except: 869 logger.error("Failed to build %s from %s", out_file, in_dir) 870 raise 871 872 873if __name__ == '__main__': 874 try: 875 main(sys.argv[1:]) 876 finally: 877 common.Cleanup() 878