Differences between revisions 30 and 31
Revision 30 as of 2013-03-24 03:45:21
Size: 9663
Editor: ?AndresMejia
Comment: Add section on using bind mounts
Revision 31 as of 2013-03-24 04:35:59
Size: 13124
Editor: ?AndresMejia
Comment: Add section on customizing sbuild chroots
Deletions are marked like this. Additions are marked like this.
Line 189: Line 189:

== Customizations of sbuild chroots ==

Sometimes it is desirable to further customize your sbuild chroot environment. Typical customizations done are installing more packages inside the chroot, modifying /etc/apt/sources.list, and installing custom scripts to be run inside the chroot.

To modify a chroot, start a session for the chroot with the prefix 'source:'. For example, to modify the sid-$arch-sbuild chroot, start a session for the chroot as follows.

{{{
sudo sbuild-shell source:sid-$arch-sbuild
}}}

This will start a session inside the sid-$arch-sbuild chroot. Any modifications done inside the chroot will be saved upon exiting. Inside this chroot, you may run {{{apt-get}}} commands to install or remove packages as desired. For example, to install ccache, do the following in the chroot session.

{{{
apt-get install ccache
}}}

Note that you are already root inside a chroot session. Also, sudo would typically not be installed in the chroot anyway.

Making other modifications such as editing /etc/apt/sources.list or adding scripts inside the chroot is best done from outside the chroot session. In order to do this, leave the current chroot session open and start another terminal session. In the other terminal session, find the path used for the existing chroot session as follows.

{{{
schroot --info --all-sessions | grep Path
}}}

This should output exactly one line and should specify the path of the chroot session. It should look something like this.

{{{
  Path /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c
}}}

Doing an {{{ls}}} on this directory should resemble a standard listing of the '/' directory.

With the above path, the sources.list file will be in the following path.

{{{
/var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list
}}}

Open the sources.list file at this path with root privileges using your favorite editor. For example, using nano, do the following in your secondary terminal session.

{{{
sudo nano /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list
}}}

For the graphically inclined, here is an example using {{{kate}}}, a KDE based text editor.
{{{
kdesudo kate /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list
}}}

Here's another example using {{{gedit}}}
{{{
gksudo gedit /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list
}}}

Proceed to edit the sources.list file as you see fit, then save.

To add scripts inside the chroot, simply place the scripts in the following directory.

{{{
/var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/usr/local/bin
}}}

Be sure to make the scripts executable.

