Commit 164ea07a authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Create EFI loader stubs instead of modifying the system EFIVARS

parent 454a0ccd
......@@ -15,14 +15,16 @@
"""Support class for grub on the target system."""
import distutils.dir_util
import os
import re
import shutil
from typing import Optional
from .asyncsubprocess import AsyncSubprocess, OutputCallback
# pylama:ignore=R0201,R0914
# pylama:ignore=R0201,R0913,R0914,C901
class SysGrub:
"""The SysGrub class handles configuring and installing grub on the target system."""
......@@ -121,19 +123,49 @@ class SysGrub:
if proc.retcode != 0:
raise OSError(f'Failed to run grub-install on the target device {device}, return code {proc.retcode}')
def install_efi(self, system_path: str, efi_dir: str, output_callback: OutputCallback = None):
# pylama:ignore=R0913
def install_efi(self, system_path: str, efi_dir: str, boot_uuid: str, boot_hint: str, output_callback: OutputCallback = None):
"""Install grub on the EFI."""
# If we didn't get an output_callback, set it to our own class method
if not output_callback:
output_callback = self._default_output_callback
# Run grub-install
proc = AsyncSubprocess(['arch-chroot', system_path, 'grub-install', '--target', 'x86_64-efi',
'--efi-directory', efi_dir, '--bootloader-id', 'IDMS Linux'],
# Make EFI boot directory
efi_path = f'{system_path}{efi_dir}/EFI/BOOT'
if not os.path.exists(efi_path):
os.makedirs(efi_path)
# Copy grub modules
efi_path_grub = f'{system_path}{efi_dir}/boot/grub/x86_64-efi'
distutils.dir_util.copy_tree('/usr/lib/grub/x86_64-efi', efi_path_grub)
grub_fontdir = f'{system_path}{efi_dir}/boot/grub/fonts'
grub_font = f'{system_path}/usr/share/grub/unicode.pf2'
# Make font dir and copy in the unicode.pf2 font
if not os.path.exists(grub_fontdir):
os.makedirs(grub_fontdir)
shutil.copy(grub_font, grub_fontdir)
grub_modules_extra = []
if boot_hint and boot_hint.startswith('mduuid'):
grub_modules_extra.append('mdraid1x')
# Run grub-mkimage
proc = AsyncSubprocess(['arch-chroot', system_path, 'grub-mkimage', '--format', 'x86_64-efi', '--prefix', '/boot/grub',
'--output', f'{efi_dir}/EFI/BOOT/BOOTX64.EFI', 'part_gpt', 'part_msdos', 'fat',
*grub_modules_extra],
output_callback=output_callback)
proc.run()
# Write out our stub grub.cfg to locate the main /boot grub.cfg and load it
with open(f'{system_path}{efi_dir}/boot/grub/grub.cfg', 'w') as grubcfg:
hint = ''
if boot_hint:
hint = f'--hint=\'{boot_hint}\' '
grubcfg.write(f'search --no-floppy --fs-uuid --set=bootfs {hint}{boot_uuid}\n')
grubcfg.write('configfile ($bootfs)/grub/grub.cfg\n')
# Raise an exception if we didn't get a positive response back
if proc.retcode != 0:
raise OSError(f'Failed to run grub-install on the efi directory {efi_dir}, return code {proc.retcode}')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment