Grub2 is now the default bootloader for most of the distros. It is the next generation of GNU GRUB and aims to be cleaner, safer and more powerful than its previous version Grub legacy. In order to provide such features it was written from scratch and so the way it is configured is quite different from Grub Legacy. Since, we linuxers always want to run our systems our way ;), you might be interested in learning how to configure grub2 so that you may comfortably tweak and play around with it.
Grub2 Configuration Files
Like Grub Legacy grub2 resides in /boot/grub but there is no menu.lst file in Grub2. Its configuration file is /boot/grub/grub.cfg. But be careful, this file is not meant to be manually edited (read the last part of this article if you want to edit it anyway). Don't worry, that doesn't mean that you can't configure grub2. Actually, grub.cfg is generated by individual scripts residing in /etc/grub.d/ when "update-grub" command is executed and only read permissions are given to this file to avoid manual editing.
This file grub.cfg is divided into sections, each section is handled by an individual script file (in /etc/grub.d folder). You may see entries similar to the following in your grub.cfg file.
### BEGIN /etc/grub.d/10_linux ###
set root=(hd0,5)
search --fs-uuid --set b02e1934-12dd-418a-be3a-9ff7d3e7e7ea
menuentry "Ubuntu, linux 2.6.28-13-generic" {
linux /boot/vmlinuz-2.6.28-13-generic root=UUID=b02e1934-12dd-418a ro quiet splash vga800
initrd /boot/initrd.img-2.6.28-13-generic
}
### END /etc/grub.d/10_linux ###
These lines mean that this section is handled by the script file /etc/grub.d/10_linux. There are similar sections for each script file. Their naming scheme is similar too (XX_filename, XX is the 2 digit number e.g 10, 20). These special names decide which script to run first. The file that comes before another in the alphabetical ordering will be executed first (e.g. 10_hurd will be executed before 10_linux which will be execute before 20_memtest86).
There is another important file responsible for the content of grub.cfg, it is /etc/default/grub. This file holds the content previously found in the upper section of menu.lst. These settings are primarily concerned with Grub's menu display. So, if you want to change any setting related to the grub display or interface then change this file. And yes, this file can be manually configured.
Adding Entries in the Grub2 Menu
Since, the grub.cfg file is not meant to be manually edited you will have to add scripts in /etc/grub.d/ in order to add entries to the grub menu. Trust me, it sounds quite difficult but its not. The syntax is really easy and anyone can learn it within no time.
The user added custom scripts should have a name with prefix 40_ (eg 40_custom). Now, if we want to write a script that adds a menu entry to boot an OS residing on another partition or another disk in the Grub. The script should look something like this.
echo "Adding Custom Kernel & SystemRescue" >&2
cat << EOF
menuentry "Ubuntu, linux 2.6.31-11-custom" {
set root=(hd0,9)
linux /boot/vmlinuz-2.6.31-11-custom root=UUID=c6829e27-2350-4e84-bdbb-91b83f018f98 ro
initrd /boot/initrd.img-2.6.28-11-generic
}EOF
The first line will print the message in quotes "Adding custom .." when this script runs just to give a visual feedback that this script is executing. So, this line is not compulsory.
As you can notice the "menuentry" tag is used to define the menu entry we are going to enter followed by the name of that menu entry (the name that appears in the menu). The grub2 commands that should execute when this menu entry is clicked are entered in curly braces '{}'. You might have already noticed that the "kernel" command of the grub legacy which was used to mention the kernel image is replaced by "linux" in grub2. Rest of the syntax is almost same. After creating your own script file make sure to give it executable permissions.
[shredder12]$ sudo chmod +x /etc/grub.d/<filename>
Removing Entries from Grub2
You don't need to write scipts for removing entries. Either remove the executable permissions from the scripts in /etc/grub.d/ or remove those files from that directory and execute "update-grub" to generate new grub.cfg. You can remove the permission using the following command
[shredder12]$ sudo chmod -x /etc/grub.d/<filename>
Consider an example, if you don't want memtest86+ menu entry all you have to do is remove the executable permissions from the script that generates that entry (/etc/grub.d/20_memtest86+).
[shredder12]$ sudo chmod -x /etc/grub.d/20_memtest86+
If you want to remove the "boot in recovery mode" entry then you can do that by uncommenting this line in /etc/default/grub file
GRUB_DISABLE_LINUX_RECOVERY=true
Manually editing grub.cfg file
Warning: Please don't try to manually edit grub.cfg until and unless you really know what you are doing.
When the command "update-grub" is executed grub.cfg is generated and its permissions are set to read only. You can grant write permissions to the file as root using the following command.
[shredder12]$ sudo chmod +w /boot/grub/grub.cfg
and then open the file with your favourite editor ( i will use gedit).
[shredder12]$ sudo gedit /boot/grub/grub.cfg
P.S - The file will return to "read only" mode and your changes will be overwritten anytime "update-grub" is executed.
I hope this article has covered most of the Grub2 basics. If you think that there is something wrong here then please comment, I have already checked it a few times and I hope I haven't missed anything.
"