Skip to content

Commit 4b91840

Browse files
committed
boot-qemu.py: Add '--ephemeral-initrd'
When running boot-qemu.py in parallel, the extracted rootfs.cpio can get clobbered. Add an option to extract the initrd to a temporary location that gets cleaned up when the script exits to avoid any issues with parallelism. Signed-off-by: Nathan Chancellor <nathan@kernel.org>
1 parent ccda3e3 commit 4b91840

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎boot-qemu.py‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self) -> None:
4949
# Properties that can be adjusted by the user or class
5050
self.cmdline: list[str] = []
5151
self.efi: bool = False
52+
self.ephemeral_initrd: bool = False
5253
self.gdb: bool = False
5354
self.gdb_bin: str = ''
5455
self.gh_json_file: Path = utils.UNINIT_PATH
@@ -176,14 +177,17 @@ def _have_dev_kvm_access() -> bool:
176177
return os.access('/dev/kvm', os.R_OK | os.W_OK)
177178

178179
def _prepare_initrd(self) -> Path:
179-
if self.initrd != utils.UNINIT_PATH:
180-
return self.initrd
181-
if not self._initrd_arch:
182-
msg = 'No initrd architecture specified?'
183-
raise RuntimeError(msg)
184-
return utils.prepare_initrd(
185-
self._initrd_arch, gh_json_file=self.gh_json_file, modules=self.modules
186-
)
180+
if self.initrd == utils.UNINIT_PATH:
181+
if not self._initrd_arch:
182+
msg = 'No initrd architecture specified?'
183+
raise RuntimeError(msg)
184+
self.initrd = utils.prepare_initrd(
185+
self._initrd_arch,
186+
gh_json_file=self.gh_json_file,
187+
modules=self.modules,
188+
ephemeral=self.ephemeral_initrd,
189+
)
190+
return self.initrd
187191

188192
def _run_fg(self) -> None:
189193
# Pretty print and run QEMU command
@@ -207,6 +211,9 @@ def _run_fg(self) -> None:
207211
else:
208212
utils.red("ERROR: QEMU did not exit cleanly!")
209213
sys.exit(err.returncode)
214+
finally:
215+
if self.ephemeral_initrd:
216+
self.initrd.unlink()
210217

211218
def _run_gdb(self) -> None:
212219
qemu_cmd = [self._qemu_path, *self._qemu_args]
@@ -251,6 +258,8 @@ def _run_gdb(self) -> None:
251258

252259
answer = input('Re-run QEMU + gdb [y/n] ')
253260
if answer.lower() == 'n':
261+
if self.ephemeral_initrd:
262+
self.initrd.unlink()
254263
break
255264

256265
def _set_kernel_vars(self) -> None:
@@ -785,6 +794,7 @@ def guess_arch(kernel_arg: Path) -> str:
785794

786795
def parse_arguments():
787796
parser = ArgumentParser(description='Boot a Linux kernel in QEMU')
797+
initrd_group = parser.add_mutually_exclusive_group()
788798

789799
parser.add_argument(
790800
'-a',
@@ -794,6 +804,11 @@ def parse_arguments():
794804
metavar='ARCH',
795805
)
796806
parser.add_argument('--efi', action='store_true', help='Boot kernel via UEFI (x86_64 only)')
807+
initrd_group.add_argument(
808+
'--ephemeral-initrd',
809+
action='store_true',
810+
help='Extract compressed initrd to a randomized name and clean up after booting (default: extract compressed initrd to a consistent path)',
811+
)
797812
parser.add_argument(
798813
'-g',
799814
'--gdb',
@@ -809,7 +824,7 @@ def parse_arguments():
809824
'--gh-json-file',
810825
help='Use file for downloading rootfs images, instead of querying GitHub API directly',
811826
)
812-
parser.add_argument(
827+
initrd_group.add_argument(
813828
'-I',
814829
'--initrd',
815830
help='Initial ramdisk to use (default: Download ramdisk from ClangBuiltLinux/boot-utils releases)',
@@ -940,6 +955,7 @@ def main():
940955
if args.smp:
941956
runner.smp = args.smp
942957

958+
runner.ephemeral_initrd = args.ephemeral_initrd
943959
runner.interactive = args.interactive
944960
runner.timeout = args.timeout
945961

‎utils.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
import subprocess
99
import sys
10+
import tempfile
1011
from pathlib import Path
1112
from typing import TYPE_CHECKING, Any, NoReturn
1213

@@ -185,6 +186,7 @@ def prepare_initrd(
185186
rootfs_format: str = 'cpio',
186187
gh_json_file: Path | None = None,
187188
modules: Path | None = None,
189+
ephemeral: bool = False,
188190
) -> Path:
189191
"""
190192
Returns a decompressed initial ramdisk.
@@ -236,7 +238,13 @@ def prepare_initrd(
236238
download_initrd(gh_json_rel, src)
237239

238240
check_cmd('zstd')
239-
(dst := src.with_suffix('')).unlink(missing_ok=True)
241+
if ephemeral:
242+
fd, tmp_path = tempfile.mkstemp(dir=src.parent, text=True, suffix=f".{rootfs_format}")
243+
os.close(fd) # we don't need this
244+
dst = Path(tmp_path)
245+
else:
246+
dst = src.with_suffix('')
247+
dst.unlink(missing_ok=True)
240248
subprocess.run(['zstd', '-d', src, '-o', dst, '-q'], check=True)
241249

242250
if modules and modules != UNINIT_PATH:
@@ -247,7 +255,8 @@ def prepare_initrd(
247255
msg = f"{modules} does not have cpio magic bytes, was it generated with the 'modules-cpio-pkg' target?"
248256
raise RuntimeError(msg)
249257

250-
(new_dst := dst.parent.joinpath('rootfs-modules.cpio')).unlink(missing_ok=True)
258+
new_dst = dst.parent.joinpath(f"{dst.with_suffix('').name}-modules.cpio")
259+
new_dst.unlink(missing_ok=True)
251260
with (
252261
subprocess.Popen(
253262
['cat', dst, modules], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
@@ -258,6 +267,8 @@ def prepare_initrd(
258267
msg = 'cat stdout is None?'
259268
raise RuntimeError(msg)
260269
dst_file.write(proc.stdout.read())
270+
if ephemeral:
271+
dst.unlink()
261272
dst = new_dst
262273

263274
return dst

0 commit comments

Comments
 (0)