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).

The best way to dual boot Linux and Windows

I’m going to explain how to configure your PC in order to dual boot Linux and Windows Vista or Windows 7, assuming Windows is already installed.

If you already have Windows Vista or Windows 7 installed in a single partition, then the method described below is the best way of ensuring that your Windows installation will still be bootable even if the Linux installation or GRUB bootloader become damaged in future.

Furthermore, if your PC has a hidden factory restore partition for Windows, the method described below is the best way of ensuring that you will still be able to recover Windows in future using that hidden partition. The reason why this is the best method is because it does not alter the contents of the MBR (Master Boot Record), which, in addition to the Windows bootloader, may contain code created by the manufacturer to boot the hidden factory restore partition instead of the Windows Vista/7 partition if it detects that you pressed a defined key or keys while the PC is booting. If the contents of the original MBR are overwritten — even if it is only by using the Bootrec.exe tool on a standard Windows Vista/7 Installation DVD — then the manufacturer’s code will no longer be in the MBR and so it will no longer be possible to boot the hidden factory restore partition by pressing the key(s) specified by the manufacturer.

Note also that some Windows applications can make a PC using GRUB 2 unbootable if GRUB 2 is installed in the MBR. See the blog article Windows applications making GRUB 2 unbootable for details. The method described below does not install any GRUB 2 code in the MBR, and thus it will avoid this problem.

So, to reiterate, the method described below avoids two potential major problems: 1) it avoids the possibility of making a Windows factory restore partition unusable; 2) it avoids the possibility of Windows applications overwriting some of the GRUB 2 code.

If you already have Windows installed, the procedure to prepare the PC and install Linux is divided into the following three main stages.

Stage 1: Reduce the size of the existing Windows partition

Do not use the partition managers Parted, GParted or KDE Partition Manager to reduce the size of the existing Windows partition, or you will damage your Windows installation.

Step A: Use the Windows defragmenter or a third-party defragmenter to defragment the Windows partition.

Step B: Use Windows’ Disk Management to shrink the partition (see Resize a Partition for Free in Windows 7 or Vista). The problem with this method is that Windows will only shrink the partition until it reaches the MFT (Master File Table), which means there may be free space in the Windows partition that you would have wanted to use in a Linux partition but cannot. However, if you are satisfied with the resulting size of the Windows partition then you can skip Step C below and proceed directly to Stage 2.

Step C: If you cannot shrink the Windows partition to the size you want by using Windows’ Disk Management, then you need to move the MFT (see Working Around Windows Vista’s “Shrink Volume” Inadequacy Problems). Basically you need to download a tool such as PerfectDisk (it has a free trial period) that will defragment the Windows partition and move the MFT in the process.

Stage 2: Create the new partitions for Linux

Boot a LiveCD or LiveDVD which has the partition editor GParted on it, and run GParted to create and format partitions for Linux (for example /, /boot, /home and swap) in the free space you created in Stage 1 above. I prefer to use GParted to create the partitions before running the Linux distribution’s installer, rather than using a partitioning tool integrated into the Linux installer itself. GParted has more functionality and enables better control over the partitioning process than a partitioning tool incorporated in the Linux distribution’s installer. Also, I have found on some occasions that installation of some Linux distributions fails if using the partitioning tool integrated in the Linux installer, but is successful if the partitions were created beforehand.

SystemRescueCD is a good LiveCD to use for running GParted, but any LiveCD/DVD which includes GParted will suffice. You can download the ISO file from the SystemRescueCd Web site http://www.sysresccd.org/Main_Page.

It is only possible to have up to four primary partitions on a hard disk, so, depending on the Linux partitions you decide to create, it is possible that you will need to create an extended partition containing logical partitions. If, for example, your hard disk has a hidden Windows factory restore partition and a partition for Windows itself, and you decide you want to have /boot and /home on separate partitions to the root directory, then one of many possible partitioning schemes would be:

– the hidden Windows factory restore partition (a Primary partition)

– the Windows partition (a Primary partition)

– the /boot partition (a Primary partition)

– an Extended partition containing the following Logical partitions:

– the swap partition

– the / (root) partition

– the /home partition

For example, on my main laptop I created and formatted the partitions /dev/sda3 to /dev/sda7 as follows:

/dev/sda1 – the hidden Windows factory restore partition

/dev/sda2 – the Windows C: drive partition

/dev/sda3 – the Linux /boot partition

/dev/sda4 – an Extended partition containing the following Logical partitions:

/dev/sda5 – the swap partition

/dev/sda6 – the / (root) partition

/dev/sda7 – the /home partition

Note that it is not essential to have /boot, / and /home in different partitions; they could all be in the same partition. However, I would recommend that at least /home be given a separate partition, so that personal files (videos, music, documents, etc.) would not be overwritten if you were to re-install Linux at some point.

You are free to choose your own partitioning scheme, but I create the Linux partitions in the order shown above so that I can assign specific sizes to the /boot, swap and / partitions, leaving the remainder of the disk space free for the /home partition.

If you do decide to put /boot on its own partition, it only needs to be small (I make it around 100 MB). On my laptop the /dev/sda4 Extended partition is around 143 GB so I created a partition of 60 GB for / (root) — which is more than ample — and a partition for /home using the rest of the available space on the laptop’s 320 GB hard disk. You’ll have to decide on the size of these partitions based on the size of the hard disk and the root partition’s space requirements recommended by the Linux distribution’s developers.

