Skip to content

Commit

Permalink
Use Initrds= for qemu direct kernel boot as a fallback
Browse files Browse the repository at this point in the history
Fixes #3180
  • Loading branch information
DaanDeMeyer committed Jan 21, 2025
1 parent f09ea40 commit b117816
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
24 changes: 2 additions & 22 deletions mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import datetime
import functools
import hashlib
import io
import itertools
import json
import logging
Expand Down Expand Up @@ -93,6 +92,7 @@
copy_ephemeral,
finalize_credentials,
finalize_kernel_command_line_extra,
join_initrds,
run_qemu,
run_ssh,
start_journal_remote,
Expand Down Expand Up @@ -138,7 +138,6 @@
make_executable,
one_zero,
read_env_file,
round_up,
scopedenv,
)
from mkosi.versioncomp import GenericVersion
Expand Down Expand Up @@ -1500,25 +1499,6 @@ def find_devicetree(context: Context, kver: str) -> Path:
die(f"Requested devicetree {context.config.devicetree} not found")


def join_initrds(initrds: Sequence[Path], output: Path) -> Path:
assert initrds

if len(initrds) == 1:
shutil.copy2(initrds[0], output)
return output

seq = io.BytesIO()
for p in initrds:
initrd = p.read_bytes()
n = len(initrd)
padding = b"\0" * (round_up(n, 4) - n) # pad to 32 bit alignment
seq.write(initrd)
seq.write(padding)

output.write_bytes(seq.getbuffer())
return output


def want_signed_pcrs(config: Config) -> bool:
return config.sign_expected_pcr == ConfigFeature.enabled or (
config.sign_expected_pcr == ConfigFeature.auto
Expand Down Expand Up @@ -2338,7 +2318,7 @@ def copy_initrd(context: Context) -> None:
if context.config.kernel_modules_initrd:
kver = next(gen_kernel_images(context))[0]
initrds += [build_kernel_modules_initrd(context, kver)]
join_initrds(initrds, context.staging / context.config.output_split_initrd)
join_initrds(context.config, initrds, context.staging / context.config.output_split_initrd)
break


Expand Down
29 changes: 27 additions & 2 deletions mkosi/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import errno
import fcntl
import hashlib
import io
import json
import logging
import os
Expand Down Expand Up @@ -638,6 +639,25 @@ def rm() -> None:
fork_and_wait(rm)


def join_initrds(config: Config, initrds: Sequence[Path], output: Path) -> Path:
assert initrds

if len(initrds) == 1:
copy_tree(initrds[0], output, sandbox=config.sandbox)
return output

seq = io.BytesIO()
for p in initrds:
initrd = p.read_bytes()
n = len(initrd)
padding = b"\0" * (round_up(n, 4) - n) # pad to 32 bit alignment
seq.write(initrd)
seq.write(padding)

output.write_bytes(seq.getbuffer())
return output


def qemu_version(config: Config, binary: Path) -> GenericVersion:
return GenericVersion(
run(
Expand Down Expand Up @@ -1343,9 +1363,14 @@ def add_virtiofs_mount(
kernel
and KernelType.identify(config, kernel) != KernelType.uki
and "-initrd" not in args.cmdline
and (config.output_dir_or_cwd() / config.output_split_initrd).exists()
):
cmdline += ["-initrd", config.output_dir_or_cwd() / config.output_split_initrd]
if (config.output_dir_or_cwd() / config.output_split_initrd).exists():
cmdline += ["-initrd", config.output_dir_or_cwd() / config.output_split_initrd]
elif config.initrds:
initrd = config.output_dir_or_cwd() / f"initrd-{uuid.uuid4().hex}"
join_initrds(config, config.initrds, initrd)
stack.callback(lambda: initrd.unlink())
cmdline += ["-initrd", fname]

if config.output_format in (OutputFormat.disk, OutputFormat.esp):
direct = fname.stat().st_size % resource.getpagesize() == 0
Expand Down

0 comments on commit b117816

Please sign in to comment.