Setup a chroot environment on Ubuntu with debootstrap

16 Flares Filament.io 16 Flares ×

Chroot

Chroot provides a way to setup an isolated environment inside an OS, where specific applications can run without interacting with the rest of the system. It is also called a jail and is often used to create a test environment for applications which may not be directly installed on the system due to various concerns like stability or security.







Running 32 bit applications in a chroot environment 64 bit ubuntu

Because chroot isolates application into a separate environment, it is also used to run 32 bit applications inside a 64bit OS environment. Now a days most OSes and applications are 64bit on both desktop and servers.

However there are scenarios where applications cannot be migrated to 64 bit, due to various restrictions. This happens in organisations where large legacy applications have been running for long. For such applications to keep running it is necessary to either have a 32bit OS installed or create a 32bit environment inside a 64bit OS installation.

The chroot environment

A chroot environment is a way to create a separated so-called "jail" in your system, running a different operating system than the host. In other words, it's an operating system running within another operating system. Technically, the user changes temporarily the root directory (usually /) to another root directory (in our example, /srv/chroot).

In all Unix-based operating systems, the root directory is the top of the filesystem tree, and the applications can't access any file or directory higher up , therefore, having a new, different, root and changing to it, the applications running there, are isolated from the rest of the host system.

This approach has many advantages:

-  Test applications without the risk of compromising the entire host system.
-  From the security point of view, whatever happens in the chroot environment won't affect the host system (not even under root user)
-  A different operating system running in the same hardware

In this case, we will take advantage of this feature to run 32-bit applications natively in a 64 bit host.

The necessary packages to setup the chroot environment are debootstrap and schroot, which are available in the ubuntu repository. The schroot command is used to setup the chroot environment.

Debootstrap allows you to install a new fresh copy of any Debian (or debian-based) system in a directory with all the basic commands and binaries needed to run a basic instance of the operating system.







You can chroot directly as root by simply running chroot command, but normal users  are  not  able  to  use this command. The schroot allows access to chroots for normal users using the same mechanism,  but  with  permissions  checking  and allowing additional automated setup of the chroot environment, such as mounting additional filesystems and other configuration tasks.

This automated setup is done through the action of setup scripts which may be customized  and  extended  to  perform  any actions required, explained below.

These are the steps to implement this functionality in Ubuntu 12.04:

1. Install the packages:

The first step is to install the packages with the apt-get:

$ sudo apt-get install debootstrap 
$ sudo apt-get install schroot

2. Create a schroot configuration:

We have to configure schroot to suit our needs in the configuration file.

Choose a short name for the chroot, we used precise_i386 :

$ sudo gedit /etc/schroot/chroot.d/precise_i386.conf

Then proceed with the configuration, as in this example:

[precise_i386]
description=Ubuntu 12.04 Precise for i386
location=/srv/chroot/precise_i386
personality=linux32
root-users=bob
type=directory
users=alice,bob,charlie

Modify the values of the configuration parameters in the above example to fit your system:

location - This should be a directory that is outside of the /home tree. 
The latest schroot documentation recommends /srv/chroot.

personality - Enable this line if the host system is 64-bit running on an amd64/x64 computer 
and the chroot is 32-bit for i386. Otherwise, leave it disabled, by adding "#" as first character.

root-users - These are users on the host system that can invoke the schroot program and 
get direct access to the chroot system as the root user.

users - These are users on the host system that can invoke the schroot program and 
get access to the chroot system. Your username on the host system should be here.

3. Install 32bit ubuntu with debootstrap

Debootstrap downloads and installs a minimal operating system inside your chroot environment.

You can install any debian-based distro of your preference, as long as you have a repository available. I love this utility, it saves you a lot of time and work.

In our example we placed the chroot environment under the directory /srv/chroot/precise_i386 and this is the root directory of the chroot environment.

$ sudo mkdir -p /srv/chroot/precise_i386
$ sudo debootstrap --variant=buildd --arch=i386 precise /srv/chroot/precise_i386 http://archive.ubuntu.com/ubuntu/

It takes some time (depending on your bandwidth) to download, install and configure the complete system. It takes about 500M for a minimal installation.

4. Test the chroot environment

Now, we should have our 32 bit Ubuntu installed.

First list the available chroot environments using the schroot command.

$ schroot -l
chroot:precise_i386

you should expect precise_i386 as a result.

Now let's get into it, simply type as your regular user

$ schroot -c precise_i386 -u root

Now, you are into the chrooted environment as root!

Check that the prompt message is slightly different than usual, and you have new values for the following commands

lsb_release -a
uname -a

5. Additional configuration

Now, that we have a functional 32 bit environment, let's finish to install the minimal requirements and all the necessary libraries and applications.

$ apt-get install ubuntu-minimal

This will download and install the needed packages of a minimal ubuntu installation.

That's all.

You can begin to install the packages, libraries and dependencies that your application needs, as usual with apt-get or dpkg command. It will retrieve the i386 version, as long as you do it within the chroot environment.

You can update and upgrade it, as you do with the host system; edit your /etc/apt/sources.list and modify according to your needs.

The home directories of the users within the chroot, are shared with the host.
This is very useful in order to have access to applications, documents and files that you should need.

To finish this tutorial, in order to run a graphic application from the chroot, you have to export the DISPLAY environment variable.

$ DISPLAY=:0.0 ./yourapp

Please feel free to send any question or comment regarding this article.

Hope you liked it and found it of help!!

Last Updated On : 9th August 2013

Subscribe to get updates delivered to your inbox

About Ricardo Eureka

I´m a Unix System Administrator for more than 10 years with experience in Linux (Red Hat, Ubuntu, Debian), Solaris 8,9,10 (SCSA certified for Solaris 10), AIX and HACMP. You can contact me on ricardoeureka@gmail.com.

  • Luke

    awesome article, thx man!

  • ubuntuman

    In ubuntu 13.04/13.10 in the config file “location=path-to-chroot” is deprecated. The new term is “directory=path-to-chroot”. FYI for anyone running into errors! http://manpages.ubuntu.com/manpages/raring/man5/schroot.conf.5.html

  • Anon Yser

    you explain this many times better than other websites i’ve read on how to chroot

  • http://guh.me/ Gustavo H. Mascarenhas Machado

    What a coincidence, I was just looking for something about chroot, lol!

    Nice article, thanks!

16 Flares Twitter 9 Facebook 0 Google+ 7 LinkedIn 0 StumbleUpon 0 Pin It Share 0 Filament.io 16 Flares ×