How to restore GRUB2 using an Ubuntu Live CD or Thumb Drive 09/02/2010Posted by muyiscoi in Guides.
Tags: grub, installaton, tips, tutorial
If you are a tinkerer like me, you will no doubt run into some problems with grub at one time or another.
Even though you do not tinker, you might still have a problem with grub especially if dual-boot your system with windows. In that case, the windows boot-loader overwrites GRUB in the Master boot record (MBR) (if you install Ubuntu first) thereby rendering your Linux partition not bootable. It has been a major bottleneck for a lot of Ubuntu users when they can no longer boot into their desktop and most times are forced to reinstall.
It is however, relatively easy to restore GRUB on your computer irrespective of how you lost it in the first place.
The only requirements are that you still have a healthy installation of Ubuntu on your machine and a ready live CD or a USB thumb drive with Ubuntu loaded on it.
I will assume that the readers of this post have some level of knowledge on how to get certain things done on Ubuntu so i will not be too specific in some areas. if anything is unclear, you can ask it in the comments and i will be glad to clarify.
First, you have to boot the live CD. If you are using a thumb drive, also boot from it.
After booting, you have to determine which of your hard-disk partitions is the root (/) partition. You can do this by typing
sudo fdisk -l
in the terminal.
Note: If you only have one partition or you already know the address of your root partition, you can skip this step.
The output of the above command on my computer is as shown below
Disk /dev/sda: 250.1 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xb21e563c Device Boot Start End Blocks Id System /dev/sda1 * 1 15147 121664576 7 HPFS/NTFS /dev/sda2 15148 15427 2249100 82 Linux swap / Solaris /dev/sda3 15428 30401 120277697+ f W95 Ext'd (LBA) /dev/sda5 15428 17251 14649344 83 Linux /dev/sda6 17252 30401 105627343+ 83 Linux
Admittedly, this can be a little confusing and might not really tell you which of your partitions is the root partition. Most of the time, what i do is to open up the file manager (nautilus) and check for the partition that resembles my root partition either as a result of the size or maybe a label (if you’re d kinda person that does that). i will then mount that partition from nautilus. After that, i go to the terminal and type the following
$ cat /etc/mtab
Amongst all the text that will be printed, the one containing the address of your root partition will be there. This is the output from my own system. Note that this will not show up if the partition is not mounted so ensure that only the root partiton is mounted so u dont get confused. alternatively, you can use gparted to also know the partiton address of the root partition.
/dev/sda5 /media/a2455o3f539io43pofp342 ext4 rw,errors=remount-ro 0 0 proc /proc proc rw,noexec,nosuid,nodev 0 0 none /sys sysfs rw,noexec,nosuid,nodev 0 0 none /sys/fs/fuse/connections fusectl rw 0 0 none /sys/kernel/debug debugfs rw 0 0 none /sys/kernel/security securityfs rw 0 0 none /dev devtmpfs rw,mode=0755 0 0 none /dev/pts devpts rw,noexec,nosuid,gid=5,mode=0620 0 0 none /dev/shm tmpfs rw,nosuid,nodev 0 0 none /var/run tmpfs rw,nosuid,mode=0755 0 0 none /var/lock tmpfs rw,noexec,nosuid,nodev 0 0 none /lib/init/rw tmpfs rw,nosuid,mode=0755 0
The line showing my root partition is highlighted in bold on d text above. From this, you can deduce the address of your root partition. I can see that mine is /dev/sda5. Once this has been done, the next step is to mount that partiton in a more easily accessible place. Ubuntu will by default mount the partition according to its UUID (Universally Unique Identifier) in the /media folder as you can see above. However, this makes for a very long file path and makes it very easy to make a mistake.
Go to nautilus again and this time, unmount the previously mounted partition.
Then, use the terminal to mount that partition in the /mnt folder.
In my case, i will use the following command
$ sudo mount /dev/sda5 /mnt
Replace /dev/sda5 with a appropriate address for your root partition gotten above.
after this, it is time to install grub2. To do this, use the following command
$ sudo grub-install --root-directory=/mnt /dev/sda
This command should not be edited if you followed all the steps above. this installs grub2 in the MBR of the Harddisk. This will take a short while after which a confirmatory message will come up.
That is it. To confirm your settings, type in
$ sudo update-grub
in the terminal. However, even if this gives an error message, most of the time, the installation is already done. If you receive an error message though, you might not see other operating systems on your Computer especially if they were installed after the Ubuntu installation. To remedy this, boot into your freshly repaired Ubuntu and run the above command from there. That should sort it out.
So, that’s it. I hope this helps someone out there get out of a bind. Any further questions can be posted on the comments.
Source: Ubuntu Wiki