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