Skip to content

Commit

Permalink
mkosi-initrd: add some autodetection logic if output is not specified
Browse files Browse the repository at this point in the history
Make use of installation layout (bls, uki, other...), `$BOOT_ROOT` partition and
entry token provided by `kernel-install`.
  • Loading branch information
aafeijoo-suse committed Sep 30, 2024
1 parent b7beda5 commit 53374fb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions mkosi/initrd.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +21,30 @@
from mkosi.util import resource_path


def get_layout_output_dir() -> tuple[Optional[str], str]:
output_dir = "/boot"

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 in ("bls", "uki"):
boot_root = cast(str, output["BootRoot"])
if layout == "bls":
entry_token = cast(str, output["EntryToken"])
output_dir = f"{boot_root}/{entry_token}"
else:
output_dir = f"{boot_root}/EFI/Linux"

return (layout or None, output_dir)

Check failure

Code scanning / CodeQL

Potentially uninitialized local variable Error

Local variable 'layout' may be used before it is initialized.


@uncaught_exception_handler()
def main() -> None:
log_setup()
Expand Down Expand Up @@ -88,6 +115,15 @@ 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":
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",
Expand Down

0 comments on commit 53374fb

Please sign in to comment.