Restoring a grub2 bootloader broken

After I had to change a hard drive for another my linux Opensuse 12.2 grub bootloader was broken, here are some of the learning of this debugging phase :

– Restore grub to the new disk

– Start kernel manually

– Fix hard drive references

At the end, the process to recreate the bootloader and particularly to recreate the initrd file was broken, and I assume bugged. To really make it works, I had to update the whole Linux and again at the end of the process the booloader based on grub2 was impossible to execute. The solution was to select Grub (version 1) and it worked. I assume it was possible before update the whole distribution to use yast2 booloader and choose grub instead of grub2.

When you get a message like waiting for device … to apear at boot, it may be because of the initrd does not contain the right modules to access the hard drive. It appears when the motherboard has been changed.

This article is mostly notes taken during this debugging phase … so do not consider it as an howto but as a list of command and possible way to debug this kind of issues…

Restoring grub bootloader to the new disk

To reinstall grub, I needed to run grub2-install program, the first step is to boot a linux and for this, i used the system rescue mode existing as a boot option of the installation DVD. If you do not need a qwerty keyboard, don’t forget to change the language at boot in the menu.

Once having a linux console here are the steps :

– mount target root device in /mnt : mount /dev/sda1 /mnt

– associate /dev/ and / proc to the target device for chroot:

* mount –rbind /dev /mnt/dev

* mount –rbind /proc /mnt/proc

* mount –rbind /sys /mnt/sys

– chroot to the target system : chroot /mnt

At this point it is possible to start yast2 bootloader or to update grub manually

– bugfix a strange thing in the scripts

* mkdir -p /mounts

* ln -s / /mounts/instsys

– and also to update grub2 config : grub2-mkconfig

– it is now possible to start grub2-install /dev/sda

Once this done, my grub bootloader is back on my system.

I also had to recreate initrd to add the right sata modules as the motherboard has changed.

For this, after the chroot, I started mkinitrd ans it has been rebuilt.

Boot Grub2 Manually

Before being able to get grub correctly working, I firstly had a grub command line and had to manually choose and boot my kernel … here the way I used :

set root=(hd0,1)
linux /boot/vmlinuz root=/dev/sda1
initrd /boot/initrd
boot

The path to the vmlinuz and initrd can be set by browsing the different possible choice using the TAB key.

Change the hard drive UUID

By default in OpenSuse 12.2, the device is named using ID, it means that instead of having drive named /dev/sda1, it is name something like /dev/disk/by-id/ata-153a15d315f-part1 … When you copy a disk from another this volume id change and all the previous references become invalid.

The volume ID can be obtained usingblkid /dev/sda1 command for example.  It is also possible to assign a UUID to a disk by using tune2fs -U uuid /dev/sda1. But the best way to get the right name is to take a look to /dev/disk/by-id directory.

Then the UUID/ID must be updated in the different configuration files like:

– /etc/fstab

– /boot/grub/menu.lst

Here my main issue was that in simple command line it’s hard to copy and paste this long name to the configuration files. The use of sed has been welcome :

By using tab navigation in the filesystem it is easy to get variable initialized
sda="ata-Corsair...."
sda1="/dev/disk/by-id/ata-Corsair...-part1" 

Then create a regex for device.map
regex="s@\(ata-.*\)\$@$sda@"

Then use it on device.map file
cp /mnt/boot/grub2/device.map /mnt/boot/grub2/device.map.old
sed -e $regex < /mnt/boot/grub2/device.map.old > /mnt/boot/grub2/device.map

Then for fstab, another way is to simply add a line with the name :
ls /dev/disk/by-id/ata-...-part1 >> /mnt/etc/fstab
Then use the vi editor or copy and past

 

 

References

One thought on “Restoring a grub2 bootloader broken

  1. The following few “trivial” lines really saved my day – thanks a million! I spent ages trying to figure out what was going wrong until I found this blog post. It’s a bug, which is apparently also present in later versions of openSUSE – in my case 13.1.

    > – bugfix a strange thing in the scripts
    > * mkdir -p /mounts
    > * ln -s / /mounts/instsys

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.