{{{
sudo chmod a+x /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/usr/local/bin/*
}}}

Other modifications may be done to the chroot, either by running commands available within the chroot session, or by running commands with root privileges via the secondary terminal session. Once you are done making modifications to the chroot, simply exit the session. Inside the chroot session, do the following.

{{{
exit
}}}

After exiting, your modifications will be saved and made available for every new chroot session created afterwards.

sbuild

sbuild is used on the official buildd network to build binary packages for all supported architectures. It can also be used by individuals to prepare packages. Alternatives to sbuild are cowbuilder and pbuilder.

This page is intended as a short guide to sbuild. It documents how to set up sbuild and build packages with it, but intentionally does not document all possible options to keep it short and simple.

Setup

To get started so you may build packages for Debian sid, run the following.

sudo apt-get install sbuild
sudo sbuild-createchroot --make-sbuild-tarball=/var/lib/sbuild/sbuild.tar.gz sid `mktemp -d` http://ftp.debian.org/debian
sudo sbuild-adduser $LOGNAME
sudo sbuild-update --keygen
cp /etc/sbuild/sbuild.conf ~/.sbuildrc

Now for a brief explanation on what these commands do.

The first line installs sbuild onto the system.

The second line uses sbuild-createchroot to create a chroot used by sbuild meant for building packages targeting Debian sid main. The chroot is saved as a tarball in /var/lib/sbuild/sbuild.tar.gz. The apt repository used is http://ftp.debian.org/debian. This can be changed to use a URL for a local mirror of the Debian archive.

The third line will add your username so that it may use the sbuild command. Additional users may be added by running sudo sbuild-adduser USER1 USER2 ....

The fourth line generates apt keys used internally by sbuild.

The fifth line copies the system sbuild configuration onto your home directory, to be used as your user sbuild configuration.

To finish setting up sbuild on your system, some changes should be done to your user sbuild configuration file. Open up ~/.sbuildrc with your favorite text editor. The following lines should be found and set approppriately.

$build_arch_all = 1;
$build_source = 1;
$distribution = 'unstable';
$chroot = 'sid-$arch-sbuild';

The $build_arch_all variable will enable building of architecture independent packages by default.

The $build_source variable will enable building of source packages by default.

The $distribution variable will set the distribution to build for as 'unstable'.

The $chroot variable will set the chroot to use as the 'sid-$arch-sbuild' chroot, which was built using the sbuild-createchroot program from earlier. Here $arch is the architecture installed on your machine.

These settings are typically set if you are to build packages to be uploaded to the Debian archive. All the options can be overridden by setting the appropriate arguments when running the sbuild command.

Updating

Sbuild chroot should be updated at least once per day before building packages. Use the sbuild-update to perform updates.

First, note the name of the sbuild chroot to be updated. All sbuild chroots built with sbuild-createchroot will have a suffix of '-sbuild' thus to find the names of all sbuild chroots, run the following.

schroot -l | grep sbuild

If you followed the setup instructions above, there should be one chroot named source:sid-$arch-sbuild where $arch is the architecture installed on your machine.

After noting the name of your sbuild chroot, run the following.

sudo sbuild-update -udcar sid-$arch-sbuild

The arguments '-udcar' will tell sbuild-update to run an apt-get update, dist-upgrade, clean, autoclean, and autoremove in the chroot.

Building packages

To build a package from the source directory of a debianized package, simply run the following.

sbuild

Alternatively, you may pass in the '.dsc' file of a package generated by dpkg-buildpackage, git buildpackage, and so forth so that it may be built with sbuild. For example, to build sbuild from its '.dsc' file, do the following.

sbuild sbuild_*.dsc

To build packages available from the apt repositories used in the sbuild chroot, pass in a package to sbuild in the format $package_$version. For example, to build sbuild version 0.63.2-1.1, do the following.

sbuild sbuild_0.63.2-1.1

Everything needed to build sbuild version 0.63.2-1.1 will be downloaded from the repositories and will be saved in your current directory after building of the packages is finished.

To build packages that are not yet in the repositories but which have their source package available online, the URL to the source package's '.dsc' can be passed into sbuild. This requires that the system have libwww-perl installed (so that it has the Perl module LWP::UserAgent).

First ensure that libwww-perl is installed.

sudo apt-get install libwww-perl

Now you can build any package from its '.dsc' file, whether it is stored locally on the system, or it is available online. For example, to build sbuild using the '.dsc' file from the Debian archive, do the following.

sbuild http://ftp.debian.org/debian/pool/main/s/sbuild/sbuild_0.63.2-1.1.dsc

Using lintian

The use of lintian with sbuild can greatly aid with increasing the quality of Debian packages. To use lintian with sbuild, first install lintian.

sudo apt-get install lintian

Next, edit your ~/.sbuildrc configuration file. Open ~/.sbuildrc with your favorite text editor and edit the lines with the following variables.

$run_lintian = 1;
$lintian_opts = ['-i', '-I'];

The $run_lintian variable will enable running of lintian after a successful build with sbuild.

The $lintian_opts variable is an array of options to pass to lintian. Here the options are '-i' which will output information about lintian warnings and errors, and '-I' which will output lintian "info" messages.

Using piuparts

The use of piuparts with sbuild is another feature meant to enhance the quality of Debian packages built with sbuild. To use piuparts, first install piuparts.

sudo apt-get install piuparts

Next, edit your ~/.sbuildrc configuration file. Open ~/.sbuildrc with your favorite text editor and edit the lines with the following variables.

$run_piuparts = 1;
$piuparts_opts = ['-b', '/var/lib/sbuild/sbuild.tar.gz'];

The $run_piuparts variable will enable running piuparts after a successful build with sbuild.

The $piuparts_opts variable is an array of options to pass to piuparts. Here the options are the "-b <path/to/tarball>" option in piuparts. This will instruct piuparts to use sbuild chroot '/var/lib/sbuild/sbuild.tar.gz' as the chroot used in its testing of packages.

Bind mounts

Directories on the local filesystem can be bind mounted and made available in the sbuild chroots. To do this, add an entry in /etc/schroot/sbuild/fstab.

The following example shows how to bind mount the apt archive cache inside an sbuild chroot. This is useful when not using a local mirror of the Debian repository.

sudo echo /var/cache/apt/archives /var/cache/apt/archives none rw,bind 0 0 >>/etc/schroot/sbuild/fstab

Customizations of sbuild chroots

Sometimes it is desirable to further customize your sbuild chroot environment. Typical customizations done are installing more packages inside the chroot, modifying /etc/apt/sources.list, and installing custom scripts to be run inside the chroot.

To modify a chroot, start a session for the chroot with the prefix 'source:'. For example, to modify the sid-$arch-sbuild chroot, start a session for the chroot as follows.

sudo sbuild-shell source:sid-$arch-sbuild

This will start a session inside the sid-$arch-sbuild chroot. Any modifications done inside the chroot will be saved upon exiting. Inside this chroot, you may run apt-get commands to install or remove packages as desired. For example, to install ccache, do the following in the chroot session.

apt-get install ccache

Note that you are already root inside a chroot session. Also, sudo would typically not be installed in the chroot anyway.

Making other modifications such as editing /etc/apt/sources.list or adding scripts inside the chroot is best done from outside the chroot session. In order to do this, leave the current chroot session open and start another terminal session. In the other terminal session, find the path used for the existing chroot session as follows.

schroot --info --all-sessions | grep Path

This should output exactly one line and should specify the path of the chroot session. It should look something like this.

  Path                   /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c

Doing an ls on this directory should resemble a standard listing of the '/' directory.

With the above path, the sources.list file will be in the following path.

/var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list

Open the sources.list file at this path with root privileges using your favorite editor. For example, using nano, do the following in your secondary terminal session.

sudo nano /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list

For the graphically inclined, here is an example using kate, a KDE based text editor.

kdesudo kate /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list

Here's another example using gedit

gksudo gedit /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/etc/apt/sources.list

Proceed to edit the sources.list file as you see fit, then save.

To add scripts inside the chroot, simply place the scripts in the following directory.

/var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/usr/local/bin

Be sure to make the scripts executable.

sudo chmod a+x /var/lib/schroot/mount/sid-amd64-sbuild-5bad48fe-9823-4454-815f-b869d1d7b22c/usr/local/bin/*

Other modifications may be done to the chroot, either by running commands available within the chroot session, or by running commands with root privileges via the secondary terminal session. Once you are done making modifications to the chroot, simply exit the session. Inside the chroot session, do the following.

exit

After exiting, your modifications will be saved and made available for every new chroot session created afterwards.

Using ccache with sbuild

Sometimes you are building a big package and after building (and waiting for too long) you realize that there was a problem in a install file. Using ccache one can avoid to have to wait for that long again for a simple fix.

Installing ccache in the chroot

cd /; schroot -c $distribution-$architecture-sbuild apt-get install ccache

Create a script to setup ccache on each build

This script was modified from the original from Modestas Vainius. Put it in /srv/chroot/$distribution-$architecture-sbuild/usr/local/bin/build-env.sh

if [ -z "$CCACHE_DISABLE" ]; then
    export CCACHE_DIR=/var/cache/ccache-`dpkg-architecture -qDEB_HOST_ARCH`
    export CCACHE_UMASK=002
    export CCACHE_BASEDIR=`pwd`
    export CCACHE_COMPRESS=1
    export PATH="/usr/lib/ccache:$PATH"
    # Set ccache's cache size to 4G, special for big builds.
    # Use an appropiate value here.
    ccache --max-size 4G
fi

Don't forget to chmod it at least to 744.

Then edit ~/.sbuildrc and add/uncomment the following line:

$build_env_cmnd = "build-env.sh";