I had to create some .deb packages for my work and some co-workers where also interested, so in this post I will be outlining how we created a example package.
Structure of a Debian package
Debian packages must adhere to a strict directory structure. This includes a so-called control file and other scripts that look after what happens during installation of the package.
The control file, DEBIAN/control
The control file is the core of the Debian package; it contains all relevant metadata. Things such as package name, version, supported architectures, and dependancies are all included in this file. Here is what our example control file looks like:
Depends: postfix | mail-transport-agent
Maintainer: your name here <your email @ address here>
Description: A small description about the package can be placed here
You can also do multiple lined descriptions.
The conffiles, DEBIAN/conffiles
Configuration files, or “conffiles”, are files that are included within a Debian package that may be changed by a user. When uninstalling the package, a user or system administrator dealing with the package may not want his or her configuration files removed. Running sudo apt-get remove apache2 will remove all installed package files except for those listed in the DEBIAN/conffiles file. Here is what ours looks like:
If a user truly wants to remove everything including the configuration files, this can be done by using the purge command instead of remove: sudo apt-get purge example.
The md5sums file (DEBIAN/md5sums)
This file is a list of MD5 checksums for all files contained within the package that will actually be extracted to the system. This can be most easily generated using a combination of commands:
find . -type f ! -regex '.*\.hg.*' ! -regex '.*?debian-binary.*' ! -regex '.*?DEBIAN.*' -printf '%P ' | xargs md5sum > DEBIAN/md5sums
The postinst and prerm scripts, DEBIAN/postinst and DEBIAN/prerm
Many packages need to perform actions before or after specific events during the package installation or uninstallation process. Debian packages accommodate for this via scripts. For instance, after installation, our example package installs its init script automatically. Below is the script that is run after installation, postinst:
# Make package directories.
mkdir -p /var/log/example/ || echo "Could not create /var/log/example"
The rest of it
The remainder of the directory structure must resemble the resultant structure after installation of the package. For example, because the example is installed to /usr/bin/example, there is a usr/bin/example directory within our package root.
Creating the package
When all of the above is in place, creating the actual Debian package, or .deb file, is easy:
dpkg -b example/ example.deb
dpkg-deb: building package `example' in `example.deb'.