mirror of https://git.48k.eu/ogclient
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
parent
66941e9f79
commit
8e8ed280f9
|
@ -14,6 +14,8 @@ import subprocess
|
||||||
|
|
||||||
import fdisk
|
import fdisk
|
||||||
|
|
||||||
|
EFIBOOTMGR_BIN='efibootmgr-json'
|
||||||
|
|
||||||
def _find_bootentry(entries, label):
|
def _find_bootentry(entries, label):
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry['description'] == label:
|
if entry['description'] == label:
|
||||||
|
@ -37,7 +39,7 @@ def _check_efibootmgr_json():
|
||||||
"""
|
"""
|
||||||
supported = True
|
supported = True
|
||||||
try:
|
try:
|
||||||
subprocess.run(['efibootmgr-json', '--json'], stdout=subprocess.DEVNULL,
|
subprocess.run([EFIBOOTMGR_BIN, '--json'], stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL, check=True)
|
stderr=subprocess.DEVNULL, check=True)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
supported = False
|
supported = False
|
||||||
|
@ -50,9 +52,9 @@ def is_uefi_supported():
|
||||||
|
|
||||||
def run_efibootmgr_json():
|
def run_efibootmgr_json():
|
||||||
if _check_efibootmgr_json() is False:
|
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)
|
dict_json = json.loads(proc.stdout)
|
||||||
|
|
||||||
return dict_json
|
return dict_json
|
||||||
|
@ -60,24 +62,24 @@ def run_efibootmgr_json():
|
||||||
|
|
||||||
def efibootmgr_bootnext(description):
|
def efibootmgr_bootnext(description):
|
||||||
logging.info(f'Setting BootNext to value from boot entry with label {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', [])
|
boot_entries = run_efibootmgr_json().get('vars', [])
|
||||||
|
|
||||||
entry = _find_bootentry(boot_entries, description)
|
entry = _find_bootentry(boot_entries, description)
|
||||||
num = _strip_boot_prefix(entry) # efibootmgr output uses BootXXXX for each entry, remove the "Boot" prefix.
|
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,
|
subprocess.run(shlex.split(bootnext_cmd), check=True,
|
||||||
stdout=subprocess.DEVNULL)
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def efibootmgr_delete_bootentry(label):
|
def efibootmgr_delete_bootentry(label):
|
||||||
dict_json = run_efibootmgr_json()
|
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', []):
|
for entry in dict_json.get('vars', []):
|
||||||
if entry['description'] == label:
|
if entry['description'] == label:
|
||||||
num = entry['name'][4:] # Remove "Boot" prefix to extract num
|
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)
|
subprocess.run(shlex.split(efibootmgr_cmd), check=True)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
@ -88,8 +90,8 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True
|
||||||
entries = run_efibootmgr_json().get('vars', [])
|
entries = run_efibootmgr_json().get('vars', [])
|
||||||
create_opt = '-c' if add_to_bootorder else '-C'
|
create_opt = '-c' if add_to_bootorder else '-C'
|
||||||
index_opt = f'-I {len(entries)}' # Requires efibootmgr version 18
|
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}'
|
efibootmgr_cmd = f'{EFIBOOTMGR_BIN} {create_opt} {index_opt} -d {disk} -p {part} -L {label} -l {loader}'
|
||||||
logging.info(f'efibootmgr-json command creating boot entry: {efibootmgr_cmd}')
|
logging.info(f'{EFIBOOTMGR_BIN} command creating boot entry: {efibootmgr_cmd}')
|
||||||
try:
|
try:
|
||||||
proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True)
|
proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
|
|
Loading…
Reference in New Issue