blob: 5fc8d4777556ebc352597892aef481fa4b1be43e [file] [log] [blame]
Xiaochu Liudeed0232018-06-26 10:25:34 -07001# 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 Liudeed0232018-06-26 10:25:34 -07004
Mike Frysinger88770ef2021-05-21 11:04:00 -04005"""Script to generate a DLC (Downloadable Content) artifact."""
Xiaochu Liudeed0232018-06-26 10:25:34 -07006
Chris McDonald59650c32021-07-20 15:29:28 -06007import logging
8
Xiaochu Liudeed0232018-06-26 10:25:34 -07009from chromite.lib import commandline
Chris McDonald59650c32021-07-20 15:29:28 -060010from chromite.lib import dlc_lib
Xiaochu Liudeed0232018-06-26 10:25:34 -070011
12
Xiaochu Liudeed0232018-06-26 10:25:34 -070013def GetParser():
Amin Hassanib97a5ee2019-01-23 14:44:43 -080014 """Creates an argument parser and returns it."""
Xiaochu Liudeed0232018-06-26 10:25:34 -070015 parser = commandline.ArgumentParser(description=__doc__)
Amin Hassanib97a5ee2019-01-23 14:44:43 -080016 # This script is used both for building an individual DLC or copying all final
Andrew5743d382020-06-16 09:55:04 -070017 # DLCs images to their final destination nearby chromiumos_test_image.bin,
Amin Hassanib97a5ee2019-01-23 14:44:43 -080018 # etc. These two arguments are required in both cases.
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080019 parser.add_argument(
20 '--sysroot',
21 type='path',
22 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080023 help="The root path to the board's build root, e.g. "
24 '/build/eve')
Andrew67b5fa72020-02-05 14:14:48 -080025 # 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 Kimfaca4b02020-01-09 13:49:03 -080032 parser.add_argument(
33 '--install-root-dir',
34 type='path',
35 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080036 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. '
Andrew5743d382020-06-16 09:55:04 -070040 'src/build/images/<board>/latest.' % (dlc_lib.DLC_BUILD_DIR,
41 dlc_lib.DLC_META_DIR))
Amin Hassani22a25eb2019-01-11 14:25:02 -080042
Amin Hassanib97a5ee2019-01-23 14:44:43 -080043 one_dlc = parser.add_argument_group('Arguments required for building only '
44 'one DLC')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080045 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080046 '--rootfs',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080047 type='path',
Andrew67b5fa72020-02-05 14:14:48 -080048 metavar='ROOT_FS_PATH',
49 help='Path to the platform rootfs.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080050 one_dlc.add_argument(
Jae Hoon Kim043ea152021-09-24 12:21:40 -070051 '--stateful',
52 type='path',
53 metavar='STATEFUL_PATH',
54 help='Path to the platform stateful.')
55 one_dlc.add_argument(
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080056 '--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 Hassanib97a5ee2019-01-23 14:44:43 -080061 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 Kimfaca4b02020-01-09 13:49:03 -080063 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 Kim6ef63172020-04-06 12:39:04 -070071 '--description',
72 help='The description for the DLC.')
73 one_dlc.add_argument(
Andrew06a5f812020-01-23 08:08:32 -080074 '--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 Kimfaca4b02020-01-09 13:49:03 -080078 '--fs-type',
79 metavar='FS_TYPE',
Andrew5743d382020-06-16 09:55:04 -070080 default=dlc_lib.SQUASHFS_TYPE,
81 choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE),
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080082 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.')
Andrew67b5fa72020-02-05 14:14:48 -080088 one_dlc.add_argument(
Jae Hoon Kim043ea152021-09-24 12:21:40 -070089 '--factory-install',
90 default=False,
91 action='store_true',
92 help='Allow factory installing of DLC.')
93 one_dlc.add_argument(
Jae Hoon Kimd7dbb252022-09-02 01:06:35 +000094 '--loadpin-verity-digest',
95 default=False,
96 action='store_true',
97 help='Allow DLC to be a trusted dm-verity digest.')
98 one_dlc.add_argument(
Andrew5743d382020-06-16 09:55:04 -070099 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
100 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -0700101 help='Defines how this DLC will be used so dlcservice can take proper '
102 'actions based on the type of usage. For example, if "user" is passed, '
103 'dlcservice does ref counting when DLC is installed/uninstalled. For '
104 '"system", there will be no such provisions.')
105 one_dlc.add_argument(
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700106 '--days-to-purge',
107 type=int,
108 default=0,
109 help='Defines the number of days before purging a DLC after it has '
110 'been uninstalled.')
111 one_dlc.add_argument(
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700112 '--mount-file-required',
113 default=False,
114 action='store_true',
115 help='Allow indirect mount file generation for DLC.')
116 one_dlc.add_argument(
Jae Hoon Kimc3bedb42022-01-27 19:13:37 -0800117 '--reserved',
118 default=False,
119 action='store_true',
120 help='Always reserve space for this DLC.')
121 one_dlc.add_argument(
122 '--critical-update',
123 default=False,
124 action='store_true',
125 help='Always update with the OS for this DLC.')
126 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -0800127 '--build-package',
128 default=False,
129 action='store_true',
130 help='Flag to indicate if the script is executed during the '
131 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700132 return parser
133
134
Andrew67b5fa72020-02-05 14:14:48 -0800135def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800136 """Validates the correctness of the passed arguments.
137
138 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800139 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800140 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800141 req_flags: all the required flags.
142 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800143 """
144 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800145 # are passed and none of the invalid ones are passed. This will ensure the
146 # script is called twice per DLC.
147 if opts.id:
148 if not all(vars(opts)[x] is not None for x in req_flags):
149 parser.error('If the intention is to build only one DLC, all the flags'
150 '%s required for it should be passed.' % req_flags)
151 if any(vars(opts)[x] is not None for x in invalid_flags):
152 parser.error('If the intention is to build only one DLC, all the flags'
153 '%s should be passed in the build_packages phase, not in '
154 'the build_image phase.' % invalid_flags)
155
Andrew5743d382020-06-16 09:55:04 -0700156 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800157 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800158
Amin Hassanibc1a4792019-10-24 14:39:57 -0700159 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700160 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700161 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700162 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700163
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800164
Xiaochu Liudeed0232018-06-26 10:25:34 -0700165def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800166 parser = GetParser()
167 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700168 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800169 per_dlc_req_args = ['id']
170 per_dlc_invalid_args = []
171 if opts.build_package:
172 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700173 'description', 'package', 'install_root_dir',
174 'days_to_purge']
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700175 per_dlc_invalid_args += ['src_dir', 'sysroot', 'stateful']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800176 else:
Andrew06a5f812020-01-23 08:08:32 -0800177 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800178 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
Jae Hoon Kimc3bedb42022-01-27 19:13:37 -0800179 'package', 'days_to_purge', 'reserved',
180 'critical_update']
Andrew67b5fa72020-02-05 14:14:48 -0800181
182 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
183
184 if opts.build_package:
185 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700186 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700187 dlc_id=opts.id,
188 dlc_package=opts.package,
189 fs_type=opts.fs_type,
190 name=opts.name,
191 description=opts.description,
192 pre_allocated_blocks=opts.pre_allocated_blocks,
193 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700194 preload=opts.preload,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700195 factory_install=opts.factory_install,
Jae Hoon Kimd7dbb252022-09-02 01:06:35 +0000196 loadpin_verity_digest=opts.loadpin_verity_digest,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700197 mount_file_required=opts.mount_file_required,
Jae Hoon Kimc3bedb42022-01-27 19:13:37 -0800198 reserved=opts.reserved,
199 critical_update=opts.critical_update,
Andrew06a5f812020-01-23 08:08:32 -0800200 used_by=opts.used_by,
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700201 days_to_purge=opts.days_to_purge,
Andrew06a5f812020-01-23 08:08:32 -0800202 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800203 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
204
205 else:
Andrew5743d382020-06-16 09:55:04 -0700206 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800207 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800208 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800209 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800210 preload=opts.preload,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700211 factory_install=opts.factory_install,
Andrew67b5fa72020-02-05 14:14:48 -0800212 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800213 rootfs=opts.rootfs,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700214 stateful=opts.stateful,
Andrew06a5f812020-01-23 08:08:32 -0800215 board=opts.board)