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.


    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.


    Dual Boot Ubuntu with Windows Vista/XP (with Printable Guide)

    It's back to basics for this hybrid printable guide and standard FOSSwire post, detailing how to set up a dual boot installation with Ubuntu 8.04 co-existing alongside Windows Vista or XP.

    Click here to download a fully printable PDF with all the screenshots, or just keep reading below!

    You can also grab the source OpenDocument Text, for translations and derivative works.


    Introduction

    Ubuntu 'Circle of Friends' logo

    So, you've heard a bit about this Linux thing and you're interested enough to give it a try. The problem is, installing a Linux distribution can be difficult and you're not ready to give up Windows just yet.

    Dual booting is the process of installing two operating systems alongside each other. When your computer starts up, you can choose which operating system you want to start for that session. To switch, you just reboot the machine and select the other option.

    In this extended guide, I am going to walk you through the process of installing Ubuntu Hardy Heron (8.04) alongside Windows Vista or Windows XP.

    Assumptions

    For the purposes of this guide, I'm going to assume the following about your system:

    • You have a PC currently running either Windows Vista or Windows XP.
    • Windows is installed in a single-partition layout on a single disk (just one hard drive in Computer, labelled C).
    • You have downloaded Ubuntu 8.04's Desktop CD, and have burned it to a blank CD.
    • Your PC is configured to boot from a CD.
    • You have at least 8 GB or so of free space on your C drive.

    Preparation

    While it might be possible to get started straight away, it's usually sensible to spent a little time setting things up first.

    Backup

    The process of dual booting with Ubuntu is pretty safe. I have personally done this many times with no ill effects.

    However, any major operation you do on your computer is subject to the risk of things going wrong. Before you install Ubuntu, you should make a full backup of anything you don't want to lose and have a strategy for putting things right if things do go awry.

    Storage is cheap.

    Once you have a copy of all the data you care about (and I mean all of it), we can move on. Also, make sure you fully know and understand how you would go about doing a restore should the worst happen.

    Preparation within Windows

    Boot your computer into Windows and go into Computer (My Computer under XP). Right-click on your C drive and choose Properties. Click on the Tools tab.

    Windows C Drive Properties

    There are two things we are interested in here - the Error-checking and Defragmentation sections.

    Error-checking

    First of all, let's do the error-checking. Performing this step will make sure that your Windows disk is free from problems that might cause the Ubuntu installer issues.

    Under the Error-checking header, click Check Now. Vista users will be asked to click Continue.
    Check Disk GUI

    Now, make sure 'Automatically fix file system errors' is selected, and click Start. You'll get a message saying that the disk check cannot be run while Windows is using it.

    Schedule Disk Check

    Choose the option to Schedule the check, and reboot the machine. As the machine comes back up, the disk will be checked and any errors fixed.

    Defragmentation

    Quick advance warning - this step is likely to take a lot of time. I won't go into technical details on what this entails and why it takes so long, you can read that at Wikipedia.

    Head back to Computer, right-click again on the C drive and choose Properties. Now click the Tools tab; you should be back on the screen we were at earlier.

    Windows C Drive Properties

    This time, choose Defragment Now.

    A new window will open. In here, click Defragment Now (or just Defragment under Windows XP). As I've said, this step could take a long time - probably hours, especially if you've never defragged before.

    [caption id="attachment_594" align="alignnone" width="500" caption="Disk Defragmenter under Windows Vista"]Disk Defragmenter under Windows Vista[/caption]

    [caption id="attachment_595" align="alignnone" width="500" caption="Disk Defragmenter under Windows XP"]Disk Defragmenter under Windows XP[/caption]

    If you have enough free space on your hard drive, this step isn't strictly necessary, especially if you've recently done it or have literally just installed Windows afresh. Nevertheless, I'd recommend you go through with it.

    Booting into Ubuntu

    That's it for Windows now. We are now ready to get into Ubuntu and install.

    Put the Ubuntu CD in your computer and restart it. When your computer comes back up, you should see a menu with a lot of languages - you'll want to pick the one you prefer for the install. Use the arrow keys and press Enter.

    Then you will see this menu.

    Ubuntu Boot Menu

    Use the arrow keys to scroll to Install Ubuntu and press Enter. Now you'll have to wait a while, as Ubuntu needs to load from the disc and show you the installer program.

    Installation

    Ubuntu Installer Language Select

    Welcome to the Ubuntu installer program. First of, you'll need to pick your language again to the left and click Forward.

    Ubuntu Installer Timezone Select

    Next, either use the map to click on a point close to you (this is to select the right time zone), or use the list box to select the closest city to you. Again, click Forward.

    Ubuntu Installer Keyboard Layout Select

    Now, the equally thrilling step of choosing a keyboard layout. You can use the text box to try typing any unique characters your keyboard may have (such as £ here on a UK keyboard) to make sure the correct keyboard layout is selected.

    After a brief pause to start up the Partitioner, we return.

    Ubuntu Installer Partitioner

    This stage of the installation is a little more critical. Make sure you choose the right option here, or it's possible that you could accidentally wipe out Windows.

    Provided that your Windows installation has been detected successfully, the first option should start with something like 'Guided - resize'.

    Choose this option. You can now drag the slider between the two sections below to choose how much space Windows will get, and how much Ubuntu will get.

    Let me explain further. Windows and Ubuntu can't coexist in the same space on the same drive, due to the fact that they work completely differently. What we are doing here is resizing your Windows C drive, to make a separate space, or partition, for Ubuntu to exist on.

    After you've selected how much to resize, you won't easily be able to change this or reclaim space back. Leave enough space so that you can do whatever you need to do with Windows, but also make sure there's enough space on the other side for Ubuntu. What you choose here, though, is up to you, and depends on how much you want to use Ubuntu vs Windows.

    Once you've made your selection in terms of resizing, click Forward.

    Ubuntu Installer Partition Continue

    You will be given a warning about writing your changes to disk. Once you click Continue on that screen, your Windows C drive will be permanently resized and Ubuntu's partition will be set up.

    Ubuntu Installer Applying Partitions

    Relax now - the hard part is already done!

    Ubuntu Installer User Settings

    Now, you need to enter the details for your Ubuntu user. This page should be pretty self-explanatory, with fields for your real name, username to log in with, password and the name of the computer you're installing on. As always, click Forward.

    The next stage allows you to import some of your information from Windows, if it can be extracted. Use the tickboxes to choose which user you want to import data from and the data to import.

    Or, if you prefer, don't tick anything to start afresh, and keep your Windows stuff only in one place - Windows.

    Ubuntu Installer Configuration

    This is it - you'll get a final review of your settings before the Install gets underway. Click Install and now let Ubuntu do its work!

    Ubuntu Installer progress

    Once the install finishes, click Restart Now to restart your PC.

    Ubuntu Installer restart

    The Boot Menu

    Your dual boot system is now installed and working. Windows and Ubuntu co-exist peacefully on the same machine and you can switch between them at start up.

    Let's take a quick look at how that switching happens.

    As your computer restarts, watch and wait until you see a menu like this:

    GRUB menu

    Each entry here represents a choice you can make to boot your computer with. By default, the top Ubuntu entry is the one you want to use (ignore the other recovery mode options).

    If you don't use the arrow keys and press enter to make a choice within a few seconds, Ubuntu gets launched automatically. Whenever you want to start Windows, press the down key until Windows is highlighted, then press Enter.

    Quick note - the first time you relaunch Windows after resizing it, it will want to do another disk check. Don't worry, this is normal - so just let it complete the check!

    And that's it. Your Ubuntu/Windows dual boot setup is complete!

    The following licence covers this post and the accompanying PDF and ODT versions.

    Creative Commons License

    Dual Boot Ubuntu with Windows Vista/XP by Peter Upfold is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

    Based on a work at fosswire.com.


    1. 1
    2. 2