Merge "Trivial: split away efibootmgr helpers"
This commit is contained in:
commit
bcf2846553
@ -187,6 +187,45 @@ def _get_efi_bootloaders(location):
|
|||||||
return valid_bootloaders
|
return valid_bootloaders
|
||||||
|
|
||||||
|
|
||||||
|
# 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(.*).*$')
|
||||||
|
|
||||||
|
|
||||||
|
def get_boot_records():
|
||||||
|
"""Executes efibootmgr and returns boot records.
|
||||||
|
|
||||||
|
:return: an iterator yielding pairs (boot number, boot record).
|
||||||
|
"""
|
||||||
|
efi_output = utils.execute('efibootmgr', '-v')
|
||||||
|
for line in efi_output[0].split('\n'):
|
||||||
|
match = _ENTRY_LABEL.match(line)
|
||||||
|
if match is not None:
|
||||||
|
yield (match[1], match[2])
|
||||||
|
|
||||||
|
|
||||||
|
def add_boot_record(device, efi_partition, loader, label):
|
||||||
|
"""Add an EFI boot record with efibootmgr.
|
||||||
|
|
||||||
|
:param device: the device to be used
|
||||||
|
:param efi_partition: the number of the EFI partition on the device
|
||||||
|
:param loader: path to the EFI boot loader
|
||||||
|
:param label: the record label
|
||||||
|
"""
|
||||||
|
# https://linux.die.net/man/8/efibootmgr
|
||||||
|
utils.execute('efibootmgr', '-v', '-c', '-d', device,
|
||||||
|
'-p', efi_partition, '-w', '-L', label,
|
||||||
|
'-l', loader)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_boot_record(boot_num):
|
||||||
|
"""Remove an EFI boot record with efibootmgr.
|
||||||
|
|
||||||
|
:param boot_num: the number of the boot record
|
||||||
|
"""
|
||||||
|
utils.execute('efibootmgr', '-b', boot_num, '-B')
|
||||||
|
|
||||||
|
|
||||||
def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,
|
def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,
|
||||||
mount_point):
|
mount_point):
|
||||||
"""Executes efibootmgr and removes duplicate entries.
|
"""Executes efibootmgr and removes duplicate entries.
|
||||||
@ -201,10 +240,7 @@ def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,
|
|||||||
|
|
||||||
# Before updating let's get information about the bootorder
|
# Before updating let's get information about the bootorder
|
||||||
LOG.debug("Getting information about boot order.")
|
LOG.debug("Getting information about boot order.")
|
||||||
original_efi_output = utils.execute('efibootmgr', '-v')
|
boot_records = list(get_boot_records())
|
||||||
# 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
|
label_id = 1
|
||||||
for v_bl in valid_efi_bootloaders:
|
for v_bl in valid_efi_bootloaders:
|
||||||
if 'csv' in v_bl.lower():
|
if 'csv' in v_bl.lower():
|
||||||
@ -224,23 +260,18 @@ def _run_efibootmgr(valid_efi_bootloaders, device, efi_partition,
|
|||||||
label = 'ironic' + str(label_id)
|
label = 'ironic' + str(label_id)
|
||||||
|
|
||||||
# Iterate through standard out, and look for duplicates
|
# Iterate through standard out, and look for duplicates
|
||||||
for line in original_efi_output[0].split('\n'):
|
for boot_num, boot_rec in boot_records:
|
||||||
match = entry_label.match(line)
|
|
||||||
# Look for the base label in the string if a line match
|
# Look for the base label in the string if a line match
|
||||||
# occurs, so we can identify if we need to eliminate the
|
# occurs, so we can identify if we need to eliminate the
|
||||||
# entry.
|
# entry.
|
||||||
if match and label in match.group(2):
|
if label in boot_rec:
|
||||||
boot_num = match.group(1)
|
|
||||||
LOG.debug("Found bootnum %s matching label", boot_num)
|
LOG.debug("Found bootnum %s matching label", boot_num)
|
||||||
utils.execute('efibootmgr', '-b', boot_num, '-B')
|
remove_boot_record(boot_num)
|
||||||
|
|
||||||
LOG.debug("Adding loader %(path)s on partition %(part)s of device "
|
LOG.debug("Adding loader %(path)s on partition %(part)s of device "
|
||||||
" %(dev)s", {'path': v_efi_bl_path, 'part': efi_partition,
|
" %(dev)s", {'path': v_efi_bl_path, 'part': efi_partition,
|
||||||
'dev': device})
|
'dev': device})
|
||||||
# Update the nvram using efibootmgr
|
# Update the nvram using efibootmgr
|
||||||
# https://linux.die.net/man/8/efibootmgr
|
add_boot_record(device, efi_partition, v_efi_bl_path, label)
|
||||||
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.
|
# Increment the ID in case the loop runs again.
|
||||||
label_id += 1
|
label_id += 1
|
||||||
|
Loading…
Reference in New Issue
Block a user