A few weeks ago, I wrote an article on How To Run Multiple X Sessions Without Virtualization, which demonstrated how you can run multiple desktop environments at the same time on the same computer, and switch between them at the push of a button. I admit, this was somewhat of a warmup for the real show – how to run multiple Linux distributions at the same time without having to deal with the drawbacks of virtualization software.
Today, we’re going to be using chroot to treat certain parts of your Linux filesystem as (almost) completely isolated systems. This can let you do all kinds of interesting things, like run a full Gentoo system from inside your Ubuntu system with little to no performance loss.
How it works
Before we get started, it would be a good idea to go over exactly what we’re doing and how it all works. When you start up your Linux computer, you go through a series of steps to boot up. GRUB, your bootloader, loads the kernel that’s specified in GRUB’s config file, attaches it to your root partition, and runs the init scripts stored there. From then on, your system is running from within that root partition.
We’re going to be using a host Linux system (I’ll be doing it from Debian but the steps should be pretty much identical regardless of distribution) that goes through that normal boot process, but then treating a certain portion of your filesystem (like /mnt/guest) as its own isolated installation. That guest system won’t go through the boot process, won’t load a kernel, and won’t run init scripts. You’ll just tell your host “connect to this directory as if it’s the new root partition”. The diagram below should help demonstrate what I mean.
This is approximately how the files will be laid out on your system, just sitting there as normal files on a normal partition, treated the same as any other files until we use “chroot” to make them into something special.
But once we use chroot (CHange ROOT) and point it to /mnt/guest (or wherever we put the files in the next step), that directory is now its own root filesystem. It becomes completely oblivious to the host system. All programs and commands run from within that new root are run based on the software inside that new branch.
For example, if you’re chrooted into /mnt/guest and type “ls”, it’s reading the “ls” command from inside that chrooted environment, completely unaware of the “ls” command on the host system. If you type “firefox”, it runs the Firefox from inside the new root, whether or not you’ve got Firefox installed on the host.
Getting and Using the Guest Distro
To make this work, you just need the base filesystem of a Linux distro, whatever distro that may be. Debian has a great tool for just this purpose called debbootstrap which downloads the base files and places them wherever you like. To keep things interesting, and to demonstrate what chroot can do, we’re going to use a Gentoo filesystem for our guest. To keep things simple and direct, I’m going to include the download of the tarball as one of the steps we’ll do at the command line.
These steps are going to download all the base files for a Gentoo system, extract them into our target directory (I’ll be using /mnt/guest but you can use anything you like) and then chroot into that directory so it’s treated as the new root (/) directory. You may need root (sudo) permissions for some of the following commands.
mkdir /mnt/guest cd /mnt/guest wget http://distfiles.gentoo.org/releases/x86/2008.0/stages/stage3-x86-2008.0.tar.bz2 tar -jxvf stage3-x86-2008.0.tar.bz2 chroot /mnt/guest
Those commands take the following steps, in order of line number:
- Create the space for our guest system to live
- Move into that space
- Download the Gentoo system files
- Extract them
- Treat this new space as the root (/) directory
The following screenshot should help demonstrate that once we’ve used chroot to connect our new directory as root, it is now isolated from the previous branches in the filesystem tree.
To explain the screenshot, we’re starting at a normal command prompt on the host system. I’m using pwd to display my current directory, where all Gentoo’s files are sitting as plain old files with no special meaning. I then use chroot to treat this directory as the new root, and you can now see visually that my prompt has changed because the command line is now pulling all its info (including how to draw the prompt) from within Gentoo’s root environment. ALL commands and actions I take while chrooted in depend on those commands existing within that new file tree. When I type pwd again, it no longer says /mnt/guest, because as far as that shell knows, nothing exists other than what it sees inside that tree.
Things to Keep In Mind
As I hinted above, this isn’t 100% isolated from the host system. The chrooted environment was never “booted up”, so it never loaded its own kernel, and never ran its own init scripts. In our example, we’ve got Gentoo running on the kernel that the host (in my case Debian) booted up with.
This method is certainly not a replacement for virtualization, and can not do everything VM software can do. The guest system never actually “boots” or runs init scripts, and it’s possible your guest OS may require some features your host kernel does not offer.
In the times I’ve mentioned this technique to people in the past, they’ve often said something along the lines of “That’s cool, but what would you use it for?”. Personally I’ve found several uses for chroot:
- Test developing software in a pristine environment
- Run software meant for another distribution
- Run software that requires an older or newer version of your current distro
- Access a Linux installation on a different partition without having to reboot
- Boot a Live CD and use chroot to repair your installation or GRUB
- Access a drive where you’ve forgotten the user/pass
- Run a 32-bit app in a 64-bit environment
And I’m sure there are plenty more that I’ve never though of. Have you ever used chroot to run multiple distros? How about to repair a Linux system from a Live CD? Let us know in the comments.
Our latest tutorials delivered straight to your inbox