Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 5 | """Script to generate a DLC (Downloadable Content) artifact.""" |
| 6 | |
Mike Frysinger | 93e8ffa | 2019-07-03 20:24:18 -0400 | [diff] [blame] | 7 | from __future__ import division |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
| 10 | import hashlib |
| 11 | import json |
| 12 | import math |
| 13 | import os |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 14 | import re |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 15 | import shutil |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 16 | |
| 17 | from chromite.lib import commandline |
| 18 | from chromite.lib import cros_build_lib |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 19 | from chromite.lib import cros_logging as logging |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 20 | from chromite.lib import osutils |
| 21 | |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 22 | from chromite.scripts import cros_set_lsb_release |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 23 | |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 24 | DLC_META_DIR = 'opt/google/dlc/' |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 25 | DLC_TMP_META_DIR = 'meta' |
| 26 | DLC_BUILD_DIR = 'build/rootfs/dlc/' |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 27 | LSB_RELEASE = 'etc/lsb-release' |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 28 | DLC_IMAGE = 'dlc.img' |
| 29 | IMAGELOADER_JSON = 'imageloader.json' |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 30 | EBUILD_PARAMETERS = 'ebuild_parameters.json' |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 31 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 32 | # This file has major and minor version numbers that the update_engine client |
| 33 | # supports. These values are needed for generating a delta/full payload. |
| 34 | UPDATE_ENGINE_CONF = 'etc/update_engine.conf' |
| 35 | |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 36 | _EXTRA_RESOURCES = (UPDATE_ENGINE_CONF,) |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 37 | |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 38 | DLC_ID_KEY = 'DLC_ID' |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 39 | DLC_PACKAGE_KEY = 'DLC_PACKAGE' |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 40 | DLC_NAME_KEY = 'DLC_NAME' |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 41 | DLC_APPID_KEY = 'DLC_RELEASE_APPID' |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 42 | |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 43 | _SQUASHFS_TYPE = 'squashfs' |
| 44 | _EXT4_TYPE = 'ext4' |
| 45 | |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 46 | MAX_ID_NAME = 40 |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 47 | |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 48 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 49 | def HashFile(file_path): |
| 50 | """Calculate the sha256 hash of a file. |
| 51 | |
| 52 | Args: |
| 53 | file_path: (str) path to the file. |
| 54 | |
| 55 | Returns: |
| 56 | [str]: The sha256 hash of the file. |
| 57 | """ |
| 58 | sha256 = hashlib.sha256() |
| 59 | with open(file_path, 'rb') as f: |
| 60 | for b in iter(lambda: f.read(2048), b''): |
| 61 | sha256.update(b) |
| 62 | return sha256.hexdigest() |
| 63 | |
| 64 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 65 | def CopyFileSudo(src_path, dst_path): |
| 66 | """Copy a file with sudo permissions to a destination. |
| 67 | |
| 68 | Args: |
| 69 | src_path: File to copy. |
| 70 | dst_path: Destination path of the file. |
| 71 | """ |
| 72 | cros_build_lib.sudo_run(['cp', src_path, dst_path]) |
| 73 | |
| 74 | |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 75 | def GetValueInJsonFile(json_path, key, default_value=None): |
| 76 | """Reads file containing JSON and returns value or default_value for key. |
| 77 | |
| 78 | Args: |
| 79 | json_path: (str) File containing JSON. |
| 80 | key: (str) The desired key to lookup. |
| 81 | default_value: (default:None) The default value returned in case of missing |
| 82 | key. |
| 83 | """ |
| 84 | with open(json_path) as fd: |
| 85 | return json.load(fd).get(key, default_value) |
| 86 | |
| 87 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 88 | class EbuildParams(object): |
| 89 | """Object to store and retrieve DLC ebuild parameters. |
| 90 | |
| 91 | Attributes: |
| 92 | dlc_id: (str) DLC ID. |
| 93 | dlc_package: (str) DLC package. |
| 94 | fs_type: (str) file system type. |
| 95 | pre_allocated_blocks: (int) number of blocks pre-allocated on device. |
| 96 | version: (str) DLC version. |
| 97 | name: (str) DLC name. |
| 98 | preload: (bool) allow for preloading DLC. |
| 99 | """ |
| 100 | |
| 101 | def __init__(self, dlc_id, dlc_package, fs_type, pre_allocated_blocks, |
| 102 | version, name, preload): |
| 103 | self.dlc_id = dlc_id |
| 104 | self.dlc_package = dlc_package |
| 105 | self.fs_type = fs_type |
| 106 | self.pre_allocated_blocks = pre_allocated_blocks |
| 107 | self.version = version |
| 108 | self.name = name |
| 109 | self.preload = preload |
| 110 | |
| 111 | def StoreDlcParameters(self, install_root_dir, sudo): |
| 112 | """Store DLC parameters defined in the ebuild. |
| 113 | |
| 114 | Store DLC parameters defined in the ebuild in a temporary file so they can |
| 115 | be retrieved in the build_image phase. |
| 116 | |
| 117 | Args: |
| 118 | install_root_dir: (str) The path to the root installation directory. |
| 119 | sudo: (bool) Use sudo to write the file. |
| 120 | """ |
| 121 | ebuild_params_path = EbuildParams.GetParamsPath(install_root_dir, |
| 122 | self.dlc_id, |
| 123 | self.dlc_package) |
| 124 | osutils.WriteFile(ebuild_params_path, |
| 125 | json.dumps(self.__dict__), |
| 126 | makedirs=True, sudo=sudo) |
| 127 | |
| 128 | @staticmethod |
| 129 | def GetParamsPath(install_root_dir, dlc_id, dlc_package): |
| 130 | """Get the path to the file storing the ebuild parameters. |
| 131 | |
| 132 | Args: |
| 133 | install_root_dir: (str) The path to the root installation directory. |
| 134 | dlc_id: (str) DLC ID. |
| 135 | dlc_package: (str) DLC package. |
| 136 | |
| 137 | Returns: |
| 138 | [str]: Path to |EBUILD_PARAMETERS|. |
| 139 | """ |
| 140 | return os.path.join(install_root_dir, DLC_BUILD_DIR, dlc_id, dlc_package, |
| 141 | EBUILD_PARAMETERS) |
| 142 | |
| 143 | @classmethod |
| 144 | def LoadEbuilParams(cls, sysroot, dlc_id, dlc_package): |
| 145 | """Read the stored ebuild parameters file and return a class instance. |
| 146 | |
| 147 | Args: |
| 148 | dlc_id: (str) DLC ID. |
| 149 | dlc_package: (str) DLC package. |
| 150 | sysroot: (str) The path to the build root directory. |
| 151 | |
| 152 | Returns: |
| 153 | [bool] : True if |ebuild_params_path| exists, False otherwise. |
| 154 | """ |
| 155 | path = cls.GetParamsPath(sysroot, dlc_id, dlc_package) |
| 156 | if not os.path.exists(path): |
| 157 | return None |
| 158 | |
| 159 | with open(path) as fp: |
| 160 | return cls(**json.load(fp)) |
| 161 | |
| 162 | def __str__(self): |
| 163 | return str(self.__dict__) |
| 164 | |
| 165 | |
Amin Hassani | 174eb7e | 2019-01-18 11:11:24 -0800 | [diff] [blame] | 166 | class DlcGenerator(object): |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 167 | """Object to generate DLC artifacts.""" |
| 168 | # Block size for the DLC image. |
| 169 | # We use 4K for various reasons: |
| 170 | # 1. it's what imageloader (linux kernel) supports. |
| 171 | # 2. it's what verity supports. |
| 172 | _BLOCK_SIZE = 4096 |
| 173 | # Blocks in the initial sparse image. |
| 174 | _BLOCKS = 500000 |
| 175 | # Version of manifest file. |
| 176 | _MANIFEST_VERSION = 1 |
| 177 | |
Amin Hassani | cc7ffce | 2019-01-11 14:57:52 -0800 | [diff] [blame] | 178 | # The DLC root path inside the DLC module. |
| 179 | _DLC_ROOT_DIR = 'root' |
| 180 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 181 | def __init__(self, ebuild_params, sysroot, install_root_dir, src_dir=None): |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 182 | """Object initializer. |
| 183 | |
| 184 | Args: |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 185 | sysroot: (str) The path to the build root directory. |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 186 | install_root_dir: (str) The path to the root installation directory. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 187 | ebuild_params: (EbuildParams) Ebuild variables. |
| 188 | src_dir: (str) Optional path to the DLC source root directory. When None, |
| 189 | the default directory in |DLC_BUILD_DIR| is used. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 190 | """ |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 191 | # Use a temporary directory to avoid having to use sudo every time we write |
| 192 | # into the build directory. |
| 193 | self.temp_root = osutils.TempDir(prefix='dlc', sudo_rm=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 194 | self.src_dir = src_dir |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 195 | self.sysroot = sysroot |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 196 | self.install_root_dir = install_root_dir |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 197 | self.ebuild_params = ebuild_params |
| 198 | # If the client is not overriding the src_dir, use the default one. |
| 199 | if not self.src_dir: |
| 200 | self.src_dir = os.path.join(self.sysroot, DLC_BUILD_DIR, |
| 201 | self.ebuild_params.dlc_id, |
| 202 | self.ebuild_params.dlc_package, |
| 203 | self._DLC_ROOT_DIR) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 204 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 205 | self.image_dir = os.path.join(self.temp_root.tempdir, |
| 206 | DLC_BUILD_DIR, |
| 207 | self.ebuild_params.dlc_id, |
| 208 | self.ebuild_params.dlc_package) |
| 209 | |
| 210 | self.meta_dir = os.path.join(self.image_dir, DLC_TMP_META_DIR) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 211 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 212 | # Create path for all final artifacts. |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 213 | self.dest_image = os.path.join(self.image_dir, DLC_IMAGE) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 214 | self.dest_table = os.path.join(self.meta_dir, 'table') |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 215 | self.dest_imageloader_json = os.path.join(self.meta_dir, IMAGELOADER_JSON) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 216 | |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 217 | # Log out the member variable values initially set. |
| 218 | logging.debug('Initial internal values of DlcGenerator: %s', |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 219 | repr({k:str(i) for k, i in self.__dict__.items()})) |
| 220 | |
| 221 | def CopyTempContentsToBuildDir(self): |
| 222 | """Copy the temp files to the build directory using sudo.""" |
| 223 | logging.info('Copy files from temporary directory (%s) to build directory ' |
| 224 | '(%s).', self.temp_root.tempdir.rstrip('/') + '/.', |
| 225 | self.install_root_dir) |
| 226 | cros_build_lib.sudo_run(['cp', '-dR', |
| 227 | self.temp_root.tempdir.rstrip('/') + '/.', |
| 228 | self.install_root_dir]) |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 229 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 230 | def SquashOwnerships(self, path): |
| 231 | """Squash the owernships & permissions for files. |
| 232 | |
| 233 | Args: |
| 234 | path: (str) path that contains all files to be processed. |
| 235 | """ |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 236 | cros_build_lib.sudo_run(['chown', '-R', '0:0', path]) |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 237 | cros_build_lib.sudo_run([ |
| 238 | 'find', path, '-exec', 'touch', '-h', '-t', '197001010000.00', '{}', '+' |
| 239 | ]) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 240 | |
| 241 | def CreateExt4Image(self): |
| 242 | """Create an ext4 image.""" |
| 243 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
| 244 | mount_point = os.path.join(temp_dir, 'mount_point') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 245 | # Create the directory where the image is located if it doesn't exist. |
| 246 | osutils.SafeMakedirs(os.path.split(self.dest_image)[0]) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 247 | # Create a raw image file. |
| 248 | with open(self.dest_image, 'w') as f: |
| 249 | f.truncate(self._BLOCKS * self._BLOCK_SIZE) |
| 250 | # Create an ext4 file system on the raw image. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 251 | cros_build_lib.run([ |
| 252 | '/sbin/mkfs.ext4', '-b', |
| 253 | str(self._BLOCK_SIZE), '-O', '^has_journal', self.dest_image |
| 254 | ], |
| 255 | capture_output=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 256 | # Create the mount_point directory. |
| 257 | osutils.SafeMakedirs(mount_point) |
| 258 | # Mount the ext4 image. |
| 259 | osutils.MountDir(self.dest_image, mount_point, mount_opts=('loop', 'rw')) |
Amin Hassani | cc7ffce | 2019-01-11 14:57:52 -0800 | [diff] [blame] | 260 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 261 | try: |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 262 | self.SetupDlcImageFiles(mount_point) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 263 | finally: |
| 264 | # Unmount the ext4 image. |
| 265 | osutils.UmountDir(mount_point) |
| 266 | # Shrink to minimum size. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 267 | cros_build_lib.run(['/sbin/e2fsck', '-y', '-f', self.dest_image], |
| 268 | capture_output=True) |
| 269 | cros_build_lib.run(['/sbin/resize2fs', '-M', self.dest_image], |
| 270 | capture_output=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 271 | |
| 272 | def CreateSquashfsImage(self): |
| 273 | """Create a squashfs image.""" |
| 274 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 275 | squashfs_root = os.path.join(temp_dir, 'squashfs-root') |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 276 | self.SetupDlcImageFiles(squashfs_root) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 277 | |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 278 | cros_build_lib.run([ |
| 279 | 'mksquashfs', squashfs_root, self.dest_image, '-4k-align', '-noappend' |
| 280 | ], |
| 281 | capture_output=True) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 282 | |
| 283 | # We changed the ownership and permissions of the squashfs_root |
| 284 | # directory. Now we need to remove it manually. |
| 285 | osutils.RmDir(squashfs_root, sudo=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 286 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 287 | def SetupDlcImageFiles(self, dlc_dir): |
| 288 | """Prepares the directory dlc_dir with all the files a DLC needs. |
| 289 | |
| 290 | Args: |
| 291 | dlc_dir: (str) The path to where to setup files inside the DLC. |
| 292 | """ |
| 293 | dlc_root_dir = os.path.join(dlc_dir, self._DLC_ROOT_DIR) |
| 294 | osutils.SafeMakedirs(dlc_root_dir) |
Jae Hoon Kim | d14646b | 2019-08-21 14:49:26 -0700 | [diff] [blame] | 295 | osutils.CopyDirContents(self.src_dir, dlc_root_dir, symlinks=True) |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 296 | self.PrepareLsbRelease(dlc_dir) |
| 297 | self.CollectExtraResources(dlc_dir) |
| 298 | self.SquashOwnerships(dlc_dir) |
| 299 | |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 300 | def PrepareLsbRelease(self, dlc_dir): |
| 301 | """Prepare the file /etc/lsb-release in the DLC module. |
| 302 | |
| 303 | This file is used dropping some identification parameters for the DLC. |
| 304 | |
| 305 | Args: |
| 306 | dlc_dir: (str) The path to root directory of the DLC. e.g. mounted point |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 307 | when we are creating the image. |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 308 | """ |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 309 | # Reading the platform APPID and creating the DLC APPID. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 310 | platform_lsb_release = osutils.ReadFile( |
| 311 | os.path.join(self.sysroot, LSB_RELEASE)) |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 312 | app_id = None |
| 313 | for line in platform_lsb_release.split('\n'): |
| 314 | if line.startswith(cros_set_lsb_release.LSB_KEY_APPID_RELEASE): |
| 315 | app_id = line.split('=')[1] |
| 316 | if app_id is None: |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 317 | raise Exception( |
| 318 | '%s does not have a valid key %s' % |
| 319 | (platform_lsb_release, cros_set_lsb_release.LSB_KEY_APPID_RELEASE)) |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 320 | |
Mike Frysinger | 1c834ea | 2019-10-14 04:29:41 -0400 | [diff] [blame] | 321 | fields = ( |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 322 | (DLC_ID_KEY, self.ebuild_params.dlc_id), |
| 323 | (DLC_PACKAGE_KEY, self.ebuild_params.dlc_package), |
| 324 | (DLC_NAME_KEY, self.ebuild_params.name), |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 325 | # The DLC appid is generated by concatenating the platform appid with |
| 326 | # the DLC ID using an underscore. This pattern should never be changed |
| 327 | # once set otherwise it can break a lot of things! |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 328 | (DLC_APPID_KEY, '%s_%s' % (app_id, self.ebuild_params.dlc_id)), |
Mike Frysinger | 1c834ea | 2019-10-14 04:29:41 -0400 | [diff] [blame] | 329 | ) |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 330 | |
| 331 | lsb_release = os.path.join(dlc_dir, LSB_RELEASE) |
| 332 | osutils.SafeMakedirs(os.path.dirname(lsb_release)) |
Mike Frysinger | 1c834ea | 2019-10-14 04:29:41 -0400 | [diff] [blame] | 333 | content = ''.join('%s=%s\n' % (k, v) for k, v in fields) |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 334 | osutils.WriteFile(lsb_release, content) |
| 335 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 336 | def CollectExtraResources(self, dlc_dir): |
| 337 | """Collect the extra resources needed by the DLC module. |
| 338 | |
| 339 | Look at the documentation around _EXTRA_RESOURCES. |
| 340 | |
| 341 | Args: |
| 342 | dlc_dir: (str) The path to root directory of the DLC. e.g. mounted point |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 343 | when we are creating the image. |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 344 | """ |
| 345 | for r in _EXTRA_RESOURCES: |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 346 | source_path = os.path.join(self.sysroot, r) |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 347 | target_path = os.path.join(dlc_dir, r) |
| 348 | osutils.SafeMakedirs(os.path.dirname(target_path)) |
| 349 | shutil.copyfile(source_path, target_path) |
| 350 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 351 | def CreateImage(self): |
| 352 | """Create the image and copy the DLC files to it.""" |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 353 | logging.info('Creating the DLC image.') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 354 | if self.ebuild_params.fs_type == _EXT4_TYPE: |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 355 | self.CreateExt4Image() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 356 | elif self.ebuild_params.fs_type == _SQUASHFS_TYPE: |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 357 | self.CreateSquashfsImage() |
| 358 | else: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 359 | raise ValueError('Wrong fs type: %s used:' % self.ebuild_params.fs_type) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 360 | |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 361 | def VerifyImageSize(self): |
Xiaochu Liu | 36b3059 | 2019-08-06 09:39:54 -0700 | [diff] [blame] | 362 | """Verify the image can fit to the reserved file.""" |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 363 | logging.info('Verifying the DLC image size.') |
| 364 | image_bytes = os.path.getsize(self.dest_image) |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 365 | preallocated_bytes = (self.ebuild_params.pre_allocated_blocks * |
| 366 | self._BLOCK_SIZE) |
Xiaochu Liu | 36b3059 | 2019-08-06 09:39:54 -0700 | [diff] [blame] | 367 | # Verifies the actual size of the DLC image is NOT smaller than the |
| 368 | # preallocated space. |
| 369 | if preallocated_bytes < image_bytes: |
| 370 | raise ValueError( |
| 371 | 'The DLC_PREALLOC_BLOCKS (%s) value set in DLC ebuild resulted in a ' |
| 372 | 'max size of DLC_PREALLOC_BLOCKS * 4K (%s) bytes the DLC image is ' |
| 373 | 'allowed to occupy. The value is smaller than the actual image size ' |
| 374 | '(%s) required. Increase DLC_PREALLOC_BLOCKS in your ebuild to at ' |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 375 | 'least %d.' % |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 376 | (self.ebuild_params.pre_allocated_blocks, preallocated_bytes, |
| 377 | image_bytes, self.GetOptimalImageBlockSize(image_bytes))) |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 378 | |
| 379 | def GetOptimalImageBlockSize(self, image_bytes): |
| 380 | """Given the image bytes, get the least amount of blocks required.""" |
| 381 | return int(math.ceil(image_bytes / self._BLOCK_SIZE)) |
Xiaochu Liu | 36b3059 | 2019-08-06 09:39:54 -0700 | [diff] [blame] | 382 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 383 | def GetImageloaderJsonContent(self, image_hash, table_hash, blocks): |
| 384 | """Return the content of imageloader.json file. |
| 385 | |
| 386 | Args: |
| 387 | image_hash: (str) sha256 hash of the DLC image. |
| 388 | table_hash: (str) sha256 hash of the DLC table file. |
| 389 | blocks: (int) number of blocks in the DLC image. |
| 390 | |
| 391 | Returns: |
| 392 | [str]: content of imageloader.json file. |
| 393 | """ |
| 394 | return { |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 395 | 'fs-type': self.ebuild_params.fs_type, |
| 396 | 'id': self.ebuild_params.dlc_id, |
| 397 | 'package': self.ebuild_params.dlc_package, |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 398 | 'image-sha256-hash': image_hash, |
| 399 | 'image-type': 'dlc', |
| 400 | 'is-removable': True, |
| 401 | 'manifest-version': self._MANIFEST_VERSION, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 402 | 'name': self.ebuild_params.name, |
| 403 | 'pre-allocated-size': |
| 404 | str(self.ebuild_params.pre_allocated_blocks * self._BLOCK_SIZE), |
Jae Hoon Kim | bd8ae6e | 2020-02-03 18:54:29 -0800 | [diff] [blame] | 405 | 'size': str(blocks * self._BLOCK_SIZE), |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 406 | 'table-sha256-hash': table_hash, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 407 | 'version': self.ebuild_params.version, |
| 408 | 'preload-allowed': self.ebuild_params.preload, |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | def GenerateVerity(self): |
| 412 | """Generate verity parameters and hashes for the image.""" |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 413 | logging.info('Generating DLC image verity.') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 414 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
| 415 | hash_tree = os.path.join(temp_dir, 'hash_tree') |
| 416 | # Get blocks in the image. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 417 | blocks = math.ceil(os.path.getsize(self.dest_image) / self._BLOCK_SIZE) |
| 418 | result = cros_build_lib.run([ |
| 419 | 'verity', 'mode=create', 'alg=sha256', 'payload=' + self.dest_image, |
| 420 | 'payload_blocks=' + str(blocks), 'hashtree=' + hash_tree, |
| 421 | 'salt=random' |
| 422 | ], |
| 423 | capture_output=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 424 | table = result.output |
| 425 | |
| 426 | # Append the merkle tree to the image. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 427 | osutils.WriteFile( |
| 428 | self.dest_image, osutils.ReadFile(hash_tree, mode='rb'), mode='a+b') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 429 | |
| 430 | # Write verity parameter to table file. |
Mike Frysinger | 1c834ea | 2019-10-14 04:29:41 -0400 | [diff] [blame] | 431 | osutils.WriteFile(self.dest_table, table, mode='wb') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 432 | |
| 433 | # Compute image hash. |
| 434 | image_hash = HashFile(self.dest_image) |
| 435 | table_hash = HashFile(self.dest_table) |
| 436 | # Write image hash to imageloader.json file. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 437 | blocks = math.ceil(os.path.getsize(self.dest_image) / self._BLOCK_SIZE) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 438 | imageloader_json_content = self.GetImageloaderJsonContent( |
| 439 | image_hash, table_hash, int(blocks)) |
| 440 | with open(self.dest_imageloader_json, 'w') as f: |
| 441 | json.dump(imageloader_json_content, f) |
| 442 | |
| 443 | def GenerateDLC(self): |
| 444 | """Generate a DLC artifact.""" |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 445 | # Create directories. |
| 446 | osutils.SafeMakedirs(self.image_dir) |
| 447 | osutils.SafeMakedirs(self.meta_dir) |
| 448 | |
| 449 | # Create the image into |self.temp_root| and copy the DLC files to it. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 450 | self.CreateImage() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 451 | # Verify the image created is within pre-allocated size. |
Jae Hoon Kim | e5b8862 | 2019-08-23 11:11:33 -0700 | [diff] [blame] | 452 | self.VerifyImageSize() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 453 | # Generate hash tree and other metadata and save them under |
| 454 | # |self.temp_root|. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 455 | self.GenerateVerity() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 456 | # Copy the files from |self.temp_root| into the build directory. |
| 457 | self.CopyTempContentsToBuildDir() |
| 458 | |
| 459 | # Now that the image was successfully generated, delete |ebuild_params_path| |
| 460 | # to indicate that the image in the build directory is in sync with the |
| 461 | # files installed during the build_package phase. |
| 462 | ebuild_params_path = EbuildParams.GetParamsPath( |
| 463 | self.sysroot, self.ebuild_params.dlc_id, self.ebuild_params.dlc_package) |
| 464 | osutils.SafeUnlink(ebuild_params_path, sudo=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 465 | |
| 466 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 467 | def IsDlcPreloadingAllowed(dlc_id, dlc_build_dir): |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 468 | """Validates that DLC and it's packages all were built with DLC_PRELOAD=true. |
| 469 | |
| 470 | Args: |
| 471 | dlc_id: (str) DLC ID. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 472 | dlc_build_dir: (str) the root path where DLC build files reside. |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 473 | """ |
| 474 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 475 | dlc_id_meta_dir = os.path.join(dlc_build_dir, dlc_id) |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 476 | if not os.path.exists(dlc_id_meta_dir): |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 477 | logging.error('DLC build directory (%s) does not exist for preloading ' |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 478 | 'check, will not preload', dlc_id_meta_dir) |
| 479 | return False |
| 480 | |
| 481 | packages = os.listdir(dlc_id_meta_dir) |
| 482 | if not packages: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 483 | logging.error('DLC ID build directory (%s) does not have any ' |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 484 | 'packages, will not preload.', dlc_id_meta_dir) |
| 485 | return False |
| 486 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 487 | for package in packages: |
| 488 | image_loader_json = os.path.join(dlc_id_meta_dir, package, DLC_TMP_META_DIR, |
| 489 | IMAGELOADER_JSON) |
| 490 | if not os.path.exists(image_loader_json): |
| 491 | logging.error('DLC metadata file (%s) does not exist, will not preload.', |
| 492 | image_loader_json) |
| 493 | return False |
| 494 | if not GetValueInJsonFile(json_path=image_loader_json, |
| 495 | key='preload-allowed', default_value=False): |
| 496 | return False |
| 497 | # All packages support preload. |
| 498 | return True |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 499 | |
| 500 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 501 | def InstallDlcImages(sysroot, dlc_id=None, install_root_dir=None, preload=False, |
| 502 | rootfs=None, src_dir=None): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 503 | """Copies all DLC image files into the images directory. |
| 504 | |
| 505 | Copies the DLC image files in the given build directory into the given DLC |
| 506 | image directory. If the DLC build directory does not exist, or there is no DLC |
| 507 | for that board, this function does nothing. |
| 508 | |
| 509 | Args: |
| 510 | sysroot: Path to directory containing DLC images, e.g /build/<board>. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 511 | dlc_id: (str) DLC ID. If None, all the DLCs will be installed. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 512 | install_root_dir: Path to DLC output directory, e.g. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 513 | src/build/images/<board>/<version>. If None, the image will be generated |
| 514 | but will not be copied to a destination. |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 515 | preload: When true, only copies DLC(s) if built with DLC_PRELOAD=true. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 516 | rootfs: (str) Path to the platform rootfs. |
| 517 | src_dir: (str) Path to the DLC source root directory. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 518 | """ |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 519 | dlc_build_dir = os.path.join(sysroot, DLC_BUILD_DIR) |
| 520 | if not os.path.exists(dlc_build_dir): |
| 521 | logging.info('DLC build directory (%s) does not exist, ignoring.', |
| 522 | dlc_build_dir) |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 523 | return |
| 524 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 525 | if dlc_id is not None: |
| 526 | if not os.path.exists(os.path.join(dlc_build_dir, dlc_id)): |
| 527 | raise Exception( |
| 528 | 'DLC "%s" does not exist in the build directory %s.' % |
| 529 | (dlc_id, dlc_build_dir)) |
| 530 | dlc_ids = [dlc_id] |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 531 | else: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 532 | # Process all DLCs. |
| 533 | dlc_ids = os.listdir(dlc_build_dir) |
| 534 | if not dlc_ids: |
| 535 | logging.info('There are no DLC(s) to copy to output, ignoring.') |
| 536 | return |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 537 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 538 | logging.info('Detected the following DLCs: %s', ', '.join(dlc_ids)) |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 539 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 540 | for d_id in dlc_ids: |
| 541 | dlc_id_path = os.path.join(dlc_build_dir, d_id) |
| 542 | dlc_packages = [direct for direct in os.listdir(dlc_id_path) |
| 543 | if os.path.isdir(os.path.join(dlc_id_path, direct))] |
| 544 | for d_package in dlc_packages: |
| 545 | logging.info('Building image: DLC %s', d_id) |
| 546 | params = EbuildParams.LoadEbuilParams(sysroot=sysroot, dlc_id=d_id, |
| 547 | dlc_package=d_package) |
| 548 | # Because portage sandboxes every ebuild package during build_packages |
| 549 | # phase, we cannot delete the old image during that phase, but we can use |
| 550 | # the existence of the file |EBUILD_PARAMETERS| to know if the image |
| 551 | # has to be generated or not. |
| 552 | if not params: |
| 553 | logging.info('The ebuild parameters file (%s) for DLC (%s) does not ' |
| 554 | 'exist. This means that the image was already ' |
| 555 | 'generated and there is no need to create it again.', |
| 556 | EbuildParams.GetParamsPath(sysroot, d_id, d_package), d_id) |
| 557 | else: |
| 558 | dlc_generator = DlcGenerator( |
| 559 | src_dir=src_dir, |
| 560 | sysroot=sysroot, |
| 561 | install_root_dir=sysroot, |
| 562 | ebuild_params=params) |
| 563 | dlc_generator.GenerateDLC() |
| 564 | |
| 565 | # Copy the dlc images to install_root_dir. |
| 566 | if install_root_dir: |
| 567 | if preload and not IsDlcPreloadingAllowed(d_id, dlc_build_dir): |
| 568 | logging.info('Skipping installation of DLC %s because the preload ' |
| 569 | 'flag is set and the DLC does not support preloading.', |
| 570 | d_id) |
| 571 | else: |
| 572 | osutils.SafeMakedirs(install_root_dir, sudo=True) |
| 573 | source_dlc_dir = os.path.join(dlc_build_dir, d_id, d_package) |
| 574 | install_dlc_dir = os.path.join(install_root_dir, d_id, d_package) |
| 575 | osutils.SafeMakedirs(install_dlc_dir, sudo=True) |
| 576 | for filepath in (os.path.join(source_dlc_dir, fname) for fname in |
| 577 | os.listdir(source_dlc_dir) if |
| 578 | fname.endswith('.img')): |
| 579 | logging.info('Copying DLC(%s) image from %s to %s: ', d_id, |
| 580 | filepath, install_dlc_dir) |
| 581 | CopyFileSudo(filepath, install_dlc_dir) |
| 582 | logging.info('Done copying DLC to %s.', install_dlc_dir) |
| 583 | else: |
| 584 | logging.info('install_root_dir value was not provided. Copying dlc' |
| 585 | ' image skipped.') |
| 586 | |
| 587 | # Create metadata directory in rootfs. |
| 588 | if rootfs: |
| 589 | meta_rootfs = os.path.join(rootfs, DLC_META_DIR, d_id, d_package) |
| 590 | osutils.SafeMakedirs(meta_rootfs, sudo=True) |
| 591 | # Copy the metadata files to rootfs. |
| 592 | meta_dir_src = os.path.join(dlc_build_dir, d_id, d_package, |
| 593 | DLC_TMP_META_DIR) |
| 594 | logging.info('Copying DLC(%s) metadata from %s to %s: ', d_id, |
| 595 | meta_dir_src, meta_rootfs) |
| 596 | # Use sudo_run since osutils.CopyDirContents doesn't support sudo. |
| 597 | cros_build_lib.sudo_run(['cp', '-dR', |
| 598 | meta_dir_src.rstrip('/') + '/.', |
| 599 | meta_rootfs], print_cmd=False, stderr=True) |
| 600 | |
| 601 | else: |
| 602 | logging.info('rootfs value was not provided. Copying metadata skipped.') |
| 603 | |
| 604 | logging.info('Done installing DLCs.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 605 | |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 606 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 607 | def GetParser(): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 608 | """Creates an argument parser and returns it.""" |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 609 | parser = commandline.ArgumentParser(description=__doc__) |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 610 | # This script is used both for building an individual DLC or copying all final |
| 611 | # DLCs images to their final destination nearby chromiumsos_test_image.bin, |
| 612 | # etc. These two arguments are required in both cases. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 613 | parser.add_argument( |
| 614 | '--sysroot', |
| 615 | type='path', |
| 616 | metavar='DIR', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 617 | help="The root path to the board's build root, e.g. " |
| 618 | '/build/eve') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 619 | # TODO(andrewlassalle): Remove src-dir in the future(2021?) if nobody uses it. |
| 620 | parser.add_argument( |
| 621 | '--src-dir', |
| 622 | type='path', |
| 623 | metavar='SRC_DIR_PATH', |
| 624 | help='Override the default Root directory path that contains all DLC ' |
| 625 | 'files to be packed.') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 626 | parser.add_argument( |
| 627 | '--install-root-dir', |
| 628 | type='path', |
| 629 | metavar='DIR', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 630 | help='If building a specific DLC, it is the root path to' |
| 631 | ' install DLC images (%s) and metadata (%s). Otherwise it' |
| 632 | ' is the target directory where the Chrome OS images gets' |
| 633 | ' dropped in build_image, e.g. ' |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 634 | 'src/build/images/<board>/latest.' % (DLC_BUILD_DIR, DLC_META_DIR)) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 635 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 636 | one_dlc = parser.add_argument_group('Arguments required for building only ' |
| 637 | 'one DLC') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 638 | one_dlc.add_argument( |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 639 | '--rootfs', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 640 | type='path', |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 641 | metavar='ROOT_FS_PATH', |
| 642 | help='Path to the platform rootfs.') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 643 | one_dlc.add_argument( |
| 644 | '--pre-allocated-blocks', |
| 645 | type=int, |
| 646 | metavar='PREALLOCATEDBLOCKS', |
| 647 | help='Number of blocks (block size is 4k) that need to' |
| 648 | 'be pre-allocated on device.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 649 | one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.') |
| 650 | one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 651 | one_dlc.add_argument( |
| 652 | '--package', |
| 653 | metavar='PACKAGE', |
| 654 | help='The package ID that is unique within a DLC, One' |
| 655 | ' DLC cannot have duplicate package IDs.') |
| 656 | one_dlc.add_argument( |
| 657 | '--name', metavar='NAME', help='A human-readable name for the DLC.') |
| 658 | one_dlc.add_argument( |
| 659 | '--fs-type', |
| 660 | metavar='FS_TYPE', |
| 661 | default=_SQUASHFS_TYPE, |
| 662 | choices=(_SQUASHFS_TYPE, _EXT4_TYPE), |
| 663 | help='File system type of the image.') |
| 664 | one_dlc.add_argument( |
| 665 | '--preload', |
| 666 | default=False, |
| 667 | action='store_true', |
| 668 | help='Allow preloading of DLC.') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 669 | one_dlc.add_argument( |
| 670 | '--build-package', |
| 671 | default=False, |
| 672 | action='store_true', |
| 673 | help='Flag to indicate if the script is executed during the ' |
| 674 | 'build_packages phase.') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 675 | return parser |
| 676 | |
| 677 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 678 | def ValidateDlcIdentifier(parser, name): |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 679 | """Validates the DLC identifiers like ID and package names. |
| 680 | |
| 681 | The name specifications are: |
| 682 | - No underscore. |
| 683 | - First character should be only alphanumeric. |
| 684 | - Other characters can be alphanumeric and '-' (dash). |
Jae Hoon Kim | db1cab8 | 2020-01-21 19:27:28 -0800 | [diff] [blame] | 685 | - Maximum length of 40 (MAX_ID_NAME) characters. |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 686 | |
| 687 | For more info see: |
| 688 | https://chromium.googlesource.com/chromiumos/platform2/+/master/dlcservice/docs/developer.md#create-a-dlc-module |
| 689 | |
| 690 | Args: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 691 | parser: Arguments parser. |
Jae Hoon Kim | db1cab8 | 2020-01-21 19:27:28 -0800 | [diff] [blame] | 692 | name: The value of the string to be validated. |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 693 | """ |
Jae Hoon Kim | db1cab8 | 2020-01-21 19:27:28 -0800 | [diff] [blame] | 694 | errors = [] |
| 695 | if not name: |
| 696 | errors.append('Must not be empty.') |
| 697 | if not name[0].isalnum(): |
| 698 | errors.append('Must start with alphanumeric character.') |
| 699 | if not re.match(r'^[a-zA-Z0-9][a-zA-Z0-9-]*$', name): |
| 700 | errors.append('Must only use alphanumeric and - (dash).') |
| 701 | if len(name) > MAX_ID_NAME: |
| 702 | errors.append('Must be within %d characters.' % MAX_ID_NAME) |
| 703 | |
| 704 | if errors: |
| 705 | msg = '%s is invalid:\n%s' % (name, '\n'.join(errors)) |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 706 | parser.error(msg) |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 707 | |
| 708 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 709 | def ValidateArguments(parser, opts, req_flags, invalid_flags): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 710 | """Validates the correctness of the passed arguments. |
| 711 | |
| 712 | Args: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 713 | parser: Arguments parser. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 714 | opts: Parsed arguments. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 715 | req_flags: all the required flags. |
| 716 | invalid_flags: all the flags that are not allowed. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 717 | """ |
| 718 | # Make sure if the intention is to build one DLC, all the required arguments |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 719 | # are passed and none of the invalid ones are passed. This will ensure the |
| 720 | # script is called twice per DLC. |
| 721 | if opts.id: |
| 722 | if not all(vars(opts)[x] is not None for x in req_flags): |
| 723 | parser.error('If the intention is to build only one DLC, all the flags' |
| 724 | '%s required for it should be passed.' % req_flags) |
| 725 | if any(vars(opts)[x] is not None for x in invalid_flags): |
| 726 | parser.error('If the intention is to build only one DLC, all the flags' |
| 727 | '%s should be passed in the build_packages phase, not in ' |
| 728 | 'the build_image phase.' % invalid_flags) |
| 729 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 730 | |
| 731 | if opts.fs_type == _EXT4_TYPE: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 732 | parser.error('ext4 unsupported for DLC, see https://crbug.com/890060') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 733 | |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 734 | if opts.id: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 735 | ValidateDlcIdentifier(parser, opts.id) |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 736 | if opts.package: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 737 | ValidateDlcIdentifier(parser, opts.package) |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 738 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 739 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 740 | def main(argv): |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 741 | parser = GetParser() |
| 742 | opts = parser.parse_args(argv) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 743 | opts.Freeze() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 744 | per_dlc_req_args = ['id'] |
| 745 | per_dlc_invalid_args = [] |
| 746 | if opts.build_package: |
| 747 | per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name', |
| 748 | 'package', 'install_root_dir'] |
| 749 | per_dlc_invalid_args += ['src_dir', 'sysroot'] |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 750 | else: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 751 | per_dlc_req_args += ['sysroot'] |
| 752 | per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version', |
| 753 | 'package'] |
| 754 | |
| 755 | ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args) |
| 756 | |
| 757 | if opts.build_package: |
| 758 | logging.info('Building package: DLC %s', opts.id) |
| 759 | params = EbuildParams(dlc_id=opts.id, |
| 760 | dlc_package=opts.package, |
| 761 | fs_type=opts.fs_type, |
| 762 | name=opts.name, |
| 763 | pre_allocated_blocks=opts.pre_allocated_blocks, |
| 764 | version=opts.version, |
| 765 | preload=opts.preload) |
| 766 | params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True) |
| 767 | |
| 768 | else: |
| 769 | InstallDlcImages( |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 770 | sysroot=opts.sysroot, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 771 | dlc_id=opts.id, |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 772 | install_root_dir=opts.install_root_dir, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame^] | 773 | preload=opts.preload, |
| 774 | src_dir=opts.src_dir, |
| 775 | rootfs=opts.rootfs) |