From 67eddfa7e3fedbb530045f5b43a2c89db832fa2a Mon Sep 17 00:00:00 2001 From: Julia Kreger Date: Thu, 28 Oct 2021 11:41:00 -0700 Subject: Delete EFI boot entry duplicate labels first Some firmware seems to take an objection with EFI nvram entries being deleted after one is added, resulting in the entire entry table being reset to the last known good state. This is problematic, as ultimately deployments can time out if we previously booted with Networking, and the machine, while commanded to do other wise, reboots back to networking regardless. We will now delete entries first, before proceeding. Additionally, for general use, this pattern may serve the community better by avoiding cases where we would have previously just relied upon efibootmgr[0] to warn us of duplicate entries. [0]: https://github.com/rhboot/efibootmgr/blob/103aa22ece98f09fe3ea2a0c83988f0ee2d0e5a8/src/efibootmgr.c#L228 Change-Id: Ib61a7100a059e79a8b0901fd8f46b9bc41d657dc Story: 2009649 Task: 43808 --- ironic_python_agent/extensions/image.py | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'ironic_python_agent/extensions') diff --git a/ironic_python_agent/extensions/image.py b/ironic_python_agent/extensions/image.py index a09cb140..4abb9c98 100644 --- a/ironic_python_agent/extensions/image.py +++ b/ironic_python_agent/extensions/image.py @@ -274,12 +274,10 @@ def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition, # Before updating let's get information about the bootorder LOG.debug("Getting information about boot order.") - utils.execute('efibootmgr', '-v') - # NOTE(iurygregory): regex used to identify the Warning in the stderr after - # we add the new entry. Example: - # "efibootmgr: ** Warning ** : Boot0004 has same label ironic" - duplicated_label = re.compile(r'^.*:\s\*\*.*\*\*\s:\s.*' - r'Boot([0-9a-f-A-F]+)\s.*$') + original_efi_output = utils.execute('efibootmgr', '-v') + # NOTE(TheJulia): regex used to identify entries in the efibootmgr + # output on stdout. + entry_label = re.compile(r'Boot([0-9a-f-A-F]+):\s(.*).*$') label_id = 1 for v_bl in valid_efi_bootloaders: if 'csv' in v_bl.lower(): @@ -298,20 +296,26 @@ def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition, v_efi_bl_path = '\\' + v_bl.replace('/', '\\') label = 'ironic' + str(label_id) + # Iterate through standard out, and look for duplicates + for line in original_efi_output[0].split('\n'): + match = entry_label.match(line) + # Look for the base label in the string if a line match + # occurs, so we can identify if we need to eliminate the + # entry. + if match and label in match.group(2): + boot_num = match.group(1) + LOG.debug("Found bootnum %s matching label", boot_num) + utils.execute('efibootmgr', '-b', boot_num, '-B') + LOG.debug("Adding loader %(path)s on partition %(part)s of device " " %(dev)s", {'path': v_efi_bl_path, 'part': efi_partition, 'dev': device}) # Update the nvram using efibootmgr # https://linux.die.net/man/8/efibootmgr - cmd = utils.execute('efibootmgr', '-v', '-c', '-d', device, - '-p', efi_partition, '-w', '-L', label, - '-l', v_efi_bl_path) - for line in cmd[1].split('\n'): - match = duplicated_label.match(line) - if match: - boot_num = match.group(1) - LOG.debug("Found bootnum %s matching label", boot_num) - utils.execute('efibootmgr', '-b', boot_num, '-B') + utils.execute('efibootmgr', '-v', '-c', '-d', device, + '-p', efi_partition, '-w', '-L', label, + '-l', v_efi_bl_path) + # Increment the ID in case the loop runs again. label_id += 1 -- cgit v1.2.1