Using a chroot environment

by richard on Fri, 12/09/2011 - 08:59

A Chroot environment is usually a folder on your filesystem that contains the entire base system for an operating system.

This tutorial will assume you are installing debian squeeze into a folder /chroot/squeeze

You can create a chroot environment itself using debootstrap.

richard@vpceh:~$ debootstrap squeeze /chroot/squeeze http://ftp.uk.debian.org/debian

That sets up the base packages, and then you need to mount the extra filesystems, and create the list of mounted systems.

The /dev filesystem contains all the device nodes for your system. I find it easiest to bind-mount this from the host operating system.

richard@vpceh:~$ sudo mount -o bind /dev /chroot/squeeze/dev

The /proc filesystem contains information about current running processes. This is mounted using the following command

richard@vpceh:~$ sudo mount -t proc none /chroot/squeeze/proc

you can then chroot into the system using the command

richard@vpceh:~$ sudo chroot /chroot/squeeze /bin/bash

Finally, just read the mounted filesystems into /etc/mtab by running

root@vpceh:/# cat /proc/mounts > /etc/mtab

and then you can perform operations as if you were on your own base system :)

debootstrap - creating a chroot environment on a Debian system

by richard on Fri, 12/09/2011 - 00:26

Being a software developer, and owning quite a few old computers (x86), I sometimes regret my choice of running a 64-bit operating system on my desktop. Then again, it allows me to use the full potential of my processor (whatever that may be - I'm not convinced that it's all that much better than running it 32-bit, but hohum), so I deal with it.

Having cut my metaphorical linux teeth on Gentoo, from before it had an automated installer, I'm no stranger to chroot. A wonderful command that allows you to isolate different installs of the operating system.

A chroot is by far the easiest way to compile 32-bit software on a 64-bit debian install (I know it's possible to install all the 32-bit libraries, but I've always found it to be a faff!).

Enter our friend debootstrap.

debootstrap allows you to create a minimal debian filesystem within a folder on your current system. If you look closely when installing Debian in the first place, you will notice that the installer uses debootstrap to set up your initial system.

It's really easy to use. You just need to point it at a directory, tell it what architecture and version you want to install, and point it at a mirror.

For example, to install a native chroot environment on a debian amd64 system, you would run

richard@nemo:~$ debootstrap squeeze /srv/chroot/squeeze-chroot http://ftp.uk.debian.org/debian

Thats basically telling it to bootstrap squeeze into the folder /srv/chroot/squeeze-chroot from the mirror http://ftp.uk.debian.org

If, however, you want a 32-bit environment on a 64-bit machine, and you want it from wheezy (testing) rather than squeeze (stable) then you would run

richard@nemo:~$ debootstrap --arch=i386 wheezy/srv/chroot/wheezy-chroot http://ftp.uk.debian.org/debian

You can then chroot into your new environment with

richard@nemo:~$ chroot /srv/chroot/wheezy-chroot /bin/bash

In order to make it usable, you would need to create the /dev layout, and mount the proc and sys filesystems. I'll cover them in another post!