blob: 26e936d6691d604f8e8097e211c73f7f125714e8 [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(
51 '--pre-allocated-blocks',
52 type=int,
53 metavar='PREALLOCATEDBLOCKS',
54 help='Number of blocks (block size is 4k) that need to'
55 'be pre-allocated on device.')
Amin Hassanib97a5ee2019-01-23 14:44:43 -080056 one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.')
57 one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080058 one_dlc.add_argument(
59 '--package',
60 metavar='PACKAGE',
61 help='The package ID that is unique within a DLC, One'
62 ' DLC cannot have duplicate package IDs.')
63 one_dlc.add_argument(
64 '--name', metavar='NAME', help='A human-readable name for the DLC.')
65 one_dlc.add_argument(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -070066 '--description',
67 help='The description for the DLC.')
68 one_dlc.add_argument(
Andrew06a5f812020-01-23 08:08:32 -080069 '--board', metavar='BOARD', help='The target board we are building for.')
70 one_dlc.add_argument('--fullnamerev', metavar='FULL_NAME_REV',
71 help='The full ebuild package name.')
72 one_dlc.add_argument(
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080073 '--fs-type',
74 metavar='FS_TYPE',
Andrew5743d382020-06-16 09:55:04 -070075 default=dlc_lib.SQUASHFS_TYPE,
76 choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE),
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080077 help='File system type of the image.')
78 one_dlc.add_argument(
79 '--preload',
80 default=False,
81 action='store_true',
82 help='Allow preloading of DLC.')
Andrew67b5fa72020-02-05 14:14:48 -080083 one_dlc.add_argument(
Andrew5743d382020-06-16 09:55:04 -070084 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
85 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -070086 help='Defines how this DLC will be used so dlcservice can take proper '
87 'actions based on the type of usage. For example, if "user" is passed, '
88 'dlcservice does ref counting when DLC is installed/uninstalled. For '
89 '"system", there will be no such provisions.')
90 one_dlc.add_argument(
Amin Hassani4f9cb9a2021-06-03 11:55:59 -070091 '--days-to-purge',
92 type=int,
93 default=0,
94 help='Defines the number of days before purging a DLC after it has '
95 'been uninstalled.')
96 one_dlc.add_argument(
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -070097 '--mount-file-required',
98 default=False,
99 action='store_true',
100 help='Allow indirect mount file generation for DLC.')
101 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -0800102 '--build-package',
103 default=False,
104 action='store_true',
105 help='Flag to indicate if the script is executed during the '
106 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700107 return parser
108
109
Andrew67b5fa72020-02-05 14:14:48 -0800110def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800111 """Validates the correctness of the passed arguments.
112
113 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800114 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800115 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800116 req_flags: all the required flags.
117 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800118 """
119 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800120 # are passed and none of the invalid ones are passed. This will ensure the
121 # script is called twice per DLC.
122 if opts.id:
123 if not all(vars(opts)[x] is not None for x in req_flags):
124 parser.error('If the intention is to build only one DLC, all the flags'
125 '%s required for it should be passed.' % req_flags)
126 if any(vars(opts)[x] is not None for x in invalid_flags):
127 parser.error('If the intention is to build only one DLC, all the flags'
128 '%s should be passed in the build_packages phase, not in '
129 'the build_image phase.' % invalid_flags)
130
Andrew5743d382020-06-16 09:55:04 -0700131 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800132 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800133
Amin Hassanibc1a4792019-10-24 14:39:57 -0700134 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700135 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700136 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700137 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700138
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800139
Xiaochu Liudeed0232018-06-26 10:25:34 -0700140def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800141 parser = GetParser()
142 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700143 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800144 per_dlc_req_args = ['id']
145 per_dlc_invalid_args = []
146 if opts.build_package:
147 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700148 'description', 'package', 'install_root_dir',
149 'days_to_purge']
Andrew67b5fa72020-02-05 14:14:48 -0800150 per_dlc_invalid_args += ['src_dir', 'sysroot']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800151 else:
Andrew06a5f812020-01-23 08:08:32 -0800152 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800153 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700154 'package', 'days_to_purge']
Andrew67b5fa72020-02-05 14:14:48 -0800155
156 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
157
158 if opts.build_package:
159 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700160 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700161 dlc_id=opts.id,
162 dlc_package=opts.package,
163 fs_type=opts.fs_type,
164 name=opts.name,
165 description=opts.description,
166 pre_allocated_blocks=opts.pre_allocated_blocks,
167 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700168 preload=opts.preload,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700169 mount_file_required=opts.mount_file_required,
Andrew06a5f812020-01-23 08:08:32 -0800170 used_by=opts.used_by,
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700171 days_to_purge=opts.days_to_purge,
Andrew06a5f812020-01-23 08:08:32 -0800172 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800173 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
174
175 else:
Andrew5743d382020-06-16 09:55:04 -0700176 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800177 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800178 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800179 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800180 preload=opts.preload,
181 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800182 rootfs=opts.rootfs,
183 board=opts.board)