Monday, July 25, 2011

Creating a Chroot from an Ubuntu Live CD/USB

Chroot is an operation that changes the apparent root directory for the current running process and its children. It has got many uses but most importantly, it can be used to repair a broken, non-bootable install from a Live environment. May the problem be with the boot-loader, non-configured packages etc, Chroot would at least give you an environment from where you can try to repair the broken install.

There are several other advanced uses of Chroot like testing unstable applications, creating an environment with dependency control etc. We are just discussing the simple ones here.

Creating a Chroot

If you've got a non-bootable PC and want to attempt to repair it, boot an Ubuntu Live CD/USB on that PC. Go to a Terminal and find out your '/' partition by running this command:

sudo fdisk -l

You need to figure the partition notation for your '/' drive which looks like 'sda1', 'sda2' etc. You can also start GParted from System > Administration menu or by searching the Dash in Unity, and try to find your partition there.

For creating the Chroot, you need to run these commands one-by-one:

sudo mkdir /mnt/temp
sudo mount /dev/sdXY /mnt/temp
for i in /dev /dev/pts /proc /sys; do sudo mount -B $i /mnt/temp$i; done
sudo cp /etc/resolv.conf /mnt/temp/etc/resolv.conf
sudo chroot /mnt/temp

Where 'sdXY' is your '/' partition.

The 4th command is meant to connect your Chroot environment to the internet but it would obviously only be successful if you've got internet access in the Live environment.

If all the commands were successful, you will be taken to a prompt like this:


Here you can run any commands you want to, and they would be run as if you were running commands from within your broken install. You don't need to add 'sudo' to any command as you are already 'root'.

After you've finished working in the Chroot environment, simply type 'exit' in the Terminal to exit the Chroot. Then un-mount the previously mounted locations by running this command:

for i in /dev/pts /dev /proc /sys; do sudo umount /mnt/temp$i ; done

An Example

While installing upgrades or attempting a version upgrade, laptops are used to run out of battery power if they were not plugged into a power source. In that case, you need to create a Chroot as mentioned above and then from the 'root@ubuntu:/#' prompt, run these commands one-by-one:

apt-get update
apt-get upgrade
apt-get install -f
dpkg --configure -a
apt-get dist-upgrade

If the commands go well, your broken install would be repaired, hopefully.

Related Posts:

System , Troubleshooting