How to configure Grub2 in Ubuntu 9.10

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

Editing the /etc/default/grub file

As I have already mentioned above this file contains the settings that were used to be in the upper part of the menu.lst file, above the grub2 menu entries. So, if you want to change the timeout, disable/enable some default settings(like the recovery mode setting above) then you should probably look into this file, make the changes you wish and then update grub.

If you want to know the meanings to the term in the /etc/default/grub file then you may refer to this ubuntu wiki page.

Changing grub2 splash images

I have written a separate article for grub2 splash images. You may read it here.

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.

20 Comments

Betelgeuse (not verified)
November 19th, 2009 10:27 pm
Good article but just for entries. Another important things are timeout and default. Must I edit manually grub.cfg then?. Of course the background image is important too. Thanks.
November 20th, 2009 12:24 am

Thankyou Betelgeuse for pointing them out. I have added a link to the ubuntu grub2 documentation for editing the /etc/default/grub2 file and added another link to my article on grub2 splash images.

Betelgeuse (not verified)
November 21st, 2009 01:08 pm
Quite complete now (for most users). Thank you.
Paul (not verified)
December 1st, 2009 06:06 am
Thank you Betelgeuse for pointing out how to add entries to Grub2. My upgrade from the previous version of Ubuntu to 9.10 did not go smoothly. I had the Grub error 15 and then I lost my desktop and ended up disconnecting all my other hard drives to do a fresh install of 9.10. Of course, I lost all the work that I had done in the previous version, as well as Grub. Somehow I got a Grub menu to show up, but the entries for my other operating systems don't work. Any suggestions or websites to review to get this corrected would be most appreciated. Paul
December 1st, 2009 07:34 pm

Hi Paul,

If you are facing this problem after a fresh install of Ubuntu, then you may try running "sudo update-grub" after booting into the system. This should solve the problem but if not then you might want to check the scripts in /etc/grub.d/ that is responsible for recognizing other operating systems, consider reinstalling grub2 as the last option (in case nothing works).

Paul G. Brandon (not verified)
December 2nd, 2009 03:07 am
Hi Shashank, Thank you for your quick response. I will give the sudo update-grub a try. I ran the probe command (even though I can remember the syntax), but it didn't resolve the issue. I'm wondering if I am causing my own problems as Ubuntu is loaded on sdf, while I have versions of Windows on sdb and sdc. They are all separate hard drives. The other hard drives just have data on them. How would I update grub to the latest version? Thank you again for helping a newbie get his grub working. Paul
Paul (not verified)
December 2nd, 2009 07:19 am
Hi, Here is my output from sudo update-grub. Thank you again for your assistance. Paul paul@paul-desktop:~$ sudo update-grub [sudo] password for paul: Generating grub.cfg ... Found linux image: /boot/vmlinuz-2.6.31-15-generic Found initrd image: /boot/initrd.img-2.6.31-15-generic Found linux image: /boot/vmlinuz-2.6.31-14-generic Found initrd image: /boot/initrd.img-2.6.31-14-generic Found memtest86+ image: /boot/memtest86+.bin Found Microsoft Windows XP Home Edition on /dev/sdb1 grub-probe: error: Cannot find a GRUB drive for /dev/sdb1. Check your device.map. Found Microsoft Windows XP Professional(SP3) on /dev/sdc1 grub-probe: error: Cannot find a GRUB drive for /dev/sdc1. Check your device.map. Found Ubuntu 9.04 (9.04) on /dev/sde1 grub-probe: error: Cannot find a GRUB drive for /dev/sde1. Check your device.map. grub-probe: error: Cannot find a GRUB drive for /dev/sde1. Check your device.map. grub-probe: error: Cannot find a GRUB drive for /dev/sde1. Check your device.map. done paul@paul-desktop:~$
December 2nd, 2009 03:18 pm

Hi Paul,

It seems Grub2 is unable to probe the boot devices in other hard drives. I am not sure about this problem because I have never dealt with booting OSs from external hard drives but I think its a fair chance that there is nothing wrong with the grub installation you are just doing it the wrong way.

I would suggest you to post it on ubuntuforums, there you might find some users experienced with such kind of installations or who have faced such errors. It would be good if you could post its link here so that other users with similar problems can follow.

