Using rootstrap with an APT cache

The rootstrap tool can be used to build Debian root images from the APT cache of Debian packages on your system. It is most useful when you wish to avoid the use of a Debian mirror, perhaps because you only have intermittent Internet access or wish to avoid wastefully downloading the same packages each time you build a new image.

I wrote this document, because it took me some time to work out how to use rootstrap with only local files. I tried using tools such as apt-move and debarchiver to create a local Debian archive, but neither produces archives in a format that rootstrap can use. (Debootstrap needs a Release file, which only exists in a full copy of the Debian archive.) Thus, I arrived at this method.

I have assumed you wish to build on the current stable release of Debian - if you're competent enough to use testing or stable you will surely be able to adapt the procedure accordingly.

Things you need

Rootstrap can be installed with your favourite Debian package manager.

You can get the basedebs.tar file from any Debian mirror, or from the Debian CDs/DVDs. It lives in the debian/dists/woody/main/disks-i386/base-images-current directory. You need this file, as it contains many essential packages that will not exist in your APT cache.

The Basics

As an introduction we will produce a root image using only the packages in the basedebs.tar file. The following section will show you how to add extra packages.

  1. Make a working directory and change into it:
    $ mkdir uml; cd uml
    
    We will use this working directory to hold all rootstrap related files. I call it uml, but it can be anything you like.
  2. Copy the basedebs.tar file into your working directory:
    $ cp /path/to/basedebs.tar .
    
  3. Copy the system-wide rootstrap.conf to your working directory:
    $ cp /etc/rootstrap/rootstrap.conf .
    
  4. Open the rootstrap.conf file with your favourite text editor.
  5. Find the line that begins 'modules='. The 'modules' option controls which rootstrap modules will be loaded. We do not need networking, so we remove the 'network' option:
    modules=mkfs mount debian uml umount
    
  6. If you are using Debian 3.0, you need to work around a bug; some network options must be specified even if you are not using networking. In the network module section (it begins with '[network]'), uncomment the interface option:
    interface=eth0
    
    Also uncomment the host option:
    host=192.168.10.1
    
    and the transport option:
    transport=tuntap
    
    You need not pay any attention to the values in these options, as they will not be used.
  7. In the [debian] section, create a new option, basedebs, and set the full path to your basedebs.tar file as its value:
    basedebs=/path/to/basedebs.tar
    
  8. Now all you need to do is run rootstrap. It will pick up the the configuration file in your working directory and build the new root image you specify:
    $ rootstrap uml01.img
    
    You can give your image any name that seems more sensible to you.
  9. All being well, rootstrap should produce lots of output and a file which can now be tested with User Mode Linux:
    $ linux ubd0=uml01.img
    
  10. Log in as root, with a null password (i.e. just hit [Enter] when prompted for a password)

Adding extra packages

So, we now wish to add more packages to our root filesystem builds, packages which do not exist in basedebs.tar. In this example I will add ssh from my APT cache.

First I check that ssh is installed:

$ dpkg -l ssh
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
ii  ssh            3.4p1-1.woody. Secure rlogin/rsh/rcp replacement (OpenSSH)

Now I add the ssh package from my APT cache into the basedebs.tar file. Usually I make a copy of basedebs.tar and modify that, so if I mess something up I can go back to a pristine file:

$ cp basedebs.tar gdb_basedebs.tar
$ tar -rvf gdb_basedebs.tar /var/cache/apt/archives/ssh_1%3a3.4p1-1.woody.3_i386.deb 
tar: Removing leading `/' from member names
var/cache/apt/archives/ssh_1%3a3.4p1-1.woody.3_i386.deb
Please note that the filename of the ssh package will probably be different to mine, if you are using a different release, or there has been a security fix to SSH since I wrote this article. If there is more than one .deb file for the package you are copying, try doing:
# apt-get autoclean

This will remove obseleted package files from your APT cache.

Now we edit the rootstrap.conf file to specify that SSH should be installed and update the basedebs option, if necessary:

[debian]

...

basedebs=/path/to/gdb_basedebs.tar

...

install=ssh

Run rootstrap:

$ rootstrap uml02.img

Finally I test the images with UML:

$ linux ubd0=uml02.img