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. |
| 5 | |
| 6 | """Script to generate a DLC (Downloadable Content) artifact.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import hashlib |
| 11 | import json |
| 12 | import math |
| 13 | import os |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 14 | import shutil |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 15 | |
| 16 | from chromite.lib import commandline |
| 17 | from chromite.lib import cros_build_lib |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 18 | from chromite.lib import cros_logging as logging |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 19 | from chromite.lib import osutils |
| 20 | |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 21 | from chromite.scripts import cros_set_lsb_release |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 22 | |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 23 | DLC_META_DIR = 'opt/google/dlc/' |
| 24 | DLC_IMAGE_DIR = 'build/rootfs/dlc/' |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 25 | LSB_RELEASE = 'etc/lsb-release' |
| 26 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 27 | # This file has major and minor version numbers that the update_engine client |
| 28 | # supports. These values are needed for generating a delta/full payload. |
| 29 | UPDATE_ENGINE_CONF = 'etc/update_engine.conf' |
| 30 | |
| 31 | _EXTRA_RESOURCES = ( |
| 32 | UPDATE_ENGINE_CONF, |
| 33 | ) |
| 34 | |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 35 | DLC_ID_KEY = 'DLC_ID' |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 36 | DLC_PACKAGE_KEY = 'DLC_PACKAGE' |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 37 | DLC_NAME_KEY = 'DLC_NAME' |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 38 | DLC_APPID_KEY = 'DLC_RELEASE_APPID' |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 39 | |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 40 | _SQUASHFS_TYPE = 'squashfs' |
| 41 | _EXT4_TYPE = 'ext4' |
| 42 | |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 43 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 44 | def HashFile(file_path): |
| 45 | """Calculate the sha256 hash of a file. |
| 46 | |
| 47 | Args: |
| 48 | file_path: (str) path to the file. |
| 49 | |
| 50 | Returns: |
| 51 | [str]: The sha256 hash of the file. |
| 52 | """ |
| 53 | sha256 = hashlib.sha256() |
| 54 | with open(file_path, 'rb') as f: |
| 55 | for b in iter(lambda: f.read(2048), b''): |
| 56 | sha256.update(b) |
| 57 | return sha256.hexdigest() |
| 58 | |
| 59 | |
Amin Hassani | 174eb7e | 2019-01-18 11:11:24 -0800 | [diff] [blame] | 60 | class DlcGenerator(object): |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 61 | """Object to generate DLC artifacts.""" |
| 62 | # Block size for the DLC image. |
| 63 | # We use 4K for various reasons: |
| 64 | # 1. it's what imageloader (linux kernel) supports. |
| 65 | # 2. it's what verity supports. |
| 66 | _BLOCK_SIZE = 4096 |
| 67 | # Blocks in the initial sparse image. |
| 68 | _BLOCKS = 500000 |
| 69 | # Version of manifest file. |
| 70 | _MANIFEST_VERSION = 1 |
| 71 | |
Amin Hassani | cc7ffce | 2019-01-11 14:57:52 -0800 | [diff] [blame] | 72 | # The DLC root path inside the DLC module. |
| 73 | _DLC_ROOT_DIR = 'root' |
| 74 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 75 | def __init__(self, src_dir, sysroot, install_root_dir, fs_type, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 76 | pre_allocated_blocks, version, dlc_id, dlc_package, name): |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 77 | """Object initializer. |
| 78 | |
| 79 | Args: |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 80 | src_dir: (str) path to the DLC source root directory. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 81 | sysroot: (str) The path to the build root directory. |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 82 | install_root_dir: (str) The path to the root installation directory. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 83 | fs_type: (str) file system type. |
| 84 | pre_allocated_blocks: (int) number of blocks pre-allocated on device. |
| 85 | version: (str) DLC version. |
| 86 | dlc_id: (str) DLC ID. |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 87 | dlc_package: (str) DLC Package. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 88 | name: (str) DLC name. |
| 89 | """ |
| 90 | self.src_dir = src_dir |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 91 | self.sysroot = sysroot |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 92 | self.install_root_dir = install_root_dir |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 93 | self.fs_type = fs_type |
| 94 | self.pre_allocated_blocks = pre_allocated_blocks |
| 95 | self.version = version |
| 96 | self.dlc_id = dlc_id |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 97 | self.dlc_package = dlc_package |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 98 | self.name = name |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 99 | |
| 100 | self.meta_dir = os.path.join(self.install_root_dir, DLC_META_DIR, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 101 | self.dlc_id, self.dlc_package) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 102 | self.image_dir = os.path.join(self.install_root_dir, DLC_IMAGE_DIR, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 103 | self.dlc_id, self.dlc_package) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 104 | osutils.SafeMakedirs(self.meta_dir) |
| 105 | osutils.SafeMakedirs(self.image_dir) |
| 106 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 107 | # Create path for all final artifacts. |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 108 | self.dest_image = os.path.join(self.image_dir, 'dlc.img') |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 109 | self.dest_table = os.path.join(self.meta_dir, 'table') |
| 110 | self.dest_imageloader_json = os.path.join(self.meta_dir, 'imageloader.json') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 111 | |
| 112 | def SquashOwnerships(self, path): |
| 113 | """Squash the owernships & permissions for files. |
| 114 | |
| 115 | Args: |
| 116 | path: (str) path that contains all files to be processed. |
| 117 | """ |
| 118 | cros_build_lib.SudoRunCommand(['chown', '-R', '0:0', path]) |
| 119 | cros_build_lib.SudoRunCommand( |
| 120 | ['find', path, '-exec', 'touch', '-h', '-t', '197001010000.00', '{}', |
| 121 | '+']) |
| 122 | |
| 123 | def CreateExt4Image(self): |
| 124 | """Create an ext4 image.""" |
| 125 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
| 126 | mount_point = os.path.join(temp_dir, 'mount_point') |
| 127 | # Create a raw image file. |
| 128 | with open(self.dest_image, 'w') as f: |
| 129 | f.truncate(self._BLOCKS * self._BLOCK_SIZE) |
| 130 | # Create an ext4 file system on the raw image. |
| 131 | cros_build_lib.RunCommand( |
| 132 | ['/sbin/mkfs.ext4', '-b', str(self._BLOCK_SIZE), '-O', |
| 133 | '^has_journal', self.dest_image], capture_output=True) |
| 134 | # Create the mount_point directory. |
| 135 | osutils.SafeMakedirs(mount_point) |
| 136 | # Mount the ext4 image. |
| 137 | osutils.MountDir(self.dest_image, mount_point, mount_opts=('loop', 'rw')) |
Amin Hassani | cc7ffce | 2019-01-11 14:57:52 -0800 | [diff] [blame] | 138 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 139 | try: |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 140 | self.SetupDlcImageFiles(mount_point) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 141 | finally: |
| 142 | # Unmount the ext4 image. |
| 143 | osutils.UmountDir(mount_point) |
| 144 | # Shrink to minimum size. |
| 145 | cros_build_lib.RunCommand( |
| 146 | ['/sbin/e2fsck', '-y', '-f', self.dest_image], capture_output=True) |
| 147 | cros_build_lib.RunCommand( |
| 148 | ['/sbin/resize2fs', '-M', self.dest_image], capture_output=True) |
| 149 | |
| 150 | def CreateSquashfsImage(self): |
| 151 | """Create a squashfs image.""" |
| 152 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 153 | squashfs_root = os.path.join(temp_dir, 'squashfs-root') |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 154 | self.SetupDlcImageFiles(squashfs_root) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 155 | |
| 156 | cros_build_lib.RunCommand(['mksquashfs', squashfs_root, self.dest_image, |
| 157 | '-4k-align', '-noappend'], |
| 158 | capture_output=True) |
| 159 | |
| 160 | # We changed the ownership and permissions of the squashfs_root |
| 161 | # directory. Now we need to remove it manually. |
| 162 | osutils.RmDir(squashfs_root, sudo=True) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 163 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 164 | def SetupDlcImageFiles(self, dlc_dir): |
| 165 | """Prepares the directory dlc_dir with all the files a DLC needs. |
| 166 | |
| 167 | Args: |
| 168 | dlc_dir: (str) The path to where to setup files inside the DLC. |
| 169 | """ |
| 170 | dlc_root_dir = os.path.join(dlc_dir, self._DLC_ROOT_DIR) |
| 171 | osutils.SafeMakedirs(dlc_root_dir) |
| 172 | osutils.CopyDirContents(self.src_dir, dlc_root_dir) |
| 173 | self.PrepareLsbRelease(dlc_dir) |
| 174 | self.CollectExtraResources(dlc_dir) |
| 175 | self.SquashOwnerships(dlc_dir) |
| 176 | |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 177 | def PrepareLsbRelease(self, dlc_dir): |
| 178 | """Prepare the file /etc/lsb-release in the DLC module. |
| 179 | |
| 180 | This file is used dropping some identification parameters for the DLC. |
| 181 | |
| 182 | Args: |
| 183 | dlc_dir: (str) The path to root directory of the DLC. e.g. mounted point |
| 184 | when we are creating the image. |
| 185 | """ |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 186 | # Reading the platform APPID and creating the DLC APPID. |
| 187 | platform_lsb_release = osutils.ReadFile(os.path.join(self.sysroot, |
| 188 | LSB_RELEASE)) |
| 189 | app_id = None |
| 190 | for line in platform_lsb_release.split('\n'): |
| 191 | if line.startswith(cros_set_lsb_release.LSB_KEY_APPID_RELEASE): |
| 192 | app_id = line.split('=')[1] |
| 193 | if app_id is None: |
| 194 | raise Exception('%s does not have a valid key %s' % |
| 195 | (platform_lsb_release, |
| 196 | cros_set_lsb_release.LSB_KEY_APPID_RELEASE)) |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 197 | |
| 198 | fields = { |
| 199 | DLC_ID_KEY: self.dlc_id, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 200 | DLC_PACKAGE_KEY: self.dlc_package, |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 201 | DLC_NAME_KEY: self.name, |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 202 | # The DLC appid is generated by concatenating the platform appid with |
| 203 | # the DLC ID using an underscore. This pattern should never be changed |
| 204 | # once set otherwise it can break a lot of things! |
| 205 | DLC_APPID_KEY: '%s_%s' % (app_id, self.dlc_id), |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 206 | } |
Amin Hassani | 8f1cc0f | 2019-03-06 15:34:53 -0800 | [diff] [blame] | 207 | |
| 208 | lsb_release = os.path.join(dlc_dir, LSB_RELEASE) |
| 209 | osutils.SafeMakedirs(os.path.dirname(lsb_release)) |
Amin Hassani | d5742d3 | 2019-01-22 21:13:34 -0800 | [diff] [blame] | 210 | content = ''.join(['%s=%s\n' % (k, v) for k, v in fields.items()]) |
| 211 | osutils.WriteFile(lsb_release, content) |
| 212 | |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 213 | def CollectExtraResources(self, dlc_dir): |
| 214 | """Collect the extra resources needed by the DLC module. |
| 215 | |
| 216 | Look at the documentation around _EXTRA_RESOURCES. |
| 217 | |
| 218 | Args: |
| 219 | dlc_dir: (str) The path to root directory of the DLC. e.g. mounted point |
| 220 | when we are creating the image. |
| 221 | """ |
| 222 | for r in _EXTRA_RESOURCES: |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 223 | source_path = os.path.join(self.sysroot, r) |
Amin Hassani | 11a88cf | 2019-01-29 15:31:24 -0800 | [diff] [blame] | 224 | target_path = os.path.join(dlc_dir, r) |
| 225 | osutils.SafeMakedirs(os.path.dirname(target_path)) |
| 226 | shutil.copyfile(source_path, target_path) |
| 227 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 228 | def CreateImage(self): |
| 229 | """Create the image and copy the DLC files to it.""" |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 230 | if self.fs_type == _EXT4_TYPE: |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 231 | self.CreateExt4Image() |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 232 | elif self.fs_type == _SQUASHFS_TYPE: |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 233 | self.CreateSquashfsImage() |
| 234 | else: |
| 235 | raise ValueError('Wrong fs type: %s used:' % self.fs_type) |
| 236 | |
Xiaochu Liu | 36b3059 | 2019-08-06 09:39:54 -0700 | [diff] [blame^] | 237 | self.VerifyImageSize(os.path.getsize(self.dest_image)) |
| 238 | |
| 239 | def VerifyImageSize(self, image_bytes): |
| 240 | """Verify the image can fit to the reserved file.""" |
| 241 | preallocated_bytes = self.pre_allocated_blocks * self._BLOCK_SIZE |
| 242 | # Verifies the actual size of the DLC image is NOT smaller than the |
| 243 | # preallocated space. |
| 244 | if preallocated_bytes < image_bytes: |
| 245 | raise ValueError( |
| 246 | 'The DLC_PREALLOC_BLOCKS (%s) value set in DLC ebuild resulted in a ' |
| 247 | 'max size of DLC_PREALLOC_BLOCKS * 4K (%s) bytes the DLC image is ' |
| 248 | 'allowed to occupy. The value is smaller than the actual image size ' |
| 249 | '(%s) required. Increase DLC_PREALLOC_BLOCKS in your ebuild to at ' |
| 250 | 'least %d.' % ( |
| 251 | self.pre_allocated_blocks, preallocated_bytes, image_bytes, |
| 252 | image_bytes // self._BLOCK_SIZE)) |
| 253 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 254 | def GetImageloaderJsonContent(self, image_hash, table_hash, blocks): |
| 255 | """Return the content of imageloader.json file. |
| 256 | |
| 257 | Args: |
| 258 | image_hash: (str) sha256 hash of the DLC image. |
| 259 | table_hash: (str) sha256 hash of the DLC table file. |
| 260 | blocks: (int) number of blocks in the DLC image. |
| 261 | |
| 262 | Returns: |
| 263 | [str]: content of imageloader.json file. |
| 264 | """ |
| 265 | return { |
| 266 | 'fs-type': self.fs_type, |
| 267 | 'id': self.dlc_id, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 268 | 'package': self.dlc_package, |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 269 | 'image-sha256-hash': image_hash, |
| 270 | 'image-type': 'dlc', |
| 271 | 'is-removable': True, |
| 272 | 'manifest-version': self._MANIFEST_VERSION, |
| 273 | 'name': self.name, |
| 274 | 'pre-allocated-size': self.pre_allocated_blocks * self._BLOCK_SIZE, |
| 275 | 'size': blocks * self._BLOCK_SIZE, |
| 276 | 'table-sha256-hash': table_hash, |
| 277 | 'version': self.version, |
| 278 | } |
| 279 | |
| 280 | def GenerateVerity(self): |
| 281 | """Generate verity parameters and hashes for the image.""" |
| 282 | with osutils.TempDir(prefix='dlc_') as temp_dir: |
| 283 | hash_tree = os.path.join(temp_dir, 'hash_tree') |
| 284 | # Get blocks in the image. |
| 285 | blocks = math.ceil( |
| 286 | os.path.getsize(self.dest_image) / self._BLOCK_SIZE) |
| 287 | result = cros_build_lib.RunCommand( |
| 288 | ['verity', 'mode=create', 'alg=sha256', 'payload=' + self.dest_image, |
| 289 | 'payload_blocks=' + str(blocks), 'hashtree=' + hash_tree, |
| 290 | 'salt=random'], capture_output=True) |
| 291 | table = result.output |
| 292 | |
| 293 | # Append the merkle tree to the image. |
| 294 | osutils.WriteFile(self.dest_image, osutils.ReadFile(hash_tree), 'a+') |
| 295 | |
| 296 | # Write verity parameter to table file. |
| 297 | osutils.WriteFile(self.dest_table, table) |
| 298 | |
| 299 | # Compute image hash. |
| 300 | image_hash = HashFile(self.dest_image) |
| 301 | table_hash = HashFile(self.dest_table) |
| 302 | # Write image hash to imageloader.json file. |
| 303 | blocks = math.ceil( |
| 304 | os.path.getsize(self.dest_image) / self._BLOCK_SIZE) |
| 305 | imageloader_json_content = self.GetImageloaderJsonContent( |
| 306 | image_hash, table_hash, int(blocks)) |
| 307 | with open(self.dest_imageloader_json, 'w') as f: |
| 308 | json.dump(imageloader_json_content, f) |
| 309 | |
| 310 | def GenerateDLC(self): |
| 311 | """Generate a DLC artifact.""" |
| 312 | # Create the image and copy the DLC files to it. |
| 313 | self.CreateImage() |
| 314 | # Generate hash tree and other metadata. |
| 315 | self.GenerateVerity() |
| 316 | |
| 317 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 318 | def CopyAllDlcs(sysroot, install_root_dir): |
| 319 | """Copies all DLC image files into the images directory. |
| 320 | |
| 321 | Copies the DLC image files in the given build directory into the given DLC |
| 322 | image directory. If the DLC build directory does not exist, or there is no DLC |
| 323 | for that board, this function does nothing. |
| 324 | |
| 325 | Args: |
| 326 | sysroot: Path to directory containing DLC images, e.g /build/<board>. |
| 327 | install_root_dir: Path to DLC output directory, |
| 328 | e.g. src/build/images/<board>/<version>. |
| 329 | """ |
| 330 | output_dir = os.path.join(install_root_dir, 'dlc') |
| 331 | build_dir = os.path.join(sysroot, DLC_IMAGE_DIR) |
| 332 | |
| 333 | if not os.path.exists(build_dir) or not os.listdir(build_dir): |
Amin Hassani | 6c0228b | 2019-03-04 13:42:33 -0800 | [diff] [blame] | 334 | logging.info('There is no DLC to copy to output, ignoring.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 335 | return |
| 336 | |
Amin Hassani | 6c0228b | 2019-03-04 13:42:33 -0800 | [diff] [blame] | 337 | logging.info('Copying all DLC images to their destination path.') |
| 338 | logging.info('Detected the following DLCs: %s', |
| 339 | ', '.join(os.listdir(build_dir))) |
| 340 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 341 | osutils.SafeMakedirs(output_dir) |
| 342 | osutils.CopyDirContents(build_dir, output_dir) |
| 343 | |
Amin Hassani | 6c0228b | 2019-03-04 13:42:33 -0800 | [diff] [blame] | 344 | logging.info('Done copying the DLCs to their destination.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 345 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 346 | def GetParser(): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 347 | """Creates an argument parser and returns it.""" |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 348 | parser = commandline.ArgumentParser(description=__doc__) |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 349 | # This script is used both for building an individual DLC or copying all final |
| 350 | # DLCs images to their final destination nearby chromiumsos_test_image.bin, |
| 351 | # etc. These two arguments are required in both cases. |
| 352 | parser.add_argument('--sysroot', type='path', metavar='DIR', required=True, |
| 353 | help="The root path to the board's build root, e.g. " |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 354 | '/build/eve') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 355 | parser.add_argument('--install-root-dir', type='path', metavar='DIR', |
| 356 | required=True, |
| 357 | help='If building a specific DLC, it is the root path to' |
| 358 | ' install DLC images (%s) and metadata (%s). Otherwise it' |
| 359 | ' is the target directory where the Chrome OS images gets' |
| 360 | ' dropped in build_image, e.g. ' |
| 361 | 'src/build/images/<board>/latest.' % (DLC_IMAGE_DIR, |
| 362 | DLC_META_DIR)) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 363 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 364 | one_dlc = parser.add_argument_group('Arguments required for building only ' |
| 365 | 'one DLC') |
| 366 | one_dlc.add_argument('--src-dir', type='path', metavar='SRC_DIR_PATH', |
| 367 | help='Root directory path that contains all DLC files ' |
| 368 | 'to be packed.') |
| 369 | one_dlc.add_argument('--pre-allocated-blocks', type=int, |
| 370 | metavar='PREALLOCATEDBLOCKS', |
| 371 | help='Number of blocks (block size is 4k) that need to' |
| 372 | 'be pre-allocated on device.') |
| 373 | one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.') |
| 374 | one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).') |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 375 | one_dlc.add_argument('--package', metavar='PACKAGE', |
| 376 | help='The package ID that is unique within a DLC, One' |
| 377 | ' DLC cannot have duplicate package IDs.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 378 | one_dlc.add_argument('--name', metavar='NAME', |
| 379 | help='A human-readable name for the DLC.') |
| 380 | one_dlc.add_argument('--fs-type', metavar='FS_TYPE', default=_SQUASHFS_TYPE, |
| 381 | choices=(_SQUASHFS_TYPE, _EXT4_TYPE), |
| 382 | help='File system type of the image.') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 383 | return parser |
| 384 | |
| 385 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 386 | def ValidateArguments(opts): |
| 387 | """Validates the correctness of the passed arguments. |
| 388 | |
| 389 | Args: |
| 390 | opts: Parsed arguments. |
| 391 | """ |
| 392 | # Make sure if the intention is to build one DLC, all the required arguments |
| 393 | # are passed. |
| 394 | per_dlc_req_args = ('src_dir', 'pre_allocated_blocks', 'version', 'id', |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 395 | 'package', 'name') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 396 | if (opts.id and |
| 397 | not all(vars(opts)[arg] is not None for arg in per_dlc_req_args)): |
| 398 | raise Exception('If the intention is to build only one DLC, all the flags' |
| 399 | '%s required for it should be passed .' % per_dlc_req_args) |
| 400 | |
| 401 | if opts.fs_type == _EXT4_TYPE: |
| 402 | raise Exception('ext4 unsupported, see https://crbug.com/890060') |
| 403 | |
| 404 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 405 | def main(argv): |
| 406 | opts = GetParser().parse_args(argv) |
| 407 | opts.Freeze() |
| 408 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 409 | ValidateArguments(opts) |
Amin Hassani | 2af75a9 | 2019-01-22 21:07:45 -0800 | [diff] [blame] | 410 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 411 | if opts.id: |
| 412 | logging.info('Building DLC %s', opts.id) |
| 413 | dlc_generator = DlcGenerator(src_dir=opts.src_dir, |
| 414 | sysroot=opts.sysroot, |
| 415 | install_root_dir=opts.install_root_dir, |
| 416 | fs_type=opts.fs_type, |
| 417 | pre_allocated_blocks=opts.pre_allocated_blocks, |
| 418 | version=opts.version, |
| 419 | dlc_id=opts.id, |
Amin Hassani | b5a4804 | 2019-03-18 14:30:51 -0700 | [diff] [blame] | 420 | dlc_package=opts.package, |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 421 | name=opts.name) |
| 422 | dlc_generator.GenerateDLC() |
| 423 | else: |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 424 | CopyAllDlcs(opts.sysroot, opts.install_root_dir) |