Thursday, October 2, 2014

GRUB : Lost and Recovered

This happened when I was updating my Fedora 20 using a Hotel's Wifi ( WHICH WAS FREE ). It wasn't my fault. I couldn't do anything and hence, I don't regret it. If anything, I'm happy it happened because I got to learn something new from this experience.

So, I started the update process and waited for the downloading of some new packages to complete. I noticed that this time, a new KERNEL was also being downloaded. After the download was over, it was time for YUM to move all the newly downloaded packages to their respective places and clean up the previous versions ( remove them ). During this process, my Laptop froze. It was the worst thing that could happen. I had no choice but to perform a manually shutdown. When I restarted my Laptop, all I could see was this:


This was devastating for me. Let me make this clear that this hadn't happened to me for the first time. I handled it before. But I was so worried because of two reasons :

  1. The repair required a Windows / any Linux Ditro CD.
  2. I had 2½ days of FREE Wifi left with me and i didn't have an OS on my laptop.
To understand how this happened, we will have to dig deep inside the Boot Sector. So, what is a boot sector?

    - According to wiki, a boot sector is a region of a hard disk, that contains machine code to be loaded into random-access memory (RAM) by a computer system's built-in firmware. The purpose of a boot sector is to allow the boot process of a computer to load a program stored on the same storage device.

The BIOS selects a boot device ( the internal hard disk ), then copies the first sector from the device into physical memory at memory address 0x7C00. There are two important Boot Sectors :
  1. A Master Boot Record (MBR) is the first sector of a data storage device that has been partitioned. The MBR sector may contain code to locate the active partition and invoke its Volume Boot Record.
  2.  A Volume Boot Record (VBR) is the first sector of a data storage device that contains code to load and invoke an operating system installed on that device or within a particular partition.
These are just the definitions. This is something I read on https://www.serverfault.com :
    • As I understand it, MBR = First sector on the entire drive and Boot Sector is the first sector in a partition. Your BIOS reads the MBR first then gets pointed to the Boot Sector of the system partition which controls the loading of that partion or will present you with a menu so you can choose another partition to boot off.
Let me tell you how the boot process works step by step using an example.

Scenario I

Say, I have a laptop with no OS currently installed on it. So when I install OS-1, OS-1 modifies the MBR to point to VBR-1. So, now, when I boot my laptop, the MBR is loaded first, it points to VBR-1 which points to OS-1.

If the OS-1 is Windows, the VBR-1 directly boots into Windows.

But if the OS-1 is a Linux Distribution, VBR-1 will show a menu; the Grub Menu. Grub stands for GRand Unified Bootloader. This GRUB menu contains options to either boot into a different OS or boot into the Linux Distribution using a specific KERNEL. Since, in this case, we have only one OS, so no other OSes would be seen on the menu.

Scenario II

Now, let's install another OS alongside OS-1, called OS-2. When OS-2 is installed, VBR-2 is created and the MBR is modified to point to VBR-2. So, now, when I boot my laptop, the MBR is loaded first, it points to VBR-2. But VBR-2 does not, or at least it shouldn't, point to OS-2. VBR-2 should present a menu to chose between OS-1 and OS-2.

Considering OS-1 is Windows and OS-2 is a Linux Distribution, VBR-2 will show the GRUB menu which will give an option for booting into both the OSes. This happens because, during the installation process of Linux, GRUB menu is generated and the Hard Disk is searched for other OSes. If an OS is found, a corresponding entry is created in the GRUB menu.

Considering OS-1 is a Linux Distribution and OS-2 is Windows, VBR-2 will directly boot into Windows without showing an option for Linux. A boot menu can be created at VBR-2 to give an option for Windows and Linux using a software "EasyBCD".

Scenario III

Now, Let's consider that two OSes, OS-1 and OS-2, are installed on my laptop. One of them is a Linux Distribution and the other is Windows. I have also created a boot menu for the VBR-Windows so that it shows an option to boot into Linux. And, as we know, Linux-VBR already has a GRUB menu.

I boot my laptop, MBR is loaded first. It points to, say VBR-Linux, and I get options to boot into Linux or Windows. I choose "Windows". Now I see the boot menu, which I created using EasyBCD, asking for which OS to boot - Linux or Windows. I choose "Linux". I am taken back to the GRUB menu. This cycle repeats itself till I choose either Linux from GRUB menu or I choose Windows from the boot menu I created.

