uefi: define EFIBOOTMGR_BIN to ease changing the efibootmgr binary

The json functionality proposed upstream might be merged one day
in efibootmgr so deploying a fork would not be needed anymore.
This change aims to ease the migration once that day comes.
master
Alejandro Sirgo Rica 2024-03-04 11:13:46 +01:00
parent 66941e9f79
commit 8e8ed280f9
1 changed files with 11 additions and 9 deletions

View File

@ -14,6 +14,8 @@ import subprocess
import fdisk
EFIBOOTMGR_BIN='efibootmgr-json'
def _find_bootentry(entries, label):
for entry in entries:
if entry['description'] == label:
@ -37,7 +39,7 @@ def _check_efibootmgr_json():
"""
supported = True
try:
subprocess.run(['efibootmgr-json', '--json'], stdout=subprocess.DEVNULL,
subprocess.run([EFIBOOTMGR_BIN, '--json'], stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=True)
except subprocess.CalledProcessError:
supported = False
@ -50,9 +52,9 @@ def is_uefi_supported():
def run_efibootmgr_json():
if _check_efibootmgr_json() is False:
raise RuntimeError('efibootmgr-json not available')
raise RuntimeError(f'{EFIBOOTMGR_BIN} not available')
proc = subprocess.run(['efibootmgr-json', '--json'], capture_output=True, text=True)
proc = subprocess.run([EFIBOOTMGR_BIN, '--json'], capture_output=True, text=True)
dict_json = json.loads(proc.stdout)
return dict_json
@ -60,24 +62,24 @@ def run_efibootmgr_json():
def efibootmgr_bootnext(description):
logging.info(f'Setting BootNext to value from boot entry with label {description}')
bootnext_cmd = 'efibootmgr-json -n {bootnum}'
bootnext_cmd = '{efibootmgr} -n {bootnum}'
boot_entries = run_efibootmgr_json().get('vars', [])
entry = _find_bootentry(boot_entries, description)
num = _strip_boot_prefix(entry) # efibootmgr output uses BootXXXX for each entry, remove the "Boot" prefix.
bootnext_cmd = bootnext_cmd.format(bootnum=num)
bootnext_cmd = bootnext_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN)
subprocess.run(shlex.split(bootnext_cmd), check=True,
stdout=subprocess.DEVNULL)
def efibootmgr_delete_bootentry(label):
dict_json = run_efibootmgr_json()
efibootmgr_cmd = 'efibootmgr-json -q -b {bootnum} -B'
efibootmgr_cmd = '{efibootmgr} -q -b {bootnum} -B'
for entry in dict_json.get('vars', []):
if entry['description'] == label:
num = entry['name'][4:] # Remove "Boot" prefix to extract num
efibootmgr_cmd = efibootmgr_cmd.format(bootnum=num)
efibootmgr_cmd = efibootmgr_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN)
subprocess.run(shlex.split(efibootmgr_cmd), check=True)
break
else:
@ -88,8 +90,8 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True
entries = run_efibootmgr_json().get('vars', [])
create_opt = '-c' if add_to_bootorder else '-C'
index_opt = f'-I {len(entries)}' # Requires efibootmgr version 18
efibootmgr_cmd = f'efibootmgr-json {create_opt} {index_opt} -d {disk} -p {part} -L {label} -l {loader}'
logging.info(f'efibootmgr-json command creating boot entry: {efibootmgr_cmd}')
efibootmgr_cmd = f'{EFIBOOTMGR_BIN} {create_opt} {index_opt} -d {disk} -p {part} -L {label} -l {loader}'
logging.info(f'{EFIBOOTMGR_BIN} command creating boot entry: {efibootmgr_cmd}')
try:
proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True)
except subprocess.CalledProcessError as e: