Linux inside: Basic chroot

konsole

In the tutorials to install Kubuntu on a Macbook Air we created what are known as chroot environments. As you will soon discover chroot environments can be used for more than fixing the grub.

What is a chroot environment?

The root directory of your system is the top directory. On Linux the directory is self-explanatorily set to

/

creating a chroot environment is merely the act of changing the apparent root directory of a process, hence Change root. This creates a sandboxed environment, your chroot session can’t access your regular root directory and therefore it can’t access your home folder either. To create one just make sure the partition or the folder containing the directory you want to use as root is mounted and then just run:

chroot /desired/root/directory /bin/bash

However, making a fully functional chroot is a bit more trickier. In order to create a one you will need to link and mount some files from your main root directory to it. Instead of just putting the commands we will briefly explain the what it does and what is mounting or linking.

The basics

Usually, the first thing you need to do is create a directory to which you will mount your new root. For example:

sudo mkdir ~/chroot

Then we can mount the other partition (or disk image or what have you) in our new directory, for example:

sudo mount /dev/sda3 ~/chroot

Will mount partition /dev/sda3 to ~/chroot.

Understanding chroot

If you think about it, a whole system is now mounted to a part of your current system. The problem is the new system is not actually talking to your hardware, it’s not running yet in any way.

If we were to use a virtual machine, the application (say VirtualBox) would (as it name implies) simulate a set of hardware from where the new system would run. The new system would then talk to the simulated hardware and then the application would translate that into instruction for your real hardware, and viceversa.

Virtual machines then have the obvious disadvantage of needing an intermediary. So performance is considerably slower than that of your native system.

With chroot instead of creating a virtual set of hardware to “fool” the new system, we will instead make it talk directly to your hardware. Let’s enter the our new chroot directory:

cd ~/chroot

Talking to the hardware

So now let’s see how to make the necessary connections. The first thing we need to learn is that every device in the system is represented by a directory or a file. I know this sounds strange but it makes a good deal of sense if you think about it: To change the behaviour of a piece of hardware merely requires writing something into its corresponding file and things like CDs or other removable storage are treated as a directory.

The directory that holds all device files is /dev, which is short for device files. So we need to bind our current /dev directory with the /dev directory in our chroot. This way the new system has a way to talk to the hardware directly, so there’s no speed penalty:

sudo mount -o bind /dev dev/

The next (virtual) directory is  one you can think of it as the place where the kernel goes to write runtime information about your hardware, /proc. Think available RAM, mounted drives, etc.

sudo mount -t proc proc proc/

The next directory is also used to provide processes information, /sys. It provides information from the kernel to user space processes.

sudo mount -t sysfs sys /sys

Using chroot

If you want your chroot to have internet access we can bind the resolv.conf files that contain the DNS information. This allows your system to connect to the web.

sudo mount –bind /etc/resolv.conf ~/chroot/etc/resolv.conf

Now is the time to finally change the apparent root directory:

sudo ~chroot /chroot

You can run practically any command line application. Perhaps the most common thing to do with chroot is fixing the grub when something goes wrong, as we did when we wrote our guide on installing Kubuntu on a Macbook Air.

For example, say the GRUB is failing because the system installed the non-EFI version on an EFI system. If we run the following command inside the chroot we will install grub-efi in the chroot environment (in our guide to install Kubuntu, the chrooted system was the Kubuntu installation in our disk drive):

sudo apt-get install grub-efi

Then we need to install grub-efi to some disk:

sudo grub-install /dev/sdax

Once installed, we just need GRUB to check which operating systems are available:

sudo update-grub

Another useful one, say your forgot your password. You can create a chroot environment, run it, and then execute:

sudo passwd username

to change your password.

Conclusion

Chroot environments are a versatile tool that can be used to fix a wide variety of problems in your Linux installation without actually booting into it. Additionally, it’s an interesting alternative to virtualised environments, since there’s no speed penalty. It’s also useful for developers because you can create isolated environments running at native speed to test all kind of applications or even entire desktop environments.

To achieve the latter we will learn a bit about how to combine chroot with xhost and xephyr to run graphical applications in the next instalment.

Mathematics and Philosophy student at the National Autonomous University of Mexico. Website