Home \ Tutorials \ Tips & Trick \ Recovering Ubuntu After Ins...

Recovering Ubuntu After Installing Windows

Previewhow to restore or recover from a boot-loader problem. Ubuntu uses the Grub bootloader. Some common reasons for repairing your bootloader are installing Microsoft Windows, adding or removing a hard drive, or even changing hard drive settings.

 

Author: Marwan Nafea

I am designer and developer of Saudi Arabia. In 1995 and entered the competition at the level of the Arab world, and obtained the first place, thanks to God first and foremost.

Using the Ubuntu Desktop/Live CD

Quick Start

This option will use the Desktop/Live CD to install Grub into your MBR (Master Boot Record). This option will overwrite your Windows Boot Loader. It is OK to do this, in fact that is the goal of this how to (in order to boot Ubuntu)

  1. Boot the Desktop/Live CD. (Use Ubuntu 8.04 or later) - Download from her
  2. Open a terminal (Applications -> Accessories -> Terminal)
  3. Start grub as root with the following command :
    sudo grub
  4. You will get a grub prompt (see below) which we will use to find the root partition and install grub to the MBR (hd0)
             [ Minimal BASH-like line editing is supported.   For
             the   first   word,  TAB  lists  possible  command
             completions.  Anywhere else TAB lists the possible
             completions of a device/filename. ]
    
    grub>
    Type the following and press enter:
    find /boot/grub/stage1
    If you get "Error 15: File not found", try the following:
    find /grub/stage1
    Using this information, set the root device (fill in X,Y with whatever the find command returned):
    grub> root (hdX,Y)
    Install Grub:
    grub> setup (hd0)
    (In case you want to install GRUB to another drive (like hdX) use the above command as 'setup (hdX)' and then continue)
    Exit Grub:
    grub> quit
  5. Reboot (to hard drive). Grub should be installed and both Ubuntu and Windows should have been automatically detected.
  6. If, after installing grub, Windows will not boot you may need to edit /boot/grub/menu.lst (That is a small "L" and not the number 1 in menu.lst)
    Open a terminal and enter :
     gksu gedit /boot/grub/menu.lst
    Or, in Kubuntu:
     kdesu kate /boot/grub/menu.lst
    Your Windows stanza should look something like this :
     title Windows XP/Vista # You can use any title you wish, this will appear on your grub boot menu
     rootnoverify (hd0,0) #(hd0,0) will be most common, you may need to adjust accordingly
     makeactive
     chainloader +1
    Note: Put your Windows stanza before or after AUTOMAGIC KERNEL LIST in the menu.lst

Overwriting the Windows bootloader

Boot from a Live CD and open a terminal. You'll need to run a few commands as root so you can use sudo -i to get a root shell and run them normally instead of using sudo on each of them. Be extra careful when running a root shell, especially for typos !

We'll need to find which partition your Ubuntu system is installed on. Type the command fdisk -l . It will output a list of all your partitions, for example :

fdisk -l

Disk /dev/hda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1           8       64228+  83  Linux
/dev/hda2               9        1224     9767520   83  Linux
/dev/hda3   *        1225        2440     9767520   a5  FreeBSD
/dev/hda4            2441       14593    97618972+   5  Extended
/dev/hda5           14532       14593      498015   82  Linux swap / Solaris
/dev/hda6            2441       14530    97112862   83  Linux

Partition table entries are not in disk order 

Here I have three Linux partitions. /dev/hda2 is my root partition, /dev/hda1 is my /boot partition and /dev/hda6 is my /home partitions. If you only have one, obviously this is the one your Ubuntu system is installed on. If you have more than one and you don't know which one your Ubuntu is installed on, we'll look for it later. First, create a mountpoint for your partition, for example :

mkdir /mnt/root

Then mount your partition in it. If you don't know which one it is, then mount any of them, we'll se if it's the correct one.

mount -t ext3 /dev/hda2 /mnt/root

Of course, replace /dev/hda2 with the correct name of your partition. You can check if it's the correct one by running ls /mnt/root, which should output something like this :

bin    dev      home        lib    mnt   root     srv  usr
boot   etc      initrd      lib64  opt   sbin     sys  var
cdrom  initrd.img  media  proc  selinux  tmp  vmlinuz

If what you have looks not at all like this, you didn't mount the correct partition. Do umount /mnt/root to unmount it and try another one. You also need to mount your /boot partition if you made one, like this :

mount -t ext3 /dev/hda1 /mnt/root/boot

To make sure it was the correct one, run ls /mnt/root/boot, which sould output something like this :

config-2.6.18-3-686      initrd.img-2.6.18-3-686.bak  System.map-2.6.18-3-686
grub                     lost+found                   vmlinuz-2.6.18-3-686
initrd.img-2.6.18-3-686  memtest86+.bin

