blob: 7a4cc83af556d7f0b29141548fbd1d321ee30b9f [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
Andrew Geisslerc9f78652020-09-18 14:11:35 -050054def sizetype(default, size_in_bytes=False):
Andrew Geissler4ed12e12020-06-05 18:00:41 -050055 def f(arg):
56 """
57 Custom type for ArgumentParser
Andrew Geisslerc9f78652020-09-18 14:11:35 -050058 Converts size string in <num>[S|s|K|k|M|G] format into the integer value
Andrew Geissler4ed12e12020-06-05 18:00:41 -050059 """
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 Williamsd8c66bc2016-06-20 12:57:21 -050069
Andrew Geisslerc9f78652020-09-18 14:11:35 -050070
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 Geissler4ed12e12020-06-05 18:00:41 -050078 if suffix == "k" or suffix == "K":
Andrew Geisslerc9f78652020-09-18 14:11:35 -050079 return size * mult
Andrew Geissler4ed12e12020-06-05 18:00:41 -050080 if suffix == "M":
Andrew Geisslerc9f78652020-09-18 14:11:35 -050081 return size * mult * 1024
Andrew Geissler4ed12e12020-06-05 18:00:41 -050082 if suffix == "G":
Andrew Geisslerc9f78652020-09-18 14:11:35 -050083 return size * mult * 1024 * 1024
Andrew Geissler4ed12e12020-06-05 18:00:41 -050084
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085 raise ArgumentTypeError("Invalid size: %r" % arg)
Andrew Geissler4ed12e12020-06-05 18:00:41 -050086 return f
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087
88def 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
103def 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 Williamsc0f7c042017-02-23 20:41:17 -0600114def 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
132class KickStart():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 """Kickstart parser implementation."""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500134
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135 DEFAULT_EXTRA_SPACE = 10*1024
136 DEFAULT_OVERHEAD_FACTOR = 1.3
137
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500138 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 Williamsc0f7c042017-02-23 20:41:17 -0600149 part.add_argument('mountpoint', nargs='?')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 part.add_argument('--active', action='store_true')
151 part.add_argument('--align', type=int)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500152 part.add_argument('--offset', type=sizetype("K", True))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 part.add_argument('--exclude-path', nargs='+')
Andrew Geissler82c905d2020-04-13 13:39:40 -0500154 part.add_argument('--include-path', nargs='+', action='append')
155 part.add_argument('--change-directory')
Andrew Geissler5a43b432020-06-13 10:46:56 -0500156 part.add_argument("--extra-space", type=sizetype("M"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157 part.add_argument('--fsoptions', dest='fsopts')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 part.add_argument('--fstype', default='vfat',
159 choices=('ext2', 'ext3', 'ext4', 'btrfs',
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700160 'squashfs', 'vfat', 'msdos', 'erofs',
161 'swap'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500162 part.add_argument('--mkfs-extraopts', default='')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500163 part.add_argument('--label')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800164 part.add_argument('--use-label', action='store_true')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165 part.add_argument('--no-table', action='store_true')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda')
167 part.add_argument("--overhead-factor", type=overheadtype)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 part.add_argument('--part-name')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169 part.add_argument('--part-type')
170 part.add_argument('--rootfs-dir')
Brad Bishop08902b02019-08-20 09:16:51 -0400171 part.add_argument('--type', default='primary',
172 choices = ('primary', 'logical'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173
174 # --size and --fixed-size cannot be specified together; options
175 # ----extra-space and --overhead-factor should also raise a parser
176 # --error, but since nesting mutually exclusive groups does not work,
177 # ----extra-space/--overhead-factor are handled later
178 sizeexcl = part.add_mutually_exclusive_group()
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500179 sizeexcl.add_argument('--size', type=sizetype("M"), default=0)
180 sizeexcl.add_argument('--fixed-size', type=sizetype("M"), default=0)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500182 part.add_argument('--source')
183 part.add_argument('--sourceparams')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600184 part.add_argument('--system-id', type=systemidtype)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185 part.add_argument('--use-uuid', action='store_true')
186 part.add_argument('--uuid')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400187 part.add_argument('--fsuuid')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500188
189 bootloader = subparsers.add_parser('bootloader')
190 bootloader.add_argument('--append')
191 bootloader.add_argument('--configfile')
192 bootloader.add_argument('--ptable', choices=('msdos', 'gpt'),
193 default='msdos')
194 bootloader.add_argument('--timeout', type=int)
195 bootloader.add_argument('--source')
196
197 include = subparsers.add_parser('include')
198 include.add_argument('path', type=cannedpathtype)
199
200 self._parse(parser, confpath)
201 if not self.bootloader:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500202 logger.warning('bootloader config not specified, using defaults\n')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500203 self.bootloader = bootloader.parse_args([])
204
205 def _parse(self, parser, confpath):
206 """
207 Parse file in .wks format using provided parser.
208 """
209 with open(confpath) as conf:
210 lineno = 0
211 for line in conf:
212 line = line.strip()
213 lineno += 1
214 if line and line[0] != '#':
Brad Bishop19323692019-04-05 15:28:33 -0400215 line = expand_line(line)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500216 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500217 line_args = shlex.split(line)
218 parsed = parser.parse_args(line_args)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500219 except ArgumentError as err:
220 raise KickStartError('%s:%d: %s' % \
221 (confpath, lineno, err))
222 if line.startswith('part'):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800223 # SquashFS does not support filesystem UUID
224 if parsed.fstype == 'squashfs':
225 if parsed.fsuuid:
226 err = "%s:%d: SquashFS does not support UUID" \
227 % (confpath, lineno)
228 raise KickStartError(err)
229 if parsed.label:
230 err = "%s:%d: SquashFS does not support LABEL" \
231 % (confpath, lineno)
232 raise KickStartError(err)
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700233 # erofs does not support filesystem labels
234 if parsed.fstype == 'erofs' and parsed.label:
235 err = "%s:%d: erofs does not support LABEL" % (confpath, lineno)
236 raise KickStartError(err)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600237 if parsed.fstype == 'msdos' or parsed.fstype == 'vfat':
238 if parsed.fsuuid:
239 if parsed.fsuuid.upper().startswith('0X'):
240 if len(parsed.fsuuid) > 10:
241 err = "%s:%d: fsuuid %s given in wks kickstart file " \
242 "exceeds the length limit for %s filesystem. " \
243 "It should be in the form of a 32 bit hexadecimal" \
244 "number (for example, 0xABCD1234)." \
245 % (confpath, lineno, parsed.fsuuid, parsed.fstype)
246 raise KickStartError(err)
247 elif len(parsed.fsuuid) > 8:
248 err = "%s:%d: fsuuid %s given in wks kickstart file " \
249 "exceeds the length limit for %s filesystem. " \
250 "It should be in the form of a 32 bit hexadecimal" \
251 "number (for example, 0xABCD1234)." \
252 % (confpath, lineno, parsed.fsuuid, parsed.fstype)
253 raise KickStartError(err)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800254 if parsed.use_label and not parsed.label:
255 err = "%s:%d: Must set the label with --label" \
Brad Bishop316dfdd2018-06-25 12:45:53 -0400256 % (confpath, lineno)
257 raise KickStartError(err)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500258 # using ArgumentParser one cannot easily tell if option
259 # was passed as argument, if said option has a default
260 # value; --overhead-factor/--extra-space cannot be used
261 # with --fixed-size, so at least detect when these were
262 # passed with non-0 values ...
263 if parsed.fixed_size:
264 if parsed.overhead_factor or parsed.extra_space:
265 err = "%s:%d: arguments --overhead-factor and --extra-space not "\
266 "allowed with argument --fixed-size" \
267 % (confpath, lineno)
268 raise KickStartError(err)
269 else:
270 # ... and provide defaults if not using
271 # --fixed-size iff given option was not used
272 # (again, one cannot tell if option was passed but
273 # with value equal to 0)
274 if '--overhead-factor' not in line_args:
275 parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR
276 if '--extra-space' not in line_args:
277 parsed.extra_space = self.DEFAULT_EXTRA_SPACE
278
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500279 self.partnum += 1
280 self.partitions.append(Partition(parsed, self.partnum))
281 elif line.startswith('include'):
282 self._parse(parser, parsed.path)
283 elif line.startswith('bootloader'):
284 if not self.bootloader:
285 self.bootloader = parsed
Andrew Geissler82c905d2020-04-13 13:39:40 -0500286 # Concatenate the strings set in APPEND
287 append_var = get_bitbake_var("APPEND")
288 if append_var:
289 self.bootloader.append = ' '.join(filter(None, \
290 (self.bootloader.append, append_var)))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500291 else:
292 err = "%s:%d: more than one bootloader specified" \
293 % (confpath, lineno)
294 raise KickStartError(err)