Skip to content

Instantly share code, notes, and snippets.

@birgersp
Created January 13, 2024 20:41
Show Gist options
  • Save birgersp/18ffa6d89a64b656f052ddb008858780 to your computer and use it in GitHub Desktop.
Save birgersp/18ffa6d89a64b656f052ddb008858780 to your computer and use it in GitHub Desktop.
rEFInd: set or unset "Microsoft" as default
#!/usr/bin/python3
from argparse import ArgumentParser
from os.path import basename
from pathlib import Path
from typing import List
TARGET_FILE = "/boot/efi/EFI/refind/refind.conf"
TARGET_TEXT = "default_selection Microsoft"
scriptName = basename(Path(__file__).absolute())
argParser = ArgumentParser(prog=scriptName, description="configure refind.")
argParser.add_argument("-m", "--microsoft", help="set default boot entry as Microsoft", action="store_true")
args = argParser.parse_args()
lines: List[str] = []
with open(TARGET_FILE) as configFile:
lines = configFile.readlines()
edited = False
for index, line in enumerate(lines):
if line == f"#{TARGET_TEXT}\n" and args.microsoft:
newLine = f"{TARGET_TEXT}\n"
print(f"replacing line: {line}\nwith: {newLine}")
lines[index] = newLine
edited = True
break
elif line == f"{TARGET_TEXT}\n" and not args.microsoft:
newLine = f"#{TARGET_TEXT}\n"
print(f"replacing line: {line}\nwith: {newLine}")
lines[index] = newLine
edited = True
break
if edited:
print(f"overwriting file: {TARGET_FILE}")
with open(TARGET_FILE, "w") as configFile:
configFile.write("".join(lines))
else:
print("nothing to do")
print("done")
@birgersp
Copy link
Author

I'm dual-booting Pop Os and Windows, and I wanted to find a easy way to boot-into-windows with a command when I'm in Pop Os.

So I have script that invokes this one with the --microsoft flag, and then issues a reboot.

Secondly I added this script (without the --microsoft flag) to crontab "on reboot", so that when I explicitly chose to boot into Pop Os, the rEFInd default is set back to "previous boot entry".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment