From 702e236176e78aa6517be27b03b1d771d4b4a53b Mon Sep 17 00:00:00 2001 From: Antonio Alvarez Feijoo Date: Tue, 1 Oct 2024 12:42:16 +0200 Subject: [PATCH] mkosi-initrd: add some autodetection logic if output is not specified Make use of installation layout (bls or other), `$BOOT_ROOT` partition and entry token provided by `kernel-install`. --- mkosi/initrd.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/mkosi/initrd.py b/mkosi/initrd.py index 075a9ae18..2acdb414a 100644 --- a/mkosi/initrd.py +++ b/mkosi/initrd.py @@ -1,12 +1,15 @@ # SPDX-License-Identifier: LGPL-2.1-or-later import argparse +import json import os import platform import shutil +import subprocess import sys import tempfile from pathlib import Path +from typing import Optional, cast import mkosi.resources from mkosi.config import DocFormat, OutputFormat @@ -18,6 +21,28 @@ from mkosi.util import resource_path +def get_layout_output_dir() -> tuple[Optional[str], str]: + layout = "" + output_dir = "" + + if find_binary("kernel-install"): + output = json.loads( + run( + ["kernel-install", "--json=short", "inspect"], + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + check=False, + ).stdout + ) + layout = cast(str, output["Layout"]) + if layout == "bls": + boot_root = cast(str, output["BootRoot"]) + entry_token = cast(str, output["EntryToken"]) + output_dir = f"{boot_root}/{entry_token}" + + return (layout or None, output_dir) + + @uncaught_exception_handler() def main() -> None: log_setup() @@ -88,6 +113,22 @@ def main() -> None: show_docs("mkosi-initrd", DocFormat.all(), resources=r) return + if not args.output_dir: + layout, args.output_dir = get_layout_output_dir() + if layout == "bls": + if Path(f"{args.output_dir}/{args.kernel_version}").is_dir(): + args.output_dir += f"/{args.kernel_version}" + else: + args.output_dir = "" + elif layout == "other" and args.output == "initrd": + if Path("/boot/initrd").exists(): + args.output_dir = "/boot" + elif Path(f"/boot/initrd-{args.kernel_version}").exists(): + args.output_dir = "/boot" + args.output += f"-{args.kernel_version}" + elif Path(f"/usr/lib/modules/{args.kernel_version}/initrd").exists(): + args.output_dir = f"/usr/lib/modules/{args.kernel_version}" + cmdline: list[PathString] = [ "mkosi", "--force",