Skip to content

[GNU GRUB] Boot OS switching script for dual/mutiple boot environment.

License

Notifications You must be signed in to change notification settings

k1z3/grub-default-switcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛠️ GRUB default switcher

-> 日本語 / ja

🔥 Summary

For GNU GRUB users in dual/multi-boot environments, this script can easily change the OS to boot next time with only a reboot.

This script rewrites the menu number to be selected by default, which is written in /boot/grub/grub.cfg.

🎯 Target User

  • Users have difficulty accessing BIOS and GRUB (e.g., using a Bluetooth keyboard).

  • Users who want to switch OS via remote access.

⚠️ Disclaimer

This script rewrites the GRUB config file (/boot/grub/grub.cfg) directly.

It may differ from the originally recommended usage.

I am not responsible for any disadvantage you may suffer from using this script, so please use it with caution.

📌 Specification

GRUB default switcher
License MIT
Language Shell script
Author k1z3
GitHub Repo. https://github.com/k1z3/grub-default-switcher

✨ Usage

🐧 For Linux

  1. (Optional) If you want to run without sudo, I recommend that you place grub.cfg in your home directory and make a symbolic link to it.

    First make a copy and back it up.

    cp /boot/grub/grub.cfg ~/grub.cfg && \
    cp /boot/grub/grub.cfg ~/grub.cfg.bu

    Next, delete the original grub.cfg file with sudo privileges.

    sudo rm /boot/grub/grub.cfg

    Finally, make a symbolic link to the copied file.

    sudo ln -s ~/grub.cfg /boot/grub/grub.cfg
  2. Clone this repository.

  3. Running main.sh and the config file is automatically output.

    ./main.sh
  4. Edit config file.

    • cfgpath: Absolute path to grub.cfg file.

    • ubuntu, windows etc. : Line number of each OS in the GNU GRUB menu.

      Note that line numbers in the GNU GRUB menu begin with 0.

      It can also be called by any name (e.g. archlinux).

      Example:

      cfgpath="/home/k1z3/grub.cfg"
      
      ubuntu=0
      windows=4
      archlinux=5
      
  5. You can switch by calling main.sh with the variable name you just set as an argument.

    ./main.sh windows

    Expected output:

    [Success] Changed default boot entry to 'windows'. (Index: 4)
  6. Windows will be started at the next boot (reboot)! 🎉

🪟 For Windows

  1. Prepare a Linux environment with GRUB in advance according to the above procedure (For Linux).

  2. WSL environment is required for this script to work. Please install the appropriate distribution.

  3. Mount Linux on WSL to edit grub.cfg.

    Run the following command in Cmd or Powershell to find a disk that can be mounted.

    GET-CimInstance -query "SELECT * from Win32_DiskDrive"

    Example:

    DeviceID           Caption          Partitions Size          Model
    --------           -------          ---------- ----          -----
    \\.\PHYSICALDRIVE0 M.2 NVMe SSD 2TB          3 2000396321280 M.2 NVMe SSD 2TB
    \\.\PHYSICALDRIVE1 M.2 NVMe SSD 1TB          2 1000202273280 M.2 NVMe SSD 1TB

    In this case, I have Linux with GRUB on 1TB, so I mount it as follows.

    wsl --mount \\.\PHYSICALDRIVE1 --bare

    In addition, look for the partition number on the WSL.

    lsblk

    Example:

    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
    sda      8:0    0 363.3M  1 disk
    sdb      8:16   0     8G  0 disk [SWAP]
    sdc      8:32   0     1T  0 disk /snap
                                    /mnt/wslg/distro
                                    /
    sdd      8:48   0 931.5G  0 disk
    ├─sdd1   8:49   0     1G  0 part
    └─sdd2   8:50   0 930.5G  0 part

    The partition with grub.cfg is sdd2 (2nd), so mount it again with Cmd or Powershell.

    wsl --mount \\.\PHYSICALDRIVE1 --partition 2
  4. Go to WSL and clone this repository.

  5. Running main.sh and the config file is automatically output.

    ./main.sh
  6. Edit config file.

    • cfgpath: Absolute path to grub.cfg file (select the mounted one).

    • ubuntu, windows etc. : Line number of each OS in the GNU GRUB menu.

      Note that line numbers in the GNU GRUB menu begin with 0.

      It can also be called by any name (e.g. archlinux).

      Example:

      cfgpath="/mnt/wsl/PHYSICALDRIVE1p2/home/k1z3/grub.cfg"
      
      ubuntu=0
      windows=4
      archlinux=5
      
  7. You can switch by calling main.sh with the variable name you just set as an argument.

    ./main.sh ubuntu

    Expected output:

    [Success] Changed default boot entry to 'ubuntu'! (Index: 0)
  8. Ubuntu will be started at the next boot (reboot). 🎉

Confirmed to work

GNU GRUB
2.06
2.12-rc1

About

[GNU GRUB] Boot OS switching script for dual/mutiple boot environment.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published