01 September, 2006

Ubuntu chroot environments


I wrote that in INdT's internal wiki a couple of months ago. But since I'm mostly outside that intranet in my daily work I decided that it would be much easier if I could have access to it through the regular internet instead, which also has the benefit of being available to anyone.


Sometimes you face the problem where you need to develop an application using newer (likely unstable) libs then the ones provided by the linux distribution that you are using.

What do you do then?

A common "solution" is simply installing next version lib packages in your system (e.g., some ubuntu-dapper packages on a ubuntu-breezy system), which could easily make it unstable, or even worse.

Another "solution" would be to intall those newer libs inside /usr/local. But it can be problematic as your system and build tools can get confused when faced with libs that are both in /usr/lib and /usr/local/lib. LD_LIBARY_PATH environment variable helps but some times even that won't work.

Hope is not lost though :-), as there is a reasonably easy and very elegant solution for this, which is chroot. chroot enables you to run a shell having a different filesystem root directory than the real one. e.g. running a shell using the directory /home/ddtc/my_sandbox_root as its root directory. In that case /home/ddtc/my_sandbox_root/usr/bin would be seen by this "chrooted" shell as /usr/bin.

The idea is to put a basic distro in a separate directory (like /home/ddtc/my_sandbox_root) with those newer or unstable libs and build the apps that needs then through a terminal chrooted to this directory. In that way you will have those apps using newer/unstable libs without having to change anything in your system.

Putting all the necessary files into this fake root directory (like bash, libc, debian-utils, gcc, etc) is tricky and laborious. To save you from that hassle there is a clever tool called debootstrap, which downloads and installs all the necessary packages to run a minimal distro in your chrooted environment.

Creating a chroot environment

Let's suppose that you have a Ubuntu Breezy desktop and wants to install a Ubuntu Dapper filesystem at /home/ddtc/dapper_root. You would do then take following steps:

  • Get dapper's debootstrap. Breezy has debootstrap already, but it doesn't have the script to install a dapper distribution. So you will have to download dapper's debootstrap package from http://packages.ubuntu.com/
  • Download and install dapper: sudo debootstrap --arch i386 dapper /home/ddtc/dapper_root http://archive.ubuntu.com/ubuntu
  • Replicate users and things alike into your chroot environment:
    • sudo cp /etc/passwd /home/ddtc/dapper_root/etc/
    • sudo cp /etc/shadow /home/ddtc/dapper_root/etc/
    • sudo cp /etc/group /home/ddtc/dapper_root/etc/
    • sudo cp /etc/sudoers /home/ddtc/dapper_root/etc/
    • sudo cp /etc/hosts /home/ddtc/dapper_root/etc/
  • Add the following lines in your /etc/fstab:
    • /tmp /home/ddtc/dapper_root/tmp none bind 0 0 (you won't be able to run X apps from your chroot environment without it)
    • /dev /home/ddtc/dapper_root/dev none bind 0 0
    • /proc /home/ddtc/dapper_root/proc proc defaults 0 0

Now you can enter into your dapper playground with

sudo chroot /home/ddtc/dapper_root

To be able to run X (graphical) apps from inside your chrooted environment you have to configure your xhost (outside chroot) properly. The easiest way to do this is to simply issue the following command:

xhost +

That will make you xhost accept X connections from anyone (which includes your chroot'ed environment). Inside chroot you may keep DISPLAY env. variable set as usual, which is, ":0.0"

Further improvements

  • From inside your chroot shell, as you only have basic packages there, you should now "apt-get" synaptic and from there (or directly from apt-get) install all the other packages that you need (gcc, autotools, gtk, etc).
  • If you want gtk apps from chroot environment to use your desktop theme you have to install the package gnome-themes (in your chroot env).
  • Install the package language-pack-en and then run "sudo dpkg-reconfigure locales" to fix your locale settings.


This doc is essentially a version of http://www.ubuntuforums.org/showthread.php?t=24575.


Blogger M Henri Day said...

Daniel, is it correct to assume that the latest Gutsy tribe could be tested in a Feisty set up by using, mutatis mutandi, the procedure you describe in your valuable posting ? Have you tried it yourself, and do you have any tips which might be particularly relevant for Gutsy ?...


July 15, 2007 at 6:58 AM  
Blogger Unknown said...

@M Henri Day

Yes, for sure.
This method works for any Ubuntu version.
Breezy and Dapper where used just as an example. It works just as well for Feisty and Gutsy or any other "UbuntuVersion"/"UbuntuVersion+1" combination.

March 31, 2008 at 8:44 AM  

Post a Comment

<< Home