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