Installing new Debian systems with debootstrap

Posted by Steve on Thu 3 Aug 2006 at 09:39

When it comes to installing new installations of Debian GNU/Linux there is one tool which should not be ignored. Whether you're dealing with a real system, or a virtualised one, the debootstrap tool is ideal for quickly installing new Debian environments.

Put simply the debootstrap package allows you to install a fresh copy of Debian GNU/Linux into a directory. This new installation will have all the basic packages and binaries which you'd expect to be present such as:

Using debootstrap might seem a little bit strange if you're only used to the Debian installer, but it is a very useful tool for performing installations if you've got something like a LiveCD which supports your hardware. The process would be go something like this:

If you already have a system installed then you can use debootstrap to setup a "chroot" installation of Debian for testing purposes.

Chroot?

A chroot on Unix operating systems is an operation which changes the root directory. It affects only the current process and its children. The term "chroot" itself can refer either to the chroot(2) system call or the chroot(8) wrapper program.

A program that is re-rooted to another directory cannot access files outside that directory. This provides a convenient way to sandbox an untrusted, untested or otherwise dangerous program. It is also a simple kind of "jail" mechanism.

To get started with debootstrap you must first install it:

root@desktop:~# apt-get install debootstrap

Once installed you can start experimenting with it. A simple invocation would look like this:

root@desktop:# mkdir /tmp/blah
root@desktop:# debootstrap sarge /tmp/blah/

Here we're telling debootstrap that we wish to have a fresh installation of Sarge made into the directory /tmp/blah. The process will take a while since it will involve downloading packages from the Debian mirrors, as you can see from the following output:

root@desktop:/tmp# debootstrap sarge /tmp/blah/
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Found additional required dependencies: libtext-iconv-perl zlib1g
I: Checking component main on http://ftp.debian.org/debian...
I: Retrieving adduser
I: Validating adduser
I: Retrieving apt
I: Validating apt
....

Once the system has finished you can use the chroot command to "jail" yourself inside it:

root@desktop:~# chroot /tmp/blah
root@desktop:/#
root@desktop:/#
root@desktop:/# ls
bin   dev  home    lib    mnt  proc  sbin  sys  usr
boot  etc  initrd  media  opt  root  srv   tmp  var

root@desktop:/#
root@desktop:/# apt-get clean
root@desktop:/#
root@desktop:/# du . --human-readable --total | grep total
111M    total

Here you can see that you have a minimal system which occupies just over 100Mb of space. Notice that your prompt still contains the name of your "real" system? Don't be tempted to change that - since the hostname change will take effect on your host system too.

Apart from the hostname issue the new installation is independent from your real system, it just shares the IP address. If you want to use this environment properly you'll need to make a couple of changes. (Exit from the chroot first):

root@desktop:~# mount proc /tmp/blah/proc/  -t proc
root@desktop:~# cp /etc/resolv.conf /tmp/blah/etc
root@desktop:~# cp /etc/hosts /tmp/blah/etc
root@desktop:~# chroot /tmp/blah/
root@desktop:/#

Since the new system will not be using PPP, it will have access to the network via the host system, you can tidy it up a little:

root@desktop:~# chroot /tmp/blah /usr/bin/dpkg --purge ppp pppoe pppconfig pppoeconf
root@desktop:~# chroot /tmp/blah /usr/bin/apt-get install deborphan less
root@desktop:~# chroot /tmp/blah
root@desktop:/# deborphan
libpcap0.7
root@destkop:/# dpkg --purge libpcap0.7

Now we have a nice minimal installation which can be modified or purged whenever you're done with it. If you mounted /proc you should remove it first:

root@desktop:~# umount /tmp/blah/proc
root@desktop:~# rm -rf /tmp/blah/
Caching

If you're going to use this command often you'll most likely wish to use a local mirror, or even better a cache.

For example if you have the machine itchy running an installation of apt-proxy you can use that :

root@desktop:~# debootstrap sarge /tmp/blah/ http://itchy:9999/debian

This will download all the packages via your proxy, and the second time you execute it will give you a significant speedup (since the Debian packages will be coming from the cache).

So what can you use the combination of chroot and debootstrap for? Well the possibilities are endless! If you remember the previous article on testing packages with pbuilder you might see a connection. pbuilder allows you to setup pristine "environments" for testing Debian packages, and it does that by invoking debootstrap to create fresh installations of Debian into a directory.

So using debootstrap, as we've shown, you can test package installations and dependencies. You could even setup a new environment for each package build by hand if you were masochistic.

If you're using Xen or UML you'll find debootstrap is the way to install new instances of Debian. Make the installation then add in a /etc/fstab file and networking details in /etc/network/interfaces and you're done.

There are a lot of useful options which debootstrap has which we've not covered, to setup an arch, etc. See the manpage for details:

man debootstrap

This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2006 Steve - please ask for permission to republish or translate.