What's happening is that I am switching from VBR-Linux to VBR-Windows to VBR-Linux to VBR-Windows and so on.

Let me explain what a GRUB menu contains. A GRUB menu gives an option to choose from 3 kernels, i.e., 2 previous kernels and 1 current kernel. So, as I mentioned above, a new kernel had been downloaded. Therefore, the process of updating the OS also involved editing the GRUB menu, i.e., removing the oldest kernel from the GRUB menu and the Hard Disk, adding a new entry to the GRUB menu and linking it to the new kernel. So, when I manually shut down my laptop, I interrupted the all these processes somehow. And I couldn't see the GRUB menu.

Now that you understand how the boot sector and GRUB menu works, I can proceed with my story. Where was I?? Oh, right! So, I had free Wifi and no working OS on my laptop. I had to get my hands on a Windows or Fedora CD. I went out on M.G. Marg to find it, but all the stores were closed. I had my dinner and came back to my room. Luckily, I had to go to my college the next day to collect my 8th semester mark sheet. So, I contacted a computer science junior from my college and asked him to provide me with a Windows and a Fedora CD when I get there. He's using Ubuntu nowadays, so he didn't have a Fedora CD with him. So, I had no other choice but to repair my Windows and leave Fedora as it was. I had to had a working OS with me. I had so much to download. To repair or overwrite the MBR, I booted my laptop from the Windows CD and opened the Command Line. Two easy lines reapired the MBR for Windows :

  1. bootrec /fixboot
  2. bootrec /fixmbr
Then I restarted my laptop and all was done. And, as  explained above, Windows did overwrite the MBR but there were no options to boot into Linux. So, I used EasyBCD to locate VBR-Linux and show it on the VBR-Windows menu. Now, I was able to boot into Linux and Windows. But there were two tiny problems :

  1. I had to go through 2 menus before booting into Fedora
    • EasyBCD menu
    • GRUB menu
  2. I couldn't boot into Fedora using the latest KERNEL configuration.
 Anyway, I downloaded 10.5GB in 5 days!!!

I came back home and decided to solve my problems. To overwrite the MBR with Fedora, I needed a Fedora CD. I created a bootable Fedora USB and a bootable Windows 8 USB, just in case I couldn't fix the GRUB menu.

I booted into the Live Session using the Fedora USB, opened the terminal and logged in as ROOT. I was trying to follow all the instruction step by step that I found on several websites that showed "How to reinstall GRUB2 in Fedora". But I was having so much trouble. I had a different Boot partition is Fedora which required a few more steps. And, later I found out that "/dev/sda4" was giving me an error because the path that was used was "/dev/fedora_aayush/root". This I got to know when I ran the command "fdisk -l". I'll mention all the steps that I followed to restore my grub:

  1. Mount your / at /mnt/
    • mount /dev/fedora_aayush/root /mnt
  2.  If you have a separate /boot partition, mount /boot to /mnt/boot
    • mount /dev/sda5 /mnt/boot
  3. Bind these
    • mount -o bind /dev /mnt/dev
    • mount -o bind /proc /mnt/proc
    • mount -o bind /sys /mnt/sys
    • mount -t tmpfs /mnt/tmpfs /mnt/tmp
  4. Chroot /mnt
    • chroot /mnt
  5. Reinstall GRUB2
    • grub2-install /dev/sda
Or after step #3,
    • grub2-install --root-directory=/mnt /dev/sda
After GRUB is installed, recreate the GRUB menu :
    • grub2-mkconfig -o /boot/grub2/grub.cfg
NOTE : My / was at /dev/sda4. But when I tried to mount it at /mnt, it gave an error. After hours of searching for the solution, I found this :
    • fdisk -l ("l" as in lion)
This command gave me a path for /dev/sda4 which was /dev/fedora_aayush/root. After all this, I restarted my laptop and was soo happy to have my GRUB menu on top again. I booted into Windows and removed EasyBCD's menu and all was back to normal. Just one more thing was left. I had to remove the latest KERNEL entry from the GRUB menu. This could be easily done by modifying the grub.cfg file. But there's an easier way. "grub-customizer" is a package I always install in any Linux Distribution. I used grub-customizer to edit the GRUB menu entry and now it shows just two KERNELS, a rescue mode and Windows 8 loader (/dev/sda1).

I started to repair my GRUB menu at 12:30AM and I was done by 4:50AM. It took a lot of time but at least I got to learn something.