blob: 4b7598b4bbcbef80be077846f2659abb08a68557 [file] [log] [blame]
Xiaochu Liudeed0232018-06-26 10:25:34 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Xiaochu Liudeed0232018-06-26 10:25:34 -07005"""Script to generate a DLC (Downloadable Content) artifact."""
6
7from __future__ import print_function
8
Andreweff865f2020-04-24 13:39:44 -07009import sys
Xiaochu Liudeed0232018-06-26 10:25:34 -070010
Andrew5743d382020-06-16 09:55:04 -070011from chromite.lib import dlc_lib
Xiaochu Liudeed0232018-06-26 10:25:34 -070012from chromite.lib import commandline
Amin Hassanib97a5ee2019-01-23 14:44:43 -080013from chromite.lib import cros_logging as logging
Xiaochu Liudeed0232018-06-26 10:25:34 -070014
15
Andreweff865f2020-04-24 13:39:44 -070016assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
17
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080018
Xiaochu Liudeed0232018-06-26 10:25:34 -070019def GetParser():
Amin Hassanib97a5ee2019-01-23 14:44:43 -080020 """Creates an argument parser and returns it."""
Xiaochu Liudeed0232018-06-26 10:25:34 -070021 parser = commandline.ArgumentParser(description=__doc__)
Amin Hassanib97a5ee2019-01-23 14:44:43 -080022 # This script is used both for building an individual DLC or copying all final
Andrew5743d382020-06-16 09:55:04 -070023 # DLCs images to their final destination nearby chromiumos_test_image.bin,
Amin Hassanib97a5ee2019-01-23 14:44:43 -080024 # etc. These two arguments are required in both cases.
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080025 parser.add_argument(
26 '--sysroot',
27 type='path',
28 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080029 help="The root path to the board's build root, e.g. "
30 '/build/eve')
Andrew67b5fa72020-02-05 14:14:48 -080031 # TODO(andrewlassalle): Remove src-dir in the future(2021?) if nobody uses it.
32 parser.add_argument(
33 '--src-dir',
34 type='path',
35 metavar='SRC_DIR_PATH',
36 help='Override the default Root directory path that contains all DLC '
37 'files to be packed.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080038 parser.add_argument(
39 '--install-root-dir',
40 type='path',
41 metavar='DIR',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080042 help='If building a specific DLC, it is the root path to'
43 ' install DLC images (%s) and metadata (%s). Otherwise it'
44 ' is the target directory where the Chrome OS images gets'
45 ' dropped in build_image, e.g. '
Andrew5743d382020-06-16 09:55:04 -070046 'src/build/images/<board>/latest.' % (dlc_lib.DLC_BUILD_DIR,
47 dlc_lib.DLC_META_DIR))
Amin Hassani22a25eb2019-01-11 14:25:02 -080048
Amin Hassanib97a5ee2019-01-23 14:44:43 -080049 one_dlc = parser.add_argument_group('Arguments required for building only '
50 'one DLC')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080051 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080052 '--rootfs',
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080053 type='path',
Andrew67b5fa72020-02-05 14:14:48 -080054 metavar='ROOT_FS_PATH',
55 help='Path to the platform rootfs.')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080056 one_dlc.add_argument(
57 '--pre-allocated-blocks',
58 type=int,
59 metavar='PREALLOCATEDBLOCKS',
60 help='Number of blocks (block size is 4k) that need to'
61 'be pre-allocated on device.')
Amin Hassanib97a5ee2019-01-23 14:44:43 -080062 one_dlc.add_argument('--version', metavar='VERSION', help='DLC Version.')
63 one_dlc.add_argument('--id', metavar='ID', help='DLC ID (unique per DLC).')
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080064 one_dlc.add_argument(
65 '--package',
66 metavar='PACKAGE',
67 help='The package ID that is unique within a DLC, One'
68 ' DLC cannot have duplicate package IDs.')
69 one_dlc.add_argument(
70 '--name', metavar='NAME', help='A human-readable name for the DLC.')
71 one_dlc.add_argument(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -070072 '--description',
73 help='The description for the DLC.')
74 one_dlc.add_argument(
Andrew06a5f812020-01-23 08:08:32 -080075 '--board', metavar='BOARD', help='The target board we are building for.')
76 one_dlc.add_argument('--fullnamerev', metavar='FULL_NAME_REV',
77 help='The full ebuild package name.')
78 one_dlc.add_argument(
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080079 '--fs-type',
80 metavar='FS_TYPE',
Andrew5743d382020-06-16 09:55:04 -070081 default=dlc_lib.SQUASHFS_TYPE,
82 choices=(dlc_lib.SQUASHFS_TYPE, dlc_lib.EXT4_TYPE),
Jae Hoon Kimfaca4b02020-01-09 13:49:03 -080083 help='File system type of the image.')
84 one_dlc.add_argument(
85 '--preload',
86 default=False,
87 action='store_true',
88 help='Allow preloading of DLC.')
Andrew67b5fa72020-02-05 14:14:48 -080089 one_dlc.add_argument(
Andrew5743d382020-06-16 09:55:04 -070090 '--used-by', default=dlc_lib.USED_BY_SYSTEM,
91 choices=(dlc_lib.USED_BY_USER, dlc_lib.USED_BY_SYSTEM),
Amin Hassani160e12e2020-04-13 14:29:36 -070092 help='Defines how this DLC will be used so dlcservice can take proper '
93 'actions based on the type of usage. For example, if "user" is passed, '
94 'dlcservice does ref counting when DLC is installed/uninstalled. For '
95 '"system", there will be no such provisions.')
96 one_dlc.add_argument(
Andrew67b5fa72020-02-05 14:14:48 -080097 '--build-package',
98 default=False,
99 action='store_true',
100 help='Flag to indicate if the script is executed during the '
101 'build_packages phase.')
Xiaochu Liudeed0232018-06-26 10:25:34 -0700102 return parser
103
104
Andrew67b5fa72020-02-05 14:14:48 -0800105def ValidateArguments(parser, opts, req_flags, invalid_flags):
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800106 """Validates the correctness of the passed arguments.
107
108 Args:
Andrew67b5fa72020-02-05 14:14:48 -0800109 parser: Arguments parser.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800110 opts: Parsed arguments.
Andrew67b5fa72020-02-05 14:14:48 -0800111 req_flags: all the required flags.
112 invalid_flags: all the flags that are not allowed.
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800113 """
114 # Make sure if the intention is to build one DLC, all the required arguments
Andrew67b5fa72020-02-05 14:14:48 -0800115 # are passed and none of the invalid ones are passed. This will ensure the
116 # script is called twice per DLC.
117 if opts.id:
118 if not all(vars(opts)[x] is not None for x in req_flags):
119 parser.error('If the intention is to build only one DLC, all the flags'
120 '%s required for it should be passed.' % req_flags)
121 if any(vars(opts)[x] is not None for x in invalid_flags):
122 parser.error('If the intention is to build only one DLC, all the flags'
123 '%s should be passed in the build_packages phase, not in '
124 'the build_image phase.' % invalid_flags)
125
Andrew5743d382020-06-16 09:55:04 -0700126 if opts.fs_type == dlc_lib.EXT4_TYPE:
Andrew67b5fa72020-02-05 14:14:48 -0800127 parser.error('ext4 unsupported for DLC, see https://crbug.com/890060')
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800128
Amin Hassanibc1a4792019-10-24 14:39:57 -0700129 if opts.id:
Andrew5743d382020-06-16 09:55:04 -0700130 dlc_lib.ValidateDlcIdentifier(opts.id)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700131 if opts.package:
Andrew5743d382020-06-16 09:55:04 -0700132 dlc_lib.ValidateDlcIdentifier(opts.package)
Amin Hassanibc1a4792019-10-24 14:39:57 -0700133
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800134
Xiaochu Liudeed0232018-06-26 10:25:34 -0700135def main(argv):
Andrew67b5fa72020-02-05 14:14:48 -0800136 parser = GetParser()
137 opts = parser.parse_args(argv)
Xiaochu Liudeed0232018-06-26 10:25:34 -0700138 opts.Freeze()
Andrew67b5fa72020-02-05 14:14:48 -0800139 per_dlc_req_args = ['id']
140 per_dlc_invalid_args = []
141 if opts.build_package:
142 per_dlc_req_args += ['pre_allocated_blocks', 'version', 'name',
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700143 'description', 'package', 'install_root_dir']
Andrew67b5fa72020-02-05 14:14:48 -0800144 per_dlc_invalid_args += ['src_dir', 'sysroot']
Amin Hassanib97a5ee2019-01-23 14:44:43 -0800145 else:
Andrew06a5f812020-01-23 08:08:32 -0800146 per_dlc_req_args += ['sysroot', 'board']
Andrew67b5fa72020-02-05 14:14:48 -0800147 per_dlc_invalid_args += ['name', 'pre_allocated_blocks', 'version',
148 'package']
149
150 ValidateArguments(parser, opts, per_dlc_req_args, per_dlc_invalid_args)
151
152 if opts.build_package:
153 logging.info('Building package: DLC %s', opts.id)
Andrew5743d382020-06-16 09:55:04 -0700154 params = dlc_lib.EbuildParams(
Jae Hoon Kim6ef63172020-04-06 12:39:04 -0700155 dlc_id=opts.id,
156 dlc_package=opts.package,
157 fs_type=opts.fs_type,
158 name=opts.name,
159 description=opts.description,
160 pre_allocated_blocks=opts.pre_allocated_blocks,
161 version=opts.version,
Amin Hassani160e12e2020-04-13 14:29:36 -0700162 preload=opts.preload,
Andrew06a5f812020-01-23 08:08:32 -0800163 used_by=opts.used_by,
164 fullnamerev=opts.fullnamerev)
Andrew67b5fa72020-02-05 14:14:48 -0800165 params.StoreDlcParameters(install_root_dir=opts.install_root_dir, sudo=True)
166
167 else:
Andrew5743d382020-06-16 09:55:04 -0700168 dlc_lib.InstallDlcImages(
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800169 sysroot=opts.sysroot,
Andrew67b5fa72020-02-05 14:14:48 -0800170 dlc_id=opts.id,
Jae Hoon Kim5f411e42020-01-09 13:30:56 -0800171 install_root_dir=opts.install_root_dir,
Andrew67b5fa72020-02-05 14:14:48 -0800172 preload=opts.preload,
173 src_dir=opts.src_dir,
Andrew06a5f812020-01-23 08:08:32 -0800174 rootfs=opts.rootfs,
175 board=opts.board)