summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/ksparser.py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2025-11-07 13:31:53 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2025-11-07 13:31:53 +0000
commit8c22ff0d8b70d9b12f0487ef696a7e915b9e3173 (patch)
treeefdc32587159d0050a69009bdf2330a531727d95 /scripts/lib/wic/ksparser.py
parentd412d2747595c1cc4a5e3ca975e3adc31b2f7891 (diff)
downloadpoky-8c22ff0d8b70d9b12f0487ef696a7e915b9e3173.tar.gz
The poky repository master branch is no longer being updated.
You can either: a) switch to individual clones of bitbake, openembedded-core, meta-yocto and yocto-docs b) use the new bitbake-setup You can find information about either approach in our documentation: https://docs.yoctoproject.org/ Note that "poky" the distro setting is still available in meta-yocto as before and we continue to use and maintain that. Long live Poky! Some further information on the background of this change can be found in: https://lists.openembedded.org/g/openembedded-architecture/message/2179 Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/ksparser.py')
-rw-r--r--scripts/lib/wic/ksparser.py322
1 files changed, 0 insertions, 322 deletions
diff --git a/scripts/lib/wic/ksparser.py b/scripts/lib/wic/ksparser.py
deleted file mode 100644
index 4ccd70dc55..0000000000
--- a/scripts/lib/wic/ksparser.py
+++ /dev/null
@@ -1,322 +0,0 @@
1#!/usr/bin/env python3
2#
3# Copyright (c) 2016 Intel, Inc.
4#
5# SPDX-License-Identifier: GPL-2.0-only
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
15import os
16import shlex
17import logging
18import re
19import uuid
20
21from argparse import ArgumentParser, ArgumentError, ArgumentTypeError
22
23from wic.engine import find_canned
24from wic.partition import Partition
25from wic.misc import get_bitbake_var
26
27logger = logging.getLogger('wic')
28
29__expand_var_regexp__ = re.compile(r"\${[^{}@\n\t :]+}")
30
31def expand_line(line):
32 while True:
33 m = __expand_var_regexp__.search(line)
34 if not m:
35 return line
36 key = m.group()[2:-1]
37 val = get_bitbake_var(key)
38 if val is None:
39 logger.warning("cannot expand variable %s" % key)
40 return line
41 line = line[:m.start()] + val + line[m.end():]
42
43class KickStartError(Exception):
44 """Custom exception."""
45 pass
46
47class KickStartParser(ArgumentParser):
48 """
49 This class overwrites error method to throw exception
50 instead of producing usage message(default argparse behavior).
51 """
52 def error(self, message):
53 raise ArgumentError(None, message)
54
55def sizetype(default, size_in_bytes=False):
56 def f(arg):
57 """
58 Custom type for ArgumentParser
59 Converts size string in <num>[S|s|K|k|M|G] format into the integer value
60 """
61 try:
62 suffix = default
63 size = int(arg)
64 except ValueError:
65 try:
66 suffix = arg[-1:]
67 size = int(arg[:-1])
68 except ValueError:
69 raise ArgumentTypeError("Invalid size: %r" % arg)
70
71
72 if size_in_bytes:
73 if suffix == 's' or suffix == 'S':
74 return size * 512
75 mult = 1024
76 else:
77 mult = 1
78
79 if suffix == "k" or suffix == "K":
80 return size * mult
81 if suffix == "M":
82 return size * mult * 1024
83 if suffix == "G":
84 return size * mult * 1024 * 1024
85
86 raise ArgumentTypeError("Invalid size: %r" % arg)
87 return f
88
89def overheadtype(arg):
90 """
91 Custom type for ArgumentParser
92 Converts overhead string to float and checks if it's bigger than 1.0
93 """
94 try:
95 result = float(arg)
96 except ValueError:
97 raise ArgumentTypeError("Invalid value: %r" % arg)
98
99 if result < 1.0:
100 raise ArgumentTypeError("Overhead factor should be > 1.0" % arg)
101
102 return result
103
104def cannedpathtype(arg):
105 """
106 Custom type for ArgumentParser
107 Tries to find file in the list of canned wks paths
108 """
109 scripts_path = os.path.abspath(os.path.dirname(__file__) + '../../..')
110 result = find_canned(scripts_path, arg)
111 if not result:
112 raise ArgumentTypeError("file not found: %s" % arg)
113 return result
114
115def systemidtype(arg):
116 """
117 Custom type for ArgumentParser
118 Checks if the argument sutisfies system id requirements,
119 i.e. if it's one byte long integer > 0
120 """
121 error = "Invalid system type: %s. must be hex "\
122 "between 0x1 and 0xFF" % arg
123 try:
124 result = int(arg, 16)
125 except ValueError:
126 raise ArgumentTypeError(error)
127
128 if result <= 0 or result > 0xff:
129 raise ArgumentTypeError(error)
130
131 return arg
132
133class KickStart():
134 """Kickstart parser implementation."""
135
136 DEFAULT_EXTRA_FILESYSTEM_SPACE = 10*1024
137 DEFAULT_OVERHEAD_FACTOR = 1.3
138
139 def __init__(self, confpath):
140
141 self.partitions = []
142 self.bootloader = None
143 self.lineno = 0
144 self.partnum = 0
145
146 parser = KickStartParser()
147 subparsers = parser.add_subparsers()
148
149 part = subparsers.add_parser('part')
150 part.add_argument('mountpoint', nargs='?')
151 part.add_argument('--active', action='store_true')
152 part.add_argument('--align', type=int)
153 part.add_argument('--offset', type=sizetype("K", True))
154 part.add_argument('--exclude-path', nargs='+')
155 part.add_argument('--include-path', nargs='+', action='append')
156 part.add_argument('--change-directory')
157 part.add_argument('--extra-filesystem-space', '--extra-space', type=sizetype("M"))
158 part.add_argument('--extra-partition-space', type=sizetype("M"))
159 part.add_argument('--fsoptions', dest='fsopts')
160 part.add_argument('--fspassno', dest='fspassno')
161 part.add_argument('--fstype', default='vfat',
162 choices=('ext2', 'ext3', 'ext4', 'btrfs',
163 'squashfs', 'vfat', 'msdos', 'erofs',
164 'swap', 'none'))
165 part.add_argument('--mkfs-extraopts', default='')
166 part.add_argument('--label')
167 part.add_argument('--use-label', action='store_true')
168 part.add_argument('--no-table', action='store_true')
169 part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda')
170 part.add_argument("--overhead-factor", type=overheadtype)
171 part.add_argument('--part-name')
172 part.add_argument('--part-type')
173 part.add_argument('--rootfs-dir')
174 part.add_argument('--type', default='primary',
175 choices = ('primary', 'logical'))
176 part.add_argument('--hidden', action='store_true')
177
178 # --size and --fixed-size cannot be specified together; options
179 # ----extra-filesystem-space and --overhead-factor should also raise a
180 # parser error, but since nesting mutually exclusive groups does not work,
181 # ----extra-filesystem-space/--overhead-factor are handled later
182 sizeexcl = part.add_mutually_exclusive_group()
183 sizeexcl.add_argument('--size', type=sizetype("M"), default=0)
184 sizeexcl.add_argument('--fixed-size', type=sizetype("M"), default=0)
185
186 part.add_argument('--source')
187 part.add_argument('--sourceparams')
188 part.add_argument('--system-id', type=systemidtype)
189 part.add_argument('--use-uuid', action='store_true')
190 part.add_argument('--uuid')
191 part.add_argument('--fsuuid')
192 part.add_argument('--no-fstab-update', action='store_true')
193 part.add_argument('--mbr', action='store_true')
194
195 bootloader = subparsers.add_parser('bootloader')
196 bootloader.add_argument('--append')
197 bootloader.add_argument('--configfile')
198 bootloader.add_argument('--ptable', choices=('msdos', 'gpt', 'gpt-hybrid'),
199 default='msdos')
200 bootloader.add_argument('--diskid')
201 bootloader.add_argument('--timeout', type=int)
202 bootloader.add_argument('--source')
203
204 include = subparsers.add_parser('include')
205 include.add_argument('path', type=cannedpathtype)
206
207 self._parse(parser, confpath)
208 if not self.bootloader:
209 logger.warning('bootloader config not specified, using defaults\n')
210 self.bootloader = bootloader.parse_args([])
211
212 def _parse(self, parser, confpath):
213 """
214 Parse file in .wks format using provided parser.
215 """
216 with open(confpath) as conf:
217 lineno = 0
218 for line in conf:
219 line = line.strip()
220 lineno += 1
221 if line and line[0] != '#':
222 line = expand_line(line)
223 try:
224 line_args = shlex.split(line)
225 parsed = parser.parse_args(line_args)
226 except ArgumentError as err:
227 raise KickStartError('%s:%d: %s' % \
228 (confpath, lineno, err))
229 if line.startswith('part'):
230 # SquashFS does not support filesystem UUID
231 if parsed.fstype == 'squashfs':
232 if parsed.fsuuid:
233 err = "%s:%d: SquashFS does not support UUID" \
234 % (confpath, lineno)
235 raise KickStartError(err)
236 if parsed.label:
237 err = "%s:%d: SquashFS does not support LABEL" \
238 % (confpath, lineno)
239 raise KickStartError(err)
240 # erofs does not support filesystem labels
241 if parsed.fstype == 'erofs' and parsed.label:
242 err = "%s:%d: erofs does not support LABEL" % (confpath, lineno)
243 raise KickStartError(err)
244 if parsed.fstype == 'msdos' or parsed.fstype == 'vfat':
245 if parsed.fsuuid:
246 if parsed.fsuuid.upper().startswith('0X'):
247 if len(parsed.fsuuid) > 10:
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)
254 elif len(parsed.fsuuid) > 8:
255 err = "%s:%d: fsuuid %s given in wks kickstart file " \
256 "exceeds the length limit for %s filesystem. " \
257 "It should be in the form of a 32 bit hexadecimal" \
258 "number (for example, 0xABCD1234)." \
259 % (confpath, lineno, parsed.fsuuid, parsed.fstype)
260 raise KickStartError(err)
261 if parsed.use_label and not parsed.label:
262 err = "%s:%d: Must set the label with --label" \
263 % (confpath, lineno)
264 raise KickStartError(err)
265 if not parsed.extra_partition_space:
266 parsed.extra_partition_space = 0
267 # using ArgumentParser one cannot easily tell if option
268 # was passed as argument, if said option has a default
269 # value; --overhead-factor/--extra-filesystem-space
270 # cannot be used with --fixed-size, so at least detect
271 # when these were passed with non-0 values ...
272 if parsed.fixed_size:
273 if parsed.overhead_factor or parsed.extra_filesystem_space:
274 err = "%s:%d: arguments --overhead-factor and "\
275 "--extra-filesystem-space not "\
276 "allowed with argument --fixed-size" \
277 % (confpath, lineno)
278 raise KickStartError(err)
279 else:
280 # ... and provide defaults if not using
281 # --fixed-size iff given option was not used
282 # (again, one cannot tell if option was passed but
283 # with value equal to 0)
284 if not parsed.overhead_factor:
285 parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR
286 if not parsed.extra_filesystem_space:
287 parsed.extra_filesystem_space = self.DEFAULT_EXTRA_FILESYSTEM_SPACE
288
289 self.partnum += 1
290 self.partitions.append(Partition(parsed, self.partnum))
291 elif line.startswith('include'):
292 self._parse(parser, parsed.path)
293 elif line.startswith('bootloader'):
294 if not self.bootloader:
295 self.bootloader = parsed
296 # Concatenate the strings set in APPEND
297 append_var = get_bitbake_var("APPEND")
298 if append_var:
299 self.bootloader.append = ' '.join(filter(None, \
300 (self.bootloader.append, append_var)))
301 if parsed.diskid:
302 if parsed.ptable == "msdos":
303 try:
304 self.bootloader.diskid = int(parsed.diskid, 0)
305 except ValueError:
306 err = "with --ptbale msdos only 32bit integers " \
307 "are allowed for --diskid. %s could not " \
308 "be parsed" % self.ptable
309 raise KickStartError(err)
310 else:
311 try:
312 self.bootloader.diskid = uuid.UUID(parsed.diskid)
313 except ValueError:
314 err = "with --ptable %s only valid uuids are " \
315 "allowed for --diskid. %s could not be " \
316 "parsed" % (parsed.ptable, parsed.diskid)
317 raise KickStartError(err)
318
319 else:
320 err = "%s:%d: more than one bootloader specified" \
321 % (confpath, lineno)
322 raise KickStartError(err)