If you do not need Linux to be able to hibernate (‘suspend-to-disk’) then a swap partition of 512 MB would be sufficient for the typical desktop PC, especially when the latest PCs come with 4 GB or more RAM. Some people do not even bother having a swap partition at all with such large amounts of RAM. But if you do want Linux to be able to hibernate then try to find out if the Linux kernel which you will be installing was compiled with in-kernel LZO compression algorithm support (LZO compression algorithm support must not be compiled as a module). If the kernel was compiled with in-kernel LZO compression algorithm support then make the size of the swap partition the same as the RAM size, which is more than enough. For example, as my laptop has 4 GB of RAM I created /dev/sda5 as a 4 GB partition. If LZO compression algorithm support was not compiled into the kernel and you do not know how to rebuild the kernel after installing Linux then you will have to make the swap partition bigger than your RAM size (my guess would be at least 1.5 times the size to be on the safe side).

As to the choice of file system, I format /boot, / and /home as ext4 but you can choose a different native Linux file system if you want. You cannot use Windows’ FAT or NTFS for these Linux partitions.

Stage 3: Install the GRUB bootloader and Linux

Boot Windows Vista/7 and download the freeware tool EasyBCD from the NeoSmart Web site (you might like to make a small donation, to help the developer). You will need to download EasyBCD version 2 and upwards if you want to install a Linux distribution that uses GRUB 2; it is available to download from http://neosmart.net/dl.php?id=1. If you are going to install a Linux distribution that uses GRUB Legacy then the current version of EasyBCD should work too. Follow the instructions on the EasyBCD site on how to install Linux (http://neosmart.net/wiki/display/EBCD/Linux). N.B. You will reach a point in the Linux installation process when the Linux installer allows you to make a choice of where to install the GRUB bootloader: in the MBR (Master Boot Record) of the HDD or in the first sector of the Linux boot partition. You must select the latter, not the MBR. If the Linux installer offers you the option of installing GRUB to e.g. /dev/sda then that would install GRUB in the MBR, because no partition is specified. Make sure you specify the partition that will contain the /boot directory (/dev/sda3 in my particular case). If you created a separate partition for /boot then GRUB will be installed on that separate partition, whereas if you did not create a separate partition for /boot then GRUB will be installed on the partition containing the root directory.

The result of Stage 3 will be that, when you boot your PC, the Windows Vista/7 bootloader will load the GRUB bootloader, and the GRUB bootloader will load Linux. This process is called ‘chainloading’. When you boot the PC you will first see the Windows bootloader menu, and you can select Windows or Linux from it. If you select Linux then you will see the GRUB menu and you can select Linux from that menu, which will boot the distribution.

Whilst chainloading GRUB 2 from the Windows bootloader is more long-winded than booting GRUB 2 directly, you can now use Linux and Windows safe in the knowledge that GRUB 2 will not be overwritten by any Windows applications, and that a Windows factory restore partition will be bootable in an emergency.

A corner case

EDIT (June 21, 2012): A note about a ‘corner case’. This will only apply to a very small minority of users, and then probably only to some users of Gentoo Linux (in Gentoo Linux it is possible to have both /boot/grub/ and /boot/grub2/ simultaneously). If you select ‘GRUB2’ in the pull-down menu in the current version of EasyBCD (2.1.2), EasyBCD actually searches the sub-directories under /boot/ to find the GRUB 2 core.img file, and puts an entry in the Windows BCD pointing directly to that file, i.e. EasyBCD ignores the boot sector of the Linux boot partition. Therefore, when you select ‘Linux’ in the Windows boot manager’s menu, the Windows boot manager does not launch the GRUB code in the boot sector of the partition on which /boot/ resides, it launches the core.img file directly. Now, if your boot partition happens to have both the sub-directories /boot/grub/ and /boot/grub2/, and they both contain a GRUB 2 file core.img, EasyBCD 2.1.2 will create a BCD entry pointing to the one under /boot/grub/ rather than the one under /boot/grub2/. This may or may not be what you want. If you want GRUB 2 to use the core.img under /boot/grub2/, the workaround in EasyBCD 2.1.2 is to select ‘GRUB Legacy’ in the EasyBCD pull-down menu and specify the boot partition. This is counter-intuitive, but it forces EasyBCD to create an entry in the BCD that points to the boot sector of the boot partition instead of the core.img file in a sub-directory of /boot/ on that partition. The GRUB 2 code in the partition’s boot sector will then execute the core.img file in the required sub-directory. This workaround only works if the Linux boot partition is on the same drive as the Windows partition. The developer of EasyBCD will be updating EasyBCD to look for a core.img file in /boot/grub2/ before /boot/grub/, rather than the other way around, if they both exist when you select ‘GRUB2’ in the EasyBCD pull-down menu.

EDIT (September 5, 2012): The EasyBCD developer has fixed EasyBCD a) to cater for core.img being either in /boot/grub2/i386-pc/ or in /boot/grub/, and b) to look for core.img in /boot/grub2/ before looking for it in /boot/grub/. The version with the fix, EasyBCD 2.2 Beta – Build 179.exe or later, can be downloaded from NeoSmart Technologies’ EasyBCD Support Forum. I’ve tried it and it works!

EDIT (January 28, 2015): In October 2013 the GRUB 2 directory in Gentoo was changed from /boot/grub2/ to /boot/grub/, so the above two comments are redundant and you can ignore them.

Make sure you read the EasyBCD FAQ page

EDIT (September 5, 2012): Make sure you read the EasyBCD FAQ page. For example, it states that, to date, EasyBCD does not support the EFI/UEFI, only the traditional PC BIOS.

EDIT (December 18, 2013): According to the NeoSmart Knowledgebase: “As of EasyBCD 2.2, EFI/UEFI and GPT disks are fully supported, but some options may not be compatible with EFI machines.“. I have not tried Version 2.2 or above of EasyBCD with EFI/UEFI and GPT disks, so cannot corroborate this.

EDIT (June 26, 2015): Make sure the partition containing the Linux boot directory is a Primary partition, not a Logical partition. This is because of a Windows limitation.