Reinstall a corrupted or destroyed boot partition.

Lately I have had a couple of systems where the boot partition got corrupted or it's contents
deleted mistakenly. Even though I could not boot the system, I did not want to reinstall the whole
system and recover the data from backups.
So here is a quick solution that worked both on a SUSE 9.3 and a CentOS 5.4 system.

1. Boot the computer into rescue mode with the relevant CD/DVD by typing "linux rescue" at the boot prompt.

2. Mount the / partition of the system. On CentOS this is automatically done and the / is mounted on /mnt/sysimage.

Otherwise just do (assuming the root partition is on /dev/sda2)

mount /dev/sda2 /mnt/sysimage

3. Chroot into the old root partition since that is where we actually want to make all the changes

chroot /mnt/sysimage

4. If the filesystem labels got deleted up for some reason or they don't agree with what is described in /etc/fstab we should fix that (Assuming the boot partition is /dev/sda1):

e2label /dev/sda1 /boot

5. Make sure the boot partition is mounted:

mount /dev/sda1 /boot

6. Mount the CD-ROM we used to boot the machine

mkdir /media/cdrom
mount -o loop,ro /dev/hdc /media/cdrom

7. Now we need to install the relevant rpms. Make sure you pick the correct kernel for your configuration system (PAE etc..):

cd /media/cdrom/CentOS (this will vary depending on the distro)
rpm -ivh --force kernel-2.6.18-164.el5.i686.rpm
rpm -Uvh --force grub-0.97-13.5.i386.rpm
rpm -Uvh --force redhat-logos-4.9.99-11.el5.centos.noarch.rpm

8. Install GRUB:

grub-install /dev/sda1 (or whichever partition boot is mounted on)

9. If /boot/grub/grub.conf is also lost, you need to create it manually.

vim /boot/grub/grub.conf

title CentOS
        root (hd0,0)
        kernel /vmlinuz-6.18-164.el5 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-164.el5.img

10. Lastly make a soft link to grub.conf:

cd /boot/grub
ln -s grub.conf menu.lst

Reboot the system and if everything goes well it should boot normally. Run immediately a yum upgrade incase you had to install an older version of the kernel from the CD-ROM.