Paul (not verified)
December 2nd, 2009 08:03 pm
Hi Shashank, Thank you for your suggestion of posting to ubuntuforums. I will post the link here so that others can resolve the issue. My drivers are all internal hard drives. Currently, my workaround is to change the startup disc in the bios, but the Grub menu (which I got working previously with old Grub) is the preferred solution. Thank you again for a great website on Grub, as well as the multimedia applications. Take care, Paul
ZoltAi (not verified)
December 8th, 2009 03:25 am
hi Guys, after upgrading to 2.6.31-16 GRUB lost Win XP from the list. I tried to use Live CD and follow suggestions to type "sudo grub" and "find /boot/grub/stage1" and then I'm supposed to get list to setup root to different drive as far as I understand but it says :"Error 15: could not find file" I don't want to reinstall XP and then UBU 9.10 to get thing fixed. Need help !!! Cheers, ZoltAi
December 8th, 2009 02:28 pm

Hi ZoltAi,

This method you are using is actually for grub legacy. But you have grub2 in Ubuntu 9.10 and since you don't have /boot/grub/stage1 in grub2 it is giving "file not found" error. So, in order to solve your problem you should boot into Ubuntu 9.10 and run "sudo update-grub" and see if you get an output line saying that "windows XP is detected on some drive". If this doesn't work then you may want to switch to the older kernel ( you can do this from the grub menu when you boot)and again try this command. If its really a kernel issue then your problem should get resolved.

ZoltAi (not verified)
December 8th, 2009 07:53 pm
Thnaks mate, what I did was simply reinstall ubuntu as I needed to use bloody XP today ... I will search forums to find out how to tweak GRUB2 just in case this happens again. In a matter of fact I did not install 2.6.31-16 now and will wait for 17 :)))) can't risk doing everything all over again.
Paul (not verified)
December 13th, 2009 12:03 am
Hi Shashank, Doing a routine check with Update Manager, I downloaded and installed the next version of the kernel. I could see while it was installing that it was going through the GRUB commands, and low and behold, grub now detects and maps all of the other operating systems correctly. Thank you again for your website, as it provided a number of things to explore to get Grub working. I'm wondering if part of my issue was that Ubuntu is on an old IDE drive, while XP is on a SATA drive and the SATA drives come before the IDE drives when loading. Whatever the problem was, it is now solved. Thank you again for your assistance. PaulidXRH
Jamie Wood (not verified)
February 4th, 2010 03:25 pm
Hi, this a helpful article and its useful to understand the changes, though I can't say I'm hugely impressed by grub2 and its configuration. One of the few things that was actually easier to do 10 years ago in linux. Anyway, my problem is that I now have around 10 entries in my grub 2 boot menu, including two linux kernels and their recovery modes, an unused vista partition and an XP partition. I only use two of these partitions commonly but do flick between them quite a lot. What I would like is a simple menu which is basically A, B or something else. This is hampered by the entries in /etc/grub.d not having a 1:1 correspondence with the grub entries. It would appear that both the XP and vista partitions are detected using the 30_os-prober script which means I can't remove Vista from the list without removing XP. Suggestions on how to proceed? Thanks Jamie
John Lally (not verified)
March 5th, 2010 05:22 am
It looks like the order of the operating systems can be changed using the number at the start of the file name for that operating system in the grub.d folder. I think it might be possible to use text from the grub.cfg file in /boot/grub/ to create a custom file from the 40_custom file. This newly created file can then be renamed 01_custom (which should put it top of the list) or whatever you like. I'm having a play with it on a usb stick at the moment. Will post again if successful.
March 5th, 2010 05:58 am

I never really got a chance to work with grub2 custom config files. Would love to see your result.

Debabrata (not verified)
March 9th, 2010 09:02 pm
i had a problem.......i'm using ubuntu 9.10 & after installing it i took update manager's update in about 200mb. but after that when i started my computer & choose ubuntu to run it shows something like "gnu grub 1.97 beta4" & ask kor some command to run.....i coundn't understand what it is n for this i cant run my computer...........!!!!! plzzzz help me.............
Nejzac (not verified)
August 1st, 2010 05:09 pm
The easy way is reinstalling Ubuntu. The real way (interesting if you had any installed/configured program or data you don't want to lose) is reading about configuration files for your GRUB version, reaching them somehow using another OS, a boot CD, or removing the hard drive and accesing it from another computer. If you choose the boot CD, insert it, reboot and look carefully the screen before the GRUB loads (usually in the lower part) for something like "boot menu, boot priority, boot configuration" or "BIOS configuration" if none. All the required files should be in /boot/grub or /etc/grub.d
vijay (not verified)
April 27th, 2010 11:06 pm
Is there any way to remove(hide) a os in the grub list...????
Nejzac (not verified)
August 1st, 2010 05:02 pm
If your GRUB is version 1, edit menu.lst (I think it is in /boot/grub/menu.lst If your GRUB is version 2, then try to open /etc/grub.d and look into the files there. Removing permission to execute from a file there disables the menu entries specified in there. If you cannot remove the exact entries you want on this way, you will have to edit those files by hand.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.