Repair/Restore/Reinstall Grub with Ubuntu Live CD

Grub is a Linux bootloader which can boot all conceivable operating systems. Occasionally you will find yourself with a broken grub if you are spending a lot of time in Linux. Specially if you have Windows alongside Linux, re-installing Windows will wipe out grub.

To fix grub with Ubuntu live CD, boot the system from the live CD and open up a terminal.

Step#1. Mount Ubuntu partition

sudo fdisk -l
sudo mount /dev/sdXY /mnt

Replace XY. In my case it is /dev/sda1
If you are not sure where you installed Ubuntu, fdisk or gparted will show all the available partitions in your system. Ubuntu installation should be in a ext4 Linux partition.

Step#2. Mount boot partition(only if you have a separate boot partition)

sudo mount /dev/sdYY /mnt/boot

Replace YY. In my case I do not have a separate boot partition.

Step#3. Mount virtual filesystems(for step 4)

sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys

Using bind mounts, we can mount all, or even part of an already-mounted filesystem to another location, and have the filesystem accessible from both mount points at the same time[1].

Step#4. chroot
Chroot is the process of changing of the apparent disk root directory (and the current running process and its children) to another root directory. When you change root to another directory you cannot access files and commands outside that directory. This directory is called a chroot jail. Changing root is commonly done for system maintenance, such as reinstalling the bootloader or resetting a forgotten password[2].

sudo chroot /mnt

Now we are in the chrooted environment!

Step#5. Install, check, and update grub

grub-install /dev/sdX
grub-install --recheck /dev/sdX
update-grub

Replace sdX with your boot device. ‘grub-install’ command will perform a complete re-installation of grub.

Step#6 Exit chroot and unmount

exit
sudo umount /mnt/dev
sudo umount /mnt/proc
sudo umount /mnt/sys
sudo umount /mnt/boot
sudo umount /mnt

Unmount boot partition only if you followed step 2

Step#6 Reboot
sudo reboot -h now

References:
1. http://docs.1h.com/Bind_mounts
2. https://wiki.archlinux.org/index.php/Change_Root

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s