Reboot button which allows you to specify which GRUB 2 menu entry to boot

When clicking on Leave > Restart Computer in your Desktop Environment it might be convenient to be able to specify straight away which GRUB 2 menu entry to boot, rather than having to wait for the machine to reboot and display the GRUB 2 menu. Here is how to do it. I use KDE, but the same approach would work in other desktop environments.

N.B. Some distributions use /boot/grub2/ rather than /boot/grub/ for GRUB 2, so replace the path accordingly if the distribution you use is one of them.

Setting it all up

1. Use your favourite text editor to create the simple Bash script shown below in your home directory. I’ll call the script reboot.sh for example:

#!/bin/bash
echo
echo "== SELECT WHICH OS TO BOOT =="
echo
# Delete the next line if you do not have a separate boot partition:
sudo mount /dev/sda3 /boot &>/dev/null # Change "/dev/sda3" to match your boot partition.
echo
sudo cat /boot/grub/grub.cfg | grep menuentry | awk -F\" '{print N++,$(NF-1)}'
echo
read -p "Enter number (q to abort) and press ENTER: " CHOICE
if [ "$CHOICE" != "q" ] ; then
  sudo grub-reboot $CHOICE
  sudo shutdown -r now
fi

In this script, the GRUB 2 menu entries are read directly from the grub.cfg file instead of being hard-coded in the script. Therefore this script would work as-is on anyone’s installation.

2. Make the script executable:

$ chmod +x ~/reboot.sh

3. Create an icon (Desktop Config File) on your Desktop (right-click on the Desktop and select Create New > Link to Application…), configure it to execute the command sh reboot.sh in a terminal, and give it a nice icon (the icon shown below would suffice). Drag it onto your Panel so it will always be visible and only needs a single click to activate.

Reboot button

Reboot button

4. Edit the file /etc/default/grub to have GRUB_DEFAULT=saved and GRUB_SAVEDEFAULT=true (if it is not already like that).

5. Regenerate the file /boot/grub/grub.cfg (this would only be necessary if you changed anything in /etc/default/grub):

# grub-mkconfig -o /boot/grub/grub.cfg

In some distributions the command is instead:

# grub2-mkconfig -o /boot/grub/grub.cfg

How to use it

1. Click on the new icon on your Panel.

2. You will be presented with a menu similar to the following:

== SELECT WHICH OS TO BOOT ==

Password:

0 Sabayon GNU/Linux, with Linux x86_64-2.6.38-sabayon
1 Sabayon GNU/Linux, with Linux x86_64-2.6.38-sabayon (recovery mode)
2 Sabayon GNU/Linux, with Linux x86_64-2.6.37-sabayon
3 Sabayon GNU/Linux, with Linux x86_64-2.6.37-sabayon (recovery mode)
4 Windows 7 (loader) (on /dev/sda1)
5 Windows 7 (loader) (on /dev/sda2)

Enter number (q to abort) and press ENTER:

You will first be prompted to enter your password, then the menu will be displayed and you will be prompted to enter the number of the menu item you want to boot. The example above is from my machine; your menu will have different entries, depending on what is in your /boot/grub/grub.cfg file.

The machine will then reboot to the kernel or OS you just selected, without you having to touch any further keys, or will exit the script and do nothing if you selected the Abort option (q).

About Fitzcarraldo
A Linux user with an interest in all things technical.

3 Responses to Reboot button which allows you to specify which GRUB 2 menu entry to boot

  1. Fitzcarraldo says:

    I discovered today that a GUI feature which does basically the same thing is going to be added in KDE 4.7 when it is released. See the following blog post for details:

    http://ksmanis.wordpress.com/2011/04/21/hello-planet-and-grub2-support-for-kdm/

    Nice!

  2. Frank Starr says:

    Sabayon Linux now seems to emulate Grub 3. If you right click, you no longer get the option to create a shortcut. If I wanted to add a shortcut to the desktop, is there a new way?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.