blob: f6d5986536c16d318703dc2d7d2dadb444dffa15 [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"""Script to generate a DLC (Downloadable Content) artifact."""
5
Andreweff865f2020-04-24 13:39:44 -07006import sys
Xiaochu Liudeed0232018-06-26 10:25:34 -07007
Andrew5743d382020-06-16 09:55:04 -07008from chromite.lib import dlc_lib
Xiaochu Liudeed0232018-06-26 10:25:34 -07009from chromite.lib import commandline
Amin Hassanib97a5ee2019-01-23 14:44:43 -080010from chromite.lib import cros_logging as logging
Xiaochu Liudeed0232018-06-26 10:25:34 -070011
12
Andreweff865f2020-04-24 13:39:44 -070013assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
14
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080015
Xiaochu Liudeed0232018-06-26 10:25:34 -070016def GetParser():
Amin Hassanib97a5ee2019-01-23 14:44:43 -080017 """Creates an argument parser and returns it."""
Xiaochu Liudeed0232018-06-26 10:25:34 -070018 parser = commandline.ArgumentParser(description=__doc__)
Amin Hassanib97a5ee2019-01-23 14:44:43 -080019 # This script is used both for building an individual DLC or copying all final
Andrew5743d382020-06-16 09:55:04 -070020 # DLCs images to their final destination nearby chromiumos_test_image.bin,
Amin Hassanib97a5ee2019-01-23 14:44:43 -080021 # etc. These two arguments are required in both cases.
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080022 parser.add_argument(
23 '--sysroot',
24 type='path',
25 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080026 help="The root path to the board's build root, e.g. "
27 '/build/eve')
Andrew67b5fa72020-02-05 14:14:48 -080028 # TODO(andrewlassalle): Remove src-dir in the future(2021?) if nobody uses it.
29 parser.add_argument(
30 '--src-dir',
31 type='path',
32 metavar='SRC_DIR_PATH',
33 help='Override the default Root directory path that contains all DLC '
34 'files to be packed.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080035 parser.add_argument(
36 '--install-root-dir',
37 type='path',
38 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080039 help='If building a specific DLC, it is the root path to'
40 ' install DLC images (%s) and metadata (%s). Otherwise it'
41 ' is the target directory where the Chrome OS images gets'
42 ' dropped in build_image, e.g. '
Andrew5743d382020-06-16 09:55:04 -070043 'src/build/images/<board>/latest.' % (dlc_lib.DLC_BUILD_DIR,
44 dlc_lib.DLC_META_DIR))
Amin Hassani22a25eb2019-01-11 14:25:02 -080045
Amin Hassanib97a5ee2019-01-23 14:44:43 -080046 one_dlc = parser.add_argument_group('Arguments required for building only '
47 'one DLC')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080048 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080049 '--rootfs',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080050 type='path',
Andrew67b5fa72020-02-05 14:14:48 -080051 metavar='ROOT_FS_PATH',
52 help='Path to the platform rootfs.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080053 one_dlc.add_argument(
54 '--pre-allocated-blocks',
55 type=int,
56 metavar='PREALLOCATEDBLOCKS',
57 help='Number of blocks (block size is 4k) that need to'
58 'be pre-allocated on device.')
Amin Hassanib97a5ee2019-01-23 14:44:43 -080059 one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.')
60 one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080061 one_dlc.add_argument(
62 '--package',
63 metavar='PACKAGE',
64 help='The package ID that is unique within a DLC, One'
65 ' DLC cannot have duplicate package IDs.')
66 one_dlc.add_argument(
67 '--name', metavar='NAME', help='A human-readable name for the DLC.')
68 one_dlc.add_argument(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -070069 '--description',
70 help='The description for the DLC.')
71 one_dlc.add_argument(
Andrew06a5f812020-01-23 08:08:32 -080072 '--board', metavar='BOARD', help='The target board we are building for.')
73 one_dlc.add_argument('--fullnamerev', metavar='FULL_NAME_REV',
74 help='The full ebuild package name.')
75 one_dlc.add_argument(
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080076 '--fs-type',
77 metavar='FS_TYPE',
Andrew5743d382020-06-16 09:55:04 -070078 default=dlc_lib.SQUASHFS_TYPE,
79 choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE),
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080080 help='File system type of the image.')
81 one_dlc.add_argument(
82 '--preload',
83 default=False,
84 action='store_true',
85 help='Allow preloading of DLC.')
Andrew67b5fa72020-02-05 14:14:48 -080086 one_dlc.add_argument(
Andrew5743d382020-06-16 09:55:04 -070087 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
88 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -070089 help='Defines how this DLC will be used so dlcservice can take proper '
90 'actions based on the type of usage. For example, if "user" is passed, '
91 'dlcservice does ref counting when DLC is installed/uninstalled. For '
92 '"system", there will be no such provisions.')
93 one_dlc.add_argument(
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -070094 '--mount-file-required',
95 default=False,
96 action='store_true',
97 help='Allow indirect mount file generation for DLC.')
98 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080099 '--build-package',
100 default=False,
101 action='store_true',
102 help='Flag to indicate if the script is executed during the '
103 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700104 return parser
105
106
Andrew67b5fa72020-02-05 14:14:48 -0800107def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800108 """Validates the correctness of the passed arguments.
109
110 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800111 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800112 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800113 req_flags: all the required flags.
114 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800115 """
116 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800117 # are passed and none of the invalid ones are passed. This will ensure the
118 # script is called twice per DLC.
119 if opts.id:
120 if not all(vars(opts)[x] is not None for x in req_flags):
121 parser.error('If the intention is to build only one DLC, all the flags'
122 '%s required for it should be passed.' % req_flags)
123 if any(vars(opts)[x] is not None for x in invalid_flags):
124 parser.error('If the intention is to build only one DLC, all the flags'
125 '%s should be passed in the build_packages phase, not in '
126 'the build_image phase.' % invalid_flags)
127
Andrew5743d382020-06-16 09:55:04 -0700128 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800129 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800130
Amin Hassanibc1a4792019-10-24 14:39:57 -0700131 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700132 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700133 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700134 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700135
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800136
Xiaochu Liudeed0232018-06-26 10:25:34 -0700137def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800138 parser = GetParser()
139 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700140 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800141 per_dlc_req_args = ['id']
142 per_dlc_invalid_args = []
143 if opts.build_package:
144 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700145 'description', 'package', 'install_root_dir']
Andrew67b5fa72020-02-05 14:14:48 -0800146 per_dlc_invalid_args += ['src_dir', 'sysroot']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800147 else:
Andrew06a5f812020-01-23 08:08:32 -0800148 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800149 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
150 'package']
151
152 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
153
154 if opts.build_package:
155 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700156 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700157 dlc_id=opts.id,
158 dlc_package=opts.package,
159 fs_type=opts.fs_type,
160 name=opts.name,
161 description=opts.description,
162 pre_allocated_blocks=opts.pre_allocated_blocks,
163 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700164 preload=opts.preload,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700165 mount_file_required=opts.mount_file_required,
Andrew06a5f812020-01-23 08:08:32 -0800166 used_by=opts.used_by,
167 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800168 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
169
170 else:
Andrew5743d382020-06-16 09:55:04 -0700171 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800172 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800173 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800174 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800175 preload=opts.preload,
176 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800177 rootfs=opts.rootfs,
178 board=opts.board)