The art of chroot: Running Quantal within Precise.

Introduction 
 

There are many ways to try Ubuntu 12.10 without upgrading your Ubuntu
12.04 system. One simple way is to use a VM,. That’s a nice way, but
there is also chroot. This tutorial is about running a chrooted Quantal
within Precise (12.04) system, but it can be adapted to running any
Debian distribution on another Debian install (or even another Linux
distro).

Chroot allows to install a whole Linux operating system within an
isolated environment. But the isolated OS uses your kernel and has raw
access to hardware. Before I start I will tell a few reasons why you
should try this tutorial:

  • You can run (almost) any application from Quantal directly within
    your Precise install with native look and feel with native
    preformance.
  • You run potentially unstable Quantal apps on the stable and tested
    kernel from Precise.
  • You don’t have to use the new unity with the shopping lense.
  • You will learn how to install Ubuntu from command line.
  • It is worth learning chroots. It is a great starting point if you
    want to learn about Linux containers
    (LXC), FreeBSD
    Jails

    which are valuable skills for system administration and system
    hardening.

I’ll assume you are comfortable using the terminal, you have a
administrator rights on your system and you are an intermediate Linux
user. Also to make things clearer, throughout this tutorial you will
either be on a Precise host system or on a Quantal chrooted system so
always make sure you are in the right environment before executing any
command.

Step 1: Setting up a base Quantal
system

The step is about setting up a basic chrroot system within a folder.
Chroot is a command that should be already available on your system but
to proceed through this tutorial you a few other tools including
*debootstrap* and *dchroot*. The first one *debootstrap* allows to
install a base Debian system and the second one *dchroot* offers
additional administrative tools to manage chroot environments.

sudo mkdir /var/chroot
sudo apt-get install dchroot debootstrap

Now we will edit a simple config file /etc/schroot/schroot.conf
which contain information about chrooted systems, including which users
and groups can access different chroot setups.

sudo nano /etc/schroot/schroot.conf

Add the following to the end of the /etc/schroot/schroot.conffile
replacing \ with your username:

[quantal]
description=Ubuntu 12.10
directory=/var/chroot
users=username
groups=username
root-groups=root

Before we proceed any further you should choose some mirror close to
you. You can find a list of mirrors on the following website:
. Now you need to run the
following code. Replace the chosen mirror (oakland) with any mirror
which suits you. If you are running on a 32 bit system replace amd64
with i386.

sudo debootstrap --variant=buildd --arch amd64 quantal /var/chroot/ http://ubuntu.secs.oakland.edu/

If you want a 32 bit chroot system within a 64 bit install system you
can. Just change amd64 to i386 and it should work.

These additional steps will make sure that chrooted system can function
properly and have access to the internet.

sudo mount -o bind /proc/ /var/chroot/proc
sudo cp /etc/resolv.conf /var/chroot/etc/resolv.conf

Let’s check that it works!

sudo chroot /var/chroot /bin/bash
exit

If there was no error that means success.

Step 2: Installing Quantal
Quetzal

Right now all you have is a basic Quantal system without a full Ubuntu
desktop. This tutorial is about installing an ubuntu desktop setup, but
you don’t have to do install everything. If you want you can choose
which packages to install but I recommend installing the full setup.

Make sure that you are within the Precise “host” system and not within
the chroot environment. First we copy aptitude (package manager)
configuration file.

sudo cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list

Now open the aptitude (package manager) configuration file within the
chroot with your favorite editor and change all mentions of *precise*
with *quantal*. Also a good advice is to deactivate any PPA and
backports. To open the file for instance you can run.

sudo nano /var/chroot/etc/apt/sources.list

We now install locales within the chroot Quantal environment. Once that
is done we install the *ubuntu-desktop* package.

sudo chroot /var/chroot /bin/bash
apt-get update
apt-get upgrade
apt-get install language-pack-en
dpkg-reconfigure locales
apt-get install ubuntu desktop

When I first run this I got into some strange problem with the Debian
package configuration database. Usually when this problem arises it says
that the installation was not complete. In doubt, run these commands to
solve any potential problem with the database.

/usr/share/debconf/fix_db.pl
dpkg-reconfigure -a
apt-get install ubuntu-desktop
exit

Congratulations you just installed Ubuntu 12.10 Quantal Quetzal from the
command line.

Step 3: Integrating Quantal with the
Precise system

Now to make the chrooted Quantal system  usable, we need to connect it
with the “host” Precise system. The aim of this part is that you can
login with your normal user account and that you can access your files,
your devices and so on. These are done in two sub-steps, the first
sub-step consists in connecting folders and mount points, and the second
sub-step consists in copying user authentication information onto the
chrooted system. All shall be done from the Precise system and not the
chrooted Quantal system.

