Skip to content

Commit

Permalink
cleanup(8to9,efi): do not use symlinks or copy grub files
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Hecko committed Jan 28, 2025
1 parent 011a396 commit f5e5bfb
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
get_boot_partition,
get_device_number,
get_efi_device,
get_efi_partition,
GRUB2_BIOS_ENTRYPOINT,
GRUB2_BIOS_ENV_FILE
get_efi_partition
)
from leapp.libraries.stdlib import api, CalledProcessError, run
from leapp.models import ArmWorkaroundEFIBootloaderInfo, EFIBootEntry, TargetUserSpaceInfo
Expand Down Expand Up @@ -118,12 +116,6 @@ def _copy_grub_files(required, optional):
_copy_file(src_path, dst_path)


def _link_grubenv_to_upgrade_entry():
upgrade_env_file = os.path.join(LEAPP_EFIDIR_CANONICAL_PATH, 'grubenv')
upgrade_env_file_relpath = os.path.relpath(upgrade_env_file, GRUB2_BIOS_ENTRYPOINT)
run(['ln', '--symbolic', '--force', upgrade_env_file_relpath, GRUB2_BIOS_ENV_FILE])


def _add_upgrade_boot_entry(efibootinfo):
"""
Create a new UEFI bootloader entry with a upgrade label and bin file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def mock_run(cmd):
monkeypatch.setattr(addupgradebootloader.mounting, 'NspawnActions', lambda *args, **kwargs: context_mock)

monkeypatch.setattr(addupgradebootloader, '_copy_grub_files', lambda optional, required: None)
monkeypatch.setattr(addupgradebootloader, '_link_grubenv_to_upgrade_entry', lambda: None)

efibootinfo_mock = MockEFIBootInfo([TEST_RHEL_EFI_ENTRY])
monkeypatch.setattr(addupgradebootloader, 'EFIBootInfo', lambda: efibootinfo_mock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import shutil

from leapp.exceptions import StopActorExecutionError
from leapp.libraries.common.grub import GRUB2_BIOS_ENTRYPOINT, GRUB2_BIOS_ENV_FILE
from leapp.libraries.stdlib import api, CalledProcessError, run
from leapp.models import ArmWorkaroundEFIBootloaderInfo

Expand Down Expand Up @@ -35,9 +34,6 @@ def remove_upgrade_efi_entry():

bootloader_info = get_workaround_efi_info()

# _copy_grub_files(['grubenv', 'grub.cfg'], ['user.cfg'])
# _link_grubenv_to_rhel_entry()

upgrade_boot_number = bootloader_info.upgrade_entry.boot_number
try:
run([
Expand Down Expand Up @@ -65,43 +61,6 @@ def remove_upgrade_efi_entry():
run(['/bin/mount', '-a'])


def _link_grubenv_to_rhel_entry():
rhel_env_file = os.path.join(RHEL_EFIDIR_CANONICAL_PATH, 'grubenv')
rhel_env_file_relpath = os.path.relpath(rhel_env_file, GRUB2_BIOS_ENTRYPOINT)
run(['ln', '--symbolic', '--force', rhel_env_file_relpath, GRUB2_BIOS_ENV_FILE])


def _copy_file(src_path, dst_path):
if os.path.exists(dst_path):
api.current_logger().debug("The {} file already exists and its content will be overwritten.".format(dst_path))

api.current_logger().info("Copying {} to {}".format(src_path, dst_path))
try:
shutil.copy2(src_path, dst_path)
except (OSError, IOError) as err:
raise StopActorExecutionError('I/O error({}): {}'.format(err.errno, err.strerror))


def _copy_grub_files(required, optional):
"""
Copy grub files from /boot/efi/EFI/leapp/ dir to the /boot/efi/EFI/redhat/ dir.
"""

all_files = required + optional
for filename in all_files:
src_path = os.path.join(LEAPP_EFIDIR_CANONICAL_PATH, filename)
dst_path = os.path.join(RHEL_EFIDIR_CANONICAL_PATH, filename)

if not os.path.exists(src_path):
if filename in required:
msg = 'Required file {} does not exists. Aborting.'.format(filename)
raise StopActorExecutionError(msg)

continue

_copy_file(src_path, dst_path)


def _remove_upgrade_blsdir(bootloader_info):
api.current_logger().debug('Removing upgrade BLS directory: {}'.format(bootloader_info.upgrade_bls_dir))
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import shutil

import pytest
Expand Down Expand Up @@ -52,52 +51,18 @@ def test_get_workaround_efi_info_no_entry(monkeypatch):
removeupgradeefientry.get_workaround_efi_info()


def test_copy_grub_files(monkeypatch):
copy_file_calls = []

def mock_copy_file(src, dst):
copy_file_calls.append((src, dst))

monkeypatch.setattr(removeupgradeefientry, '_copy_file', mock_copy_file)
monkeypatch.setattr(os.path, 'exists', lambda path: True)

removeupgradeefientry._copy_grub_files(['required'], ['optional'])

assert (
os.path.join(removeupgradeefientry.LEAPP_EFIDIR_CANONICAL_PATH, 'required'),
os.path.join(removeupgradeefientry.RHEL_EFIDIR_CANONICAL_PATH, 'required'),
) in copy_file_calls
assert (
os.path.join(removeupgradeefientry.LEAPP_EFIDIR_CANONICAL_PATH, 'optional'),
os.path.join(removeupgradeefientry.RHEL_EFIDIR_CANONICAL_PATH, 'optional'),
) in copy_file_calls


def test_copy_grub_files_missing_required(monkeypatch):
monkeypatch.setattr(os.path, 'exists', lambda path: False)

with pytest.raises(StopActorExecutionError, match='Required file required does not exists'):
removeupgradeefientry._copy_grub_files(['required'], [])


def test_remove_upgrade_efi_entry(monkeypatch):
run_calls = []
copy_grub_files_calls = []
monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(msgs=[TEST_EFI_INFO]))

def mock_run(command, checked=False):
run_calls.append(command)
return {'exit_code': 0}

def mock_copy_grub_files(required, optional):
copy_grub_files_calls.append((required, optional))

def rmtree_mocked(tree, *args):
run_calls.append('shutil.rmtree')
assert tree == TEST_EFI_INFO.upgrade_bls_dir

monkeypatch.setattr(removeupgradeefientry, '_copy_grub_files', mock_copy_grub_files)
monkeypatch.setattr(removeupgradeefientry, '_link_grubenv_to_rhel_entry', lambda: None)
monkeypatch.setattr(removeupgradeefientry, 'run', mock_run)
monkeypatch.setattr(shutil, 'rmtree', rmtree_mocked)

Expand Down

0 comments on commit f5e5bfb

Please sign in to comment.