Thursday, 18 June 2009

Howto: transitioning to grub2 from lilo (LVM)

Failing to boot my self compiled kernel, I got to the conclusion that, in order to try to debug the initramfs issues I might have I needed to pass easily different break=* parameters on the kernel command line to identify which part of the boot within the initramfs goes awry.

Since I was stuck with lilo and booting with different parameters is a pain when using it, I decided is time to try to install and start using grub-pc, aka grub2.


Documentation on migrating from lilo (or even grub 1) is lacking, even more for cases where you have the /boot directory on LVM, like I do. IMHO, one of the most important things during a migration is to never lose the ability to boot properly your system, which means that it was a must not to write the grub code into the MBR unless I was 100% sure I will be able to boot with grub at the next reboot.


I tried quite a few approaches (installing grub in a partition, trying to create a real /boot partition), but they all failed on way or another, so I will not describe any of those failing methods.


What I found to work was to:
  • install the grub-pc package,
  • create its configuration and set it up (creates the module files in /boot/grub and /boot/grub/grub.cfg),
  • then install the boot code on an external USB stick, while nothing else was to written on the stick (so no data lost from the stick);
  • after successfully booting from the USB stick, install the boot image on the internal harddisk's MBR
  • reboot and be happy with grub2
Note that, although I run lenny, I decided to use sid's grub package since the little information on grub2 that existed pointed to grub-mkconfig which doesn't exist in lenny's grub-common package. Due to some bug I found earlier at work when trying to migrate my workstation, I knew the squeeze version wasn't good either because it failed to properly boot systems with / on LVM. The earliest version that works is 1.96+20090603-2 and installs without problems directly from sid on a lenny system.

Now lets go over the steps in more detail*.

Creating the configuration is a matter of creating the device.map file and grub.cfg:
update-grub


Create a core.img file with support for lvm:
grub-mkimage --output=/boot/grub/core.img ext2 chain pc gpt biosdisk lvm


Install on the MBR of the USB stick (making sure lvm will be visible):
grub-install --modules="pc ext2 biosdisk gpt chain lvm" /dev/sdb


Try to boot from the stick at least one kernel. If it fails, you probably didn't add in the proper modules (see the possible module names in /usr/lib/grub/i386-pc and add the necessary ones in the modules list). If you're dropped into the GRUB rescue prompt, use ls and set to check if the root and prefix variables are set correctly and if the lvm root is visible.

If you manage to boot, you're ready to replace lilo or your old boot loader.


Install on the internal disk MBR:
grub-install --modules="pc ext2 biosdisk gpt chain lvm" /dev/sda


Reboot and be happy.



Please note that you might have added some optional parameters in your old lilo.conf which aren't present in the new /boot/grub/grub.cfg file and you might want to add those. If you want to add some option to all the images, you'll probably want to append those parameters to the value of this variable from /etc/default/grub:

GRUB_CMDLINE_LINUX="quiet"

For instance, since I want to find when sleep stopped working for my machine, I changed mine into:

GRUB_CMDLINE_LINUX="acpi_sleep=beep ec_intr=0"


After that, run update-grub once more to propagate the changes into /boot/grub/grub.cfg.





* skipping package installation; just use aptitude's text user interface and install grub-pc and grub-common from sid; depending on the system you might need to install grub-efi, grub-ieee1275 or even grub-linuxbios instead of grub-pc

2 comments:

Anonymous said...

one of the most important things during a migration is to never lose the ability to boot properly your system, which means that it was a must not to write the grub code into the MBR unless I was 100% sure I will be able to boot with grub at the next reboot.

WRONG! What you did need is a valid alternate booting method before you even started tweaking your boot sector.

Think of it: what can go wrong as long as you are able to boot from, say, and USB stick? And that USB stick you can test for as long as needed till it works as requested since you still didn't even touch your HDD master boot record!

eddyp said...

WRONG! What you did need is a valid alternate booting method before you even started tweaking your boot sector.

0. nitpick :-P
1. Tell that to lilo which I was planning to replace
2. Almost the same thing
3. With the installation of GRUB on the stick MBR I made exactly that, except I didn't need to make a complete alternate boot device since booting with stick + local files worked, so booting from the internal HDD MBR + local files was sure to work