Sunday, April 13, 2014

Repairing GRUB2 on OpenSUSE

Hi today I will make a short tutorial on how to repair grub2 when you have broken it due to some reason or the other.

This tutorial assumes that you have booted into SUSE in Live mode either using a bootable USB or live DVD. If you haven’t already booted into a Live version of SUSE please do so now.

I normally go with a reinstall but there is nothing like trying to save your system first with these steps.

First we identify which partition is our root partition.

Normally hard disk partitions follow the naming convention /dev/sdXY where (X is a,b,c depending upon where the disk is mounted in most systems the internal harddisk is /dev/sda and Y corresponds to the partition number). E.g. /dev/sda6 would mean the 6'th partition of the disk device located at /dev/sda.

In order to find out what the root partition of your SUSE install from  is type "lsblk" and look at the output.

>lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 298.1G  0 disk
├─sda1   8:1    0   400M  0 part
├─sda2   8:2    0 149.4G  0 part
├─sda3   8:3    0 108.3G  0 part
├─sda4   8:4    0     1K  0 part
├─sda5   8:5    0     2G  0 part [SWAP]
├─sda6   8:6    0  15.4G  0 part /
└─sda7   8:7    0  22.6G  0 part /home
sr0     11:0    1  1024M  0 rom 

Note this is the output of a working system. What is most important is the SIZE attribute. By looking at the SIZE you probably will be able to guess which partition is your root partition. If you find it hard to identify which is your root partition by size alone you can try mounting each partition and inspecting its contents and then unmounting the partition keep trying this till you identify a partition that is your root partition. Once you know which partition it is unmount it too. (The use of the mount command to mount and unmount the partitions is out of scope of this tutorial please use "man mount" to have a look at the mount commands manual page or look online)

Then follow the steps below. Here sdXY (e.g. /dev/sda6) refers to your root partition of Suse and sdX refers to the hard disk itself (/dev/sda)
  1. su
  2. mount /dev/sdXY /mnt
  3. mount --bind /dev /mnt/dev && mount --bind /dev/pts /mnt/dev/pts && mount --bind /proc /mnt/proc && mount --bind /sys /mnt/sys
  4. chroot /mnt
  5. grub2-install /dev/sdX
  6. grub2-mkconfig -o /boot/grub2/grub.cfg

Let me briefly explain what we are doing above here.
  1. In the 1'st command we are entering into the root shell from the live distribution. 
  2. In the 2'nd command we mount the root partition of the system to /mnt of the live distro. 
  3. In the 3'rd command we bind mount certain folders from the live distribution which will be used later to repair grub. 
  4. In the 4'th command we change the root from the root of the live distribution to the root of the installed distribution. So now we are no longer running as root in the live Suse but as root in the installed Suse OS. 
  5. In the 5'th command we reinstall grub2
  6. in the 6'th we run a command to remake grub2's configuration so that it searches for installed OSes etc. and makes the configuration files 

No comments:

Post a Comment