Deprecated Behaviour

The inane, sometimes insane, ramblings from the mind of Brenton Alker.

Wiping MBR and Reinstalling Grub

If you have ever mistakenly installed grub (or any other boot manager) onto the MBR of the wrong drive (usually during setting up additional multi-boot OSs), you’ll know it can be a pain to get rid of. Most sources out there are for when you accidentally override the windows boot manager with an common *nix one (grub or lilo). This involves using the windows “repair” console on the CD. When you are running a pure linux system, this isn’t a useful option. This is the solution I found to wipe and re-install grub:

This will probably need to be done from a live CD, as if your MBR is broken, you probably can’t boot your installed system.

1. Wipe the MBR

The first 446 bytes of the drive make up the Master Boot Record, the next 66 (totalling 512) is partition information, you probably don’t want to wipe that unless you intend of re-partitioning (and/or formatting) your disk anyway.

Use dd to “zero” those 446 bytes:

1
dd if=/dev/zero of=/dev/hd? ds=446 count=1

2. Chroot into your installed system

Set up and chroot into your main OS

1
2
3
4
5
mkdir -p /mnt/root
mount /dev/hd? /mnt/root
mount -t proc none /mnt/root/proc
mount -o bind /dev /mnt/root/dev
chroot /mnt/root /bin/bash

3. Reinstall grub in the correct MBR

Start the grub console

1
grub

then, at the grub prompt:

1
2
3
find /grub/stage1
root (hd?,?)
setup (hd?)

If you “boot” is not on a seperate partition, you the first line might need to be

1
find /boot/grub/stage1

Assuming your configuration is correct (i.e doesn’t need to be changed since before it broke) you should now be able to boot into your installed system again.