Sunday, May 4, 2008

Restoring the GRUB bootloader

How to restore GRUB after a Windows installation or afte you've deleted the MBR

The first thing to do is to get a live distribution, I suggest Knoppix even if every distro is okay.

First let's make sure in the BIOS that the boot sequence on our PC is set to have as the first entry the boot from cd/dvd.

Let's boot our machine, and we will get KDE in few time, let's open a shell (or the root shell with the superuser privileges) and then go on.

First we have to know on which disk the MBR is installed (nowadays is common to have two or more hard disks), for this reason is useful to know (if you don't know it, because the boot sector is usually installed by default by the distros in the first 512 bytes of the primary master) the output of the fdisk command:

$ fdisk -l  


Disk /dev/hda: 120.0 GB, 120000000000 bytes
255 heads, 63 sectors/track, 14589 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 3115 25021206 7 HPFS/NTFS
/dev/hda2 3116 14589 92164905 f W95 Ext'd (LBA)
/dev/hda5 3116 14589 92164873+ b W95 FAT32

Disk /dev/hdb: 163.9 GB, 163928604672 bytes
255 heads, 63 sectors/track, 19929 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hdb1 * 1 2432 19535008+ 83 Linux
/dev/hdb2 2433 19929 140544652+ 5 Extended
/dev/hdb5 2433 2627 1566306 82 Linux swap / Solaris
/dev/hdb6 2628 19929 138978283+ 83 Linux


How you can see my MBR is on /dev/hda1

Now let's create the mount directory for the root partition

$ mkdir /mnt/hda1 


and make the partition visible by mounting it:

$ mount /dev/hda1 /mnt/hda1

If besides the root partition our system contains a /boot separated one we have to mount also this latter, in the same way. Create a folder

$ mkdir /mnt/hda1/boot

executing the mounting with

$ mount /dev/hda2 /mnt/hda1/boot

Now make our partition become the current root partition

$ chroot /mnt/hda1


Now we only have to execute

$ grub-install /dev/hda

exit from the chrooted environment and reboot the system

$ shutdown -r now

we will get the usual menu.

For those who use LILO it's the same: it's enough to replace grub-install with the lilo command without options

$ lilo /dev/hda

For now it's all Smile


Howto see MBR from commandline:

root@debian:~# fdisk -l


Disk /dev/hda: 40.0 GB, 40016019456 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 4775 38355156 83 Linux
/dev/hda2 4776 4865 722925 5 Extended
/dev/hda5 4776 4865 722893+ 82 Linux swap / Solaris


root@debian:~# dd if=/dev/hda of=MBR-backup.txt bs=512 count=1

1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000109 seconds, 4.7 MB/s


root@debian:~# ls

MBR-backup.txt net.sh backup.tax.gz

root@debian:~# cat MBR-backup.txt

OR

Open with any IDE. For example,

root@debian:~# emacs MBR-backup.txt

Th content will look as mentioned below:


�H\220^P\216м^@��^@^@\216�\216���^@|�^@^F�^@^B���!^F^@^@��^G8^Du^K\203�^P\201��^Gu��^V�^B�^A�^@|�\200\212t^C^B�^@^@ ^A^@^@\
^@^@^B�\220\220��\200u^B�\200�Y|^@^@1�\216�\216м^@ � @|<�t^B\210�R�^?}�4^A��\200tT�A��U�^SZRrI\201�U�uC A|\204�u^E\203�^At\ 7f\213L^P�^E|�D�^Af\213^^D|�^D^P^@�D^B^A^@f\211\^H�D^F^@pf1�\211D^Df\211D^L�B�^Sr^E�^@p�}�^H�^Ss ��\200^O\204�^@�\215^@�^E|�D�^@f1�\210�@f\211D^D1�\210���^B\210�\210�@\211D^H1�\210���^Bf\211^Df�D|f1�f�4\210T f1�f�t^D\210T^K\211D^L;D^H}<\212T^M��^F\212L ��^H�\212l^LZ\212t^K�^@p\216�1۸^A^B�^Sr*\214�\216^FH|`^^�^@^A\216�1�1����^_a�&B|�\205}�@^@�^N�\212}�8^@�^F�\224}�0^@�\231}\ �*^@��GRUB ^@Geom^@Hard Disk^@Read^@ Error^@�^A^@�^N�^P�<^@u��^@^@^@^@^@^@^@^@^@^@^@�^M�^M^@^@\200^A^A^@\203���?^@^@^@�\201\'^D^@���^E����\201'^D�^O^V^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@U�

Cheers
Smile.

2 comments:

Anonymous said...

Very good blog, Rakesh. I've discovered it late month and I read it frecuently.

I have a doubt. When you say "How you can see my MBR is on /dev/hda1", you suppose that I see it, but I don't.

Why the MBR is on hda1? The *?

Thanks.

Rakesh said...

Thanks for appreciation!

You can see MBR detail through commandline . I have added 'Howto see MBR' at end of the blog.

Cheers,
Rakesh