You are hereForums / Computers / CentOS server setup and maintenance notes / Oops, I killed the boot sector (again)

Oops, I killed the boot sector (again)


By DaveAtFraud - Posted on 04 March 2010

My undoing was acquiring a 400GB external hard drive for my laptop. It gave me the storage capacity to play around with other distros while supposedly keeping my main distro (currently CentOS 5.4) on the internal hard drive safe and stable. The only problem with this scheme is that some Linux installers act almost like Windoze and re-write the MBR. This has the unpleasant and surprising effect of making my supposedly safe and stable Linux on my hard drive inaccessible.

Sadly, I've become quite good at fixing this although I still manage to fumble around a bit. The trick is to boot from the DVD or first CD of the installation media. At the initial screen enter:

linux rescue

to boot the installer in rescue mode. Answer the various prompts as appropriate but you must select to let the system mount the existing installation under /mnt/sysimage.

The next step is to get back to the original, installed directory structure so enter the following command when you get a shell prompt:

chroot /mnt/sysimage /bin/bash

This makes /mnt/sysimage into / so everything is now where you would expect it to be. In particular, the working grub.conf that was always on the hard drive now appears as /boot/grub/grub.conf.

The next step is to run grub-install to update the master boot record. I like to fully specify critical things rather than rely on defaults so, for me, this looks like:

grub-install --root-directory=/ /dev/hda

That's it. Just hit control-D twice (once to get out of the chroot shell and once more to exit the rescue shell) and let the system reboot. Don't forget to pop out the media while the system is going down or you'll reboot back to the installer.

One other note... You need to boot with install media for the same architecture as the version of Linux that was installed on the hard drive. So, if your installed image is x86_64 then use x86_64 installation media; if the installed image is i386 then use i386 installation media. It might be possible to use an x86_64 installer to fix a 32 bit installation on a 64 bit machine but I'm not going to find out.

Cheers,
Dave