GRUB2 revisited

For once in my life I was paying attention to the monitor as the list of updates was being processed and so noticed that a new kernel (Version 2.6.32-29.58) was being downloaded. However, this didn’t show up in my boot menu since I am using a custom setup. In fact, my system was still using Version 2.6.32-24.43 of the kernel and was thus considerably out of date.

That got me to thinking about custom boot menus under GRUB2, and how to set things up so that the Ubuntu kernel gets updated when a new version is available in Update Manager, while retaining the custom part of the boot menu that ensures that the Windows side of my dual boot system continues to function normally.

Since I rather glossed over this process in my second post – Installation of Ubuntu 10.04 (well, I was new to blogging back then!) – and since I had almost forgotten the intricacies of the GRUB2 menu scheme, I thought it would be useful to revisit the issue in a little more detail.

So, the problem is that at least one new version of the kernel isn’t being used to boot into Ubuntu and, on my particular system, GRUB2 is confused about which disk partitions Vista and the Windows recovery system are occupying.

First, let’s check which version of the kernel is being used by Ubuntu. We issue the terminal command:

uname -a

and the system reports:

Linux Gateway-desktop 2.6.32-24-generic #43-Ubuntu SMP 
  Thu Sep 16 14:17:33 UTC 2010 i686 GNU/Linux

I wonder how many versions of the kernel have been downloaded and not installed for use. To find out, we can use the command:

dpkg --list | grep linux-image

The system responds with:

ii  linux-image-2.6.32-24-generic  2.6.32-24.43
 Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-26-generic  2.6.32-26.48
 Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-27-generic  2.6.32-27.49
 Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-28-generic  2.6.32-28.55
 Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-29-generic  2.6.32-29.58
 Linux kernel image for version 2.6.32 on x86
ii  linux-image-generic  2.6.32.29.35
 Generic Linux kernel image

Hmmh. Update Manager has indeed been busy. We have versions 26 through 29 that we haven’t used yet!

Now, let’s find out which is the current kernel recommended for Ubuntu 10.04. Presumably, this will be the highest numbered kernel that has been downloaded, but we can double check at: https://launchpad.net/ubuntu/+source/linux. Down near the bottom of the page are the entries relating to “The Lucid Lynx” showing that (at the time of writing) Version 2.6.32-21.32 is the main release, from 46 weeks ago, and Version 2.6.32-29.58 is a security update from three days ago. So, the highest-numbered entry in our list of available kernels is indeed the version that we want to run.

In order to change the boot menu, we need to modify the file /boot/grub/grub.cfg; however, as noted earlier, we aren’t allowed to edit this file directly. In order to make the required changes, we need to know a little about a number of subsidiary files used by GRUB2. A really good help file – “GRUB 2 bootloader – Full tutorial” – has been developed by Dedoimedo and is available on the web at: http://www.dedoimedo.com/computers/grub-2.html

The tutorial lists the script files located in /etc/grub.d, and describes their various purposes. In particular, we need to know about three of these files:

10_linux identifies all of the Linux kernels that are available on our computer and produces individual script entries that will result in a listing in the boot menu for each kernel.

30_os-prober scans the hard drives for other operating systems (such as Windows) and creates menu listings for each.

Finally, 40_custom is an open script where users can insert custom entries to create additional menu listings on the boot menu.

We need to run the update-grub command to (a) capture information about all of the current operating systems that are available, and (b) to run all the scripts in the grub.d folder, in order to produce an updated version of grub.cfg, and hence an updated boot menu.

However, the tutorial has yet one more sage piece of advice:

 

Scripts are meant to be executed. This means that they have the execute bit turned on. If you turn the execute bit off, they will not run.

GRUB 2 bootloader – Full tutorial
Dedoimedo


So, my basic procedure for updating the boot menu is as follows:

1. Run the terminal command: sudo nautilus

2. Navigate to /etc/grub.d

3. Make sure that in (right mouse click on the file) Properties – Permissions, the Execute (Allow executing file as a program) box is checked for all of the script files so that the scripts will indeed run.

