Skip to content

Commit 9a31a61

Browse files
authored
Merge pull request #128 from nathanchance/modules-cpio-pkg
boot-qemu.py: Add '-M' / '--modules'
2 parents 0509106 + f5fa543 commit 9a31a61

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

boot-qemu.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self):
5252
self.kernel = None
5353
self.kernel_dir = None
5454
self.memory = '1G'
55+
self.modules = None
5556
self.supports_efi = False
5657
# It may be tempting to use self.use_kvm during initialization of
5758
# subclasses to set certain properties but the user can explicitly opt
@@ -173,7 +174,8 @@ def _prepare_initrd(self):
173174
if not self._initrd_arch:
174175
raise RuntimeError('No initrd architecture specified?')
175176
return utils.prepare_initrd(self._initrd_arch,
176-
gh_json_file=self.gh_json_file)
177+
gh_json_file=self.gh_json_file,
178+
modules=self.modules)
177179

178180
def _run_fg(self):
179181
# Pretty print and run QEMU command
@@ -854,6 +856,12 @@ def parse_arguments():
854856
help=
855857
"Value for '-m' QEMU option (default: generally '512m', depends on machine)",
856858
)
859+
parser.add_argument(
860+
'-M',
861+
'--modules',
862+
help=
863+
'Path to .cpio generated with the Linux kernel\'s "modules-cpio-pkg" target'
864+
)
857865
parser.add_argument(
858866
'-s',
859867
'--smp',
@@ -937,6 +945,16 @@ def parse_arguments():
937945
if args.memory:
938946
runner.memory = args.memory
939947

948+
if args.modules:
949+
if not (modules := Path(args.modules).resolve()).exists():
950+
raise FileNotFoundError(
951+
f"Supplied modules .cpio ('{modules}') does not exist?")
952+
if not args.memory:
953+
utils.yellow(
954+
'Memory not specified, the default may be too small for modules...'
955+
)
956+
runner.modules = modules
957+
940958
if args.no_kvm:
941959
runner.use_kvm = False
942960

utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ def green(string):
172172
print(f"\n\033[01;32m{string}\033[0m", flush=True)
173173

174174

175-
def prepare_initrd(architecture, rootfs_format='cpio', gh_json_file=None):
175+
def prepare_initrd(architecture,
176+
rootfs_format='cpio',
177+
gh_json_file=None,
178+
modules=None):
176179
"""
177180
Returns a decompressed initial ramdisk.
178181
@@ -228,6 +231,24 @@ def prepare_initrd(architecture, rootfs_format='cpio', gh_json_file=None):
228231
(dst := src.with_suffix('')).unlink(missing_ok=True)
229232
subprocess.run(['zstd', '-d', src, '-o', dst, '-q'], check=True)
230233

234+
if modules:
235+
# "new" cpio magic bytes
236+
cpio_sig = bytes([0x30, 0x37, 0x30, 0x37, 0x30, 0x31])
237+
with modules.open('rb') as module_file:
238+
if module_file.read(6) != cpio_sig:
239+
raise RuntimeError(
240+
f"{modules} does not have cpio magic bytes, was it generated with the 'modules-cpio-pkg' target?"
241+
)
242+
243+
(new_dst :=
244+
dst.parent.joinpath('rootfs-modules.cpio')).unlink(missing_ok=True)
245+
with subprocess.Popen(['cat', dst, modules],
246+
stdout=subprocess.PIPE,
247+
stderr=subprocess.STDOUT) as proc, new_dst.open(
248+
'xb') as dst_file:
249+
dst_file.write(proc.stdout.read())
250+
dst = new_dst
251+
231252
return dst
232253

233254

0 commit comments

Comments
 (0)