We first start with the “connection” part. Note that you can omit /tmp or
/media. If
you don’t bind /home you
won’t have access to your files from within the chrooted Quantal system.

sudo mount -o bind /dev /var/chroot/dev
sudo mount -o bind /sys /var/chroot/sys
sudo mount -o bind /tmp /var/chroot/tmp
sudo mount -o bind /home /var/chroot/home
sudo mount -o bind /media /var/chroot/media
sudo cp /etc/mtab /var/chroot/mtab

Then we copy user information. Make sure you write the sed command
correctly. We need to run passwd within the chrooted environment to
regenerate the hashes.

sudo cp /etc/passwd /var/chroot/etc/passwd
sudo sed 's/([^:]*):[^:]*:/1:*:/' /etc/shadow | sudo tee /var/chroot/etc/shadow
sudo cp /etc/group /var/chroot/etc/
sudo cp /etc/hosts /var/chroot/etc/
sudo cp /etc/sudoers /var/chroot/etc/
sudo chroot /var/chroot/ /bin/bash
dpkg-reconfigure passwd
passwd username

Want to try it out: just type the following with your username:

su username
ls_release -a
exit
exit

Step 4: Creating Terminal
profiles

Honestly I have not found yet a way to start the Unity shell form the
chrooted environment directly. Nonetheless you can start any application
from the chrooted environment and run it within your current Unity shell
and everything integrates nicely with your dekstop (Precise) UI.

First we need to perform a simple check which will verify you can access
your chrooted environment directly without using *sudo*.

schroot -c quantal -p
lsb_release -a
exit

This command will directly send you inside the chrooted Quantal
environment. If it did not work please check you did everything
correctly. What I will now show you is how you can setup the Terminal
application to have profiles for each environment Precise and Quantal.

First you need to start up the Terminal application which you probably
already have. Go into Edit > Profiles then you create two new
profiles.

  • Click on new and choose as base the profile Default. Call the
    profile Precise. Then go into Terminal and Command and change
    the title to Terminal – Precise.
  • Click on new once more and choose as base the profile Default.
    Call the profile Quantal. Then go into Terminal and Command,
    change the title to Terminal – Quantal, and click on Run a
    custom command instead of my shell
    and type into the input box
    below the command: schroot -c
    quantal -p
    .

Whenever you want to start up quantal, run the Terminal application and
choose **New Window** or **New Tab** from the **File menu** and choose
*Quantal*. This also integrates with the Unity HUD. Just press on the
Alt key and once the input dialog shows up type Quantal. Once you see
Quantal click on it or press Enter.

Step 5: Making the setting
permanent

If you
reboot your system you will lose all you
mount points within Quantal which is
problematic. In order not to lose the mount point you
need to change your /etc/fstab
file to
set the mount points automatically at
start-up.

sudo cp /etc/fstab /etc/fstab.backup
sudo nano /etc/fstab

Now you need to add the following at the
end of
your /etc/fstab
file:

/proc /var/chroot/proc none rbind 0 0
/dev /var/chroot/dev none rbind 0 0
/sys /var/chroot/sys none rbind 0 0
/tmp /var/chroot/tmp none rbind 0 0
/home /var/chroot/home none rbind 0 0
/media /var/chroot/media none rbind 0 0

Final notes

If you want to uninstall the chroot environment make sure you do the
following:

  • Revert the changes inside the /etc/fstab
    file.
  • Umount all the mount points within /var/chroot.
    The changes we previously did in /etc/fstab
    should guide you. Double check you have umounted everything within
    /var/chroot
    otherwise the next steps will be dangerous.
  • Delete the Terminal profiles.
  • Remove SAFELY and CAREFULLY the /var/chroot/
    folder. Make sure that no remaining mount point is mounted inside
    /var/chroot/.
    You can run the command sudo rm -rf
    /var/chroot/
    , but please double check there are NO MOUNT POINTS,
    and
    check you typed the RIGHT COMMAND. A simple mistake and it
    could destroy the entire “host” Precise system or parts of it.

I would like to finish this article on a few other things about chroots
worth knowing about:

  • They don’t provide any extra security: if someone gets root access
    within a chrooted environment they can easily break out it. See
    http://www.bpfh.net/simes/computing/chroot-break.html.
  • Everything runs on the “host” system kernel.
  • There are ways of installing other linux operating systems including
    Fedora based distributions and Arch Linux.
  • There is a entire distribution built around the idea of chroots, and
    its called Bedrock
    Linux
    .