blob: 6a643ba3af5f64ad9bf3c2a6e7f5294dac1e9093 [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001#!/usr/bin/env python3
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05002#
3# Copyright (c) 2016 Intel, Inc.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006#
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
15import os
16import shlex
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017import logging
Brad Bishop19323692019-04-05 15:28:33 -040018import re
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020from argparse import ArgumentParser, ArgumentError, ArgumentTypeError
21
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022from wic.engine import find_canned
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050023from wic.partition import Partition
Brad Bishop19323692019-04-05 15:28:33 -040024from wic.misc import get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025
26logger = logging.getLogger('wic')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050027
Brad Bishop19323692019-04-05 15:28:33 -040028__expand_var_regexp__ = re.compile(r"\${[^{}@\n\t :]+}")
29
30def 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 Williamsd8c66bc2016-06-20 12:57:21 -050042class KickStartError(Exception):
43 """Custom exception."""
44 pass
45
46class 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
54def 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 Williamsc0f7c042017-02-23 20:41:17 -060060 return int(arg) * 1024
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061
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 Williamsc0f7c042017-02-23 20:41:17 -060069 return size * 1024
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 if arg.endswith("G"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060071 return size * 1024 * 1024
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072
73 raise ArgumentTypeError("Invalid size: %r" % arg)
74
75def 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
90def 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 Williamsc0f7c042017-02-23 20:41:17 -0600101def 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
119class KickStart():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 """Kickstart parser implementation."""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500121
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 DEFAULT_EXTRA_SPACE = 10*1024
123 DEFAULT_OVERHEAD_FACTOR = 1.3
124
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500125 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 Williamsc0f7c042017-02-23 20:41:17 -0600136 part.add_argument('mountpoint', nargs='?')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500137 part.add_argument('--active', action='store_true')
138 part.add_argument('--align', type=int)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 part.add_argument('--exclude-path', nargs='+')
140 part.add_argument("--extra-space", type=sizetype)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500141 part.add_argument('--fsoptions', dest='fsopts')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142 part.add_argument('--fstype', default='vfat',
143 choices=('ext2', 'ext3', 'ext4', 'btrfs',
144 'squashfs', 'vfat', 'msdos', 'swap'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 part.add_argument('--mkfs-extraopts', default='')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 part.add_argument('--label')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 part.add_argument('--use-label', action='store_true')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 part.add_argument('--no-table', action='store_true')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda')
150 part.add_argument("--overhead-factor", type=overheadtype)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151 part.add_argument('--part-name')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152 part.add_argument('--part-type')
153 part.add_argument('--rootfs-dir')
Brad Bishop08902b02019-08-20 09:16:51 -0400154 part.add_argument('--type', default='primary',
155 choices = ('primary', 'logical'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156
157 # --size and --fixed-size cannot be specified together; options
158 # ----extra-space and --overhead-factor should also raise a parser
159 # --error, but since nesting mutually exclusive groups does not work,
160 # ----extra-space/--overhead-factor are handled later
161 sizeexcl = part.add_mutually_exclusive_group()
162 sizeexcl.add_argument('--size', type=sizetype, default=0)
163 sizeexcl.add_argument('--fixed-size', type=sizetype, default=0)
164
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165 part.add_argument('--source')
166 part.add_argument('--sourceparams')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 part.add_argument('--system-id', type=systemidtype)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 part.add_argument('--use-uuid', action='store_true')
169 part.add_argument('--uuid')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400170 part.add_argument('--fsuuid')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171
172 bootloader = subparsers.add_parser('bootloader')
173 bootloader.add_argument('--append')
174 bootloader.add_argument('--configfile')
175 bootloader.add_argument('--ptable', choices=('msdos', 'gpt'),
176 default='msdos')
177 bootloader.add_argument('--timeout', type=int)
178 bootloader.add_argument('--source')
179
180 include = subparsers.add_parser('include')
181 include.add_argument('path', type=cannedpathtype)
182
183 self._parse(parser, confpath)
184 if not self.bootloader:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185 logger.warning('bootloader config not specified, using defaults\n')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500186 self.bootloader = bootloader.parse_args([])
187
188 def _parse(self, parser, confpath):
189 """
190 Parse file in .wks format using provided parser.
191 """
192 with open(confpath) as conf:
193 lineno = 0
194 for line in conf:
195 line = line.strip()
196 lineno += 1
197 if line and line[0] != '#':
Brad Bishop19323692019-04-05 15:28:33 -0400198 line = expand_line(line)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500199 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500200 line_args = shlex.split(line)
201 parsed = parser.parse_args(line_args)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 except ArgumentError as err:
203 raise KickStartError('%s:%d: %s' % \
204 (confpath, lineno, err))
205 if line.startswith('part'):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800206 # SquashFS does not support filesystem UUID
207 if parsed.fstype == 'squashfs':
208 if parsed.fsuuid:
209 err = "%s:%d: SquashFS does not support UUID" \
210 % (confpath, lineno)
211 raise KickStartError(err)
212 if parsed.label:
213 err = "%s:%d: SquashFS does not support LABEL" \
214 % (confpath, lineno)
215 raise KickStartError(err)
216 if parsed.use_label and not parsed.label:
217 err = "%s:%d: Must set the label with --label" \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400218 % (confpath, lineno)
219 raise KickStartError(err)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500220 # using ArgumentParser one cannot easily tell if option
221 # was passed as argument, if said option has a default
222 # value; --overhead-factor/--extra-space cannot be used
223 # with --fixed-size, so at least detect when these were
224 # passed with non-0 values ...
225 if parsed.fixed_size:
226 if parsed.overhead_factor or parsed.extra_space:
227 err = "%s:%d: arguments --overhead-factor and --extra-space not "\
228 "allowed with argument --fixed-size" \
229 % (confpath, lineno)
230 raise KickStartError(err)
231 else:
232 # ... and provide defaults if not using
233 # --fixed-size iff given option was not used
234 # (again, one cannot tell if option was passed but
235 # with value equal to 0)
236 if '--overhead-factor' not in line_args:
237 parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR
238 if '--extra-space' not in line_args:
239 parsed.extra_space = self.DEFAULT_EXTRA_SPACE
240
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241 self.partnum += 1
242 self.partitions.append(Partition(parsed, self.partnum))
243 elif line.startswith('include'):
244 self._parse(parser, parsed.path)
245 elif line.startswith('bootloader'):
246 if not self.bootloader:
247 self.bootloader = parsed
248 else:
249 err = "%s:%d: more than one bootloader specified" \
250 % (confpath, lineno)
251 raise KickStartError(err)