Once again, if what you have doesn't fit, unmount it and try another partition.

Now that everything is mounted, we just need to reinstall GRUB :

sudo grub-install --root-directory=/mnt/root /dev/hda

If you got BIOS warnings try:

sudo grub-install --root-directory=/mnt/root /dev/hda --recheck

Of course, replace /dev/hda with the location you want to install GRUB on. If all went well, you should see something like this :

Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0)   /dev/hda

Now you can reboot and the GRUB menu should appear. If you see a warning message regarding XFS filesystem, you can ignore it.

Preserving Windows Bootloader

This method allows you to restore GRUB and keep the Windows bootloader as your primary bootloader. Thanks to Ubuntu's support for NTFS writing it's now an absolute cinch.

The method shown above puts GRUB back on the MBR (master boot record) of the hard drive instead of in the root partition. But you probably won't want that, if you use a third-party boot manager like Boot Magic or System Commander. (The original poster also suggested that this would be useful to restore the Grub menu after a re-ghosting.) In that case, use this alternative.

If you have your Linux system in a second (or third...) hard disk this method will not work. Please check Super Grub Disk's method that address this problem.

Restoring GRUB

  1. Boot from a Live CD, like Ubuntu Live, Knoppix, Mepis, or similar. Ideally use Ubuntu 8.04 or higher as this has NTFS write support and makes life a bit easier; this isn't necessary, just handy.
  2. Open a Terminal. Open a root terminal (that is, type "su" in a non-Ubuntu distro, or "sudo -i" in Ubuntu). Enter root passwords as necessary.
  3. Type "grub" which makes a GRUB prompt appear.
  4. Type "find /boot/grub/stage1". You'll get a response like "(hd0)" or in my case "(hd0,3)". Use whatever your computer spits out for the following lines. Note that you should have mounted the partition which has your Linux system before typing this command. (e.g. In Knoppix Live CD partitions are shown on the desktop but they're not mounted until you double-click on them or mount them manually)
  5. Type "root (hd0,3)" note the space between root and (hd0,3).
  6. Type "setup (hd0,3)". This is key. Other instructions say to use "(hd0)", and that's fine if you want to write GRUB to the MBR. If you want to write it to your linux root partition, then you want the number after the comma, such as "(hd0,3)".
  7. Type "quit".
  8. At this stage you can either restart the system and install your own bootloader, or you can continue and tell the Windows bootloader where to find GRUB which will handle booting Linux.

Making Windows Load GRUB (and then Linux)

This is taken from Dual-Boot Linux and Windows 2000/Windows XP with GRUB HOWTO which has been helping people dual boot since 2005, or maybe before.

  1. 1. In Linux open a command window.
  2. Mount a drive which you can share with Windows; this could be a USB drive, a FAT32 partition on your machine, or if you are using a Linux distro which supports NTFS writing like Ubuntu 8.04+ (maybe earlier?) then you can mount the actual Windows c:\ drive itself! The big bonus of writing to the windows drive is that you are going to need to put a file there, so it saves copying a file around. For example
    #mkdir /tmp/windows
    #mount /dev/sda1 /tmp/windows
  3. Now you are going to make a copy of your boot partition; finding what this is called is not 100% deterministic because Linux might call your drives sdX or hdX, whereas GRUB always calls them hdX and Linux names them [hd|sd][Letter][Number] whereas GRUB names them as hd[Number][Number]; note that df won't work as you are booted from a live CD. But let's have a go; if you installed GRUB on (hd0,0), then /boot is on hda1 or sda1, (hd1,0) == hdb1 or sdb1, (hd0,1) == hda2 or sda2, etc... this narrows you down to two possibliities, now issuing ls /dev |grep hd will let you know if you have that drive on your machine, if nothing comes up which matchs, then you must have an sd drive. Ok great...
  4. Having hopefully determined your boot partition issue the command (as root) #dd if=/dev/sda2 of=/tmp/windows/linux.bin bs=512 count=1

    Where /dev/sda2 is your boot partition and /tmp/windows/ is the drive you want to copy the boot sector image to.

  5. If you haven't created linux.bin in the Windows drive then you need to copy it there now.
  6. Reboot into Windows, open c:\boot.ini in notepad and on a new line at the botton add c:\linux.bin="Linux". This file might be write protected, if it is enable write in the right click context menu -> properties; you should disable write afterwards. Make sure that at the top of the boot.ini file there is a timeout set, i.e timeout=5. (Ah ha, you say, now I have NTFS support, why don't I just edit boot.ini in Linux, the answer to this is that Linux and Windows represent line breaks in different ways, so even though you can edit the file, it won't add a new line 残念)
  7. That's it, reboot and you will be given the option of booting into Linux, selecting that will chainload GRUB and this will let you boot into your Linux distro.

From: http://ubuntuforums.org/showpost.php?p=121355&postcount=5