From 7ab965c0b57bf829c42ed54b84213d2c01f135b3 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 23 Apr 2024 01:02:39 +0200 Subject: utils: add restore_windows_efi_bootloader Add restore_windows_efi_bootloader to copy the EFI loader from the filesystem in the restored system into the EFI partition. This commit is preparatory work for the new native postinstall code. --- src/utils/uefi.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/utils/uefi.py b/src/utils/uefi.py index 48fb6c3..5ca8270 100644 --- a/src/utils/uefi.py +++ b/src/utils/uefi.py @@ -162,3 +162,34 @@ def copy_windows_efi_bootloader(disk, partition): finally: umount(mountpoint) umount(esp_mountpoint) + + +def restore_windows_efi_bootloader(disk, partition): + device = get_partition_device(disk, partition) + mountpoint = device.replace('dev', 'mnt') + if not mount_mkdir(device, mountpoint): + raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}') + + bootlabel = f'Part-{disk:02d}-{partition:02d}' + 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): + umount(mountpoint) + raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}') + + try: + loader_dir = f'{mountpoint}/ogBoot' + destination_dir = f'{esp_mountpoint}/EFI/{bootlabel}/Boot' + if os.path.exists(destination_dir): + try: + shutil.rmtree(destination_dir) + except OSError as e: + raise OgError(f'Failed to delete {destination_dir}: {e}') from e + logging.info(f'Copying {loader_dir} into {destination_dir}') + try: + shutil.copytree(loader_dir, destination_dir) + except OSError as e: + raise OgError(f'Failed to copy {loader_dir} into {destination_dir}: {e}') from e + finally: + umount(mountpoint) + umount(esp_mountpoint) \ No newline at end of file -- cgit v1.2.3-18-g5258