Re-installing the boot loader

In this recipe we will learn how to re-install the CentOS boot loader.

During the installation process it is possible that you may accidentally damage or even remove the GRUB boot loader and thereby render your server inaccessible. Accidents can happen and it is the purpose of this recipe to show you how to get your server back up and running.

Getting ready

To complete this recipe, you will require a standard installation disk of the CentOS 6 operating system. You will also require the use of a computer display, a standard keyboard, and an optical disk drive.

How to do it...

To begin this recipe you should boot your computer from the CentOS installation DVD and wait for the welcome screen to appear.

  1. From the main menu, choose Rescue installed system.
  2. On the Language Selection screen, use the Up and Down arrow keys to highlight the preferred language. When you are ready, press the Return key to proceed.
  3. On the Keyboard Selection screen, use the Up and Down arrow keys to select the preferred keyboard setting and press the Return key to proceed.
  4. On the Rescue Method screen, use the Up and Down arrow keys to select Local/DVD as the source and press the Return key to proceed.
  5. On the Setup Networking screen, use the Left and Right arrow keys to choose NO and press the Return key to proceed.
  6. We have now entered the Rescue screens which includes various confirmation sub-screens. To begin this section, use the Left and Right arrow keys to choose Continue and press the Return key to proceed.
  7. On the first sub-screen, choose OK and press the Return key to proceed.
  8. Again, in the following sub-screen choose OK and press the Return key to proceed.
  9. On the next screen, choose Start shell and by using the Tab key, highlight OK and press the Return key to proceed.
  10. By completing the preceding steps, you will launch a Shell session. You will notice this at the bottom of your display. The current status of the Shell session will read as follows:
    Starting Shell
    bash-4.1#_
  11. At the prompt, type the following instruction before pressing the Return key to complete your request:
    chroot /mnt/sysimage
    
  12. Now type the following instruction before pressing the Return key to complete your request:
    cd /boot/grub
    
  13. We will now use the fdisk command to find the name of all the current partitions. To do this, type the following instruction and then press the Return key to complete your request:
    fdisk –l
    
  14. Look for the * symbol in the fdisk listing under boot, and assuming that your boot disk is on /dev/sda1 (change this as required) type:
    grub-install /dev/sda1
    
  15. If no error is reported the console should respond as follows:
    # this device map was generated by anaconda (hd0) /dev/sda
    
  16. This message has confirmed that GRUB has now been successfully restored. To proceed we must now leave the session by typing:
    exit
    
  17. To complete this recipe, type:
    reboot
    

How it works...

GRUB, otherwise known as the GRand Unified Bootloader, is a boot loader package that provides access to the CentOS system. It is a very important file, but accidents can happen and on occasion it has been known to suffer damage or even be removed.

If this happens it is very possible that your server will not boot and for this reason this recipe serves to invoke a process that is known as Rescue Mode in order to re-install this file.

So what have we learned from this experience?

We started by learning how to access Rescue Mode and configuring the necessary language and keyboard preferences in order that we can interact with the session.

In the following stages, Rescue Mode then enabled us to mount the relevant disk partition with full read/write capability in order that we could target the correct location and re-install GRUB.

Note

Depending on the original configuration and purpose of your server, you may need to review the contents of /boot/grub/grub.conf and make additional entries. This is particularly needed for any multi-boot environment in order that GRUB can be configured to manage additional operating systems.

So in conclusion, we could say that this was a very simple recipe, but it is also a very important recipe; and as damage to GRUB is typically associated with an installation procedure it was felt that such a recipe should remain close at hand. In many respects, it is always a good idea to keep a backup of your current device.map, grub.conf, and menu.lst files but without the benefit of hindsight, and with the assistance of this recipe, a typical disaster may have been averted as the result of following a very straightforward process.

Of course, it may not save you on every occasion and experience may prove that deciding to back up the contents of all the major files in /boot/grub will provide additional insurance, but at this moment, just rest easy, the boot loader has been restored and both you and your server are ready to get back to work.