Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2016 Intel, Inc. |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 6 | # |
| 7 | # DESCRIPTION |
| 8 | # This module provides parser for kickstart format |
| 9 | # |
| 10 | # AUTHORS |
| 11 | # Ed Bartosh <ed.bartosh> (at] linux.intel.com> |
| 12 | |
| 13 | """Kickstart parser module.""" |
| 14 | |
| 15 | import os |
| 16 | import shlex |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | import logging |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 18 | import re |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 19 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | from argparse import ArgumentParser, ArgumentError, ArgumentTypeError |
| 21 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | from wic.engine import find_canned |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 23 | from wic.partition import Partition |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 24 | from wic.misc import get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | |
| 26 | logger = logging.getLogger('wic') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 27 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 28 | __expand_var_regexp__ = re.compile(r"\${[^{}@\n\t :]+}") |
| 29 | |
| 30 | def expand_line(line): |
| 31 | while True: |
| 32 | m = __expand_var_regexp__.search(line) |
| 33 | if not m: |
| 34 | return line |
| 35 | key = m.group()[2:-1] |
| 36 | val = get_bitbake_var(key) |
| 37 | if val is None: |
| 38 | logger.warning("cannot expand variable %s" % key) |
| 39 | return line |
| 40 | line = line[:m.start()] + val + line[m.end():] |
| 41 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 42 | class KickStartError(Exception): |
| 43 | """Custom exception.""" |
| 44 | pass |
| 45 | |
| 46 | class KickStartParser(ArgumentParser): |
| 47 | """ |
| 48 | This class overwrites error method to throw exception |
| 49 | instead of producing usage message(default argparse behavior). |
| 50 | """ |
| 51 | def error(self, message): |
| 52 | raise ArgumentError(None, message) |
| 53 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 54 | def sizetype(default, size_in_bytes=False): |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 55 | def f(arg): |
| 56 | """ |
| 57 | Custom type for ArgumentParser |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 58 | Converts size string in <num>[S|s|K|k|M|G] format into the integer value |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 59 | """ |
| 60 | try: |
| 61 | suffix = default |
| 62 | size = int(arg) |
| 63 | except ValueError: |
| 64 | try: |
| 65 | suffix = arg[-1:] |
| 66 | size = int(arg[:-1]) |
| 67 | except ValueError: |
| 68 | raise ArgumentTypeError("Invalid size: %r" % arg) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 70 | |
| 71 | if size_in_bytes: |
| 72 | if suffix == 's' or suffix == 'S': |
| 73 | return size * 512 |
| 74 | mult = 1024 |
| 75 | else: |
| 76 | mult = 1 |
| 77 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 78 | if suffix == "k" or suffix == "K": |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 79 | return size * mult |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 80 | if suffix == "M": |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 81 | return size * mult * 1024 |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 82 | if suffix == "G": |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 83 | return size * mult * 1024 * 1024 |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 84 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | raise ArgumentTypeError("Invalid size: %r" % arg) |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 86 | return f |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 87 | |
| 88 | def overheadtype(arg): |
| 89 | """ |
| 90 | Custom type for ArgumentParser |
| 91 | Converts overhead string to float and checks if it's bigger than 1.0 |
| 92 | """ |
| 93 | try: |
| 94 | result = float(arg) |
| 95 | except ValueError: |
| 96 | raise ArgumentTypeError("Invalid value: %r" % arg) |
| 97 | |
| 98 | if result < 1.0: |
| 99 | raise ArgumentTypeError("Overhead factor should be > 1.0" % arg) |
| 100 | |
| 101 | return result |
| 102 | |
| 103 | def cannedpathtype(arg): |
| 104 | """ |
| 105 | Custom type for ArgumentParser |
| 106 | Tries to find file in the list of canned wks paths |
| 107 | """ |
| 108 | scripts_path = os.path.abspath(os.path.dirname(__file__) + '../../..') |
| 109 | result = find_canned(scripts_path, arg) |
| 110 | if not result: |
| 111 | raise ArgumentTypeError("file not found: %s" % arg) |
| 112 | return result |
| 113 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 114 | def systemidtype(arg): |
| 115 | """ |
| 116 | Custom type for ArgumentParser |
| 117 | Checks if the argument sutisfies system id requirements, |
| 118 | i.e. if it's one byte long integer > 0 |
| 119 | """ |
| 120 | error = "Invalid system type: %s. must be hex "\ |
| 121 | "between 0x1 and 0xFF" % arg |
| 122 | try: |
| 123 | result = int(arg, 16) |
| 124 | except ValueError: |
| 125 | raise ArgumentTypeError(error) |
| 126 | |
| 127 | if result <= 0 or result > 0xff: |
| 128 | raise ArgumentTypeError(error) |
| 129 | |
| 130 | return arg |
| 131 | |
| 132 | class KickStart(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 133 | """Kickstart parser implementation.""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 134 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 135 | DEFAULT_EXTRA_SPACE = 10*1024 |
| 136 | DEFAULT_OVERHEAD_FACTOR = 1.3 |
| 137 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 138 | def __init__(self, confpath): |
| 139 | |
| 140 | self.partitions = [] |
| 141 | self.bootloader = None |
| 142 | self.lineno = 0 |
| 143 | self.partnum = 0 |
| 144 | |
| 145 | parser = KickStartParser() |
| 146 | subparsers = parser.add_subparsers() |
| 147 | |
| 148 | part = subparsers.add_parser('part') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 149 | part.add_argument('mountpoint', nargs='?') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 150 | part.add_argument('--active', action='store_true') |
| 151 | part.add_argument('--align', type=int) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 152 | part.add_argument('--offset', type=sizetype("K", True)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 153 | part.add_argument('--exclude-path', nargs='+') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 154 | part.add_argument('--include-path', nargs='+', action='append') |
| 155 | part.add_argument('--change-directory') |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 156 | part.add_argument("--extra-space", type=sizetype("M")) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 157 | part.add_argument('--fsoptions', dest='fsopts') |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame^] | 158 | part.add_argument('--fspassno', dest='fspassno') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 159 | part.add_argument('--fstype', default='vfat', |
| 160 | choices=('ext2', 'ext3', 'ext4', 'btrfs', |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 161 | 'squashfs', 'vfat', 'msdos', 'erofs', |
| 162 | 'swap')) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 163 | part.add_argument('--mkfs-extraopts', default='') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 164 | part.add_argument('--label') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 165 | part.add_argument('--use-label', action='store_true') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 166 | part.add_argument('--no-table', action='store_true') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 167 | part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda') |
| 168 | part.add_argument("--overhead-factor", type=overheadtype) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 169 | part.add_argument('--part-name') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 170 | part.add_argument('--part-type') |
| 171 | part.add_argument('--rootfs-dir') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 172 | part.add_argument('--type', default='primary', |
| 173 | choices = ('primary', 'logical')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | |
| 175 | # --size and --fixed-size cannot be specified together; options |
| 176 | # ----extra-space and --overhead-factor should also raise a parser |
| 177 | # --error, but since nesting mutually exclusive groups does not work, |
| 178 | # ----extra-space/--overhead-factor are handled later |
| 179 | sizeexcl = part.add_mutually_exclusive_group() |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 180 | sizeexcl.add_argument('--size', type=sizetype("M"), default=0) |
| 181 | sizeexcl.add_argument('--fixed-size', type=sizetype("M"), default=0) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 182 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 183 | part.add_argument('--source') |
| 184 | part.add_argument('--sourceparams') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 185 | part.add_argument('--system-id', type=systemidtype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 186 | part.add_argument('--use-uuid', action='store_true') |
| 187 | part.add_argument('--uuid') |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 188 | part.add_argument('--fsuuid') |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 189 | part.add_argument('--no-fstab-update', action='store_true') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 190 | |
| 191 | bootloader = subparsers.add_parser('bootloader') |
| 192 | bootloader.add_argument('--append') |
| 193 | bootloader.add_argument('--configfile') |
| 194 | bootloader.add_argument('--ptable', choices=('msdos', 'gpt'), |
| 195 | default='msdos') |
| 196 | bootloader.add_argument('--timeout', type=int) |
| 197 | bootloader.add_argument('--source') |
| 198 | |
| 199 | include = subparsers.add_parser('include') |
| 200 | include.add_argument('path', type=cannedpathtype) |
| 201 | |
| 202 | self._parse(parser, confpath) |
| 203 | if not self.bootloader: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 204 | logger.warning('bootloader config not specified, using defaults\n') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 205 | self.bootloader = bootloader.parse_args([]) |
| 206 | |
| 207 | def _parse(self, parser, confpath): |
| 208 | """ |
| 209 | Parse file in .wks format using provided parser. |
| 210 | """ |
| 211 | with open(confpath) as conf: |
| 212 | lineno = 0 |
| 213 | for line in conf: |
| 214 | line = line.strip() |
| 215 | lineno += 1 |
| 216 | if line and line[0] != '#': |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 217 | line = expand_line(line) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 218 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 219 | line_args = shlex.split(line) |
| 220 | parsed = parser.parse_args(line_args) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 221 | except ArgumentError as err: |
| 222 | raise KickStartError('%s:%d: %s' % \ |
| 223 | (confpath, lineno, err)) |
| 224 | if line.startswith('part'): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 225 | # SquashFS does not support filesystem UUID |
| 226 | if parsed.fstype == 'squashfs': |
| 227 | if parsed.fsuuid: |
| 228 | err = "%s:%d: SquashFS does not support UUID" \ |
| 229 | % (confpath, lineno) |
| 230 | raise KickStartError(err) |
| 231 | if parsed.label: |
| 232 | err = "%s:%d: SquashFS does not support LABEL" \ |
| 233 | % (confpath, lineno) |
| 234 | raise KickStartError(err) |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 235 | # erofs does not support filesystem labels |
| 236 | if parsed.fstype == 'erofs' and parsed.label: |
| 237 | err = "%s:%d: erofs does not support LABEL" % (confpath, lineno) |
| 238 | raise KickStartError(err) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 239 | if parsed.fstype == 'msdos' or parsed.fstype == 'vfat': |
| 240 | if parsed.fsuuid: |
| 241 | if parsed.fsuuid.upper().startswith('0X'): |
| 242 | if len(parsed.fsuuid) > 10: |
| 243 | err = "%s:%d: fsuuid %s given in wks kickstart file " \ |
| 244 | "exceeds the length limit for %s filesystem. " \ |
| 245 | "It should be in the form of a 32 bit hexadecimal" \ |
| 246 | "number (for example, 0xABCD1234)." \ |
| 247 | % (confpath, lineno, parsed.fsuuid, parsed.fstype) |
| 248 | raise KickStartError(err) |
| 249 | elif len(parsed.fsuuid) > 8: |
| 250 | err = "%s:%d: fsuuid %s given in wks kickstart file " \ |
| 251 | "exceeds the length limit for %s filesystem. " \ |
| 252 | "It should be in the form of a 32 bit hexadecimal" \ |
| 253 | "number (for example, 0xABCD1234)." \ |
| 254 | % (confpath, lineno, parsed.fsuuid, parsed.fstype) |
| 255 | raise KickStartError(err) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 256 | if parsed.use_label and not parsed.label: |
| 257 | err = "%s:%d: Must set the label with --label" \ |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 258 | % (confpath, lineno) |
| 259 | raise KickStartError(err) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 260 | # using ArgumentParser one cannot easily tell if option |
| 261 | # was passed as argument, if said option has a default |
| 262 | # value; --overhead-factor/--extra-space cannot be used |
| 263 | # with --fixed-size, so at least detect when these were |
| 264 | # passed with non-0 values ... |
| 265 | if parsed.fixed_size: |
| 266 | if parsed.overhead_factor or parsed.extra_space: |
| 267 | err = "%s:%d: arguments --overhead-factor and --extra-space not "\ |
| 268 | "allowed with argument --fixed-size" \ |
| 269 | % (confpath, lineno) |
| 270 | raise KickStartError(err) |
| 271 | else: |
| 272 | # ... and provide defaults if not using |
| 273 | # --fixed-size iff given option was not used |
| 274 | # (again, one cannot tell if option was passed but |
| 275 | # with value equal to 0) |
| 276 | if '--overhead-factor' not in line_args: |
| 277 | parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR |
| 278 | if '--extra-space' not in line_args: |
| 279 | parsed.extra_space = self.DEFAULT_EXTRA_SPACE |
| 280 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 281 | self.partnum += 1 |
| 282 | self.partitions.append(Partition(parsed, self.partnum)) |
| 283 | elif line.startswith('include'): |
| 284 | self._parse(parser, parsed.path) |
| 285 | elif line.startswith('bootloader'): |
| 286 | if not self.bootloader: |
| 287 | self.bootloader = parsed |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 288 | # Concatenate the strings set in APPEND |
| 289 | append_var = get_bitbake_var("APPEND") |
| 290 | if append_var: |
| 291 | self.bootloader.append = ' '.join(filter(None, \ |
| 292 | (self.bootloader.append, append_var))) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 293 | else: |
| 294 | err = "%s:%d: more than one bootloader specified" \ |
| 295 | % (confpath, lineno) |
| 296 | raise KickStartError(err) |