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 | |
| 54 | def sizetype(arg): |
| 55 | """ |
| 56 | Custom type for ArgumentParser |
| 57 | Converts size string in <num>[K|k|M|G] format into the integer value |
| 58 | """ |
| 59 | if arg.isdigit(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | return int(arg) * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 61 | |
| 62 | if not arg[:-1].isdigit(): |
| 63 | raise ArgumentTypeError("Invalid size: %r" % arg) |
| 64 | |
| 65 | size = int(arg[:-1]) |
| 66 | if arg.endswith("k") or arg.endswith("K"): |
| 67 | return size |
| 68 | if arg.endswith("M"): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 69 | return size * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | if arg.endswith("G"): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | return size * 1024 * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 72 | |
| 73 | raise ArgumentTypeError("Invalid size: %r" % arg) |
| 74 | |
| 75 | def overheadtype(arg): |
| 76 | """ |
| 77 | Custom type for ArgumentParser |
| 78 | Converts overhead string to float and checks if it's bigger than 1.0 |
| 79 | """ |
| 80 | try: |
| 81 | result = float(arg) |
| 82 | except ValueError: |
| 83 | raise ArgumentTypeError("Invalid value: %r" % arg) |
| 84 | |
| 85 | if result < 1.0: |
| 86 | raise ArgumentTypeError("Overhead factor should be > 1.0" % arg) |
| 87 | |
| 88 | return result |
| 89 | |
| 90 | def cannedpathtype(arg): |
| 91 | """ |
| 92 | Custom type for ArgumentParser |
| 93 | Tries to find file in the list of canned wks paths |
| 94 | """ |
| 95 | scripts_path = os.path.abspath(os.path.dirname(__file__) + '../../..') |
| 96 | result = find_canned(scripts_path, arg) |
| 97 | if not result: |
| 98 | raise ArgumentTypeError("file not found: %s" % arg) |
| 99 | return result |
| 100 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 101 | def systemidtype(arg): |
| 102 | """ |
| 103 | Custom type for ArgumentParser |
| 104 | Checks if the argument sutisfies system id requirements, |
| 105 | i.e. if it's one byte long integer > 0 |
| 106 | """ |
| 107 | error = "Invalid system type: %s. must be hex "\ |
| 108 | "between 0x1 and 0xFF" % arg |
| 109 | try: |
| 110 | result = int(arg, 16) |
| 111 | except ValueError: |
| 112 | raise ArgumentTypeError(error) |
| 113 | |
| 114 | if result <= 0 or result > 0xff: |
| 115 | raise ArgumentTypeError(error) |
| 116 | |
| 117 | return arg |
| 118 | |
| 119 | class KickStart(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 120 | """Kickstart parser implementation.""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 121 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 122 | DEFAULT_EXTRA_SPACE = 10*1024 |
| 123 | DEFAULT_OVERHEAD_FACTOR = 1.3 |
| 124 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 125 | def __init__(self, confpath): |
| 126 | |
| 127 | self.partitions = [] |
| 128 | self.bootloader = None |
| 129 | self.lineno = 0 |
| 130 | self.partnum = 0 |
| 131 | |
| 132 | parser = KickStartParser() |
| 133 | subparsers = parser.add_subparsers() |
| 134 | |
| 135 | part = subparsers.add_parser('part') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 136 | part.add_argument('mountpoint', nargs='?') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 137 | part.add_argument('--active', action='store_true') |
| 138 | part.add_argument('--align', type=int) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | part.add_argument('--exclude-path', nargs='+') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 140 | part.add_argument('--include-path', nargs='+', action='append') |
| 141 | part.add_argument('--change-directory') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 142 | part.add_argument("--extra-space", type=sizetype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 143 | part.add_argument('--fsoptions', dest='fsopts') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | part.add_argument('--fstype', default='vfat', |
| 145 | choices=('ext2', 'ext3', 'ext4', 'btrfs', |
| 146 | 'squashfs', 'vfat', 'msdos', 'swap')) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 147 | part.add_argument('--mkfs-extraopts', default='') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 148 | part.add_argument('--label') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 149 | part.add_argument('--use-label', action='store_true') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 150 | part.add_argument('--no-table', action='store_true') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda') |
| 152 | part.add_argument("--overhead-factor", type=overheadtype) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 153 | part.add_argument('--part-name') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 154 | part.add_argument('--part-type') |
| 155 | part.add_argument('--rootfs-dir') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 156 | part.add_argument('--type', default='primary', |
| 157 | choices = ('primary', 'logical')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 158 | |
| 159 | # --size and --fixed-size cannot be specified together; options |
| 160 | # ----extra-space and --overhead-factor should also raise a parser |
| 161 | # --error, but since nesting mutually exclusive groups does not work, |
| 162 | # ----extra-space/--overhead-factor are handled later |
| 163 | sizeexcl = part.add_mutually_exclusive_group() |
| 164 | sizeexcl.add_argument('--size', type=sizetype, default=0) |
| 165 | sizeexcl.add_argument('--fixed-size', type=sizetype, default=0) |
| 166 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 167 | part.add_argument('--source') |
| 168 | part.add_argument('--sourceparams') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 169 | part.add_argument('--system-id', type=systemidtype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 170 | part.add_argument('--use-uuid', action='store_true') |
| 171 | part.add_argument('--uuid') |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 172 | part.add_argument('--fsuuid') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 173 | |
| 174 | bootloader = subparsers.add_parser('bootloader') |
| 175 | bootloader.add_argument('--append') |
| 176 | bootloader.add_argument('--configfile') |
| 177 | bootloader.add_argument('--ptable', choices=('msdos', 'gpt'), |
| 178 | default='msdos') |
| 179 | bootloader.add_argument('--timeout', type=int) |
| 180 | bootloader.add_argument('--source') |
| 181 | |
| 182 | include = subparsers.add_parser('include') |
| 183 | include.add_argument('path', type=cannedpathtype) |
| 184 | |
| 185 | self._parse(parser, confpath) |
| 186 | if not self.bootloader: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 187 | logger.warning('bootloader config not specified, using defaults\n') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 188 | self.bootloader = bootloader.parse_args([]) |
| 189 | |
| 190 | def _parse(self, parser, confpath): |
| 191 | """ |
| 192 | Parse file in .wks format using provided parser. |
| 193 | """ |
| 194 | with open(confpath) as conf: |
| 195 | lineno = 0 |
| 196 | for line in conf: |
| 197 | line = line.strip() |
| 198 | lineno += 1 |
| 199 | if line and line[0] != '#': |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 200 | line = expand_line(line) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 201 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 202 | line_args = shlex.split(line) |
| 203 | parsed = parser.parse_args(line_args) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | except ArgumentError as err: |
| 205 | raise KickStartError('%s:%d: %s' % \ |
| 206 | (confpath, lineno, err)) |
| 207 | if line.startswith('part'): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 208 | # SquashFS does not support filesystem UUID |
| 209 | if parsed.fstype == 'squashfs': |
| 210 | if parsed.fsuuid: |
| 211 | err = "%s:%d: SquashFS does not support UUID" \ |
| 212 | % (confpath, lineno) |
| 213 | raise KickStartError(err) |
| 214 | if parsed.label: |
| 215 | err = "%s:%d: SquashFS does not support LABEL" \ |
| 216 | % (confpath, lineno) |
| 217 | raise KickStartError(err) |
| 218 | if parsed.use_label and not parsed.label: |
| 219 | err = "%s:%d: Must set the label with --label" \ |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 220 | % (confpath, lineno) |
| 221 | raise KickStartError(err) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 222 | # using ArgumentParser one cannot easily tell if option |
| 223 | # was passed as argument, if said option has a default |
| 224 | # value; --overhead-factor/--extra-space cannot be used |
| 225 | # with --fixed-size, so at least detect when these were |
| 226 | # passed with non-0 values ... |
| 227 | if parsed.fixed_size: |
| 228 | if parsed.overhead_factor or parsed.extra_space: |
| 229 | err = "%s:%d: arguments --overhead-factor and --extra-space not "\ |
| 230 | "allowed with argument --fixed-size" \ |
| 231 | % (confpath, lineno) |
| 232 | raise KickStartError(err) |
| 233 | else: |
| 234 | # ... and provide defaults if not using |
| 235 | # --fixed-size iff given option was not used |
| 236 | # (again, one cannot tell if option was passed but |
| 237 | # with value equal to 0) |
| 238 | if '--overhead-factor' not in line_args: |
| 239 | parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR |
| 240 | if '--extra-space' not in line_args: |
| 241 | parsed.extra_space = self.DEFAULT_EXTRA_SPACE |
| 242 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 243 | self.partnum += 1 |
| 244 | self.partitions.append(Partition(parsed, self.partnum)) |
| 245 | elif line.startswith('include'): |
| 246 | self._parse(parser, parsed.path) |
| 247 | elif line.startswith('bootloader'): |
| 248 | if not self.bootloader: |
| 249 | self.bootloader = parsed |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 250 | # Concatenate the strings set in APPEND |
| 251 | append_var = get_bitbake_var("APPEND") |
| 252 | if append_var: |
| 253 | self.bootloader.append = ' '.join(filter(None, \ |
| 254 | (self.bootloader.append, append_var))) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 255 | else: |
| 256 | err = "%s:%d: more than one bootloader specified" \ |
| 257 | % (confpath, lineno) |
| 258 | raise KickStartError(err) |