From 8a6a14db49607540462ba4a497a4a755b02953ee Mon Sep 17 00:00:00 2001 From: Katerina Koukiou Date: Wed, 13 Nov 2024 19:27:22 +0100 Subject: [PATCH] bootloader: utilize grub2-common posttrans script for handling config file generation Our backend now executes the grub2-common posttrans script to generate grub config files. This is an ugly hack, but still better than having this logic on our side. I requested that this logic get's extracted to some sanely reusable helper scripts: https://bugzilla.redhat.com/show_bug.cgi?id=2327644 --- anaconda.spec.in | 2 ++ pyanaconda/modules/storage/bootloader/efi.py | 25 ------------------- .../modules/storage/bootloader/grub2.py | 25 +++++++++++++------ 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/anaconda.spec.in b/anaconda.spec.in index bccdfd41fc2..28847521afc 100644 --- a/anaconda.spec.in +++ b/anaconda.spec.in @@ -143,6 +143,8 @@ Requires: python3-pid Requires: crypto-policies Requires: crypto-policies-scripts +Requires: grub2-common + # required because of the rescue mode and RDP question Requires: anaconda-tui = %{version}-%{release} diff --git a/pyanaconda/modules/storage/bootloader/efi.py b/pyanaconda/modules/storage/bootloader/efi.py index a44ace5d97c..9caa105c625 100644 --- a/pyanaconda/modules/storage/bootloader/efi.py +++ b/pyanaconda/modules/storage/bootloader/efi.py @@ -177,31 +177,6 @@ def efi_config_file(self): """ Full path to EFI configuration file. """ return "%s/%s" % (self.efi_config_dir, self._config_file) - def write_config(self): - config_path = "%s%s" % (conf.target.system_root, self.efi_config_file) - - with open(config_path, "w") as fd: - grub_dir = self.config_dir - if self.stage2_device.format.type != "btrfs": - fs_uuid = self.stage2_device.format.uuid - else: - fs_uuid = self.stage2_device.format.vol_uuid - - if fs_uuid is None: - raise BootLoaderError("Could not get stage2 filesystem UUID") - - grub_dir = util.execWithCapture("grub2-mkrelpath", [grub_dir], - root=conf.target.system_root) - if not grub_dir: - raise BootLoaderError("Could not get GRUB directory path") - - fd.write("search --no-floppy --fs-uuid --set=dev %s\n" % fs_uuid) - fd.write("set prefix=($dev)%s\n" % grub_dir) - fd.write("export $prefix\n") - fd.write("configfile $prefix/grub.cfg\n") - - super().write_config() - class EFISystemdBoot(EFIBase, SystemdBoot): """EFI Systemd-boot""" diff --git a/pyanaconda/modules/storage/bootloader/grub2.py b/pyanaconda/modules/storage/bootloader/grub2.py index 6d986094583..414249a6d22 100644 --- a/pyanaconda/modules/storage/bootloader/grub2.py +++ b/pyanaconda/modules/storage/bootloader/grub2.py @@ -25,7 +25,7 @@ from pyanaconda.core import util from pyanaconda.core.configuration.anaconda import conf from pyanaconda.core.i18n import _ -from pyanaconda.core.path import open_with_perm +from pyanaconda.core.path import join_paths, open_with_perm from pyanaconda.core.product import get_product_name from pyanaconda.anaconda_loggers import get_module_logger @@ -363,14 +363,23 @@ def write_config(self): if rc: log.error("failed to set menu_auto_hide=1") - # now tell grub2 to generate the main configuration file - rc = util.execWithRedirect( - "grub2-mkconfig", - ["-o", self.config_file], - root=conf.target.system_root - ) + # Executes the grub2-common posttrans script to generate grub config files + # This should be autohandled by grub2-common, but posttrans scriptlets + # in live image scenarios are not triggered. + # FIXME: https://bugzilla.redhat.com/show_bug.cgi?id=2327644 + + # Drop the first line as it's the posttrans scriptlet header + script = util.execWithCapture("rpm", ["-q", "--scripts", "grub2-common"], root=conf.target.system_root).splitlines()[1:] + script = "\n".join(script) + + # Write the script to a temporary file in the chroot + with open(join_paths(conf.target.system_root, "/tmp/grub2-posttrans.sh"), "w") as f: + f.write(script) + + # Run the script in the chroot + rc = util.execWithRedirect("/bin/sh", ["/tmp/grub2-posttrans.sh"], root=conf.target.system_root) if rc: - raise BootLoaderError("failed to write boot loader configuration") + raise BootLoaderError("grub2-common posttrans script failed") # # installation