2024-09-13 16:52:01 +02:00
|
|
|
"""Wrap GRUB commands."""
|
|
|
|
|
2024-09-13 23:55:02 +02:00
|
|
|
from rwx import cmd, ps
|
2024-02-16 21:31:38 +01:00
|
|
|
|
2024-06-10 09:50:13 +02:00
|
|
|
cmd.need("grub-mkimage")
|
2023-10-10 22:11:57 +02:00
|
|
|
|
2024-06-10 09:50:13 +02:00
|
|
|
COMPRESSION = "xz"
|
2023-10-10 22:11:57 +02:00
|
|
|
ENV_BYTES = 1024
|
2024-06-10 09:50:13 +02:00
|
|
|
ENV_COMMENT = "#"
|
|
|
|
ENV_HEADER = f"""{ENV_COMMENT} GRUB Environment Block
|
|
|
|
"""
|
2023-10-10 22:11:57 +02:00
|
|
|
MODULES = {
|
2024-06-10 09:50:13 +02:00
|
|
|
"i386-pc": [
|
|
|
|
("biosdisk",),
|
|
|
|
("ntldr",),
|
2024-06-10 12:49:49 +02:00
|
|
|
],
|
2023-10-10 22:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-12 14:46:34 +02:00
|
|
|
def make_image(
|
|
|
|
image_format: str,
|
|
|
|
image_path: str,
|
|
|
|
modules: list[str],
|
|
|
|
memdisk_path: str,
|
|
|
|
pubkey_path: str | None = None,
|
|
|
|
) -> None:
|
2024-09-17 14:53:21 +02:00
|
|
|
"""Make a binary bootable image.
|
|
|
|
|
|
|
|
:param image_format: output format (x86_64-efi, i386-pc, arm64-efi)
|
|
|
|
:type image_format: str
|
|
|
|
:param image_path: output file
|
|
|
|
:type image_path: str
|
|
|
|
:param modules: modules to embed
|
|
|
|
:type modules: list[str]
|
|
|
|
:param memdisk_path: archive to include
|
|
|
|
:type memdisk_path: str
|
|
|
|
:param pubkey_path: extra public key to add
|
|
|
|
:type pubkey_path: str | None
|
|
|
|
"""
|
2024-09-14 00:17:17 +02:00
|
|
|
args: list[str | tuple[str, ...]] = [
|
|
|
|
"grub-mkimage",
|
2024-06-10 09:50:13 +02:00
|
|
|
("--compress", COMPRESSION),
|
|
|
|
("--format", image_format),
|
|
|
|
("--output", image_path),
|
|
|
|
("--memdisk", memdisk_path),
|
2023-10-10 22:11:57 +02:00
|
|
|
]
|
|
|
|
if pubkey_path:
|
2024-06-10 09:50:13 +02:00
|
|
|
args.append(("--pubkey", pubkey_path))
|
2023-10-10 22:11:57 +02:00
|
|
|
args.extend(modules)
|
2024-09-14 00:07:34 +02:00
|
|
|
if extra_modules := MODULES.get(image_format):
|
|
|
|
args.extend(extra_modules)
|
2023-10-10 22:11:57 +02:00
|
|
|
ps.run(*args)
|