Friday, 1 January 2010

Rebuilding Grub2 grub.cfg from Ubuntu Live CD

After installing Windows 7 the other day I lost my Grub install. Not a problem, in the past I've used my Super Grub Disc to repair it.

This didn't work for me though (may be my disc is a bit dated, or maybe it doesn't support grub 2 just yet). Instead I booted up using my Ubuntu 9.10 Live CD. I could easily re-install grub using the "grub-install" command.

  1. Open up a terminal
  2. sudo apt-get install grub-pc (install grub2 incase the live version doesn't have it)
  3. sudo fdisk -l (Find the partition you want)
  4. sudo mount /dev/sda5 /mnt (Mount the partition)
  5. sudo grub-install --root-directory=/mnt /dev/sda

I've used the drive /dev/sda and partition /dev/sda5 in my example. Notice the exact spelling.

This all went ok, but I was left with a screwed up grub menu (I messed it up myself). There's a "update-grub" command you can do from within Ubuntu, but I couldn't actually boot in. I only had the Live CD. Hence updating it proved to be quite difficult, and there's nothing on the Live CD to help. I eventually found a solution, these are the commands I had to run in order to regenerate it (you can copy and paste the whole lot to run them all at once).

sudo mkdir /mnt &&
sudo mount /dev/sda5 /mnt &&
sudo mount -o bind /dev /mnt/dev &&
sudo mount -o bind /proc /mnt/proc &&
sudo mount -o bind /sys /mnt/sys &&
sudo chroot /mnt bash &&
sudo update-grub &&
reboot;

Without binding the /dev and /proc it would throw an error.

All credit goes to seeker5528's post for the above solution.

If you simply want to update grub2, and you are able to boot into linux, then you can run the command:

sudo update-grub

It will rebuild your grub config, finding all the available bootable partitions.