Skip to content

Commit

Permalink
Merge pull request #3395 from DaanDeMeyer/addon
Browse files Browse the repository at this point in the history
kernel-install: Various fixes
  • Loading branch information
DaanDeMeyer authored Jan 21, 2025
2 parents 00db6f1 + 233b830 commit f09ea40
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 40 deletions.
15 changes: 8 additions & 7 deletions kernel-install/50-mkosi.install
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,25 @@ def build_microcode_initrd(output: Path) -> Optional[Path]:

@uncaught_exception_handler()
def main() -> None:
log_setup()

context = KernelInstallContext.parse(
"kernel-install plugin to build initrds or Unified Kernel Images using mkosi",
"50-mkosi.install COMMAND KERNEL_VERSION ENTRY_DIR KERNEL_IMAGE INITRD…",
name="50-mkosi.install",
description="kernel-install plugin to build initrds or Unified Kernel Images using mkosi",
)

log_setup(default_log_level="info" if context.verbose else "warning")

if context.command != "add" or not we_are_wanted(context):
logging.info("mkosi-initrd is not enabled, skipping")
return

# If kernel-install was passed a UKI, there's no need to build anything ourselves.
if context.image_type == "uki":
logging.info("Provided kernel image is already a unified kernel image, skipping mkosi-initrd")
return

# If the initrd was provided on the kernel command line, we shouldn't generate our own.
if context.layout != "uki" and context.initrds:
logging.info("Pre-built initrds were provided, skipping mkosi-initrd")
return

if context.layout == "uki" and context.uki_generator == "mkosi":
Expand All @@ -80,11 +83,9 @@ def main() -> None:
"--format", str(format),
"--output", output,
"--output-dir", context.staging_area,
"--kernel-image", context.kernel_image,
] # fmt: skip

if context.kernel_image:
cmdline += ["--kernel-image", context.kernel_image]

if context.verbose:
cmdline += ["--debug"]

Expand Down
19 changes: 8 additions & 11 deletions kernel-install/51-mkosi-addon.install
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ from mkosi.types import PathString

@uncaught_exception_handler()
def main() -> None:
log_setup()

context = KernelInstallContext.parse(
"kernel-install plugin to build local addon for initrd/cmdline",
"51-mkosi-addon.install COMMAND KERNEL_VERSION ENTRY_DIR KERNEL_IMAGE…",
name="51-mkosi-addon.install",
description="kernel-install plugin to build local addon for initrd/cmdline",
)

log_setup(default_log_level="info" if context.verbose else "warning")

# No local configuration? Then nothing to do
if not Path("/etc/mkosi-addon").exists() and not Path("/run/mkosi-addon").exists():
if context.verbose:
logging.info("No local configuration defined, skipping mkosi-addon")
logging.info("No local configuration defined, skipping mkosi-addon")
return

if context.command != "add" or context.layout != "uki":
if context.verbose:
logging.info("Not an UKI layout 'add' step, skipping mkosi-addon")
logging.info("Not an UKI layout 'add' step, skipping mkosi-addon")
return

if not context.kernel_image or not context.kernel_image.exists():
if context.verbose:
logging.info("No kernel image provided, skipping mkosi-addon")
if context.image_type != "uki":
logging.info("Provided kernel image is not a unified kernel image, skipping mkosi-addon")
return

cmdline: list[PathString] = [
Expand Down
12 changes: 2 additions & 10 deletions mkosi/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mkosi.resources
from mkosi.config import DocFormat
from mkosi.documentation import show_docs
from mkosi.initrd import initrd_common_args, initrd_finalize, process_crypttab
from mkosi.initrd import include_system_config, initrd_common_args, initrd_finalize, process_crypttab
from mkosi.log import log_setup
from mkosi.run import run, uncaught_exception_handler
from mkosi.types import PathString
Expand Down Expand Up @@ -69,15 +69,7 @@ def main() -> None:
"--output-mode=600",
]

for d in (
"/usr/lib/mkosi-addon",
"/usr/local/lib/mkosi-addon",
"/run/mkosi-addon",
"/etc/mkosi-addon",
):
if Path(d).exists():
cmdline += ["--include", d]

cmdline += include_system_config("mkosi-addon")
cmdline += process_crypttab(staging_dir)

if Path("/etc/kernel/cmdline").exists():
Expand Down
24 changes: 14 additions & 10 deletions mkosi/initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class KernelInstallContext:
verbose: bool

@staticmethod
def parse(description: str, usage: str) -> "KernelInstallContext":
def parse(*, name: str, description: str) -> "KernelInstallContext":
parser = argparse.ArgumentParser(
description=description,
allow_abbrev=False,
usage=usage,
usage=f"{name} COMMAND KERNEL_VERSION ENTRY_DIR KERNEL_IMAGE…",
)

parser.add_argument(
Expand Down Expand Up @@ -176,6 +176,17 @@ def initrd_common_args(parser: argparse.ArgumentParser) -> None:
)


def include_system_config(name: str) -> list[str]:
cmdline = []

for d in ("/usr/lib", "/usr/local/lib", "/run", "/etc"):
p = Path(d) / name
if p.exists():
cmdline += ["--include", os.fspath(p)]

return cmdline


@uncaught_exception_handler()
def main() -> None:
log_setup()
Expand Down Expand Up @@ -255,14 +266,7 @@ def main() -> None:
if args.format != OutputFormat.directory.value:
cmdline += ["--output-mode=600"]

for d in (
"/usr/lib/mkosi-initrd",
"/usr/local/lib/mkosi-initrd",
"/run/mkosi-initrd",
"/etc/mkosi-initrd",
):
if Path(d).exists():
cmdline += ["--include", d]
cmdline += include_system_config("mkosi-initrd")

# Make sure we don't use any of mkosi's default repositories.
for p in (
Expand Down
6 changes: 4 additions & 2 deletions mkosi/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ def format(self, record: logging.LogRecord) -> str:
return self.formatters[record.levelno].format(record)


def log_setup() -> None:
def log_setup(default_log_level: str = "info") -> None:
handler = logging.StreamHandler(stream=sys.stderr)
handler.setFormatter(Formatter())

logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.getLevelName(os.getenv("SYSTEMD_LOG_LEVEL", "info").upper()))
logging.getLogger().setLevel(
logging.getLevelName(os.getenv("SYSTEMD_LOG_LEVEL", default_log_level).upper())
)

0 comments on commit f09ea40

Please sign in to comment.