4. Exit from Nautilus (if the system responds with a usershare error, this can be cleared with Ctrl-Z)

5. Run the command: sudo update-grub

This creates an up to date list of all the operating systems available on the computer and creates menu entries for all of them in the file grub.cfg.

6. Run the command: sudo gedit /boot/grub/grub.cfg

I know. We aren’t allowed to edit this file directly. But, what we want to do is to use Ctrl-A to select the entire contents of the file, and Ctrl-C to copy the contents to the clipboard.

7. While still in gedit, open the file /etc/grub.d/40_custom

Leave the initial five lines of the script at the top of the file and, on the line immediately below, press Ctrl-V to paste the contents of the clipboard into the file.

Now, edit any sections of the file that are not required, or which need to be otherwise modified. For example, I need to leave the first menu entry for “ Linux 2.6.32-29-generic” in order to enable the latest version of the kernel. Consequently, the first section of my custom script reads as:

menuentry 'Ubuntu 10.04, with Linux 2.6.32-29-generic' 
  --class ubuntu --class gnu-linux --class gnu --class os {
	recordfail
	insmod ext2
	set root='(hd0,7)'
	search --no-floppy --fs-uuid
  --set 118e816d-6a2e-4ed9-9d1a-9c76a63fadbf
	linux	/boot/vmlinuz-2.6.32-29-generic
  root=UUID=118e816d-6a2e-4ed9-9d1a-9c76a63fadbf ro quiet splash
	initrd	/boot/initrd.img-2.6.32-29-generic
}

Each additional menu listing uses a similar portion of script, i.e. starting with menuentry and ending with the } character. Now, because I don’t need kernels 2.6.32-26.48 through 2.6.32-28.55, I can eliminate their sections of script from the file. By so doing, there will be no menu entries for these kernels. However, note that I leave the entry for Version 2.6.32-24.43. That is the kernel that was working correctly prior to this upgrade. It is wise to leave this as a working option, just in case there happens to be a problem with the latest kernel. We can always remove such outdated entries at a later time, and we will see how that is done – at a later time.

I also leave the section headed: menuentry “Windows Vista (loader) (on /dev/sda1)” since this is the custom entry that tells the boot loader to use the (hd0,2) disk partition to boot Windows Vista. I eliminate the entry for the Windows recovery partition since I don’t use this.

The other sections of the script are recovery modes in various version of Ubuntu and memory tests. Personally, I never use these, but generally leave a couple of such entries in the boot menu – just in case! I might also mention that I leave the section for “Ubuntu 8.04.3 LTS, kernel 2.6.24-24-generic (on /dev/sda5)” in the boot menu for precisely the same reason!

So, after all that, we have obtained a list of the available Ubuntu kernels and bootable Windows partitions from grub.cfg, and have created a customized version of the boot menu generator in the file 40_custom. Now back to our task list:

8. Save the 40_custom file and exit from gedit

 

What happened to the good old days of GRUB – now “legacy GRUB” – where all we had to do was edit the file menu.lst?

Linux North

9. Run the command: sudo nautilus and remove the checkmarks from the execute bit of the files 10_linux, 20_memtest86+, and 30_os-prober in the folder /etc/grub.d

10. Exit from nautilus

11. Run the command: sudo update-grub

This final command should create a new boot menu in the file /boot/grub/grub.cfg that contains the custom menu items that we have specified. Because we disabled the other script files from running by removing their execute bits, entries for other Linux kernels, Windows partitions, and memory tests are not generated this time. Only the custom menu entries contained in 40_custom are included when we update GRUB.

You can check, ahead of rebooting, just to be sure, by using the command: gedit /boot/grub/grub.cfg. This will open the file in read-only mode.

So, finally, we need to:

12. Reboot

And, the boot menu should now be exactly as advertised!

But, since we have disabled 10_linux, any new kernels that are downloaded by Update Manager will not be automatically added to the boot menu. We need to use a little more magic for this to happen.

Advertisements
This entry was posted in GRUB. Bookmark the permalink.

