sabato 19 giugno 2010

Fixing GRUB errors on Ubuntu Lucid Lynx 10.04

Sometimes getting GRUB to work properly on our computer can be a painful process. In my experience this difficulty often derives from the complexity of multiple hard drive system. For example, my brother has an old pc with 2 hd drives, (SATA and IDE). The SATA disk is the main bootable drive, the IDE one is used for storage purposes.
It should be easy to get GRUB to work properly on a pc with 2 drives on 2010. Well, it's not.
When setup finishes to install Ubuntu, we reboot the machine and we should be presented with this screen:


Unfortunately it doesn't happen. We could see a lot of different errors, no matter what, the result is that GRUB isn't capable of loading our preferred OS. Anyway there are good news: if the Ubuntu installation went fine, we don't need to install the OS again, we have to fix GRUB, specifically its configuration file.
First of all, a preamble: this guide cannot be exhaustive since there are such different configurations, systems and situations. We need to pay attention to play with hard disk drives. We have to back up all our data (for example a preexisting Windows partition.
So what's the point? The point is a generic way to mount our filesystem, fix GRUB and install it again. Probably we will need additional informations to get GRUB to work properly and fix the issue in the right way, but internet is great and Google is our friend.

So, without further ado, let's get started.
  1. We will use Ubuntu LiveCD to access our fresh install of Ubuntu. So, let's boot Ubuntu LiveCD as usual: we want to try Ubuntu without any change to our box, even though it's has been installed properly.
  2. At this moment we need to retrieve a couple of informations. We have to know the name of the partition where we installed Ubuntu (available as /) and the filesystem type. We can use GParted to retrieve these parameters:


    In the above example,  /dev/sda3 is our root device, the filesystem is ext3. Run the terminal from theUbuntu LiveCD and execute these commands:

     stefano@SERVER:~$ sudo mkdir /mnt/root
     stefano@SERVER:~$ sudo mount -t ext3 /dev/sda3 /mnt/root
     stefano@SERVER:~$ sudo mount -t proc none /mnt/root/proc
     stefano@SERVER:~$ sudo mount -o bind /dev /mnt/root/dev
     stefano@SERVER:~$ sudo chroot /mnt/root /bin/bash
     root@SERVER:~$ cd /boot/grub/
     root@SERVER:~$ nano grub.cfg

  3. nano is a simple terminal-based text editor. The Page Up and Page Down keys, the Arrow keys, and the Insert key and Delete key all work as in most other text editors.
    Now, before continuing this procedure, we have to understand that GRUB uses a different way to classify the hard disk drives: (hdX,Y). More specifically we  have (hdX=drive where Ubuntu has been installed,Y=partition where Ubuntu has been installed-1). sda = 0, sdb = 1 and so on.
    /dev/sda3 is written as (hd0,2) in grub because Y is always one digit less in (hdX,Y).
    We have to find all occurrences of (hdX,Y) and replace them with (hd0,2).
  4. After that, we can save grub.cfg and install again GRUB executing the command:

     root@SERVER:~$ grub-install /dev/sda
This guide is a work in progress so if you find something wrong, please let me know it.

Best regards