How to Build deb Packages in Ubuntu

By rebuilding the .deb file that is used to build a Debian package, you can change it
to better suit the way you use the software (for example, including an md5sum file).
To begin, you need to extract a .deb file that you want to modify into a working
directory. You then modify the file tree and control files to suit your needs.
For example, you could download and extract the rsync package and control files into
the current directory by typing the following commands (your $RANDOM directory will
be different of course):

$ aptitude download rsync

Then extract the package contents and the control files from the downloaded file. Note that the $RANDOM directory is found by typing /tmp/rsync_ and pressing Tab:

$ sudo dpkg -x rsync_2.6.9-3ubuntu1.1_i386.deb /tmp/rsync_$RANDOM
$ sudo dpkg -e rsync_2.6.9-3ubuntu1.1_i386.deb /tmp/rsync_17197/

Now change to your package directory, where you extracted the .deb file to, and have
a look around. You should see a directory structure that looks very similar to this:

$ cd /tmp/rsync_17197
$ ls -lart
-rwxr-xr-x 1 root root 491 2007-08-17 20:47 prerm
-rwxr-xr-x 1 root root 110 2007-08-17 20:47 postrm
-rwxr-xr-x 1 root root 523 2007-08-17 20:47 postinst
drwxr-xr-x 4 root root 4096 2007-08-17 20:48 usr
drwxr-xr-x 4 root root 4096 2007-08-17 20:48 etc
-rw-r--r-- 1 root root 37 2007-08-17 20:48 conffiles
-rw-r--r-- 1 root root 985 2007-09-02 12:02 control
drwxr-xr-x 4 root root 4096 2007-09-02 12:02 .
drwxrwxrwt 10 root root 4096 2007-09-02 13:24 ..

Now you have to configure the package directory to fit the formats that dpkg will want for building the .deb file. This involves creating a subdirectory named rsync_2.6.9-3cn1.1/DEBIAN and moving the install files into it. The control file itself is a specially formatted file that contains header and content fields and is parsed by the package tools to print out information about the package:

$ sudo mkdir –p rsync_2.6.9-3cn1.1/DEBIAN
$ sudo mv control conffiles prerm postrm postinst rsync_2.6.9-3cn1.1/DEBIAN

You also need to move the etc/ and usr/ directories under the rsync_2.6.9-3cn1.1
directory:

$ sudo mv usr etc rsync_2.6.9-3cn1.1

You should end up with everything filed away correctly, and all that is left is the
rsync_2.6.9-3cn1.1 directory in your current directory. Now move the md5sums file you made earlier into your DEBIAN subdirectory and rename it to md5sums. This will allow debsums to have some md5sums to check:

$ sudo mv /var/lib/dpkg/info/rsync.md5sums rsync_2.6.9-3cn1.1/DEBIAN/md5sums

Now edit the control file to modify some of the information. You certainly don’t want
to install your modified version of rsync with the same package info as the original.
Open the control file in vi or another editor and change the Version line to reflect the one below. You will notice the word Version has a colon after it; this is the header field. The information field follows right after it. Be sure to maintain the space after the colon, and do not put any extra carriage returns or spaces in the file. It is very picky about formatting.

$ sudo vi rsync_2.6.9-3cn1.1/DEBIAN/control
...
Version: 2.6.9-3cn1.1
...

A little farther down, you can add to the Description field. This will show up in the
descriptions whenever someone views the package details. Notice the space right
before the words fast remote .... The space is part of the special formatting and is
how dpkg tells the description text from the multiline header. Be sure to put a space
in the first column if you wrap the description to the next line:
...

Description: Modified by CN 2007-09-02 to include md5sums.
fast remote file copy program (like rcp)
...

Now build your new package using dpkg –b and the name of the control file subdirectory you created. You will get a warning about Original-Maintainer being a
user-defined field. You can safely ignore the warning.

$ sudo dpkg -b rsync_2.6.9-3cn1.1
warning, `rsync_2.6.9-3cn1.1/DEBIAN/control' contains user-defined field
`Original-Maintainer'
dpkg-deb: building package `rsync' in `rsync_2.6.9-3cn1.1.deb'.
dpkg-deb: ignoring 1 warnings about the control file(s)
You now have a new .deb file and can ask dpkg to display information about it. Just
run dpkg with the –I option to see the new package info:
$ dpkg -I rsync_2.6.9-3cn1.1.deb

new debian package, version 2.0.
size 1004 bytes: control archive= 712 bytes.
970 bytes, 21 lines control
Package: rsync
Version: 2.6.9-3cn1.1
...

You could install the new rsync package at this point. This exercise is mainly a
demonstration for building a custom package, not necessarily for hacking up the
system needlessly. Nonetheless, the following code shows that this package will
install and act like a regular Debian package. You want debsums to work also.
Notice dpkg tells you about the downgrade:

$ sudo dpkg -i rsync_2.6.9-3cn1.1.deb

dpkg - warning: downgrading rsync from 2.6.9-3ubuntu1 to 2.6.9-3cn1.1.
(Reading database ... 88107 files and directories currently installed.)
Preparing to replace rsync 2.6.9-3ubuntu1 (using rsync_2.6.9-3cn1.1.deb) ...
Unpacking replacement rsync ...
Setting up rsync (2.6.9-3cn1.1) ...

The debsums utility now has some md5sum files to test with, and anywhere your new
rsync package is installed, this will be the same:

$ debsums rsync
/usr/bin/rsync OK
/usr/share/doc/rsync/examples/rsyncd.conf OK
/usr/share/doc/rsync/README.gz OK
...

You can also ask dpkg to list your rsync package using the –l option to confirm that
the new version is installed:

$ dpkg -l rsync
...
ii rsync 2.6.9-3cn1.1 Modified by CN 2007-09-02 to include md5sums.
NOTE You can find out more about building .deb files by visiting the Debian
Binary Package Building HOWTO (http://tldp.org/HOWTO/Debian- Binary-Package-Building-HOWTO). The dpkg-deb man page is also a good source of info on deb package building.