From 33c612e7a61aea616f18a79ee0b7c66e713b68b6 Mon Sep 17 00:00:00 2001 From: Antonio Alvarez Feijoo Date: Tue, 1 Oct 2024 09:08:47 +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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/mkosi/initrd.py b/mkosi/initrd.py index 075a9ae18f..7930eebc17 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,30 @@ 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}" + elif layout == "other": + output_dir = "/boot" + + return (layout or None, output_dir) + + @uncaught_exception_handler() def main() -> None: log_setup() @@ -88,6 +115,16 @@ 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 args.output_dir: + if layout == "bls": + args.output_dir += "/" + args.kernel_version + if not Path(args.output_dir).is_dir(): + args.output_dir = "" + elif layout == "other" and args.output == "initrd": + args.output += "-" + args.kernel_version + cmdline: list[PathString] = [ "mkosi", "--force",