Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 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 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 18 | Builds output_image from the given input_directory, properties_file, |
| 19 | and writes the image to target_output_directory. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 20 | |
Tao Bao | 2bbb07c | 2019-05-07 13:12:21 -0700 | [diff] [blame] | 21 | Usage: build_image input_directory properties_file output_image \\ |
Yifan Hong | 8c3dce0 | 2019-04-09 17:03:57 +0000 | [diff] [blame] | 22 | target_output_directory |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 23 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 24 | |
| 25 | from __future__ import print_function |
| 26 | |
Inseob Kim | 9cda397 | 2021-10-12 22:59:12 +0900 | [diff] [blame] | 27 | import glob |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 28 | import logging |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 29 | import os |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 30 | import os.path |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 31 | import re |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 32 | import shutil |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 33 | import sys |
| 34 | |
| 35 | import common |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 36 | import verity_utils |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 37 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 38 | logger = logging.getLogger(__name__) |
| 39 | |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 40 | OPTIONS = common.OPTIONS |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 41 | BLOCK_SIZE = common.BLOCK_SIZE |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 42 | BYTES_IN_MB = 1024 * 1024 |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 43 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 44 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 45 | class BuildImageError(Exception): |
| 46 | """An Exception raised during image building.""" |
| 47 | |
| 48 | def __init__(self, message): |
| 49 | Exception.__init__(self, message) |
| 50 | |
| 51 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 52 | def GetDiskUsage(path): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 53 | """Returns the number of bytes that "path" occupies on host. |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 54 | |
| 55 | Args: |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 56 | path: The directory or file to calculate size on. |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 57 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 58 | Returns: |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 59 | The number of bytes based on a 1K block_size. |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 60 | """ |
Chirayu Desai | 96a913e | 2020-03-27 03:49:31 +0530 | [diff] [blame] | 61 | cmd = ["du", "-b", "-k", "-s", path] |
Tao Bao | f3fc62c | 2018-10-25 12:23:12 -0700 | [diff] [blame] | 62 | output = common.RunAndCheckOutput(cmd, verbose=False) |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 63 | return int(output.split()[0]) * 1024 |
| 64 | |
| 65 | |
| 66 | def 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. |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 74 | """ |
| 75 | cmd = ["find", path, "-print"] |
Tao Bao | f3fc62c | 2018-10-25 12:23:12 -0700 | [diff] [blame] | 76 | output = common.RunAndCheckOutput(cmd, verbose=False) |
David Anderson | 203057c | 2021-03-31 20:01:41 -0700 | [diff] [blame] | 77 | # increase by > 6% as number of files and directories is not whole picture. |
Mark Salyzyn | c25b2bf | 2019-01-16 08:03:10 -0800 | [diff] [blame] | 78 | inodes = output.count('\n') |
David Anderson | 203057c | 2021-03-31 20:01:41 -0700 | [diff] [blame] | 79 | spare_inodes = inodes * 6 // 100 |
Mark Salyzyn | 60fa99d | 2019-01-16 08:03:10 -0800 | [diff] [blame] | 80 | min_spare_inodes = 12 |
Mark Salyzyn | c25b2bf | 2019-01-16 08:03:10 -0800 | [diff] [blame] | 81 | if spare_inodes < min_spare_inodes: |
| 82 | spare_inodes = min_spare_inodes |
| 83 | return inodes + spare_inodes |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 84 | |
| 85 | |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 86 | def GetFilesystemCharacteristics(fs_type, image_path, sparse_image=True): |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 87 | """Returns various filesystem characteristics of "image_path". |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 88 | |
| 89 | Args: |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 90 | image_path: The file to analyze. |
| 91 | sparse_image: Image is sparse |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 92 | |
| 93 | Returns: |
| 94 | The characteristics dictionary. |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 95 | """ |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 96 | unsparse_image_path = image_path |
| 97 | if sparse_image: |
| 98 | unsparse_image_path = UnsparseImage(image_path, replace=False) |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 99 | |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 105 | try: |
| 106 | output = common.RunAndCheckOutput(cmd, verbose=False) |
Tao Bao | f3fc62c | 2018-10-25 12:23:12 -0700 | [diff] [blame] | 107 | finally: |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 108 | if sparse_image: |
| 109 | os.remove(unsparse_image_path) |
Tao Bao | f3fc62c | 2018-10-25 12:23:12 -0700 | [diff] [blame] | 110 | fs_dict = {} |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 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 |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 116 | |
| 117 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 118 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 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): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 123 | if replace: |
| 124 | os.unlink(unsparse_image_path) |
| 125 | else: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 126 | return unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 127 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 128 | try: |
| 129 | common.RunAndCheckOutput(inflate_command) |
| 130 | except: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 131 | os.remove(unsparse_image_path) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 132 | raise |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 133 | return unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 134 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 135 | |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 136 | def ConvertBlockMapToBaseFs(block_map_file): |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 137 | base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 138 | convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 139 | common.RunAndCheckOutput(convert_command) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 140 | return base_fs_file |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 141 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 142 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 143 | def SetUpInDirAndFsConfig(origin_in, prop_dict): |
| 144 | """Returns the in_dir and fs_config that should be used for image building. |
| 145 | |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 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. |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 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") |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 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": |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 164 | return origin_in, fs_config |
| 165 | |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 166 | if "first_pass" in prop_dict: |
| 167 | prop_dict["mount_point"] = "/" |
| 168 | return prop_dict["first_pass"] |
| 169 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 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 |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 193 | prop_dict["first_pass"] = (in_dir, fs_config) |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 194 | return in_dir, fs_config |
| 195 | |
| 196 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 197 | def 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 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 209 | Raises: |
| 210 | AssertionError: On invalid input. |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 211 | BuildImageError: On check failure. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 212 | """ |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 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 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 218 | ext4fs_stats = re.compile( |
| 219 | r'Created filesystem with .* (?P<used_blocks>[0-9]+)/' |
| 220 | r'(?P<total_blocks>[0-9]+) blocks') |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 221 | last_line = ext4fs_output.strip().split('\n')[-1] |
| 222 | m = ext4fs_stats.match(last_line) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 223 | used_blocks = int(m.groupdict().get('used_blocks')) |
| 224 | total_blocks = int(m.groupdict().get('total_blocks')) |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 225 | headroom_blocks = int(prop_dict['partition_headroom']) // BLOCK_SIZE |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 226 | adjusted_blocks = total_blocks - headroom_blocks |
| 227 | if used_blocks > adjusted_blocks: |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 228 | mount_point = prop_dict["mount_point"] |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 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)) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 234 | |
Huang Jianan | 6552727 | 2021-09-08 18:28:32 +0800 | [diff] [blame] | 235 | def 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 |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 251 | |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 252 | def 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. |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 254 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 255 | Args: |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 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. |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 264 | fs_config: The fs_config file that drives the prototype |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 265 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 266 | Raises: |
| 267 | BuildImageError: On build image failures. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 268 | """ |
| 269 | build_command = [] |
| 270 | fs_type = prop_dict.get("fs_type", "") |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 271 | run_fsck = None |
Daniel Rosenberg | 6cc2c81 | 2019-12-17 17:36:31 -0800 | [diff] [blame] | 272 | needs_projid = prop_dict.get("needs_projid", 0) |
| 273 | needs_casefold = prop_dict.get("needs_casefold", 0) |
Jaegeuk Kim | ed754fb | 2020-10-12 19:50:05 -0700 | [diff] [blame] | 274 | needs_compress = prop_dict.get("needs_compress", 0) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 275 | |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 276 | disable_sparse = "disable_sparse" in prop_dict |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 277 | manual_sparse = False |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 278 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 279 | if fs_type.startswith("ext"): |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 280 | build_command = [prop_dict["ext_mkuserimg"]] |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 281 | if "extfs_sparse_flag" in prop_dict and not disable_sparse: |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 282 | build_command.append(prop_dict["extfs_sparse_flag"]) |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 283 | run_e2fsck = RunE2fsck |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 284 | build_command.extend([in_dir, out_file, fs_type, |
| 285 | prop_dict["mount_point"]]) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 286 | build_command.append(prop_dict["image_size"]) |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 287 | if "journal_size" in prop_dict: |
| 288 | build_command.extend(["-j", prop_dict["journal_size"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 289 | if "timestamp" in prop_dict: |
| 290 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 291 | if fs_config: |
Doug Zongker | 8282282 | 2014-06-16 09:10:55 -0700 | [diff] [blame] | 292 | build_command.extend(["-C", fs_config]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 293 | if target_out: |
| 294 | build_command.extend(["-D", target_out]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 295 | if "block_list" in prop_dict: |
| 296 | build_command.extend(["-B", prop_dict["block_list"]]) |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 297 | if "base_fs_file" in prop_dict: |
| 298 | base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"]) |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 299 | build_command.extend(["-d", base_fs_file]) |
Christoffer Dall | 8ed01f3 | 2014-12-17 21:34:12 +0100 | [diff] [blame] | 300 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 301 | if "extfs_inode_count" in prop_dict: |
| 302 | build_command.extend(["-i", prop_dict["extfs_inode_count"]]) |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 303 | if "extfs_rsv_pct" in prop_dict: |
| 304 | build_command.extend(["-M", prop_dict["extfs_rsv_pct"]]) |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 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"]]) |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 309 | # Specify UUID and hash_seed if using mke2fs. |
HÃ¥kan Kvist | 2e1f527 | 2021-05-11 11:14:48 +0200 | [diff] [blame] | 310 | if os.path.basename(prop_dict["ext_mkuserimg"]) == "mkuserimg_mke2fs": |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 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"]]) |
Tamas Petz | c0a8c63 | 2020-02-03 15:41:02 +0100 | [diff] [blame] | 315 | if prop_dict.get("ext4_share_dup_blocks") == "true": |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 316 | build_command.append("-c") |
Daniel Rosenberg | 6cc2c81 | 2019-12-17 17:36:31 -0800 | [diff] [blame] | 317 | if (needs_projid): |
| 318 | build_command.extend(["--inode_size", "512"]) |
| 319 | else: |
| 320 | build_command.extend(["--inode_size", "256"]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 321 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 322 | build_command.append(prop_dict["selinux_fc"]) |
Gao Xiang | 961041a | 2020-06-17 13:59:16 +0800 | [diff] [blame] | 323 | elif fs_type.startswith("erofs"): |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 324 | build_command = ["mkfs.erofs"] |
| 325 | |
David Anderson | 40a821f | 2021-09-22 18:02:01 -0700 | [diff] [blame] | 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]) |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 333 | |
Dmitrii Merkurev | 8ab6603 | 2022-05-17 23:10:37 +0000 | [diff] [blame^] | 334 | compress_hints = None |
| 335 | if "erofs_default_compress_hints" in prop_dict: |
| 336 | compress_hints = prop_dict["erofs_default_compress_hints"] |
| 337 | if "erofs_compress_hints" in prop_dict: |
| 338 | compress_hints = prop_dict["erofs_compress_hints"] |
| 339 | if compress_hints: |
| 340 | build_command.extend(["--compress-hints", compress_hints]) |
| 341 | |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 342 | build_command.extend(["--mount-point", prop_dict["mount_point"]]) |
| 343 | if target_out: |
| 344 | build_command.extend(["--product-out", target_out]) |
| 345 | if fs_config: |
| 346 | build_command.extend(["--fs-config-file", fs_config]) |
| 347 | if "selinux_fc" in prop_dict: |
| 348 | build_command.extend(["--file-contexts", prop_dict["selinux_fc"]]) |
David Anderson | d29e537 | 2021-10-08 18:33:43 -0700 | [diff] [blame] | 349 | if "timestamp" in prop_dict: |
| 350 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
| 351 | if "uuid" in prop_dict: |
| 352 | build_command.extend(["-U", prop_dict["uuid"]]) |
| 353 | if "block_list" in prop_dict: |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 354 | build_command.extend(["--block-list-file", prop_dict["block_list"]]) |
David Anderson | 64b351b | 2021-10-13 00:20:43 -0700 | [diff] [blame] | 355 | if "erofs_pcluster_size" in prop_dict: |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 356 | build_command.extend(["-C", prop_dict["erofs_pcluster_size"]]) |
David Anderson | 64b351b | 2021-10-13 00:20:43 -0700 | [diff] [blame] | 357 | if "erofs_share_dup_blocks" in prop_dict: |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 358 | build_command.extend(["--chunksize", "4096"]) |
David Anderson | f54665f | 2022-03-04 14:42:18 -0800 | [diff] [blame] | 359 | if "erofs_use_legacy_compression" in prop_dict: |
| 360 | build_command.extend(["-E", "legacy-compress"]) |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 361 | |
| 362 | build_command.extend([out_file, in_dir]) |
| 363 | if "erofs_sparse_flag" in prop_dict and not disable_sparse: |
| 364 | manual_sparse = True |
| 365 | |
| 366 | run_fsck = RunErofsFsck |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 367 | elif fs_type.startswith("squash"): |
| 368 | build_command = ["mksquashfsimage.sh"] |
| 369 | build_command.extend([in_dir, out_file]) |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 370 | if "squashfs_sparse_flag" in prop_dict and not disable_sparse: |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 371 | build_command.extend([prop_dict["squashfs_sparse_flag"]]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 372 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 373 | if target_out: |
| 374 | build_command.extend(["-d", target_out]) |
Mohamad Ayyash | 8837882 | 2016-04-07 22:10:51 -0700 | [diff] [blame] | 375 | if fs_config: |
| 376 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 377 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 378 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Mohamad Ayyash | c3484f7 | 2016-06-13 09:46:58 -0700 | [diff] [blame] | 379 | if "block_list" in prop_dict: |
| 380 | build_command.extend(["-B", prop_dict["block_list"]]) |
Ng Zhi An | 9446c1d | 2018-01-19 15:51:46 -0800 | [diff] [blame] | 381 | if "squashfs_block_size" in prop_dict: |
| 382 | build_command.extend(["-b", prop_dict["squashfs_block_size"]]) |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 383 | if "squashfs_compressor" in prop_dict: |
| 384 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 385 | if "squashfs_compressor_opt" in prop_dict: |
| 386 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 387 | if prop_dict.get("squashfs_disable_4k_align") == "true": |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 388 | build_command.extend(["-a"]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 389 | elif fs_type.startswith("f2fs"): |
| 390 | build_command = ["mkf2fsuserimg.sh"] |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 391 | build_command.extend([out_file, prop_dict["image_size"]]) |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 392 | if "f2fs_sparse_flag" in prop_dict and not disable_sparse: |
Alistair Delva | 91238cc | 2019-10-16 10:53:41 -0700 | [diff] [blame] | 393 | build_command.extend([prop_dict["f2fs_sparse_flag"]]) |
Jaegeuk Kim | 2ea1eba | 2017-11-28 19:21:28 -0800 | [diff] [blame] | 394 | if fs_config: |
| 395 | build_command.extend(["-C", fs_config]) |
| 396 | build_command.extend(["-f", in_dir]) |
| 397 | if target_out: |
| 398 | build_command.extend(["-D", target_out]) |
| 399 | if "selinux_fc" in prop_dict: |
| 400 | build_command.extend(["-s", prop_dict["selinux_fc"]]) |
| 401 | build_command.extend(["-t", prop_dict["mount_point"]]) |
| 402 | if "timestamp" in prop_dict: |
| 403 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Jaegeuk Kim | 3dc4728 | 2021-06-13 08:54:01 -0700 | [diff] [blame] | 404 | if "block_list" in prop_dict: |
| 405 | build_command.extend(["-B", prop_dict["block_list"]]) |
Jaegeuk Kim | 2ea1eba | 2017-11-28 19:21:28 -0800 | [diff] [blame] | 406 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Daniel Rosenberg | 6cc2c81 | 2019-12-17 17:36:31 -0800 | [diff] [blame] | 407 | if (needs_projid): |
| 408 | build_command.append("--prjquota") |
| 409 | if (needs_casefold): |
| 410 | build_command.append("--casefold") |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 411 | if (needs_compress or prop_dict.get("f2fs_compress") == "true"): |
Jaegeuk Kim | ed754fb | 2020-10-12 19:50:05 -0700 | [diff] [blame] | 412 | build_command.append("--compression") |
Jaegeuk Kim | 3dc4728 | 2021-06-13 08:54:01 -0700 | [diff] [blame] | 413 | if (prop_dict.get("mount_point") != "data"): |
Jaegeuk Kim | 46e0ea2 | 2021-05-20 23:13:59 -0700 | [diff] [blame] | 414 | build_command.append("--readonly") |
Jaegeuk Kim | 3dc4728 | 2021-06-13 08:54:01 -0700 | [diff] [blame] | 415 | if (prop_dict.get("f2fs_compress") == "true"): |
Robin Hsu | 3e51f42 | 2020-11-04 09:29:09 +0800 | [diff] [blame] | 416 | build_command.append("--sldc") |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 417 | if (prop_dict.get("f2fs_sldc_flags") == None): |
Robin Hsu | 3e51f42 | 2020-11-04 09:29:09 +0800 | [diff] [blame] | 418 | build_command.append(str(0)) |
| 419 | else: |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 420 | sldc_flags_str = prop_dict.get("f2fs_sldc_flags") |
Robin Hsu | 3e51f42 | 2020-11-04 09:29:09 +0800 | [diff] [blame] | 421 | sldc_flags = sldc_flags_str.split() |
| 422 | build_command.append(str(len(sldc_flags))) |
| 423 | build_command.extend(sldc_flags) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 424 | else: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 425 | raise BuildImageError( |
| 426 | "Error: unknown filesystem type: {}".format(fs_type)) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 427 | |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 428 | try: |
| 429 | mkfs_output = common.RunAndCheckOutput(build_command) |
| 430 | except: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 431 | try: |
| 432 | du = GetDiskUsage(in_dir) |
| 433 | du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 434 | # Suppress any errors from GetDiskUsage() to avoid hiding the real errors |
| 435 | # from common.RunAndCheckOutput(). |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 436 | except Exception: # pylint: disable=broad-except |
| 437 | logger.exception("Failed to compute disk usage with du") |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 438 | du_str = "unknown" |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 439 | print( |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 440 | "Out of space? Out of inodes? The tree size of {} is {}, " |
| 441 | "with reserved space of {} bytes ({} MB).".format( |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 442 | in_dir, du_str, |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 443 | int(prop_dict.get("partition_reserved_size", 0)), |
| 444 | int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB)) |
Huang Jianan | f63abb1 | 2021-04-29 15:24:50 +0800 | [diff] [blame] | 445 | if ("image_size" in prop_dict and "partition_size" in prop_dict): |
| 446 | print( |
| 447 | "The max image size for filesystem files is {} bytes ({} MB), " |
| 448 | "out of a total partition size of {} bytes ({} MB).".format( |
| 449 | int(prop_dict["image_size"]), |
| 450 | int(prop_dict["image_size"]) // BYTES_IN_MB, |
| 451 | int(prop_dict["partition_size"]), |
| 452 | int(prop_dict["partition_size"]) // BYTES_IN_MB)) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 453 | raise |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 454 | |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 455 | if run_fsck and prop_dict.get("skip_fsck") != "true": |
| 456 | run_fsck(out_file) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 457 | |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 458 | if manual_sparse: |
| 459 | temp_file = out_file + ".sparse" |
| 460 | img2simg_argv = ["img2simg", out_file, temp_file] |
| 461 | common.RunAndCheckOutput(img2simg_argv) |
| 462 | os.rename(temp_file, out_file) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 463 | |
| 464 | return mkfs_output |
| 465 | |
David Anderson | 94ad5bb | 2022-03-04 10:57:58 -0800 | [diff] [blame] | 466 | |
| 467 | def RunE2fsck(out_file): |
| 468 | unsparse_image = UnsparseImage(out_file, replace=False) |
| 469 | |
| 470 | # Run e2fsck on the inflated image file |
| 471 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
| 472 | try: |
| 473 | common.RunAndCheckOutput(e2fsck_command) |
| 474 | finally: |
| 475 | os.remove(unsparse_image) |
| 476 | |
| 477 | |
| 478 | def RunErofsFsck(out_file): |
| 479 | fsck_command = ["fsck.erofs", "--extract", out_file] |
| 480 | try: |
| 481 | common.RunAndCheckOutput(fsck_command) |
| 482 | except: |
| 483 | print("Check failed for EROFS image {}".format(out_file)) |
| 484 | raise |
| 485 | |
| 486 | |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 487 | def BuildImage(in_dir, prop_dict, out_file, target_out=None): |
| 488 | """Builds an image for the files under in_dir and writes it to out_file. |
| 489 | |
| 490 | Args: |
| 491 | in_dir: Path to input directory. |
| 492 | prop_dict: A property dict that contains info like partition size. Values |
| 493 | will be updated with computed values. |
| 494 | out_file: The output image file. |
| 495 | target_out: Path to the TARGET_OUT directory as in Makefile. It actually |
| 496 | points to the /system directory under PRODUCT_OUT. fs_config (the one |
| 497 | under system/core/libcutils) reads device specific FS config files from |
| 498 | there. |
| 499 | |
| 500 | Raises: |
| 501 | BuildImageError: On build image failures. |
| 502 | """ |
| 503 | in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict) |
| 504 | |
| 505 | build_command = [] |
| 506 | fs_type = prop_dict.get("fs_type", "") |
| 507 | |
| 508 | fs_spans_partition = True |
Huang Jianan | 62d926e | 2020-12-04 16:53:06 +0800 | [diff] [blame] | 509 | if fs_type.startswith("squash") or fs_type.startswith("erofs"): |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 510 | fs_spans_partition = False |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 511 | elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true": |
| 512 | fs_spans_partition = False |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 513 | |
| 514 | # Get a builder for creating an image that's to be verified by Verified Boot, |
| 515 | # or None if not applicable. |
| 516 | verity_image_builder = verity_utils.CreateVerityImageBuilder(prop_dict) |
| 517 | |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 518 | disable_sparse = "disable_sparse" in prop_dict |
Huang Jianan | ffa1d57 | 2021-09-08 18:11:22 +0800 | [diff] [blame] | 519 | mkfs_output = None |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 520 | if (prop_dict.get("use_dynamic_partition_size") == "true" and |
| 521 | "partition_size" not in prop_dict): |
| 522 | # If partition_size is not defined, use output of `du' + reserved_size. |
Huang Jianan | 35f015e | 2020-12-04 16:58:24 +0800 | [diff] [blame] | 523 | # For compressed file system, it's better to use the compressed size to avoid wasting space. |
| 524 | if fs_type.startswith("erofs"): |
Huang Jianan | ffa1d57 | 2021-09-08 18:11:22 +0800 | [diff] [blame] | 525 | mkfs_output = BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) |
| 526 | if "erofs_sparse_flag" in prop_dict and not disable_sparse: |
| 527 | image_path = UnsparseImage(out_file, replace=False) |
| 528 | size = GetDiskUsage(image_path) |
| 529 | os.remove(image_path) |
| 530 | else: |
| 531 | size = GetDiskUsage(out_file) |
Huang Jianan | 35f015e | 2020-12-04 16:58:24 +0800 | [diff] [blame] | 532 | else: |
| 533 | size = GetDiskUsage(in_dir) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 534 | logger.info( |
| 535 | "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB) |
Huang Jianan | 6552727 | 2021-09-08 18:28:32 +0800 | [diff] [blame] | 536 | size = CalculateSizeAndReserved(prop_dict, size) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 537 | # Round this up to a multiple of 4K so that avbtool works |
| 538 | size = common.RoundUpTo4K(size) |
| 539 | if fs_type.startswith("ext"): |
| 540 | prop_dict["partition_size"] = str(size) |
| 541 | prop_dict["image_size"] = str(size) |
| 542 | if "extfs_inode_count" not in prop_dict: |
| 543 | prop_dict["extfs_inode_count"] = str(GetInodeUsage(in_dir)) |
| 544 | logger.info( |
| 545 | "First Pass based on estimates of %d MB and %s inodes.", |
| 546 | size // BYTES_IN_MB, prop_dict["extfs_inode_count"]) |
| 547 | BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 548 | sparse_image = False |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 549 | if "extfs_sparse_flag" in prop_dict and not disable_sparse: |
Mark Salyzyn | 6541d0a | 2019-01-10 14:30:51 -0800 | [diff] [blame] | 550 | sparse_image = True |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 551 | fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 552 | os.remove(out_file) |
| 553 | block_size = int(fs_dict.get("Block size", "4096")) |
| 554 | free_size = int(fs_dict.get("Free blocks", "0")) * block_size |
| 555 | reserved_size = int(prop_dict.get("partition_reserved_size", 0)) |
| 556 | partition_headroom = int(fs_dict.get("partition_headroom", 0)) |
| 557 | if fs_type.startswith("ext4") and partition_headroom > reserved_size: |
| 558 | reserved_size = partition_headroom |
| 559 | if free_size <= reserved_size: |
| 560 | logger.info( |
| 561 | "Not worth reducing image %d <= %d.", free_size, reserved_size) |
| 562 | else: |
| 563 | size -= free_size |
| 564 | size += reserved_size |
Mark Salyzyn | 60a716f | 2019-01-10 08:36:34 -0800 | [diff] [blame] | 565 | if reserved_size == 0: |
Mark Salyzyn | c25b2bf | 2019-01-16 08:03:10 -0800 | [diff] [blame] | 566 | # add .3% margin |
| 567 | size = size * 1003 // 1000 |
Mark Salyzyn | 60a716f | 2019-01-10 08:36:34 -0800 | [diff] [blame] | 568 | # Use a minimum size, otherwise we will fail to calculate an AVB footer |
| 569 | # or fail to construct an ext4 image. |
| 570 | size = max(size, 256 * 1024) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 571 | if block_size <= 4096: |
| 572 | size = common.RoundUpTo4K(size) |
| 573 | else: |
| 574 | size = ((size + block_size - 1) // block_size) * block_size |
| 575 | extfs_inode_count = prop_dict["extfs_inode_count"] |
| 576 | inodes = int(fs_dict.get("Inode count", extfs_inode_count)) |
| 577 | inodes -= int(fs_dict.get("Free inodes", "0")) |
Mark Salyzyn | c25b2bf | 2019-01-16 08:03:10 -0800 | [diff] [blame] | 578 | # add .2% margin or 1 inode, whichever is greater |
| 579 | spare_inodes = inodes * 2 // 1000 |
| 580 | min_spare_inodes = 1 |
| 581 | if spare_inodes < min_spare_inodes: |
| 582 | spare_inodes = min_spare_inodes |
| 583 | inodes += spare_inodes |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 584 | prop_dict["extfs_inode_count"] = str(inodes) |
| 585 | prop_dict["partition_size"] = str(size) |
| 586 | logger.info( |
| 587 | "Allocating %d Inodes for %s.", inodes, out_file) |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 588 | elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true": |
| 589 | prop_dict["partition_size"] = str(size) |
| 590 | prop_dict["image_size"] = str(size) |
| 591 | BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) |
| 592 | sparse_image = False |
David Anderson | 9e95a02 | 2021-08-31 21:32:45 -0700 | [diff] [blame] | 593 | if "f2fs_sparse_flag" in prop_dict and not disable_sparse: |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 594 | sparse_image = True |
| 595 | fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image) |
| 596 | os.remove(out_file) |
| 597 | block_count = int(fs_dict.get("block_count", "0")) |
| 598 | log_blocksize = int(fs_dict.get("log_blocksize", "12")) |
| 599 | size = block_count << log_blocksize |
| 600 | prop_dict["partition_size"] = str(size) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 601 | if verity_image_builder: |
| 602 | size = verity_image_builder.CalculateDynamicPartitionSize(size) |
| 603 | prop_dict["partition_size"] = str(size) |
| 604 | logger.info( |
| 605 | "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file) |
| 606 | |
| 607 | prop_dict["image_size"] = prop_dict["partition_size"] |
| 608 | |
| 609 | # Adjust the image size to make room for the hashes if this is to be verified. |
| 610 | if verity_image_builder: |
| 611 | max_image_size = verity_image_builder.CalculateMaxImageSize() |
| 612 | prop_dict["image_size"] = str(max_image_size) |
| 613 | |
Huang Jianan | ffa1d57 | 2021-09-08 18:11:22 +0800 | [diff] [blame] | 614 | if not mkfs_output: |
| 615 | mkfs_output = BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config) |
Mark Salyzyn | 3cd2460 | 2018-11-07 07:40:31 -0800 | [diff] [blame] | 616 | |
David Anderson | 009d6f8 | 2021-11-12 02:01:29 +0000 | [diff] [blame] | 617 | # Update the image (eg filesystem size). This can be different eg if mkfs |
| 618 | # rounds the requested size down due to alignment. |
| 619 | prop_dict["image_size"] = common.sparse_img.GetImagePartitionSize(out_file) |
| 620 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 621 | # Check if there's enough headroom space available for ext4 image. |
Tao Bao | 79d52f8 | 2017-12-07 14:07:44 -0800 | [diff] [blame] | 622 | if "partition_headroom" in prop_dict and fs_type.startswith("ext4"): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 623 | CheckHeadroom(mkfs_output, prop_dict) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 624 | |
Tao Bao | 7549e5e | 2018-10-03 14:23:59 -0700 | [diff] [blame] | 625 | if not fs_spans_partition and verity_image_builder: |
| 626 | verity_image_builder.PadSparseImage(out_file) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 627 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 628 | # Create the verified image if this is to be verified. |
Tao Bao | 7549e5e | 2018-10-03 14:23:59 -0700 | [diff] [blame] | 629 | if verity_image_builder: |
| 630 | verity_image_builder.Build(out_file) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 631 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 632 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 633 | """Build an image property dictionary from the global dictionary. |
| 634 | |
| 635 | Args: |
| 636 | glob_dict: the global dictionary from the build system. |
| 637 | mount_point: such as "system", "data" etc. |
| 638 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 639 | d = {} |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame] | 640 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 641 | if "build.prop" in glob_dict: |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 642 | timestamp = glob_dict["build.prop"].GetProp("ro.build.date.utc") |
| 643 | if timestamp: |
| 644 | d["timestamp"] = timestamp |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 645 | |
| 646 | def copy_prop(src_p, dest_p): |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 647 | """Copy a property from the global dictionary. |
| 648 | |
| 649 | Args: |
| 650 | src_p: The source property in the global dictionary. |
| 651 | dest_p: The destination property. |
| 652 | Returns: |
| 653 | True if property was found and copied, False otherwise. |
| 654 | """ |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 655 | if src_p in glob_dict: |
| 656 | d[dest_p] = str(glob_dict[src_p]) |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 657 | return True |
| 658 | return False |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 659 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 660 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 661 | "extfs_sparse_flag", |
David Anderson | 40a821f | 2021-09-22 18:02:01 -0700 | [diff] [blame] | 662 | "erofs_default_compressor", |
Dmitrii Merkurev | 8ab6603 | 2022-05-17 23:10:37 +0000 | [diff] [blame^] | 663 | "erofs_default_compress_hints", |
David Anderson | 64b351b | 2021-10-13 00:20:43 -0700 | [diff] [blame] | 664 | "erofs_pcluster_size", |
| 665 | "erofs_share_dup_blocks", |
Gao Xiang | 961041a | 2020-06-17 13:59:16 +0800 | [diff] [blame] | 666 | "erofs_sparse_flag", |
David Anderson | f54665f | 2022-03-04 14:42:18 -0800 | [diff] [blame] | 667 | "erofs_use_legacy_compression", |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 668 | "squashfs_sparse_flag", |
Jaegeuk Kim | 1369654 | 2021-05-22 09:47:48 -0700 | [diff] [blame] | 669 | "system_f2fs_compress", |
Robin Hsu | 3e51f42 | 2020-11-04 09:29:09 +0800 | [diff] [blame] | 670 | "system_f2fs_sldc_flags", |
Alistair Delva | 91238cc | 2019-10-16 10:53:41 -0700 | [diff] [blame] | 671 | "f2fs_sparse_flag", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 672 | "skip_fsck", |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 673 | "ext_mkuserimg", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 674 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 675 | "verity_key", |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 676 | "verity_signer_cmd", |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 677 | "verity_fec", |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 678 | "verity_disable", |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 679 | "avb_enable", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 680 | "avb_avbtool", |
Yifan Hong | 2dae572 | 2018-07-31 12:47:27 -0700 | [diff] [blame] | 681 | "use_dynamic_partition_size", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 682 | ) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 683 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 684 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 685 | |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 686 | ro_mount_points = set([ |
| 687 | "odm", |
| 688 | "odm_dlkm", |
| 689 | "oem", |
| 690 | "product", |
| 691 | "system", |
Ramji Jiyani | 13a4137 | 2022-01-27 07:05:08 +0000 | [diff] [blame] | 692 | "system_dlkm", |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 693 | "system_ext", |
| 694 | "system_other", |
| 695 | "vendor", |
| 696 | "vendor_dlkm", |
| 697 | ]) |
David Anderson | aac502f | 2021-09-23 15:48:29 -0700 | [diff] [blame] | 698 | |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 699 | # Tuple layout: (readonly, specific prop, general prop) |
| 700 | fmt_props = ( |
| 701 | # Generic first, then specific file type. |
| 702 | (False, "fs_type", "fs_type"), |
| 703 | (False, "{}_fs_type", "fs_type"), |
| 704 | |
| 705 | # Ordering for these doesn't matter. |
| 706 | (False, "{}_selinux_fc", "selinux_fc"), |
| 707 | (False, "{}_size", "partition_size"), |
| 708 | (True, "avb_{}_add_hashtree_footer_args", "avb_add_hashtree_footer_args"), |
| 709 | (True, "avb_{}_algorithm", "avb_algorithm"), |
| 710 | (True, "avb_{}_hashtree_enable", "avb_hashtree_enable"), |
| 711 | (True, "avb_{}_key_path", "avb_key_path"), |
| 712 | (True, "avb_{}_salt", "avb_salt"), |
David Anderson | f54665f | 2022-03-04 14:42:18 -0800 | [diff] [blame] | 713 | (True, "erofs_use_legacy_compression", "erofs_use_legacy_compression"), |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 714 | (True, "ext4_share_dup_blocks", "ext4_share_dup_blocks"), |
| 715 | (True, "{}_base_fs_file", "base_fs_file"), |
| 716 | (True, "{}_disable_sparse", "disable_sparse"), |
| 717 | (True, "{}_erofs_compressor", "erofs_compressor"), |
Dmitrii Merkurev | 8ab6603 | 2022-05-17 23:10:37 +0000 | [diff] [blame^] | 718 | (True, "{}_erofs_compress_hints", "erofs_compress_hints"), |
David Anderson | 64b351b | 2021-10-13 00:20:43 -0700 | [diff] [blame] | 719 | (True, "{}_erofs_pcluster_size", "erofs_pcluster_size"), |
| 720 | (True, "{}_erofs_share_dup_blocks", "erofs_share_dup_blocks"), |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 721 | (True, "{}_extfs_inode_count", "extfs_inode_count"), |
| 722 | (True, "{}_f2fs_compress", "f2fs_compress"), |
| 723 | (True, "{}_f2fs_sldc_flags", "f2fs_sldc_flags"), |
| 724 | (True, "{}_reserved_size", "partition_reserved_size"), |
| 725 | (True, "{}_squashfs_block_size", "squashfs_block_size"), |
| 726 | (True, "{}_squashfs_compressor", "squashfs_compressor"), |
| 727 | (True, "{}_squashfs_compressor_opt", "squashfs_compressor_opt"), |
| 728 | (True, "{}_squashfs_disable_4k_align", "squashfs_disable_4k_align"), |
| 729 | (True, "{}_verity_block_device", "verity_block_device"), |
| 730 | ) |
| 731 | |
| 732 | # Translate prefixed properties into generic ones. |
| 733 | if mount_point == "data": |
| 734 | prefix = "userdata" |
| 735 | else: |
| 736 | prefix = mount_point |
| 737 | |
| 738 | for readonly, src_prop, dest_prop in fmt_props: |
| 739 | if readonly and mount_point not in ro_mount_points: |
| 740 | continue |
| 741 | |
| 742 | if src_prop == "fs_type": |
| 743 | # This property is legacy and only used on a few partitions. b/202600377 |
| 744 | allowed_partitions = set(["system", "system_other", "data", "oem"]) |
| 745 | if mount_point not in allowed_partitions: |
| 746 | continue |
| 747 | |
Po Hu | 1c48b59 | 2022-02-18 09:10:22 +0000 | [diff] [blame] | 748 | if (mount_point == "system_other") and (dest_prop != "partition_size"): |
David Anderson | 271dab6 | 2021-10-11 17:31:26 -0700 | [diff] [blame] | 749 | # Propagate system properties to system_other. They'll get overridden |
| 750 | # after as needed. |
| 751 | copy_prop(src_prop.format("system"), dest_prop) |
| 752 | |
| 753 | copy_prop(src_prop.format(prefix), dest_prop) |
| 754 | |
| 755 | # Set prefixed properties that need a default value. |
| 756 | if mount_point in ro_mount_points: |
| 757 | prop = "{}_journal_size".format(prefix) |
| 758 | if not copy_prop(prop, "journal_size"): |
| 759 | d["journal_size"] = "0" |
| 760 | |
| 761 | prop = "{}_extfs_rsv_pct".format(prefix) |
| 762 | if not copy_prop(prop, "extfs_rsv_pct"): |
| 763 | d["extfs_rsv_pct"] = "0" |
| 764 | |
| 765 | # Copy partition-specific properties. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 766 | d["mount_point"] = mount_point |
| 767 | if mount_point == "system": |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 768 | copy_prop("system_headroom", "partition_headroom") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 769 | copy_prop("system_root_image", "system_root_image") |
Tao Bao | 8bfd3c7 | 2018-07-20 15:20:28 -0700 | [diff] [blame] | 770 | copy_prop("root_dir", "root_dir") |
| 771 | copy_prop("root_fs_config", "root_fs_config") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 772 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 773 | # Copy the generic fs type first, override with specific one if available. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 774 | copy_prop("flash_logical_block_size", "flash_logical_block_size") |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 775 | copy_prop("flash_erase_block_size", "flash_erase_block_size") |
Daniel Rosenberg | 6cc2c81 | 2019-12-17 17:36:31 -0800 | [diff] [blame] | 776 | copy_prop("needs_casefold", "needs_casefold") |
| 777 | copy_prop("needs_projid", "needs_projid") |
Jaegeuk Kim | ed754fb | 2020-10-12 19:50:05 -0700 | [diff] [blame] | 778 | copy_prop("needs_compress", "needs_compress") |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 779 | d["partition_name"] = mount_point |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 780 | return d |
| 781 | |
| 782 | |
| 783 | def LoadGlobalDict(filename): |
| 784 | """Load "name=value" pairs from filename""" |
| 785 | d = {} |
| 786 | f = open(filename) |
| 787 | for line in f: |
| 788 | line = line.strip() |
| 789 | if not line or line.startswith("#"): |
| 790 | continue |
| 791 | k, v = line.split("=", 1) |
| 792 | d[k] = v |
| 793 | f.close() |
| 794 | return d |
| 795 | |
| 796 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 797 | def GlobalDictFromImageProp(image_prop, mount_point): |
| 798 | d = {} |
| 799 | def copy_prop(src_p, dest_p): |
| 800 | if src_p in image_prop: |
| 801 | d[dest_p] = image_prop[src_p] |
| 802 | return True |
| 803 | return False |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 804 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 805 | if mount_point == "system": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 806 | copy_prop("partition_size", "system_size") |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 807 | elif mount_point == "system_other": |
Bowgo Tsai | 867ab66 | 2019-01-29 13:30:18 +0800 | [diff] [blame] | 808 | copy_prop("partition_size", "system_other_size") |
Yifan Hong | 749062d | 2018-06-19 16:23:16 -0700 | [diff] [blame] | 809 | elif mount_point == "vendor": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 810 | copy_prop("partition_size", "vendor_size") |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 811 | elif mount_point == "odm": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 812 | copy_prop("partition_size", "odm_size") |
Yifan Hong | cfb917a | 2020-05-07 14:58:20 -0700 | [diff] [blame] | 813 | elif mount_point == "vendor_dlkm": |
| 814 | copy_prop("partition_size", "vendor_dlkm_size") |
Yifan Hong | f496f1b | 2020-07-15 16:52:59 -0700 | [diff] [blame] | 815 | elif mount_point == "odm_dlkm": |
| 816 | copy_prop("partition_size", "odm_dlkm_size") |
Ramji Jiyani | 13a4137 | 2022-01-27 07:05:08 +0000 | [diff] [blame] | 817 | elif mount_point == "system_dlkm": |
| 818 | copy_prop("partition_size", "system_dlkm_size") |
Yifan Hong | 56a6c3b | 2018-07-20 15:19:34 -0700 | [diff] [blame] | 819 | elif mount_point == "product": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 820 | copy_prop("partition_size", "product_size") |
Justin Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 821 | elif mount_point == "system_ext": |
| 822 | copy_prop("partition_size", "system_ext_size") |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 823 | return d |
| 824 | |
| 825 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 826 | def main(argv): |
Jooyung Han | d9d0d69 | 2022-04-22 01:51:34 +0900 | [diff] [blame] | 827 | args = common.ParseOptions(argv, __doc__) |
| 828 | |
| 829 | if len(args) != 4: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 830 | print(__doc__) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 831 | sys.exit(1) |
| 832 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 833 | common.InitLogging() |
| 834 | |
Jooyung Han | d9d0d69 | 2022-04-22 01:51:34 +0900 | [diff] [blame] | 835 | in_dir = args[0] |
| 836 | glob_dict_file = args[1] |
| 837 | out_file = args[2] |
| 838 | target_out = args[3] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 839 | |
| 840 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 841 | if "mount_point" in glob_dict: |
Mark Salyzyn | 780f595 | 2018-10-19 13:44:36 -0700 | [diff] [blame] | 842 | # The caller knows the mount point and provides a dictionary needed by |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 843 | # BuildImage(). |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 844 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 845 | else: |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 846 | image_filename = os.path.basename(out_file) |
| 847 | mount_point = "" |
| 848 | if image_filename == "system.img": |
| 849 | mount_point = "system" |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 850 | elif image_filename == "system_other.img": |
| 851 | mount_point = "system_other" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 852 | elif image_filename == "userdata.img": |
| 853 | mount_point = "data" |
| 854 | elif image_filename == "cache.img": |
| 855 | mount_point = "cache" |
| 856 | elif image_filename == "vendor.img": |
| 857 | mount_point = "vendor" |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 858 | elif image_filename == "odm.img": |
| 859 | mount_point = "odm" |
Yifan Hong | cfb917a | 2020-05-07 14:58:20 -0700 | [diff] [blame] | 860 | elif image_filename == "vendor_dlkm.img": |
| 861 | mount_point = "vendor_dlkm" |
Yifan Hong | f496f1b | 2020-07-15 16:52:59 -0700 | [diff] [blame] | 862 | elif image_filename == "odm_dlkm.img": |
| 863 | mount_point = "odm_dlkm" |
Ramji Jiyani | 13a4137 | 2022-01-27 07:05:08 +0000 | [diff] [blame] | 864 | elif image_filename == "system_dlkm.img": |
| 865 | mount_point = "system_dlkm" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 866 | elif image_filename == "oem.img": |
| 867 | mount_point = "oem" |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 868 | elif image_filename == "product.img": |
| 869 | mount_point = "product" |
Justin Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 870 | elif image_filename == "system_ext.img": |
| 871 | mount_point = "system_ext" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 872 | else: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 873 | logger.error("Unknown image file name %s", image_filename) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 874 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 875 | |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 876 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 877 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 878 | try: |
| 879 | BuildImage(in_dir, image_properties, out_file, target_out) |
| 880 | except: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 881 | logger.error("Failed to build %s from %s", out_file, in_dir) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 882 | raise |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 883 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 884 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 885 | if __name__ == '__main__': |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 886 | try: |
| 887 | main(sys.argv[1:]) |
| 888 | finally: |
| 889 | common.Cleanup() |