Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 4 | |
Mike Frysinger | 88770ef | 2021-05-21 11:04:00 -0400 | [diff] [blame^] | 5 | """Script to generate a DLC (Downloadable Content) artifact.""" |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 6 | |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 7 | from chromite.lib import dlc_lib |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 8 | from chromite.lib import commandline |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 9 | from chromite.lib import cros_logging as logging |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 10 | |
| 11 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 12 | def GetParser(): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 13 | """Creates an argument parser and returns it.""" |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 14 | parser = commandline.ArgumentParser(description=__doc__) |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 15 | # This script is used both for building an individual DLC or copying all final |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 16 | # DLCs images to their final destination nearby chromiumos_test_image.bin, |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 17 | # etc. These two arguments are required in both cases. |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 18 | parser.add_argument( |
| 19 | '--sysroot', |
| 20 | type='path', |
| 21 | metavar='DIR', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 22 | help="The root path to the board's build root, e.g. " |
| 23 | '/build/eve') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 24 | # TODO(andrewlassalle): Remove src-dir in the future(2021?) if nobody uses it. |
| 25 | parser.add_argument( |
| 26 | '--src-dir', |
| 27 | type='path', |
| 28 | metavar='SRC_DIR_PATH', |
| 29 | help='Override the default Root directory path that contains all DLC ' |
| 30 | 'files to be packed.') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 31 | parser.add_argument( |
| 32 | '--install-root-dir', |
| 33 | type='path', |
| 34 | metavar='DIR', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 35 | help='If building a specific DLC, it is the root path to' |
| 36 | ' install DLC images (%s) and metadata (%s). Otherwise it' |
| 37 | ' is the target directory where the Chrome OS images gets' |
| 38 | ' dropped in build_image, e.g. ' |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 39 | 'src/build/images/<board>/latest.' % (dlc_lib.DLC_BUILD_DIR, |
| 40 | dlc_lib.DLC_META_DIR)) |
Amin Hassani | 22a25eb | 2019-01-11 14:25:02 -0800 | [diff] [blame] | 41 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 42 | one_dlc = parser.add_argument_group('Arguments required for building only ' |
| 43 | 'one DLC') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 44 | one_dlc.add_argument( |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 45 | '--rootfs', |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 46 | type='path', |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 47 | metavar='ROOT_FS_PATH', |
| 48 | help='Path to the platform rootfs.') |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 49 | one_dlc.add_argument( |
| 50 | '--pre-allocated-blocks', |
| 51 | type=int, |
| 52 | metavar='PREALLOCATEDBLOCKS', |
| 53 | help='Number of blocks (block size is 4k) that need to' |
| 54 | 'be pre-allocated on device.') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 55 | one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.') |
| 56 | 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] | 57 | one_dlc.add_argument( |
| 58 | '--package', |
| 59 | metavar='PACKAGE', |
| 60 | help='The package ID that is unique within a DLC, One' |
| 61 | ' DLC cannot have duplicate package IDs.') |
| 62 | one_dlc.add_argument( |
| 63 | '--name', metavar='NAME', help='A human-readable name for the DLC.') |
| 64 | one_dlc.add_argument( |
Jae Hoon Kim | 6ef6317 | 2020-04-06 12:39:04 -0700 | [diff] [blame] | 65 | '--description', |
| 66 | help='The description for the DLC.') |
| 67 | one_dlc.add_argument( |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 68 | '--board', metavar='BOARD', help='The target board we are building for.') |
| 69 | one_dlc.add_argument('--fullnamerev', metavar='FULL_NAME_REV', |
| 70 | help='The full ebuild package name.') |
| 71 | one_dlc.add_argument( |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 72 | '--fs-type', |
| 73 | metavar='FS_TYPE', |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 74 | default=dlc_lib.SQUASHFS_TYPE, |
| 75 | choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE), |
Jae Hoon Kim | faca4b0 | 2020-01-09 13:49:03 -0800 | [diff] [blame] | 76 | help='File system type of the image.') |
| 77 | one_dlc.add_argument( |
| 78 | '--preload', |
| 79 | default=False, |
| 80 | action='store_true', |
| 81 | help='Allow preloading of DLC.') |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 82 | one_dlc.add_argument( |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 83 | '--used-by', default=dlc_lib.USED_BY_SYSTEM, |
| 84 | choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM), |
Amin Hassani | 160e12e | 2020-04-13 14:29:36 -0700 | [diff] [blame] | 85 | help='Defines how this DLC will be used so dlcservice can take proper ' |
| 86 | 'actions based on the type of usage. For example, if "user" is passed, ' |
| 87 | 'dlcservice does ref counting when DLC is installed/uninstalled. For ' |
| 88 | '"system", there will be no such provisions.') |
| 89 | one_dlc.add_argument( |
Jae Hoon Kim | f6cafaf | 2020-06-25 13:15:33 -0700 | [diff] [blame] | 90 | '--mount-file-required', |
| 91 | default=False, |
| 92 | action='store_true', |
| 93 | help='Allow indirect mount file generation for DLC.') |
| 94 | one_dlc.add_argument( |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 95 | '--build-package', |
| 96 | default=False, |
| 97 | action='store_true', |
| 98 | help='Flag to indicate if the script is executed during the ' |
| 99 | 'build_packages phase.') |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 100 | return parser |
| 101 | |
| 102 | |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 103 | def ValidateArguments(parser, opts, req_flags, invalid_flags): |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 104 | """Validates the correctness of the passed arguments. |
| 105 | |
| 106 | Args: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 107 | parser: Arguments parser. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 108 | opts: Parsed arguments. |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 109 | req_flags: all the required flags. |
| 110 | invalid_flags: all the flags that are not allowed. |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 111 | """ |
| 112 | # 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] | 113 | # are passed and none of the invalid ones are passed. This will ensure the |
| 114 | # script is called twice per DLC. |
| 115 | if opts.id: |
| 116 | if not all(vars(opts)[x] is not None for x in req_flags): |
| 117 | parser.error('If the intention is to build only one DLC, all the flags' |
| 118 | '%s required for it should be passed.' % req_flags) |
| 119 | if any(vars(opts)[x] is not None for x in invalid_flags): |
| 120 | parser.error('If the intention is to build only one DLC, all the flags' |
| 121 | '%s should be passed in the build_packages phase, not in ' |
| 122 | 'the build_image phase.' % invalid_flags) |
| 123 | |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 124 | if opts.fs_type == dlc_lib.EXT4_TYPE: |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 125 | parser.error('ext4 unsupported for DLC, see https://crbug.com/890060') |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 126 | |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 127 | if opts.id: |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 128 | dlc_lib.ValidateDlcIdentifier(opts.id) |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 129 | if opts.package: |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 130 | dlc_lib.ValidateDlcIdentifier(opts.package) |
Amin Hassani | bc1a479 | 2019-10-24 14:39:57 -0700 | [diff] [blame] | 131 | |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 132 | |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 133 | def main(argv): |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 134 | parser = GetParser() |
| 135 | opts = parser.parse_args(argv) |
Xiaochu Liu | deed023 | 2018-06-26 10:25:34 -0700 | [diff] [blame] | 136 | opts.Freeze() |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 137 | per_dlc_req_args = ['id'] |
| 138 | per_dlc_invalid_args = [] |
| 139 | if opts.build_package: |
| 140 | per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name', |
Jae Hoon Kim | 6ef6317 | 2020-04-06 12:39:04 -0700 | [diff] [blame] | 141 | 'description', 'package', 'install_root_dir'] |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 142 | per_dlc_invalid_args += ['src_dir', 'sysroot'] |
Amin Hassani | b97a5ee | 2019-01-23 14:44:43 -0800 | [diff] [blame] | 143 | else: |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 144 | per_dlc_req_args += ['sysroot', 'board'] |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 145 | per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version', |
| 146 | 'package'] |
| 147 | |
| 148 | ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args) |
| 149 | |
| 150 | if opts.build_package: |
| 151 | logging.info('Building package: DLC %s', opts.id) |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 152 | params = dlc_lib.EbuildParams( |
Jae Hoon Kim | 6ef6317 | 2020-04-06 12:39:04 -0700 | [diff] [blame] | 153 | dlc_id=opts.id, |
| 154 | dlc_package=opts.package, |
| 155 | fs_type=opts.fs_type, |
| 156 | name=opts.name, |
| 157 | description=opts.description, |
| 158 | pre_allocated_blocks=opts.pre_allocated_blocks, |
| 159 | version=opts.version, |
Amin Hassani | 160e12e | 2020-04-13 14:29:36 -0700 | [diff] [blame] | 160 | preload=opts.preload, |
Jae Hoon Kim | f6cafaf | 2020-06-25 13:15:33 -0700 | [diff] [blame] | 161 | mount_file_required=opts.mount_file_required, |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 162 | used_by=opts.used_by, |
| 163 | fullnamerev=opts.fullnamerev) |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 164 | params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True) |
| 165 | |
| 166 | else: |
Andrew | 5743d38 | 2020-06-16 09:55:04 -0700 | [diff] [blame] | 167 | dlc_lib.InstallDlcImages( |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 168 | sysroot=opts.sysroot, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 169 | dlc_id=opts.id, |
Jae Hoon Kim | 5f411e4 | 2020-01-09 13:30:56 -0800 | [diff] [blame] | 170 | install_root_dir=opts.install_root_dir, |
Andrew | 67b5fa7 | 2020-02-05 14:14:48 -0800 | [diff] [blame] | 171 | preload=opts.preload, |
| 172 | src_dir=opts.src_dir, |
Andrew | 06a5f81 | 2020-01-23 08:08:32 -0800 | [diff] [blame] | 173 | rootfs=opts.rootfs, |
| 174 | board=opts.board) |