blob: e4d648797c8528feadabff552459c7d6770a2d94 [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(
Amin Hassani4f9cb9a2021-06-03 11:55:59 -070090 '--days-to-purge',
91 type=int,
92 default=0,
93 help='Defines the number of days before purging a DLC after it has '
94 'been uninstalled.')
95 one_dlc.add_argument(
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -070096 '--mount-file-required',
97 default=False,
98 action='store_true',
99 help='Allow indirect mount file generation for DLC.')
100 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -0800101 '--build-package',
102 default=False,
103 action='store_true',
104 help='Flag to indicate if the script is executed during the '
105 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700106 return parser
107
108
Andrew67b5fa72020-02-05 14:14:48 -0800109def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800110 """Validates the correctness of the passed arguments.
111
112 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800113 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800114 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800115 req_flags: all the required flags.
116 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800117 """
118 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800119 # are passed and none of the invalid ones are passed. This will ensure the
120 # script is called twice per DLC.
121 if opts.id:
122 if not all(vars(opts)[x] is not None for x in req_flags):
123 parser.error('If the intention is to build only one DLC, all the flags'
124 '%s required for it should be passed.' % req_flags)
125 if any(vars(opts)[x] is not None for x in invalid_flags):
126 parser.error('If the intention is to build only one DLC, all the flags'
127 '%s should be passed in the build_packages phase, not in '
128 'the build_image phase.' % invalid_flags)
129
Andrew5743d382020-06-16 09:55:04 -0700130 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800131 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800132
Amin Hassanibc1a4792019-10-24 14:39:57 -0700133 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700134 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700135 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700136 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700137
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800138
Xiaochu Liudeed0232018-06-26 10:25:34 -0700139def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800140 parser = GetParser()
141 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700142 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800143 per_dlc_req_args = ['id']
144 per_dlc_invalid_args = []
145 if opts.build_package:
146 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700147 'description', 'package', 'install_root_dir',
148 'days_to_purge']
Andrew67b5fa72020-02-05 14:14:48 -0800149 per_dlc_invalid_args += ['src_dir', 'sysroot']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800150 else:
Andrew06a5f812020-01-23 08:08:32 -0800151 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800152 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700153 'package', 'days_to_purge']
Andrew67b5fa72020-02-05 14:14:48 -0800154
155 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
156
157 if opts.build_package:
158 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700159 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700160 dlc_id=opts.id,
161 dlc_package=opts.package,
162 fs_type=opts.fs_type,
163 name=opts.name,
164 description=opts.description,
165 pre_allocated_blocks=opts.pre_allocated_blocks,
166 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700167 preload=opts.preload,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700168 mount_file_required=opts.mount_file_required,
Andrew06a5f812020-01-23 08:08:32 -0800169 used_by=opts.used_by,
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700170 days_to_purge=opts.days_to_purge,
Andrew06a5f812020-01-23 08:08:32 -0800171 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800172 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
173
174 else:
Andrew5743d382020-06-16 09:55:04 -0700175 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800176 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800177 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800178 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800179 preload=opts.preload,
180 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800181 rootfs=opts.rootfs,
182 board=opts.board)