Occasionally there comes a time when you need to create your own Debian packages. Whether it be for deployment or needing to customize an install to your needs or whatever other reason you may have.
Personally, in my work, I deploy configurations of various sorts to hundreds of Ubuntu systems and have found that automating the creation of debian packages to be very useful. It allows me to track revisions, easily install, or remove the packages that I have made with scripting hooks that are all part of the package. In this example, we are going to construct a static debian package. This packages only purpose will be to copy files to the OS, and make any permission changes needed. It will also show a simple example of pre-install and post-install scripting and how/where those scripts go.
First, let's disect a simple debian package. In this example, the package we are going to make will be called 'mypackage'. We'll have a parent directory named 'mypackage' Inside the 'mypackage' directory will be a sub-directory called 'DEBIAN' and also contain a simple OS directory replica of where you want to place the file or files on the OS.
mypackage/
----------/DEBIAN/
----------/etc/mycoolfile
In the above example, we are placing 'mycoolfile' in the /etc directory. The real magic in all of this lies in what is actually in the DEBIAN dir.
mypackage/DEBIAN/
----------------control
----------------postrm
----------------preinst
----------------postinst
----------------prerm
Inside mypackage/DEBIAN/ you'll place a file describing your package called 'control', and any scripts that you want to be ran at various points during the installation and/or removal of your package. Here is an example 'control' file
Package: mypackage
Version: 1
Section: base
Priority: optional
Architecture: all
Depends: bash
Maintainer: luke@nerdliness.com
Description: My super cool package
Here is a short description of the other files you may find in the mypackage/DEBIAN/ directory.
postrm = postremove
preinst = preinstall postinst = postinstall prerm = preremove
All the scripts are run at the named times, meaning 'preinstall' is run before the contents of your package are installed. Here is an example 'postinst' script. Keep in mind that these could just be very simple shell scripts.
#!/bin/sh
ln -s /etc/mycoolfile /home/user/
If for whatever reason you want your file to be linked into your home directory putting the above into the mypackage/DEBIAN/postinst file would do it. I would definitely experiment and play around with exactly what you can do with these scripts. One thing to keep in mind, you aren't just limited to shell scripting in these scripts. Personally, I've used ruby as well.
Now, the only thing left is to build our package. We're going to use the 'dpkg' command to do this. From the parent directory that your 'mypackage' folder is in run
dpkg --build mypackage ./
Command Breakdown: dpkg is the debian package manager. In this case we are asking it to build a package, hence the --build parameter. The name of our package is 'mypackage' from that name it will also look for a folder in the parent directory called 'mypackage' and decend into that. It will then look for the DEBIAN directory, and parse the 'control' file for the various options. NOTE: if you didn't create the control file in the right fashion, you will receive errors letting you know this. We are also using ./ to tell dpkg too build the package in the present directory.
If all went well with the above command, you should end up with a freshly created package in your working directory called 'mypackage_1_all.deb' and it should be ready to install. This is a very easy process to automate if your content changes. Happy packaging!


