Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python -tt |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # Copyright (c) 2016 Intel, Inc. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify it |
| 8 | # under the terms of the GNU General Public License as published by the Free |
| 9 | # Software Foundation; version 2 of the License |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, but |
| 12 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | # for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License along |
| 17 | # with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 18 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | # |
| 20 | # DESCRIPTION |
| 21 | # This module provides parser for kickstart format |
| 22 | # |
| 23 | # AUTHORS |
| 24 | # Ed Bartosh <ed.bartosh> (at] linux.intel.com> |
| 25 | |
| 26 | """Kickstart parser module.""" |
| 27 | |
| 28 | import os |
| 29 | import shlex |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 30 | import logging |
| 31 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 32 | from argparse import ArgumentParser, ArgumentError, ArgumentTypeError |
| 33 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 34 | from wic.engine import find_canned |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | from wic.partition import Partition |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 36 | |
| 37 | logger = logging.getLogger('wic') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 38 | |
| 39 | class KickStartError(Exception): |
| 40 | """Custom exception.""" |
| 41 | pass |
| 42 | |
| 43 | class KickStartParser(ArgumentParser): |
| 44 | """ |
| 45 | This class overwrites error method to throw exception |
| 46 | instead of producing usage message(default argparse behavior). |
| 47 | """ |
| 48 | def error(self, message): |
| 49 | raise ArgumentError(None, message) |
| 50 | |
| 51 | def sizetype(arg): |
| 52 | """ |
| 53 | Custom type for ArgumentParser |
| 54 | Converts size string in <num>[K|k|M|G] format into the integer value |
| 55 | """ |
| 56 | if arg.isdigit(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 57 | return int(arg) * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | |
| 59 | if not arg[:-1].isdigit(): |
| 60 | raise ArgumentTypeError("Invalid size: %r" % arg) |
| 61 | |
| 62 | size = int(arg[:-1]) |
| 63 | if arg.endswith("k") or arg.endswith("K"): |
| 64 | return size |
| 65 | if arg.endswith("M"): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | return size * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | if arg.endswith("G"): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 68 | return size * 1024 * 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | |
| 70 | raise ArgumentTypeError("Invalid size: %r" % arg) |
| 71 | |
| 72 | def overheadtype(arg): |
| 73 | """ |
| 74 | Custom type for ArgumentParser |
| 75 | Converts overhead string to float and checks if it's bigger than 1.0 |
| 76 | """ |
| 77 | try: |
| 78 | result = float(arg) |
| 79 | except ValueError: |
| 80 | raise ArgumentTypeError("Invalid value: %r" % arg) |
| 81 | |
| 82 | if result < 1.0: |
| 83 | raise ArgumentTypeError("Overhead factor should be > 1.0" % arg) |
| 84 | |
| 85 | return result |
| 86 | |
| 87 | def cannedpathtype(arg): |
| 88 | """ |
| 89 | Custom type for ArgumentParser |
| 90 | Tries to find file in the list of canned wks paths |
| 91 | """ |
| 92 | scripts_path = os.path.abspath(os.path.dirname(__file__) + '../../..') |
| 93 | result = find_canned(scripts_path, arg) |
| 94 | if not result: |
| 95 | raise ArgumentTypeError("file not found: %s" % arg) |
| 96 | return result |
| 97 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | def systemidtype(arg): |
| 99 | """ |
| 100 | Custom type for ArgumentParser |
| 101 | Checks if the argument sutisfies system id requirements, |
| 102 | i.e. if it's one byte long integer > 0 |
| 103 | """ |
| 104 | error = "Invalid system type: %s. must be hex "\ |
| 105 | "between 0x1 and 0xFF" % arg |
| 106 | try: |
| 107 | result = int(arg, 16) |
| 108 | except ValueError: |
| 109 | raise ArgumentTypeError(error) |
| 110 | |
| 111 | if result <= 0 or result > 0xff: |
| 112 | raise ArgumentTypeError(error) |
| 113 | |
| 114 | return arg |
| 115 | |
| 116 | class KickStart(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 117 | """"Kickstart parser implementation.""" |
| 118 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 119 | DEFAULT_EXTRA_SPACE = 10*1024 |
| 120 | DEFAULT_OVERHEAD_FACTOR = 1.3 |
| 121 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 122 | def __init__(self, confpath): |
| 123 | |
| 124 | self.partitions = [] |
| 125 | self.bootloader = None |
| 126 | self.lineno = 0 |
| 127 | self.partnum = 0 |
| 128 | |
| 129 | parser = KickStartParser() |
| 130 | subparsers = parser.add_subparsers() |
| 131 | |
| 132 | part = subparsers.add_parser('part') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 133 | part.add_argument('mountpoint', nargs='?') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 134 | part.add_argument('--active', action='store_true') |
| 135 | part.add_argument('--align', type=int) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 136 | part.add_argument('--exclude-path', nargs='+') |
| 137 | part.add_argument("--extra-space", type=sizetype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 138 | part.add_argument('--fsoptions', dest='fsopts') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 139 | part.add_argument('--fstype', default='vfat', |
| 140 | choices=('ext2', 'ext3', 'ext4', 'btrfs', |
| 141 | 'squashfs', 'vfat', 'msdos', 'swap')) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 142 | part.add_argument('--label') |
| 143 | part.add_argument('--no-table', action='store_true') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 144 | part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda') |
| 145 | part.add_argument("--overhead-factor", type=overheadtype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 146 | part.add_argument('--part-type') |
| 147 | part.add_argument('--rootfs-dir') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 148 | |
| 149 | # --size and --fixed-size cannot be specified together; options |
| 150 | # ----extra-space and --overhead-factor should also raise a parser |
| 151 | # --error, but since nesting mutually exclusive groups does not work, |
| 152 | # ----extra-space/--overhead-factor are handled later |
| 153 | sizeexcl = part.add_mutually_exclusive_group() |
| 154 | sizeexcl.add_argument('--size', type=sizetype, default=0) |
| 155 | sizeexcl.add_argument('--fixed-size', type=sizetype, default=0) |
| 156 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 157 | part.add_argument('--source') |
| 158 | part.add_argument('--sourceparams') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 159 | part.add_argument('--system-id', type=systemidtype) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 160 | part.add_argument('--use-uuid', action='store_true') |
| 161 | part.add_argument('--uuid') |
| 162 | |
| 163 | bootloader = subparsers.add_parser('bootloader') |
| 164 | bootloader.add_argument('--append') |
| 165 | bootloader.add_argument('--configfile') |
| 166 | bootloader.add_argument('--ptable', choices=('msdos', 'gpt'), |
| 167 | default='msdos') |
| 168 | bootloader.add_argument('--timeout', type=int) |
| 169 | bootloader.add_argument('--source') |
| 170 | |
| 171 | include = subparsers.add_parser('include') |
| 172 | include.add_argument('path', type=cannedpathtype) |
| 173 | |
| 174 | self._parse(parser, confpath) |
| 175 | if not self.bootloader: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 176 | logger.warning('bootloader config not specified, using defaults\n') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | self.bootloader = bootloader.parse_args([]) |
| 178 | |
| 179 | def _parse(self, parser, confpath): |
| 180 | """ |
| 181 | Parse file in .wks format using provided parser. |
| 182 | """ |
| 183 | with open(confpath) as conf: |
| 184 | lineno = 0 |
| 185 | for line in conf: |
| 186 | line = line.strip() |
| 187 | lineno += 1 |
| 188 | if line and line[0] != '#': |
| 189 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 190 | line_args = shlex.split(line) |
| 191 | parsed = parser.parse_args(line_args) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 192 | except ArgumentError as err: |
| 193 | raise KickStartError('%s:%d: %s' % \ |
| 194 | (confpath, lineno, err)) |
| 195 | if line.startswith('part'): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 196 | # using ArgumentParser one cannot easily tell if option |
| 197 | # was passed as argument, if said option has a default |
| 198 | # value; --overhead-factor/--extra-space cannot be used |
| 199 | # with --fixed-size, so at least detect when these were |
| 200 | # passed with non-0 values ... |
| 201 | if parsed.fixed_size: |
| 202 | if parsed.overhead_factor or parsed.extra_space: |
| 203 | err = "%s:%d: arguments --overhead-factor and --extra-space not "\ |
| 204 | "allowed with argument --fixed-size" \ |
| 205 | % (confpath, lineno) |
| 206 | raise KickStartError(err) |
| 207 | else: |
| 208 | # ... and provide defaults if not using |
| 209 | # --fixed-size iff given option was not used |
| 210 | # (again, one cannot tell if option was passed but |
| 211 | # with value equal to 0) |
| 212 | if '--overhead-factor' not in line_args: |
| 213 | parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR |
| 214 | if '--extra-space' not in line_args: |
| 215 | parsed.extra_space = self.DEFAULT_EXTRA_SPACE |
| 216 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 217 | self.partnum += 1 |
| 218 | self.partitions.append(Partition(parsed, self.partnum)) |
| 219 | elif line.startswith('include'): |
| 220 | self._parse(parser, parsed.path) |
| 221 | elif line.startswith('bootloader'): |
| 222 | if not self.bootloader: |
| 223 | self.bootloader = parsed |
| 224 | else: |
| 225 | err = "%s:%d: more than one bootloader specified" \ |
| 226 | % (confpath, lineno) |
| 227 | raise KickStartError(err) |