return None
-def find_grub_binary(context: Context, binary: str) -> Optional[Path]:
+def find_grub_binary(binary: str, root: Path = Path("/")) -> Optional[Path]:
assert "grub" in binary and "grub2" not in binary
- return find_binary(binary, root=context.root) or find_binary(binary.replace("grub", "grub2"), root=context.root)
-
-
-def find_grub_prefix(context: Context) -> Optional[str]:
- path = find_grub_binary(context, "grub-mkimage")
- if path is None:
- return None
-
- return "grub2" if "grub2" in os.fspath(path) else "grub"
+ return find_binary(binary, binary.replace("grub", "grub2"), root=root)
def want_grub_efi(context: Context) -> bool:
installed = True
for binary in ("grub-mkimage", "grub-bios-setup"):
- path = find_grub_binary(context, binary)
- if path is not None:
+ if find_grub_binary(binary, root=context.config.tools()):
continue
if context.config.bootable == ConfigFeature.enabled:
def prepare_grub_config(context: Context) -> Optional[Path]:
- prefix = find_grub_prefix(context)
- if not prefix:
- return None
-
- config = context.root / "efi" / prefix / "grub.cfg"
+ config = context.root / "efi" / context.config.distribution.grub_prefix() / "grub.cfg"
with umask(~0o700):
config.parent.mkdir(exist_ok=True)
if not want_grub_efi(context):
return
- prefix = find_grub_prefix(context)
- assert prefix
-
# Signed EFI grub shipped by distributions reads its configuration from /EFI/<distribution>/grub.cfg in
# the ESP so let's put a shim there to redirect to the actual configuration file.
earlyconfig = context.root / "efi/EFI" / context.config.distribution.name / "grub.cfg"
earlyconfig.parent.mkdir(parents=True, exist_ok=True)
# Read the actual config file from the root of the ESP.
- earlyconfig.write_text(f"configfile /{prefix}/grub.cfg\n")
+ earlyconfig.write_text(f"configfile /{context.config.distribution.grub_prefix()}/grub.cfg\n")
config = prepare_grub_config(context)
assert config
# so we're forced to reimplement its functionality. Luckily that's pretty simple, run grub-mkimage to
# generate the required core.img and copy the relevant files to the ESP.
- mkimage = find_grub_binary(context, "grub-mkimage")
+ mkimage = find_grub_binary("grub-mkimage", root=context.config.tools())
assert mkimage
directory = find_grub_bios_directory(context)
assert directory
- prefix = find_grub_prefix(context)
- assert prefix
-
- dst = context.root / "efi" / prefix / "i386-pc"
+ dst = context.root / "efi" / context.config.distribution.grub_prefix() / "i386-pc"
dst.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile("w", prefix="grub-early-config") as earlyconfig:
earlyconfig.write(
textwrap.dedent(
f"""\
- search --no-floppy --set=root --file /{prefix}/grub.cfg
- set prefix=($root)/{prefix}
+ search --no-floppy --set=root --file /{context.config.distribution.grub_prefix()}/grub.cfg
+ set prefix=($root)/{context.config.distribution.grub_prefix()}
"""
)
)
mkimage,
"--directory", directory,
"--config", earlyconfig.name,
- "--prefix", f"/{prefix}",
+ "--prefix", f"/{context.config.distribution.grub_prefix()}",
"--output", dst / "core.img",
"--format", "i386-pc",
*(["--verbose"] if ARG_DEBUG.get() else []),
],
sandbox=context.sandbox(
options=[
- "--ro-bind", context.root / "usr", "/usr",
"--bind", context.root, context.root,
"--ro-bind", earlyconfig.name, earlyconfig.name,
],
shutil.copy2(directory / "modinfo.sh", dst)
shutil.copy2(directory / "boot.img", dst)
- dst = context.root / "efi" / prefix / "fonts"
+ dst = context.root / "efi" / context.config.distribution.grub_prefix() / "fonts"
with umask(~0o700):
dst.mkdir(exist_ok=True)
- for prefix in ("grub", "grub2"):
- unicode = context.root / "usr/share" / prefix / "unicode.pf2"
+ for d in ("grub", "grub2"):
+ unicode = context.root / "usr/share" / d / "unicode.pf2"
if unicode.exists():
shutil.copy2(unicode, dst)
if not want_grub_bios(context, partitions):
return
- setup = find_grub_binary(context, "grub-bios-setup")
+ setup = find_grub_binary("grub-bios-setup", root=context.config.tools())
assert setup
- prefix = find_grub_prefix(context)
- assert prefix
-
with (
complete_step("Installing grub boot loader…"),
tempfile.NamedTemporaryFile(mode="w") as mountinfo,
[
"sh", "-c", f"mount --bind {mountinfo.name} /proc/$$/mountinfo && exec $0 \"$@\"",
setup,
- "--directory", context.root / "efi" / prefix / "i386-pc",
+ "--directory", context.root / "efi" / context.config.distribution.grub_prefix() / "i386-pc",
*(["--verbose"] if ARG_DEBUG.get() else []),
context.staging / context.config.output_with_format,
],
sandbox=context.sandbox(
options=[
- "--bind", context.root / "usr", "/usr",
"--bind", context.root, context.root,
"--bind", context.staging, context.staging,
"--bind", mountinfo.name, mountinfo.name,