Skip to content

Instantly share code, notes, and snippets.

@sbraz
Created November 9, 2017 21:43
Show Gist options
  • Select an option

  • Save sbraz/5dfe73fc1e342ba4b1cd4a6ef9bd8df2 to your computer and use it in GitHub Desktop.

Select an option

Save sbraz/5dfe73fc1e342ba4b1cd4a6ef9bd8df2 to your computer and use it in GitHub Desktop.
A quick hack to temporarily change grub2's default entry from Windows
import subprocess
import sys
import ctypes
import msvcrt
import os
import re
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if __name__ == "__main__":
params = subprocess.list2cmdline(sys.argv)
try:
if not ctypes.windll.shell32.IsUserAnAdmin():
raise Exception
except:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, params, None, 1)
sys.exit(0)
if not os.path.exists("B:/"):
subprocess.run(["mountvol", "B:", "/s"])
buf = b"# GRUB Environment Block\n"
buf += b"next_entry=0\n"
buf += b"#" * (1024 - len(buf))
with open("B:/grub/grubenv", "wb") as f:
f.write(buf)
print("Press return to reboot, any other key to continue")
choice = msvcrt.getch()
if choice == b"\r":
subprocess.call(["shutdown", "/r", "/t", "0"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment