Thursday 9 February 2012

Reinstall grub after it is overwritten.

If you have linux on a hard drive and for some reason you have overwritten grub (e.g. installed windows on a partition of the same hard drive) you will notice that grub doesn't appear at all. I had a bit of trouble re-installing legacy grub after I had to do a quick format on a windows partition, so I looked a bit into it. Following the next steps will work on most cases.

  1. Burn any linux distro that comes with a live boot option on a flash drive/cd and boot.
  2. Run fdisk -l and find  which is your root partition, which is your boot partition and which hard drive you're using (e.g. /dev/sda1)
  3. If you still have linux installed on your system, make a temp directory (e.g. /mnt/root ), and mount your root ( / ) partition on it, then mount your boot ( /boot ) partition on /mnt/root/boot (assuming you mounted the correct root partition, /mnt/root/boot will exist).
  4. mount /mnt/root/dev and /mnt/root/proc (explained later)

  5. chroot to /mnt/root

  6. reinstall grub to /dev/sdX NOT /dev/sdX1 or any other number. Grub should install on the physical drive, not on partitions.

Here's the code I use for it: (if you're not root, you will need to put 'sudo' before each command).

fdisk -l
mkdir /mnt/root
mount /dev/sda2 /mnt/root
mount /dev/sda1 /mnt/root/boot
mount -t proc none /mnt/root/proc
mount -o bind /dev /mnt/root/dev
chroot /mnt/root
grub-install --recheck /dev/sda

After that reboot and you should see grub (remove the live medium!)

No comments:

Post a Comment