From 373c1b2a724a3855f93d9cc4b48d0c33310a482c Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 11 Oct 2024 12:15:09 +0200 Subject: grub: replace legacy grub install scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Translate old legacy grub scripts into grub.py Implement ogGrubInstallMbr as install_main_grub() and ogGrubInstallPartition as install_linux_grub(). Add grub configuration file generator through the classes GrubConfig and MenuEntry. Ensure EFI tree structure compatibility with legacy code. The structure of the created folders in the ESP is non-standard, efi binaries are usually located in the folder below the EFI/ directory. Structure used by ogClient: EFI/ ├── grub/ │ └── Boot/ │ ├── BOOTX64.CSV │ ├── grub.cfg │ ├── mmx64.efi │ ├── shimx64.efi │ ├── BOOTX64.EFI │ ├── grubx64.efi │ └── ogloader.efi ... The function _mangle_efi_folder handles the folder structure after grub-install to comply with the location expected by ogLive. install_linux_grub() installs a grub local to each Linux install to enable chainloading, each grub is located in EFI/Part-xx-yy/ in UEFI. The local linux BIOS grub in legacy scripts is unreliable, grub-install reports a failure during the install process. install_main_grub() installs a global grub in EFI/grub/ to show a grub menu when the pxe boot fails. The global grub contains entries to every installed os. No global grub is installed for BIOS systems, a Boot partition would be required to store the grub configuration. --- src/utils/uefi.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/utils/uefi.py') diff --git a/src/utils/uefi.py b/src/utils/uefi.py index 27b7ba0..10095a7 100644 --- a/src/utils/uefi.py +++ b/src/utils/uefi.py @@ -125,6 +125,24 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True except OSError as e: raise OgError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e +def efibootmgr_set_entry_order(label, position): + logging.info(f'Setting {label} entry to position {position} of boot order') + boot_info = run_efibootmgr_json(validate=False) + boot_entries = boot_info.get('vars', []) + boot_order = boot_info.get('BootOrder', []) + + entry = _find_bootentry(boot_entries, label) + target_grub_entry = _strip_boot_prefix(entry) + + if target_grub_entry in boot_order: + boot_order.remove(target_grub_entry) + + boot_order.insert(position, target_grub_entry) + + try: + proc = subprocess.run([EFIBOOTMGR_BIN, "-o", ",".join(boot_order)], check=True, text=True) + except OSError as e: + raise OgError(f'Unexpected error setting boot order to NVRAM. UEFI firmware might be buggy') from e def _find_efi_loader(loader_paths): for efi_app in loader_paths: -- cgit v1.2.3-18-g5258