]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use kernel-install entry token if available
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 10 Dec 2023 13:24:04 +0000 (14:24 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 10 Dec 2023 17:06:31 +0000 (18:06 +0100)
Let's try and look for the kernel-install entry token if
kernel-install v255 or newer is available. This makes us more
compatible with package manager upgrades as we'll use the same
directory that kernel-install will look for when invoked by a
package manager.

action.yaml
mkosi/__init__.py

index 0827326a49ba1417ba081d35511195e544a31b4c..3b012d36eb3904470e4d158c2297d1c95c340df7 100644 (file)
@@ -82,6 +82,7 @@ runs:
 
       BINARIES=(
         bootctl
+        kernel-install
         systemctl
         systemd-dissect
         systemd-firstboot
index bc2a51b14ad0b7c9aad474252faa69233ffba482..9abce4185f54515ba90ef855ed1d1c16949cfeb2 100644 (file)
@@ -18,7 +18,7 @@ import textwrap
 import uuid
 from collections.abc import Iterator, Mapping, Sequence
 from pathlib import Path
-from typing import Optional, TextIO, Union
+from typing import Optional, TextIO, Union, cast
 
 import mkosi.resources
 from mkosi.architecture import Architecture
@@ -1063,7 +1063,9 @@ def prepare_grub_bios(state: MkosiState, partitions: Sequence[Partition]) -> Non
     root = finalize_root(partitions)
     assert root
 
-    dst = state.root / "efi" / state.config.distribution.name
+    token = find_entry_token(state)
+
+    dst = state.root / "efi" / token
     with umask(~0o700):
         dst.mkdir(exist_ok=True)
 
@@ -1087,7 +1089,6 @@ def prepare_grub_bios(state: MkosiState, partitions: Sequence[Partition]) -> Non
                 ]
                 initrds += [Path(shutil.copy2(kmods, kdst / "kmods"))]
 
-                distribution = state.config.distribution
                 image = Path("/") / kimg.relative_to(state.root / "efi")
                 cmdline = " ".join(state.config.kernel_command_line)
                 initrds = " ".join(
@@ -1097,7 +1098,7 @@ def prepare_grub_bios(state: MkosiState, partitions: Sequence[Partition]) -> Non
                 f.write(
                     textwrap.dedent(
                         f"""\
-                        menuentry "{distribution}-{kver}" {{
+                        menuentry "{token}-{kver}" {{
                             linux {image} {root} {cmdline}
                             initrd {initrds}
                         }}
@@ -1521,6 +1522,19 @@ def want_efi(config: MkosiConfig) -> bool:
     return True
 
 
+def find_entry_token(state: MkosiState) -> str:
+    if (
+        "--version" not in run(["kernel-install", "--help"], stdout=subprocess.PIPE).stdout or
+        systemd_tool_version("kernel-install") < "255.1"
+    ):
+        return state.config.image_id or state.config.distribution.name
+
+    output = json.loads(run(["kernel-install", "--root", state.root, "--json=pretty", "inspect"],
+                        stdout=subprocess.PIPE).stdout)
+    logging.debug(json.dumps(output, indent=4))
+    return cast(str, output["EntryToken"])
+
+
 def install_uki(state: MkosiState, partitions: Sequence[Partition]) -> None:
     # Iterates through all kernel versions included in the image and generates a combined
     # kernel+initrd+cmdline+osrelease EFI file from it and places it in the /EFI/Linux directory of the ESP.
@@ -1539,8 +1553,6 @@ def install_uki(state: MkosiState, partitions: Sequence[Partition]) -> None:
     roothash = finalize_roothash(partitions)
 
     for kver, kimg in gen_kernel_images(state):
-        image_id = state.config.image_id or state.config.distribution.name
-
         # See https://systemd.io/AUTOMATIC_BOOT_ASSESSMENT/#boot-counting
         boot_count = ""
         if (state.root / "etc/kernel/tries").exists():
@@ -1551,15 +1563,13 @@ def install_uki(state: MkosiState, partitions: Sequence[Partition]) -> None:
                 boot_binary = state.root / shim_second_stage_binary(state)
             else:
                 boot_binary = state.root / efi_boot_binary(state)
-        elif state.config.image_version:
-            boot_binary = (
-                state.root / f"efi/EFI/Linux/{image_id}_{state.config.image_version}-{kver}{boot_count}.efi"
-            )
-        elif roothash:
-            _, _, h = roothash.partition("=")
-            boot_binary = state.root / f"efi/EFI/Linux/{image_id}-{kver}-{h}{boot_count}.efi"
         else:
-            boot_binary = state.root / f"efi/EFI/Linux/{image_id}-{kver}{boot_count}.efi"
+            token = find_entry_token(state)
+            if roothash:
+                _, _, h = roothash.partition("=")
+                boot_binary = state.root / f"efi/EFI/Linux/{token}-{kver}-{h}{boot_count}.efi"
+            else:
+                boot_binary = state.root / f"efi/EFI/Linux/{token}-{kver}{boot_count}.efi"
 
         microcode = build_microcode_initrd(state)