Linux Articles August 12, 2010 0

GRUB 2 Restore after Windows installation

GRUB 2 Restore after Windows (re)installation

Scenario: Multiple OS in one machine – Linux Grub2 Boot Loader doesn’t start after Windows (re) installation. A selfish Windows OS starts automatically by default. It happens when installing Linux and Windows. This can be easily avoided by installing multiple operating systems in the following order; first Windows then Linux.

Example of existing disk partitions:

/sda1 ntfs  Windows 7 (here, Windows installer set up the Boot flag to active)
/sda2 ext3 Ubuntu ( bootloader’s home partition and the Boot flag has been unset)
/sda3 ext3 home
/sda4 ext3 ….
/sda5 ntfs data/whatever

Requirements:
–    A patient – Computer with multiple OS’s and the bootloader broken by the Windows installer.
–    Ubuntu LiveCD (installation CD) or
–    A light GParted Live CD/USB/HD/PXE – gparted-live-xxx.iso ~121.4 MB (recommended)
–    Mind the OS architecture! Don’t run 32 bit live cd against 64 bit operating system.

Solution: GRUB-2 Boot Loader has been restored by applying the following remedy:

  1. Boot the machine using GParted LiveCD – it allows users to start partitioning tool – Gparted.. It ‘s also possible to run Ubuntu Live CD, if this one has been used for Linux Installation since it does have Partitioning tool included – Gnome Partition Editor (see picture below).
  2. Find out Linux boot partition – In this example: sda2
  3. Set up BOOT flag on sda2 partition as it was before and save changes
  4. Restart computer, GRUB menu should be active and will start instead of Win
  5. Start Linux OS, open terminal and issue the following command as a root user: update-grub – This will refresh all OS labels in a Grub menu.

UPDATE

I tried and it did not work against Windows 7 installation! It looks like windows installer don’t care and takes it all, writing its own rubbish into a MBR sector. To fix this OS disaster do the following:

  1. Once again, mind the OS architecture; use 32 bit live cd against 32 bit existing OS installation and 64 bit live cd accordingly.
  2. Start up either gparted or Ubuntu Live cd (recommend) and issue “fdisk -l” command to learn about existing partitions on the hard disk
  3. Identify Linux boot partition and mount the file system into /mnt directory
  4. chroot into mounted FS and reinstall grub on the hard drive
  5. Update Windows 7 entries in grub2 config file and refresh grub configuration.

Run script:

$sudo fdisk -l

  Device Boot  Start         End      Blocks   Id  System
/dev/sda1          1        1244     9990144   83  Linux
/dev/sda2       1244        1306      492545    5  Extended
/dev/sda3       1244        1306      492544   82  Linux swap / Solaris
/dev/sda4 *	8370	   13995    45190845   69  HPFS/NTFS

#mount existing Linux installation file system (sda1):

$sudo mount /dev/sda1 /mnt
$sudo mount --bind /dev /mnt/dev
$sudo mount --bind /proc /mnt/proc

# chroot into the environment:
$sudo chroot /mnt

#reinstall Grub2 :
$ sudo grub-install /dev/sda
Installation finished. No error reported.

 

Reboot computer, grub menu should start now.

In case that Windows 7 menu entry is missing do the follwoing to add windows 7 to the grub2 menu list:

  1. Create Windows config file in /etc/grub.d/ directory
  2. Grand execute rights to created file
  3. Execute grub update script: sudo update-grub

Script:

# create and fill up 11_Windows configuration file

$ sudo vi /etc/grub.d/11_Windows

# next add the following lines:

#! /bin/sh -e
echo "Adding Windows" >&2
cat << EOF
menuentry "Windows 7" {
set root=(hd0,4)
chainloader +1
}
EOF

 

Save and exit :wq vi editor, this will create a new file.

Mind the fact if the Windows was installed on fourth partition (like in example above) we need to give root=(hd0,4).

GRUB2 recognizes partitions in the following way:

First partition (/dev/sda1): root=(hd0,1)
2nd  partition (/dev/sda2): root=(hd0,2), and so on…

# next grand execute permission to a file:
$ sudo chmod a+x /etc/grub.d/11_Windows

# issue the following command to refresh Grub2 configuration:

$ sudo update-grub

Reboot computer.

Cheers!!