From e0ba9cc98cd01f88877561ab58e1bbd768af2b06 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 12 Feb 2025 14:31:43 +0100 Subject: [PATCH] uefi: log efibootmgr errors as warnings Log as warning when efibootmgr fails to update the NVRAM. Raise an exception when the command is not available or when there are not enough permissions to execute. Provide contextual information in the error message. --- src/utils/uefi.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/utils/uefi.py b/src/utils/uefi.py index baa53f6..31cd253 100644 --- a/src/utils/uefi.py +++ b/src/utils/uefi.py @@ -95,8 +95,13 @@ def efibootmgr_bootnext(description): entry = _find_bootentry(boot_entries, description) num = _strip_boot_prefix(entry) # efibootmgr output uses BootXXXX for each entry, remove the "Boot" prefix. bootnext_cmd = bootnext_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN) - subprocess.run(shlex.split(bootnext_cmd), check=True, - stdout=subprocess.DEVNULL) + try: + subprocess.run(shlex.split(bootnext_cmd), check=True, + stdout=subprocess.DEVNULL) + except subprocess.CalledProcessError: + logging.warning("Failed to update BootNext. UEFI firmware might be buggy") + except OSError as e: + raise OgError(f"Unexpected error updating BootNext: {e}") from e def efibootmgr_delete_bootentry(label): @@ -107,7 +112,12 @@ def efibootmgr_delete_bootentry(label): if entry['description'] == label: num = entry['name'][4:] # Remove "Boot" prefix to extract num efibootmgr_cmd = efibootmgr_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN) - subprocess.run(shlex.split(efibootmgr_cmd), check=True) + try: + subprocess.run(shlex.split(efibootmgr_cmd), check=True) + except subprocess.CalledProcessError: + logging.warning(f"Failed to delete boot entry {label}. UEFI firmware might be buggy") + except OSError as e: + raise OgError(f"Unexpected error deleting boot entry {label}: {e}") from e break else: logging.info(f'Cannot delete boot entry {label} because it was not found.') @@ -121,8 +131,10 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True logging.info(f'{EFIBOOTMGR_BIN} command creating boot entry: {efibootmgr_cmd}') try: proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True) + except subprocess.CalledProcessError: + logging.warning(f"Failed to add boot entry {label} to NVRAM. UEFI firmware might be buggy") except OSError as e: - raise OgError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e + raise OgError(f"Unexpected error adding boot entry {label}: {e}") from e def efibootmgr_set_entry_order(label, position): logging.info(f'Setting {label} entry to position {position} of boot order') @@ -140,8 +152,10 @@ def efibootmgr_set_entry_order(label, position): try: proc = subprocess.run([EFIBOOTMGR_BIN, "-o", ",".join(boot_order)], check=True, text=True) + except subprocess.CalledProcessError: + logging.warning("Failed to set boot order to NVRAM. UEFI firmware might be buggy") except OSError as e: - raise OgError(f'Unexpected error setting boot order to NVRAM. UEFI firmware might be buggy') from e + raise OgError(f"Unexpected error updating boot order: {e}") from e def egibootmgr_reorder_disabled_entries(): logging.info(f'Setting disabled entries at the end of boot order') @@ -157,8 +171,10 @@ def egibootmgr_reorder_disabled_entries(): try: proc = subprocess.run([EFIBOOTMGR_BIN, "-o", ",".join(boot_order)], check=True, text=True) + except subprocess.CalledProcessError: + logging.warning("Failed to update boot order. UEFI firmware might be buggy") except OSError as e: - raise OgError(f'Unexpected error updating boot order of inactive entries in NVRAM. UEFI firmware might be buggy') from e + raise OgError(f"Unexpected error updating boot order: {e}") from e def _find_efi_loader(loader_paths): for efi_app in loader_paths: