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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

So I wanted to install Backtrack 4.2 on the external hard drive and I have updated the CentOS image on the internal disk to 5.6 since my original posting. As usual, the Backtrack installer overwrote the MBR on the internal hard drive leaving me with only the Backtrack installation on the external drive and Windoze on the internal drive available and then only with the external drive attached.

I found my trusty old CentOS 5.3 first installation disk and proceeded as described above only to find that the executables on the internal hard disk wouldn't run in rescue mode under the CentOS 5.3 kernel. This meant that doing a chroot /mnt/sysimage /bin/bash failed. I tried getting around that by copying /bin/bash from the CentOS 5.3 CD to /mnt/sysimage/bin/bash53. This allowed me to perform the chroot but then grub-install failed.

My next thought was that I didn't really need to chroot so I skipped the chroot and just ran the copy of grub-install on the CentOS 5.3 installation CD. This time the complaint was that /sbin/grub wasn't found. I fixed this by just creating a soft link in /sbin to /usr/sbin/grub (where it resides on the installation CD). I was then able to do a:

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

This appears to have succeeded and I can now boot Cent)S 5.6 from the laptop's internal hard drive.

Cheers,
Dave