Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe

    Restoring an Overwritten GRUB Boot Loader

    I like to have lots of choice about which operating system I can boot to. Between my desktop PC’s two hard drives, I have at least three distributions of Linux and several versions of Windows, so I have complete OS flexibility.

    Unfortunately, maintaining a multi-boot configuration like this can be a pain, especially if you later install an operating system which overwrites the GRUB boot loader you had in place (such as a version of Windows). If your boot loader is overwritten, you could be left with no choice but to boot the most recently installed OS.

    In this tutorial, I will show you how to restore an overwritten copy of the GRUB boot loader by using a Linux live CD. In this example, my master GRUB installation is on a Kubuntu 8.10 installation, and I’m using an older Kubuntu 8.04 Live CD I have lying around.

    This tutorial does require you to have some understanding of how your multi-boot system operates, disk partitions and using the command line. If you are not confident, perhaps find a friend who knows Linux more intimately to do this process.

    Before Starting

    It’s worth mentioning that you may need to use a live CD somewhat similar to that of your ‘master’ OS, where your boot loader configuration is stored. This is due to the technique we use to re-run the GRUB installer.

    Also, you need to actually know which system holds the configuration file for your GRUB boot loader and on which partition it is located. If you have a more complicated multi-boot setup, like myself, you probably know this. If you have a more simple Windows-Linux dual boot, there should only be one Linux data partition where it could be.

    Boot the Live CD

    Start the Live CD up as normal. Don’t choose to install the OS if prompted, you want to come to a full live desktop to run the specific commands we need.

    Identify your Partitions

    You need to know on which partition the GRUB config file and associated programs are stored. You may wish to use a graphical program such as Gparted (if available). You’ll want to find out the device string (such as sda5) of the relevant partition.

    GParted screenshot

    (The screenshot above is actually from my triple-boot MacBook, but still shows you how you can identify the (ext3) partition of Ubuntu on that system.)

    If you can’t use a graphical program to work this out, open a Terminal program and use the following command:

    $ sudo fdisk -l

    This will list all of the partitions on all the devices on your system. Under the ‘System’ column, you can see all of the partitions labelled as ‘Linux’. This won’t show you the difference between data and OS partitions, so is less useful in a more complex partition layout.

    Fdisk -l screenshot

    If you can work out where your Linux is from this, note down the information under ‘Device’.

    Mount the Partition

    We now must mount your partition, so that we can access it. Some Live CDs may do this for you, or offer to do so, but here we will perform the process manually.

    We will first make a folder in which the partition is mounted and then do the mounting. Replace the device string /dev/sda5 with the device string that you identified earlier.

    $ sudo mkdir /mnt/system
    $ sudo mount /dev/sda5 /mnt/system

    You should now be able to browse your hard drive by navigating to that folder. The next process we are going to perform is to temporarily change the root directory of our terminal (chroot), so that we can run the GRUB installer directly from the hard drive. It won’t even realise it’s not running from the real system

    On Ubuntu and other sudo-based distros, we must first do this:

    $ sudo -i

    to become root fully (sudo is not enough here).

    Doing the chroot

    The GRUB installer requires to read the devices on disk directly in order to write the GRUB boot record back onto the system properly. It therefore needs a working copy of /dev, inside the mounted directory.

    # mount -o bind /dev /mnt/system/dev

    Next, we can run chroot:

    # chroot /mnt/system

    From this point forward, be very careful. You have root privileges and full write access to your hard drive. The usual caveats apply.

    Run the GRUB Installer

    All we need to do now is to simply run the GRUB installer, which plonks the GRUB boot record back on the hard disk and gives us back all of our choices.

    # grub-install /dev/sda

    If you need to install GRUB elsewhere (such as a different disk or a specific partition), change /dev/sda. In most cases, just leave this as-is.

    GRUB splash

    GRUB should be re-instated on disk. You can now simply close your terminal, reboot the machine safely and everything should be back to normal.


    Command Line Tip - Verify Downloaded Files

    CD image - source http://www.sxc.hu/photo/1015749

    Ever wondered what that MD5sum and SHA1sum things are when you are downloading ISO images? Whether it's a Linux distro, or any other file, you might have seen these 'checksums' floating around.

    Their purpose is to allow you to verify that you have a complete and uncorrupted copy of a file. If you can generate the same checksum with your copy of the file, then the file must be a true copy.

    So, how do you verify these checksums?


    For Windows users

    Windows users - you need to first download both md5sum.exe and sha1sum.exe from the CentOS dostools page. Once they've downloaded, copy them to C:\WINDOWS\system32 (so you can use them from the command line without typing the whole path).


    Pretty simple. Open up your command line terminal - usually in Accessories or System Tools on Linux. Windows users - you'll want to do Start > Run > cmd.exe > OK.

    Then simply type either md5sum or sha1sum, followed by the full path to the downloaded file.

    For example, if I want to verify the MD5 of ubuntu-8.04.1-desktop-i386.iso stored in /home/peter/Downloads, I'd do this:

    md5sum /home/peter/Downloads/ubuntu-8.04.1-desktop-i386.iso

    Similarly, to check the SHA1 sum:

    sha1sum /home/peter/Downloads/ubuntu-8.04.1-desktop-i386.iso

    After some calculation, which might take a while depending on file size, you'll get a checksum of your own. Compare this to the one listed on the website you downloaded from.

    If the two checksums match, your copy is complete and true. Burn with confidence!

    Windows users - your paths will look something more like: md5sum "C:\Documents and Settings\Peter\My Documents\Downloads\ubuntu-8.04.1-desktop-i386.iso". It's just the same idea, though!

    [image source]


    Reorder your Boot Menu Manually

    Last week, I covered how to use KGRUBEditor to re-order the boot menu you see at startup, so that dual boot users could move Windows to the top of the list if they would like.

    It is also relatively simple, however, to manually reorder this menu by editing the configuration file of GRUB (that's the boot loader).

    This tutorial will focus on Debian/Ubuntu, however these instructions should be able to be followed on any distribution (just ignore things about the automagic kernel list).

    First of all, a word of warning. Editing configuration files can be a potentially risky business. Make sure you know what you're doing. You should also make a copy of the file /boot/grub/menu.lst and store it somewhere safe (you can restore it with a Live CD later if the worst does happen). If anything does go wrong, see the end of this post.

    Right, let's get started. First of all, we need to open up the configuration file in an editor.

    Assuming you're running GNOME, press Alt-F2 to bring up the Run Application window. Type gksu gedit /boot/grub/menu.lst and click Run. Enter your password when prompted.

    Run Application window

    This will open up a text editor, with the GRUB configuration file ready to edit. So let's assume once again that you want Windows to be the default entry.

    Scroll towards the bottom of the file, you should see a section similar to this:

    # This entry automatically added by the Debian installer for a non-linux OS
    # on /dev/sda1
    title Microsoft Windows XP Home Edition
    root (hd0,0)
    savedefault
    makeactive
    chainloader +1

    Select this whole section and choose Edit > Cut.

    Scroll back up to find a line like this:

    #
    # Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST

    Just after that line, but before BEGIN AUTOMAGIC KERNEL LIST, use Edit > Paste to put the Windows entry above all the Ubuntu entries.

    Save the file and quit the editor. Assuming you did the process correctly, when you reboot, Windows should be at the top of the list and the default boot option. To start Linux, simply press down once and then Enter!

    If you did run into problems, you can restore the functionality of Windows by booting the computer with the Windows XP CD in, choosing to go into the Recovery Console and typing fixmbr at the prompt. Linux can then be fixed by re-running the GRUB install from a Live CD.


    1. 1
    2. 2