blob: d32a085b387eef374c57cd55929fbf69b2d2d820 [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
Andrew5743d382020-06-16 09:55:04 -07007from chromite.lib import dlc_lib
Xiaochu Liudeed0232018-06-26 10:25:34 -07008from chromite.lib import commandline
Amin Hassanib97a5ee2019-01-23 14:44:43 -08009from chromite.lib import cros_logging as logging
Xiaochu Liudeed0232018-06-26 10:25:34 -070010
11
Xiaochu Liudeed0232018-06-26 10:25:34 -070012def GetParser():
Amin Hassanib97a5ee2019-01-23 14:44:43 -080013 """Creates an argument parser and returns it."""
Xiaochu Liudeed0232018-06-26 10:25:34 -070014 parser = commandline.ArgumentParser(description=__doc__)
Amin Hassanib97a5ee2019-01-23 14:44:43 -080015 # This script is used both for building an individual DLC or copying all final
Andrew5743d382020-06-16 09:55:04 -070016 # DLCs images to their final destination nearby chromiumos_test_image.bin,
Amin Hassanib97a5ee2019-01-23 14:44:43 -080017 # etc. These two arguments are required in both cases.
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080018 parser.add_argument(
19 '--sysroot',
20 type='path',
21 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080022 help="The root path to the board's build root, e.g. "
23 '/build/eve')
Andrew67b5fa72020-02-05 14:14:48 -080024 # 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 Kimfaca4b02020-01-09 13:49:03 -080031 parser.add_argument(
32 '--install-root-dir',
33 type='path',
34 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080035 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. '
Andrew5743d382020-06-16 09:55:04 -070039 'src/build/images/<board>/latest.' % (dlc_lib.DLC_BUILD_DIR,
40 dlc_lib.DLC_META_DIR))
Amin Hassani22a25eb2019-01-11 14:25:02 -080041
Amin Hassanib97a5ee2019-01-23 14:44:43 -080042 one_dlc = parser.add_argument_group('Arguments required for building only '
43 'one DLC')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080044 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080045 '--rootfs',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080046 type='path',
Andrew67b5fa72020-02-05 14:14:48 -080047 metavar='ROOT_FS_PATH',
48 help='Path to the platform rootfs.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080049 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 Hassanib97a5ee2019-01-23 14:44:43 -080055 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 Kimfaca4b02020-01-09 13:49:03 -080057 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 Kim6ef63172020-04-06 12:39:04 -070065 '--description',
66 help='The description for the DLC.')
67 one_dlc.add_argument(
Andrew06a5f812020-01-23 08:08:32 -080068 '--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 Kimfaca4b02020-01-09 13:49:03 -080072 '--fs-type',
73 metavar='FS_TYPE',
Andrew5743d382020-06-16 09:55:04 -070074 default=dlc_lib.SQUASHFS_TYPE,
75 choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE),
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080076 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.')
Andrew67b5fa72020-02-05 14:14:48 -080082 one_dlc.add_argument(
Andrew5743d382020-06-16 09:55:04 -070083 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
84 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -070085 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 Kimf6cafaf2020-06-25 13:15:33 -070090 '--mount-file-required',
91 default=False,
92 action='store_true',
93 help='Allow indirect mount file generation for DLC.')
94 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080095 '--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 Liudeed0232018-06-26 10:25:34 -0700100 return parser
101
102
Andrew67b5fa72020-02-05 14:14:48 -0800103def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800104 """Validates the correctness of the passed arguments.
105
106 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800107 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800108 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800109 req_flags: all the required flags.
110 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800111 """
112 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800113 # 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
Andrew5743d382020-06-16 09:55:04 -0700124 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800125 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800126
Amin Hassanibc1a4792019-10-24 14:39:57 -0700127 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700128 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700129 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700130 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700131
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800132
Xiaochu Liudeed0232018-06-26 10:25:34 -0700133def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800134 parser = GetParser()
135 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700136 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800137 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 Kim6ef63172020-04-06 12:39:04 -0700141 'description', 'package', 'install_root_dir']
Andrew67b5fa72020-02-05 14:14:48 -0800142 per_dlc_invalid_args += ['src_dir', 'sysroot']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800143 else:
Andrew06a5f812020-01-23 08:08:32 -0800144 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800145 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)
Andrew5743d382020-06-16 09:55:04 -0700152 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700153 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 Hassani160e12e2020-04-13 14:29:36 -0700160 preload=opts.preload,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700161 mount_file_required=opts.mount_file_required,
Andrew06a5f812020-01-23 08:08:32 -0800162 used_by=opts.used_by,
163 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800164 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
165
166 else:
Andrew5743d382020-06-16 09:55:04 -0700167 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800168 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800169 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800170 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800171 preload=opts.preload,
172 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800173 rootfs=opts.rootfs,
174 board=opts.board)