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.


    Personalise your GRUB Boot Menu with a Custom Image

    If you dual (or triple, or more) boot your machine, i.e. you have many different operating systems you choose between at startup, you might have to use your bootloader fairly frequently.

    GRUB is a very functional boot loader and can do the job in most cases. But its default text-based view is a little visually unattractive, is it not?

    In this tutorial, I'll show you how to put together a custom image which will be the background for the boot menu - so your multi-boot system can be a little more stylish.

    Custom GRUB splash image

    I'll assume the primary operating system where GRUB was installed from is Ubuntu (8.10) for the purposes of this tutorial, but instructions should follow for most Unix-like operating systems. We will be using GIMP to convert the background image.

    Getting Creative (Within Constraints)

    The GRUB background needs to be an image of exactly 640×480 pixels to begin with. In addition, due to the limited environment in which GRUB runs, the image needs to be fairly simple, particularly with respect to the number of colours it uses.

    We need to mix the final file down to an image containing just 14 different colours, so this may affect what kind of design you'd like for your background image. You'll also want to it bear in mind the colours available for the actual GRUB text are also quite limited, along with the fact that the middle of the picture will be taken up by the menu.

    Crafting your image in GIMP

    Once you've got your design done, I recommend you save it in GIMP's native XCF format, in case you want to make edits later.

    Save As and Change to Indexed

    Now, use Save As and make a new copy of the image called splash.xpm. Entering this file extension will automatically tell GIMP to save in the X Pixmap Format, which GRUB requires.

    Save as XPM

    Once you've make this new copy in XPM format, we now need to mix this image down to just the 14 colours that GRUB will let us use.

    Go to Image > Mode > Indexed. In this dialogue, choose Generate optimum palette and set the number of colours to 14. Now click Convert.

    Convert to Indexed Colours

    You will now notice how much your image is affected by this palette change. This is how the image will look in GRUB.

    Once you're happy, save the file (keeping it in the XPM format).

    Copy to GRUB Folder

    Now our image is ready, we need to copy it to the GRUB folder and instruct GRUB to use it as a splash image.

    You'll likely need root privileges to copy the splash image to the right place, so we'll perform this step from Terminal.

    $ sudo cp /wherever/it/is/splash.xpm /boot/grub/splash.xpm

    Copy file to GRUB folder

    Edit the GRUB Config File

    Before we proceed with this final step, be careful. Don't go messing with things in the GRUB configuration file, or you could end up having problems booting your machine. You have been warned.

    You'll need to open up the file /boot/grub/menu.lst in your favourite text editor, with root privileges. For example:

    $ sudo gedit /boot/grub/menu.lst

    Simply add the following line somewhere near the top and save the file:

    splashimage=(hd0,0)/boot/grub/splash.xpm

    Note - this assumes that the primary OS where the GRUB bootloader was installed from is on the first physical hard drive, on the first partition. If your setup is different, you may need to use different numbers. For example, the second physical disk and third partition would be (hd1,2).

    Editing GRUB configuration file

    Once you've saved and rebooted, you should see your new boot screen!

    You may also want to play around with the colours of the menu text to fit in with your new theme.


    Video Tutorial - Use rssh to Create an SFTP-only SSH Account

    Peter Upfold shows you how to use rssh to set up an account on your system that can only be used for SFTP or SCP file transfers and that cannot log in and execute commands at a shell.

    1. 1
    2. 2
    3. 3
    4. ...
    5. Go to