Using chroot to install the GRUB2 bootloader

You can use chroot from a live CD or USB to update or install GRUB on a Linux system where, for example, your master boot record has been overwritten by installing windows. This post explains how… If you need to chroot to do something other than fix GRUB you can use the relevant sections of this procedure.

Boot from live CD and access the partition where Linux is installed.

Boot the system from a live CD (you can obtain one from the download section of www.ubuntu.com or www.debian.org, it does not really matter which version or even which Linux distribution as long as it gives you access to a terminal session).  Once booted, on Ubuntu / Debian you can usually open a terminal window using Ctrl-Alt-T or from the Accessories menu or type terminal into the Dash.  I’ve shown all command that need to be run as root proceeded by “sudo” if you have logged in as root or become root using “sudo -i” or “su -” you don’t need to include sudo in each command.

Many modern distros provide the facility to mount the local hard drive automatically using the file manager.  If your system does not automatically mount partitions you’ll need to create a mount point e.g

sudo mkdir /root-partition

You will then need to find the partition containing your linux root partition “sudo fdisk -l” may provide this information.

sudo mount /dev/sda1 /root-partition

From here on I’ll assume your mount point is /root-partition if your distro mounted the drive for you automatically you’ll need to substitute the mount point it created in the following commands.  Alternatively you can create a link to it (this is useful if your system mounts using the very long uuid of the disk. for example if your system mounted the partition as /mount/point/created/automatically create a link to it as follows

sudo ln -s /mount/point/assigned/automatically /root-partition

Prepare for chroot

When you chroot the directory that you specify becomes the new root partition hiding the current one.  To access the devices and processes currently on the system you need to rebind /proc /sys /dev from the existing root partition to the new one this is done using “mount – o bind”

sudo mount -o bind /proc /root-partition/proc
sudo mount -o bind /sys /root-partition/sys
sudo mount -o bind /dev /root-partition/dev
sudo mount -o bind /dev/pts /root-partition/dev/pts

Updating GRUB does not require network connectivity but if you’re using chroot for some other reason you may require it, assuming your your live CD has set up network connectivity you’ll need to the save the resolv.conf and copy the one created on the live cd for DNS to work.

sudo cp /root-partition/etc/resolv.conf /root-partition/etc/resolv.conf-save

sudo  cp /etc/resolv.conf /root-partition/etc/resolv.conf

Now execute the chroot opening a bash session

sudo chroot /root-partition   /bin/bash

Update or install GRUB

Grub is often configured to use a separate partition which is mounted as /boot.  If this is how your system is set up you’ll need to mount this partition (e.g. on the system I’m using to write this tutorial it’s a the fifth partition on the first disk or /dev/sda5, your will almost certainly be different “sudo fdisk -l” may help you indentify it’s usually a small ext2 partition) This is mounted as follows

mount /dev/sda5 /boot

Note you don’t need to use sudo as when you chroot you become root.  Substitute the actual partition that /boot is located on your system.

I’m assuming GRUB was set up correctly before your master boot record was overwritten, if you need to set up grub you should use one of the excellent tutorials out there.  If you’ve installed another linux version on your system you may already have grub setup in the master boot record in which case you can simply run update-grub other wise you’ll need to run grub-install.  Most linux distros provide a GRUB configuration that does a good job of detecting other linux distros and windows.

Before you run update-grub may want to save the current grub.cfg file as follows

cp /boot/grub/grub.cfg /boot/grub/grub.cfg-save

Run update-grub to create the new configuration (this will not change the master boot record however if your system has GRUB installed on the master boot record this will alter the configuration)

update-grub

You can then examine the configuration created by looking at the file /boot/grub/grub.cfg.  Once you are happy that it’s created correctly

grub-install /dev/sda

The master boot record that you need to update is often on the first disk (device name /dev/sda) Note /dev/sda is the entire disk partitions are names from 1 e.g. /dev/sda1)

 Exit from chroot

To exit from chroot it’s the same as exiting from any shell, simply type

exit

or use Ctrl – D

When you reboot, you should now be able to access your linux partition again.

 

df displays UUID rather than the device name

Several distros set up your boot device in grub using the UUID.  Unfortunately this can cause the output from the df command to display the long uuid (e.g. /dev/disk/by-uuid/0ebcf386-83eb-4639-b1ba-be73a7f60efc) rather than the device name (e.g. /dev/sda1)

To change this edit the file /etc/default/grub on Debian use the command

sudo vi /etc/default/grub

find the line which contains

GRUB_DISABLE_LINUX_UUID=true

If it has a # as the first character on the line this character should be deleted then save the file.

Now update the grub using the command

sudo update-grub

After you reboot the df command will display the device name.