Monday, 14 June 2010

How-to: Restore Grub2 Bootloader

For my desktop machine, I am ditching 3.5in disk technology in favour of 2.5in 'laptop' drives. The combination of power consumption, size and noise finally consigned them to the recycling. This means bringing some old laptop drives out of retirement.

While you can use GParted to copy partitions from one drive to another, this doesn't bring the Master Boot Record (MBR) or bootloader with it (yes you could to a block-for-block copy with the DD command, but I didn't). I needed to recover a working Grub2 bootloader...
Option One: boot from a Live CD and re-install grub with the "grub-install" command (check the man-pages), something like:
mount /dev/your_boot_partition /boot
grub-install --root-directory /boot /dev/hda
Option Two: boot from a Live CD and re-install grub through the "grub-shell"

Open a Terminal and type in the following commands,
sudo grub
 put you into the grub “prompt”
> root (hd0,0)
 hd0,0 refers to the first hard drive and the first partition on that drive which assumes that's where you want grub installed - the partition where kernel is stored therefore where your /boot/ folder is stored. You can use tab key to show alternatives
    > setup (hd0)
setup(hd0) means where to install grub to MBR, on which disk - puts grub into the MBR on the first IDE disk
    > exit
closes the grub shell so that you can restart.

However...
Some Live CD's, such as Ubuntu Karmic 9.10 do not include grub by default. If you have an active network connection under the Live CD, you can install it from a terminal -
sudo apt-get install grub
Option Three: copy the MBR via backup and restore
Since the MBR Boot Sector is stored on the first 512 Bytes of the physical disk, you could use the DD command to backup your existing MBR from the old disk and 'restore' (copy) to the new one:

From a live CD session, open an terminal and use:
dd if=/dev/hda of=/BootSectorBackup.bin bs=512 count=1
Then copy your original MBR record over whatever junk is on the new disk with the command
dd if=/BootSectorBackup.bin of=/dev/hdb bs=512 count=1
WARNING: get this wrong, you will trash your partition table for the entire disk which will need data recovery and no guarantee of success.

In all three options, reboot without the live CD and your Grub boot menu should be back.

If it does not contain all the entries you expect you may need to boot into a session and from a terminal;
$ sudo update-grub
to update the boot menu list.

Further Info
If this fails at any stage, you may need to use the more complex methods documented at:
Ubuntu wiki - Recover Grub2 via LiveCD
and
htthe Grub2 pages in the Ubuntu Community Help, especially command line and rescue mode.

Not to be confused with: https://help.ubuntu.com/community/GrubHowto which is only valid Grub(1) for Ubuntu 9.04 or earlier.  RC

2 comments:

  1. No no no do NOT use the MBR copy method here, because you WILL trash your partition table.

    Instead,
    # dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1
    # dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1

    Note the 446, this ensures you don't overwrite your partition table. And don't waste 6 hours recopying your data from a failing disk.

    ReplyDelete
  2. Hm... that does look safer than the method I've been using (successfully). Mine is something of a blunt instrument used on disks I can afford to wipe and re-initialise should it go wrong - that's BEFORE I junk the original with the data on it. Always verify your source first and verify your disk copies after. Thanks, I'll give this a try next time. RC

    ReplyDelete

At least try to be nice, it won't kill you...