X-Git-Url: https://code.delx.au/refind/blobdiff_plain/33cc55e661add10c9fd3f3300936e3eb48570c8d..2a6f6875a46f5bec3bde957e979d7b1c4e476c76:/refind-mkdefault diff --git a/refind-mkdefault b/refind-mkdefault index 325d382..a48ac5d 100755 --- a/refind-mkdefault +++ b/refind-mkdefault @@ -23,6 +23,7 @@ along with this program. If not, see . """ import os +import shlex import shutil import sys @@ -37,7 +38,7 @@ def discover_data(): boot_entries, boot_order """ command = "efibootmgr -v" - bootinfo_bytes = (Popen(command, stdout=PIPE, shell=True) + bootinfo_bytes = (Popen(shlex.split(command), stdout=PIPE) .communicate()[0]) bootinfo = (bootinfo_bytes.decode(encoding="utf-8", errors="ignore") .splitlines()) @@ -103,10 +104,12 @@ def set_refind_first(boot_entries, boot_order, label): :param label: String used to identify rEFInd entry in efibootmgr output :returns: - True if order adjusted, False otherwise + * -1 if order already OK + * 0 if order adjusted + * 3 if label was not found in available entries """ first_refind_number = i = -1 - changed_order = False + retval = 0 found_first_refind = "" show_multiple_warning = True for entry in boot_order: @@ -122,36 +125,40 @@ def set_refind_first(boot_entries, boot_order, label): found_first_refind = entry first_refind_number = i if first_refind_number == -1: - if add_unordered_entry(boot_entries, boot_order, label): - changed_order = True - else: + if not add_unordered_entry(boot_entries, boot_order, label): print("{} was not found in the boot options list!".format(label)) print("You should create a {} entry with efibootmgr or by re-installing".format(label)) print("(with refind-install, for example)") + retval = 3 elif first_refind_number == 0: print("{} is already the first entry".format(label)) + retval = -1 elif first_refind_number > 0: del boot_order[first_refind_number] boot_order.insert(0, found_first_refind) - changed_order = True print("{} is not the first boot entry; adjusting....".format(label)) - return changed_order + return retval def save_changes(boot_order): """Save an altered boot_order. + :param boot_order: + List of boot numbers as strings, in boot order :returns: - True if there were no problems, False otherwise + 0 if there were no problems, 1 otherwise """ + retval = 0 order_string = ",".join(boot_order) command = "efibootmgr -o {}".format(order_string) print("Setting a boot order of {}".format(order_string)) try: - Popen(command, stdout=PIPE, shell=True).communicate()[0] + Popen(shlex.split(command), stdout=PIPE).communicate()[0] except: print("An error occurred setting the new boot order!") + retval = 1 + return retval def main(): @@ -160,35 +167,36 @@ def main(): parser = ArgumentParser(description=description) parser.add_argument("-L", "--label", default="rEFInd", - help=("The label used to identify rEFInd")) + help=("The label used to identify rEFInd (default=rEFInd)")) args = parser.parse_args() if sys.platform != "linux": print("This program is useful only under Linux; exiting!") - return(1) + return(4) if shutil.which("efibootmgr") is None: print("The efibootmgr utility is not installed; exiting!") - return(1) + return(4) if not os.geteuid() == 0: - print("You must be root to run this program") - return(1) + print("This program must be run as root (or via sudo); exiting!") + return(4) - problems = False retval = 0 boot_entries, boot_order = discover_data() if boot_entries == {}: - problems = True - print("No EFI boot entries available. This may indicate a firmware problem.") + print("No EFI boot entries are available. This may indicate a firmware problem.") + retval = 2 if boot_order == []: - problems = True print("The EFI BootOrder variable is not available. This may indicate a firmware") print("problem.") - if (boot_entries != {} and boot_order != [] and - set_refind_first(boot_entries, boot_order, args.label)): - save_changes(boot_order) + if (retval == 0): + changed = set_refind_first(boot_entries, boot_order, args.label) + if (changed == 0): + retval = save_changes(boot_order) + else: + print("No changes saved.") + if changed > 0: + retval = changed else: - if problems: - retval = 1 print("No changes saved.") return(retval)