Wonderful world of grub2

Sometimes I take my hard drives and juggle them up into the air before I put them randomly back into my computer. Well, perhaps not, but putting in a new drive or doing a BIOS update can do pretty much the same thing to the ordering of your disks. This is why most “techy” people (you know those that juggle their disks up into the air) end up with some sort of volume management, and in my case LVM. This makes it possible to have volume names rather than drive + partition number decide what is what. Now this only solves half the problem, because you still have to boot of a random drive. This is where grub2 shines (for me atleast), latest version (of this writing) actually supports having the boot partition (containing your kernel and grub configuration files) on an LVM partition. In other words we can use this to juggle as much as we want without having to update our bootloader all the time:

So for the practical example of grub2 (of course you have to be able to install it yourself first, be it apt-get/emerge/configure&&make install/rpm..) .. For what I needed I made one pc style partition on all the drives (on my system this gives med /dev/sda1 and /dev/sdb1). I do the usual LVM voodoo and ends up with /dev/somevg/bootlv which I mount on /boot. Now to the fun part: In grub2 you build your boot loader from modules to create the perfect bootloader, we will use the modules pc (so that it understands pc-style partitions), ext2 (so that it can read ext2 file system) and lvm (so that it can understand lvm). Of course there are other modules that are useful, but these can be loaded dynamically from the boot partition when needed.

Installing is easy:

grub-install –modules=”pc ext2 lvm” /dev/sda
This should have figured out everything it ever needed to know, unfortunately this is not the case and it complains about not finding (somevg-bootlv). But what it does do for us is copying all the modules needed into /boot/grub and creating core.img with pc, ext2 and lvm modules in it. But for the actual installation of the bootsector and bootstrapping modules:

grub-setup -r (somevg-bootlv) (hd0)
you may need to escape those parenthesis \( \), depends on your shell .. If you want to be able to boot off any drive, just install it onto all of them (hd0 and hd1 in my case).
Anyways, the format of the configuration has changed, and so has the configuration filename, it is now called grub.cfg, here is an example:

set timeout=10
set default=0
menuentry "Gentoo Linux Default" {
set root=(somevg-boot)
linux /vmlinuz dolvm2 root=/dev/somevg/root ramdisk=16384 vga=1
initrd /initramfs-genkernel-x86_64-2.6.18-gentoo-r1
}

Hope this helps:)

6 Responses to “Wonderful world of grub2”

  1. Christian says:

    Hey, this is cool :-) Had to read the post twice to understand how grub2 finds the modules and configuration files, but this is probably covered by the install-to-boot-sector-part when one runs “grub-setup -r (somevg-bootlv) (hd0)”

  2. ace says:

    Well, I had to do some research to find out how those bootstrap modules where loaded. After some chatting on irc I found that, as everyone know there on a pc there is a sector (512 bytes) loaded into memory by the bios, what is not that commonly known is that the first partition doesn’t start before the next cylinder, which means there is one cylinder available for the boot loader (about 32kb). So in this extra space is the actual place where we put the modules to figure out how to read LVM and ext2.

  3. gnd says:

    good explanation!
    so, now that grub2 has lvm support, does that mean it would be possible to boot a kernel without an initrd? seeing as it should be able to read the ext3 fs from the lvm partition..? or is that still impossible

  4. ace says:

    Thanks, a bit outdated however, I should perhaps update it…

    It does not mean the kernel can be booted without an initrd image. The reason for this is that the kernel really does not know how to find lvm volumes, this is all set up in user space (lvm utils using the device-mapper interface if I am not mistaken).

    The process is that GRUB loads the initrd image into memory (in addition to the kernel) before juming into (executing) the kernel which finds the inird image in memory.

    Personally I use a very small initrd image (without modules and such) with only the basic lvm programs doing the bare minimum to mount root.

  5. gnd says:

    what did you build it with, initrd-tools or yaird ?

  6. Robert says:

    In what version on grub2 was LVM implemented? Cause I’m trying to install ubuntu 9.10, and that defaults to using LVM and has grub2 1.97beta4, and I can’t get it to work. grub2 says “unknown filesystem”.

Leave a Reply