summaryrefslogtreecommitdiffstats
path: root/src/utils/boot.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-02 12:51:34 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-03 13:31:10 +0200
commitdfde363aa63ad3e7967da49f4ab599399b89e7f8 (patch)
tree051f1f1d1e3c8e07ae12af2bfb35174be18f1201 /src/utils/boot.py
parentc5ccc3c7e201beccf8eb3d329e478bb33b34d43e (diff)
src: log backtrace in unhandled error cases
Log an error message in known error cases and log a backtrace otherwise. Define a new error type OgError to be used in all the 'raise' blocks to define the error message to log. The exception propagates until it reaches send_internal_server_error() where the exception type is checked. If the type is OgError we log the exception message. Logs the backtrace for other types. The initial error implementation printed a backtrace everytime an error ocurred. The next iteration changed it to only print a backtrace in a very particular case but ended up omiting too much information such as syntax errors or unknown error context. The actual implementation only logs the cases we already cover in the codebase and logs a bracktrace in the others, enabling a better debugging experience.
Diffstat (limited to 'src/utils/boot.py')
-rw-r--r--src/utils/boot.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/utils/boot.py b/src/utils/boot.py
index 9ce5238..6ee7450 100644
--- a/src/utils/boot.py
+++ b/src/utils/boot.py
@@ -17,13 +17,14 @@ from src.utils.disk import get_partition_device, get_efi_partition
from src.utils.bios import *
from src.utils.uefi import *
from src.utils.fs import *
+from src.log import OgError
def _boot_bios_linux(disk, part, mountpoint):
logging.info(f'Booting Linux system')
if not get_linux_distro_id(mountpoint) == 'ubuntu':
- raise NotImplementedError(f'{os_probe(mountpoint)} detected, only Ubuntu is supported for legacy BIOS boot')
+ raise OgError(f'{os_probe(mountpoint)} detected, only Ubuntu is supported for legacy BIOS boot')
kernel_path = get_vmlinuz_path(mountpoint)
initrd_path = get_initrd_path(mountpoint)
@@ -39,7 +40,7 @@ def _boot_bios_linux(disk, part, mountpoint):
subprocess.run(shlex.split(kexec_cmd), check=True, text=True)
subprocess.run(shlex.split(kexec_reboot_cmd), check=True, text=True)
except OSError as e:
- raise OSError(f'Error processing kexec: {e}') from e
+ raise OgError(f'Error processing kexec: {e}') from e
def _boot_bios_windows(disk, part, mountpoint):
logging.info(f'Booting Windows system')
@@ -52,7 +53,7 @@ def _boot_bios_windows(disk, part, mountpoint):
with open(f'{mountpoint}/ogboot.secondboot', 'w') as f:
f.write('\0' * (3072))
except OSError as e:
- raise OSError(f'Could not create ogboot files in Windows partition: {e}') from e
+ raise OgError(f'Could not create ogboot files in Windows partition: {e}') from e
def _boot_uefi_windows(disk, part, mountpoint):
logging.info(f'Booting windows system')
@@ -60,7 +61,7 @@ def _boot_uefi_windows(disk, part, mountpoint):
esp, esp_disk, esp_part_number = get_efi_partition(disk, enforce_gpt=True)
esp_mountpoint = esp.replace('dev', 'mnt')
if not mount_mkdir(esp, esp_mountpoint):
- raise RuntimeError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
+ raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi',
f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi']
@@ -71,7 +72,7 @@ def _boot_uefi_windows(disk, part, mountpoint):
logging.info(f'Found bootloader at ESP partition: {loader}')
break
else:
- raise RuntimeError(f'Unable to locate Windows EFI bootloader bootmgfw.efi')
+ raise OgError(f'Unable to locate Windows EFI bootloader bootmgfw.efi')
efibootmgr_delete_bootentry(bootlabel)
efibootmgr_create_bootentry(esp_disk, esp_part_number, loader, bootlabel)
@@ -85,7 +86,7 @@ def _boot_uefi_linux(disk, part, mountpoint):
esp, esp_disk, esp_part_number = get_efi_partition(disk, enforce_gpt=False)
esp_mountpoint = esp.replace('dev', 'mnt')
if not mount_mkdir(esp, esp_mountpoint):
- raise RuntimeError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
+ raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')
loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/shimx64.efi',
f'{esp_mountpoint}/EFI/ubuntu/shimx64.efi']
@@ -96,7 +97,7 @@ def _boot_uefi_linux(disk, part, mountpoint):
logging.info(f'Found bootloader at ESP partition: {loader}')
break
else:
- raise RuntimeError(f'Unable to locate Linux EFI bootloader shimx64.efi')
+ raise OgError(f'Unable to locate Linux EFI bootloader shimx64.efi')
efibootmgr_delete_bootentry(bootlabel)
efibootmgr_create_bootentry(esp_disk, esp_part_number, loader, bootlabel)
@@ -109,7 +110,7 @@ def boot_os_at(disk, part):
device = get_partition_device(disk, part)
mountpoint = device.replace('dev', 'mnt')
if not mount_mkdir(device, mountpoint):
- raise RuntimeError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}')
+ raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}')
is_uefi = is_uefi_supported()
if is_uefi:
@@ -130,6 +131,6 @@ def boot_os_at(disk, part):
elif not is_uefi and os_family == OSFamily.LINUX:
_boot_bios_linux(disk, part, mountpoint)
else:
- raise RuntimeError(f'Unknown OS family {os_family}')
+ raise OgError(f'Unknown OS family {os_family}')
finally:
umount(mountpoint)