Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkosi-initrd: add some autodetection logic if output is not specified #3084

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 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,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)
Fixed Show fixed Hide fixed


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