Advanced Debian Packaging

The tool pbuilder can be used to build Debian packages in a clean room / chroot environment, e.g., building for stable on an unstable system.

Creating the Build Environment

First of all you need a build environment. pbuilder will bootstrap a Debian base system as follows:

$ pbuilder create

This will create a base system in /var/cache/pbuilder of the same distribution and architecture as your installation. If you provide some options, pbuilder can bootstrap any combination of distribution and architecture. Save the following in a file in your home directory with the name .pbuilderrc.

: ${DIST:=$(lsb_release --short --codename)}
: ${ARCH:=$(dpkg --print-architecture)}
NAME="$DIST-$ARCH"
DISTRIBUTION="$DIST"
DEBOOTSTRAPOPTS=("--arch" "$ARCH" "${DEBOOTSTRAPOPTS[@]}")
BASETGZ="`dirname $BASETGZ`/$NAME-base.tgz"
BUILDRESULT="/var/cache/pbuilder/$NAME/result/"
APTCACHE="/var/cache/pbuilder/$NAME/aptcache/"
MIRRORSITE="ftp://ftp.fu-berlin.de/pub/unix/linux/debian/"
OTHERMIRROR="deb http://uhu.mi.fu-berlin.de/packages lenny main non-free contrib"
COMPONENTS="main contrib non-free"

Now you have to call pbuilder with your options of choice:

$ DIST=lenny ARCH=i386 pbuilder create
$ DIST=sid ARCH=i386 pbuilder create
$ DIST=lenny ARCH=amd64 pbuilder create
$ DIST=sid ARCH=amd64 pbuilder create

Updating the Build Environment

$ DIST=lenny ARCH=i386 pbuilder update

This updates the tgz file of the specified distribution. First of all, apt-get update is called and then the packages are upgraded by apt-get upgrade.

Building a Debian Package

Using pdebuild you can build packages as follows when you are in the root dir of your sources  (pdebuild is the pbuilder way of doing debuild):

$ DIST=lenny ARCH=i386 pdebuild --auto-debsign

If you are using svn-buildpackage you have to tell that you want to use pdebuild instead of the default debuild:

$ DIST=lenny ARCH=i386 svn-buildpackage --svn-lintian --svn-builder="pdebuild --auto-debsign" 

 To make your life a little bit easier you can use this shell script to build multiple packages for several distribution and architecture combinations. Modify the distribution and architecture lists as required.

#!/bin/sh
PBUILDER_TGZ_DIR=/var/cache/pbuilder
DISTS="sid lenny"
ARCHS="i386 amd64"

for DIST in $DISTS; do
    for ARCH in $ARCHS; do
        mkdir -p `pwd`/../build-area/$DIST-$ARCH
        svn-buildpackage --svn-lintian --svn-move-to=`pwd`/../build-area/$DIST-$ARCH/ --svn-builder="pdebuild \
        --buildresult `pwd`/../build-area/ --auto-debsign -- --basetgz $PBUILDER_TGZ_DIR/$DIST-$ARCH-base.tgz" 
    done
done

Troubleshooting and Tweaks

  • If you get strange messages about packages being virtual you either missed to add the required repositories or forget to update the build environment.
  • Creating an amd64 chroot image is not possible on an i386 architecture while the inverse is no problem. You should have a look into qemubuilder to build packages in a virtual machine.
  • Building is too slow. Have a look at cowbuilder and ccache.
  • Downloading of packages takes too long. Install an APT cache on your computer and add it to the OTHERMIRROR environment variable or use the --othermirror option of pbuilder.

Uploading

The built files can then be uploaded as described on the APT-Repository page.