Difference Between Chroot and Virtual Machine
At first glance, you can think of chroot as similar to a virtual machine or a containerized system like docker. It is kind of similar, but chroot is a much lighter solution than a virtual machine. The virtual machine needs a hypervisor to install and work on a separate kernel, which is different from the host machine. Unlike a virtual machine, chroot shares the same kernel and processes but creates a jail in the filesystem. Inside the jail, it is not possible to look outside without root permission. Therefore, the isolated filesystem is also called chroot jail.
Different Use Cases of Chroot
Creating a Chroot Environment
This is a quick guide on creating a chroot environment in your system. You can get in-depth information on the arch wiki. We get the following output from the above command. Let’s examine what this script does. At first, this shell script asks for the binary name, then it takes this binary name and finds all the dependency of that binary and saves it inside a list variable. It next runs a for-loop which runs on every item of the list and copies the dependency from our normal “/bin” file to our “mte” chroot directory. Save this script somewhere else and refer to it when you create a new chroot environment. Next, change the permission of the script and run it inside the terminal. But to fulfill our purpose, we run the following command to activate our chroot environment. The above command activates a chroot environment in the “~/mte” directory and specifies to run a bash shell. You can see a change in your terminal prompt and can now use the touch, rm, and ls commands to create, remove and list files respectively. To exit the chroot environment, run the exit command. If you want to remove the chroot environment completely, delete the “mte” directory from your filesystem.
Fix a Broken Bootloader Using Chroot
The most fascinating thing about chroot is that you can enter a broken system and run a command inside it. Therefore, using chroot, you can easily install a critical update to fix a system or reinstall the entire bootloader to fix the issue. But for that, you should have a live Linux environment. Make a bootable USB by downloading a Linux ISO and booting from the USB. It gives you a live environment to work with. Mount your system partition to work with chroot. Change the “/dev/sda” with your intended system partition name that you want to work with and let the grub bootloader find the information that it needs to fix the bootloader issue. Let’s chroot into the “/mnt” directory and enter the broken system. Install, check, and update the grub bootloader in your system. Make sure to use the proper drive name, and don’t copy-paste these commands blindly. Exit the shell using the exit command mentioned earlier. Unbind the previously bound directories and unmount the filesystem. Run those commands one after another consecutively. Reboot your PC and unplug the live USB. When the computer boots up, your grub bootloader will shine as new, and everything should work perfectly fine.