blob: 8d81caa43f5641b3bac28f53a7daaddca492f01c [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(
Andrew5743d382020-06-16 09:55:04 -070094 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
95 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -070096 help='Defines how this DLC will be used so dlcservice can take proper '
97 'actions based on the type of usage. For example, if "user" is passed, '
98 'dlcservice does ref counting when DLC is installed/uninstalled. For '
99 '"system", there will be no such provisions.')
100 one_dlc.add_argument(
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700101 '--days-to-purge',
102 type=int,
103 default=0,
104 help='Defines the number of days before purging a DLC after it has '
105 'been uninstalled.')
106 one_dlc.add_argument(
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700107 '--mount-file-required',
108 default=False,
109 action='store_true',
110 help='Allow indirect mount file generation for DLC.')
111 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -0800112 '--build-package',
113 default=False,
114 action='store_true',
115 help='Flag to indicate if the script is executed during the '
116 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700117 return parser
118
119
Andrew67b5fa72020-02-05 14:14:48 -0800120def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800121 """Validates the correctness of the passed arguments.
122
123 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800124 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800125 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800126 req_flags: all the required flags.
127 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800128 """
129 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800130 # are passed and none of the invalid ones are passed. This will ensure the
131 # script is called twice per DLC.
132 if opts.id:
133 if not all(vars(opts)[x] is not None for x in req_flags):
134 parser.error('If the intention is to build only one DLC, all the flags'
135 '%s required for it should be passed.' % req_flags)
136 if any(vars(opts)[x] is not None for x in invalid_flags):
137 parser.error('If the intention is to build only one DLC, all the flags'
138 '%s should be passed in the build_packages phase, not in '
139 'the build_image phase.' % invalid_flags)
140
Andrew5743d382020-06-16 09:55:04 -0700141 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800142 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800143
Amin Hassanibc1a4792019-10-24 14:39:57 -0700144 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700145 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700146 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700147 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700148
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800149
Xiaochu Liudeed0232018-06-26 10:25:34 -0700150def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800151 parser = GetParser()
152 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700153 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800154 per_dlc_req_args = ['id']
155 per_dlc_invalid_args = []
156 if opts.build_package:
157 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700158 'description', 'package', 'install_root_dir',
159 'days_to_purge']
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700160 per_dlc_invalid_args += ['src_dir', 'sysroot', 'stateful']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800161 else:
Andrew06a5f812020-01-23 08:08:32 -0800162 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800163 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700164 'package', 'days_to_purge']
Andrew67b5fa72020-02-05 14:14:48 -0800165
166 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
167
168 if opts.build_package:
169 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700170 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700171 dlc_id=opts.id,
172 dlc_package=opts.package,
173 fs_type=opts.fs_type,
174 name=opts.name,
175 description=opts.description,
176 pre_allocated_blocks=opts.pre_allocated_blocks,
177 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700178 preload=opts.preload,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700179 factory_install=opts.factory_install,
Jae Hoon Kimf6cafaf2020-06-25 13:15:33 -0700180 mount_file_required=opts.mount_file_required,
Andrew06a5f812020-01-23 08:08:32 -0800181 used_by=opts.used_by,
Amin Hassani4f9cb9a2021-06-03 11:55:59 -0700182 days_to_purge=opts.days_to_purge,
Andrew06a5f812020-01-23 08:08:32 -0800183 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800184 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
185
186 else:
Andrew5743d382020-06-16 09:55:04 -0700187 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800188 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800189 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800190 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800191 preload=opts.preload,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700192 factory_install=opts.factory_install,
Andrew67b5fa72020-02-05 14:14:48 -0800193 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800194 rootfs=opts.rootfs,
Jae Hoon Kim043ea152021-09-24 12:21:40 -0700195 stateful=opts.stateful,
Andrew06a5f812020-01-23 08:08:32 -0800196 board=opts.board)