January 22, 2019

Grub2 Worms Into Ubuntu (part 1) - page 2

Why Switch, GRUB2 Structure

  • February 11, 2010
  • By Akkana Peck

Configuring Grub2

Ubuntu 9.10 uses Grub2 1.97 beta4. (Yes, "grub two" is version "one point something." No, it doesn't make sense to me either.) Be aware of that when following Grub2 howtos. Grub2 is still changing: later versions are somewhat different.

The old grub was configured primarily through the file /boot/grub/menu.lst. Grub2 replaces that with /boot/grub/grub.cfg -- but the file begins with an ominous DO NOT EDIT! It's still a text file, but any changes you make to it may be overwritten in the next security update. Don't risk it.

grub.cfg is generated by running the program update-grub, which assembles information from scripts in the directory /etc/grub.d. It also pulls a few settings from /etc/default/grub, like the timeout, resolution, boot defaults like "quiet splash" and so forth.

Some of the scripts in /etc/grub.d/ search for other operating systems, like Windows. They also scan your /boot directory for kernels and your disk for any partition that looks like a Linux root partition, and will add entries for all possible combinations.

That's not necessarily what you want -- on my system, with 8 kernels and three root partitions, it generated 56 entries! You can simplify that menu a lot with a little editing.

Custom boot menu entries

Let's start with custom entries: say you want to boot a particular kernel using a specific root partition.

Entries like that go in /etc/grub.d/40_custom. Anything in this file will be safe: it won't be overwritten by system updates.

What should custom entries look like? Start by copying sections from /boot/grub/grub.cfg. But watch out: not all of the automatic entries actually work.

For instance, if you have a separate /boot partition, grub expects kernels to have names like /vmlinux--2.6.31-14-generic. But the grub2 scripts will create some entries with filenames like /boot/vmlinux--2.6.31-14-generic -- note the extra /boot.

And some of the disk entries it generated were wrong. Sometimes it set the root as (hd0,2) when it should have been (hd0,1). In grub2, (hd0,1) means the first partition on the disk: the same thing (hd0,0) used to mean in the old grub. Very confusing.

I also found some disks specified with the wrong UUID, so check UUIDs against ls -l /dev/disk/by-uuid -- or replace with straightforward disk names like /dev/sda5.

Here are some examples that worked for me:

# A copy of the working entry generated by the 9.10 installer:
menuentry "Karmic, Linux 2.6.31-14-generic" {
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
        set quiet=1
        insmod ext2
        set root=(hd0,1)
        search --no-floppy --fs-uuid --set 3743d321-76a0-428d-77da-43214b06444e
        linux   /vmlinuz-2.6.31-14-generic root=UUID=2da2322b-92dc-2433-bbf4-4b564456a821 ro
        initrd  /initrd.img-2.6.31-14-generic

# Simpler entry to boot Lucid lynx alpha:
menuentry "Lucid, sda2" {
    insmod ext2
    set root=(hd0,1)
    linux /vmlinuz-2.6.32-12-generic root=/dev/sda2 ro
    initrd /initrd.img-2.6.32-12-generic

# Simple entry to boot Jaunty using a static kernel with no initrd:
menuentry "Jaunty kernel, sda5" {
    insmod ext2
    set root=(hd0,1)
    linux    /vmlinuz- root=/dev/sda5 ro

Anything you add to 40_custom will appear at the end of the grub2 menu. But scripts in grub.d are executed in numeric order. If you want some custom entries at the beginning of the menu, create a new file with a name like 09_early, copy 40_custom to it and add your entries there.

When you're finished making changes, be sure to run update-grub. Otherwise grub2 won't see your changes.

In the next installment, I'll talk about some other options you can configure, and how to troubleshoot problems when they arise.

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

Most Popular LinuxPlanet Stories