15 Responses to GRUB2 revisited

  1. Tod says:

    This is a great how-to article. I have a collection of operating systems on my laptop (Win7+4 linux OSs) and my Grub menu is always a mess and scares my wife away before she even boots up. I don’t understand Grub well enough to mess with in fear that I kill it and be in big trouble. This article is simple and step-by-step and worked perfectly. Now I am waiting for the next article to explain the “magic” to updating with new kernels.

    • Alan German says:

      Tod:

      Thank you for your input. To check the series of posts on the GRUB (GRUB2) menu system, the easiest route is to click on “GRUB” under “Categories” in the side menu bar on the right side of the display screen. This will bring up a list of all the relevant posts.

      Alan

  2. Coming from a Windows background and EasyBCD, the Ubuntu bootloader configuration is WAY HARDER than it should be. Thank you very much for your tutorial, it made a difficult change relatively pain free.

    • Alan German says:

      Len:

      Thank you for your kind comments. Personally, I never found the Windows’ bootloader all that easy to deal with. However, the original version of GRUB was very simple to use. It was just a matter of editing the text file menu.lst. I agree that GRUB2 is way more complex (than necessary). I often thought about replacing GRUB2 with GRUB but then decided that I should just figure out how to work with the intricacies of the current version. And, it turns out that there isn’t all that much that needs to be done to run a custom boot menu.

      Alan

  3. Abhi says:

    A quickie. Why does the update manager download all these latest kernels and NOT install them?

    Abhi

  4. Mikoyan says:

    10_linux, 20_memtest86+, and 30_os-prober these three files are chmod a-x. I have follow this very tutorial. But GRUB_TIMEOUT=60 and GRUB_DEFAULT=4 aren’t working. I had update-grub, let you know.

    • Alan German says:

      Mikoyan:

      The GRUB_TIMEOUT and GRUB_DEFAULT variables are contained in the configuration file /etc/default/grub. The instructions for modifying this file are posted in Bringing light to the darkness. This example uses a different configuration variable than the ones that you wish to change but the technique is the same.

      Alan

      • Mikoyan says:

        Please, I was hoping you understand I edited /etc/default/grub to attempt to change the way the system boots with the variables sets GRUB_TIMEOUT=60 and GRUB_DEFAULT=4 (or maybe “Windows 10 Pro (loader) (sda2)”. But something blocks the change to take effect.

      • Alan German says:

        Mikoyan:

        Did you run sudo update-grub after you edited and saved the changes to the grub configuration file? If you don’t do this, the boot menu won’t change.

        Alan

      • Mikoyan says:

        Yes! ran sudo update-grub! as I said on my first reply. Something is missing. Those three files should be chmod a-x?

      • Alan German says:

        Mikoyan:

        I regret, I don’t know what is wrong on your system. You should be able to change the default boot and timeout options in the grub configuration file simply by editing and saving values. This process shouldn’t depend on the files in grub.d; they just let you change entries in the boot menu. I just tried making changes to /etc/default/grub on my current Mint 18 system and everything worked as expected. In this version of Mint, grub.d does not contain the 10_linux, 20_memtest86+, and 30_os-prober files.

        Alan

      • Mikoyan says:

        What should I do with edited 40_custom before running `sudo chmod a+x 10_linux 20_memtest86+ 30_os-prober ; sudo update-grub`. I mean, before it had only the first five lines! I will try that with ‘default’ and see if it works. Thanks in advance

      • Alan German says:

        Mikoyan:

        I’m now unclear as to what you are actually trying to do. 40_custom, in the folder /boot/grub/grub.cfg, is a user-defined script file where you can set up a series of “menuentry” sections to determine which system boot options will be shown on grub’s boot menu. The setting for GRUB_TIMEOUT, which pauses the grub boot menu for a specified number of seconds, is in the file /etc/default/grub. In my experience these two items can be modified independently as described in my blog posts (Bringing light to the darkness and GRUB2 revisited, respectively).

        Alan

  5. Nico666 says:

    Mikoyan>

    You need to leave “00_header” with exec permissions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s