Sunday, October 17, 2010

Fix Grub after Windows Re-Install

I have a machine with Windows 7 and openSUSE 11.3 and for a few weeks now Windows has been acting up. So I decided to re-install Windows 7 and completely forgot that it replace grub with bootmgr.

Here is a quick HowTo using a Linux Live CD to get grub re-installed.

First of all you'll need a Linux Live CD, I used Parted Magic since I have it on a USB Thumb Drive which boot fairly fast.

Once you've booted into your Live CD of choice you'll need to mount your root filesystem, in my case it was /dev/sda6. Next we will need to bind /proc, /dev, and /sys to the root filesystem where grub resides, to acomplish this we will be using "mount -o bind" which re-mounts a subtree somewhere else so that its contents are available in both places.

mount -o bind /proc /ramdisk/media/sda6/proc/
mount -o bind /dev /ramdisk/media/sda6/dev/
mount -o bind /sys /ramdisk/media/sda6/sys/

Parted Magic mount its drives under /ramdisk/media

Next we need to chroot into the root filesystem in order to run grub. Since we binded /proc, /dev, and /sys the chroot environment should behave as if the we had booted directly into the root filesystem.

chroot /ramdisk/media/sda6 /bin/bash

The second parameter forces chroot to load bash as its shell, if this option is omitted /bin/sh will be loaded.

Now we can start working with grub.

root@PartedMagic:/# grub

GNU GRUB version 0.97 (640K lower / 3072K upper memory)

[ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename. ]

grub> find /boot/grub/menu.lst

grub> root (hd0,5)
Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 17 sectors are embedded.
Running "install /boot/grub/stage1 (hd0) (hd0)1+17 p (hd0,5)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded

grub> quit

Now grub is installed on /dev/sda and all we need to do is a little housekeeping and restart the system.

First we need to exit the chroot environment by typing exit. Next we need to unmount out binds

umount /ramdisk/media/sda6/dev
umount /ramdisk/media/sda6/proc
umount /ramdisk/media/sda6/sys

All done. Restart the system and you should have grub as your boot manager.

No comments:

Post a Comment