Tuesday, May 26, 2009

How to edit / manipulate a .deb file of a compiled application

First of all, as I mentioned in the title, this post is about how to create or manipulate deb files of an already compiled application. In this topic I’m not going to explain you how to create a deb starting from source code :)

I’ll try to give you every possible indication in this article, if you miss some information but find the article interesting, please comment and ask me how to make things I failed to explain.

Not going to tell you what a .deb file is and what it is for, but you should really know that a deb is just a common archive in an .ar format, camouflaged with another name. So, right clicking on the deb file and choosing to open it with a common compressed files manager (such as file roller in gnome and OpenGEU) will work and show you the contents of the file. Now, you can modify an already existent deb file or create one from scratch. Should you prefer the second option, don’t be scared, it really is a simple task.

A deb file contains two main files, so, to manipulate an existing deb, open it with an archive manager and extract those files wherever you wish. The files we are talking about are:

* control.tar.gz
* data.tar.gz

Now, create a new dir with the name of the applications you’re creating (in reality, the name of this dir means nothing but you may still want the name to have a meaning, just for your convenience..), then, inside of this dir, create a subdir called debian. Now, enter the debian dir and create another sub-dir called DEBIAN. All of this work is case sensitive so use the right letters. The dir structure at this point should look like this:

application_name –> debian –> DEBIAN

Very well now, you can open the file called control.tar.gz

Inside of this file you’ll find some other files, just extract them into the DEBIAN dir as they are. Now open the file called data.tar.gz and extract its contents into the debian dir as they are, with any contained folder and subfolder: in other words, leave the directory structure of the compressed archive unaltered.

Perfect, not we have just recreated the directory structure of the original deb file. Enter the DEBIAN dir, you’ll find there a file called “control” (without quotes, never consider quotes from now on in the article please).

control

This is the main “brain” of a debian file: it contains all of the informations reguarding this package, without this file the entire creation of a deb package has no meaning at all. A control file looks like this (quoting the contents of the opengeu-artwork-usplash’s control file as an example):

Package: opengeu-artwork-usplash
Version: 3.01
Section: x11
Priority: optional
Architecture: all
Depends: libc6 (>= 2.5-0ubuntu1), initramfs-tools (>= 0.40ubuntu30), usplash (>= 0.4-21)
Installed-Size: 1010
Maintainer: darkmaster
Conflicts: geubuntu-artwork-usplash
Replaces: geubuntu-artwork-usplash
Description: OpenGEU usplash image
The startup screen shown on OpenGEU boot, by default it is the Sunshine version, you can later change it to the Moonlight edition too using usplash-switcher.

Let’s analyze the important parts this file together:

* Package: you have to put here the name of the package, never use spaces, never use a version number. It is case sensitive, be warned!
* Version: of course, the version number / code. It can also be, for example, opengeu-3.0.2 or any other weird code, as long as the last part is a number. Always remember that chaning the version to a higher number triggers the system to consider that new file as an update of course!
* Priority: optional, high, low, only system or security updated should have something like high in the priority.
* Architecture: i386, amd64, all, you should specify here what platform this package is compiled for. If it is only an artowork or a metapackage, you can put all in the Architecture field.
* Depends: a list of all the packages this package depends on. A metapackage, for example, can include almost nothing but depend on a lot of other packages so that installing this metapackage automatically installs a lot of other packages! That’s what happens with opengeu-desktop for example!
* Conflicts: if you know that your package can’t work if another package is installed, well, you should then list that package here.
* Replaces: Here you specify that this file replaces another package, maybe an old one. Useful if you change the name of the package into something else and whant the new package to be installed INSTEAD of the package to be replaced.
* Description: put only a very short description in the first line, then enter a longer description on the second line, like it is shown in the example above.

Well, and that’s almost eveything about the control file. But this is not the only file you can find in the DEBIAN dir. For example, you could find also a prerm or postinst file, or you could find both of them: they are scripts.

postinst

This script will execute any action contained in it right after the installation of the package. Here’s an example, the contents of the postinst file contained in the opengeu-artwork-usplash package:

#!/bin/sh

set -e

case “$1″ in
configure)
update-alternatives –remove usplash-artwork.so /usr/lib/usplash/usplash-theme-opengeu-sunshine.so
update-alternatives –install /usr/lib/usplash/usplash-artwork.so usplash-artwork.so /usr/lib/usplash/usplash-theme-opengeu-sunshine.so 55
update-alternatives –install /usr/lib/usplash/usplash-artwork.so usplash-artwork.so /usr/lib/usplash/usplash-theme-opengeu-moonlight.so 55
update-alternatives –set usplash-artwork.so /usr/lib/usplash/usplash-theme-opengeu-sunshine.so
update-initramfs -u
;;
esac

So, right after the installation o the package, those commands are executed, very simply!

prerm

This script instead is executed right before the package is removed! Here are teh contents of the prerm file contained in the opengeu-artwork-usplash package:

#! /bin/sh

set -e

case “$1″ in
remove)
update-alternatives –remove usplash-artwork.so /usr/lib/usplash/usplash-theme-opengeu-sunshine.so
update-initramfs -u
;;
esac

If you are creating a new deb file from scratch, remember to right click on those files, move the permission tag and make them executable, or they won’t work.

Keeping the folders clean

Every time you create or modify a new text / script file, some temporary files are created and we don’t want them to go into the deb. So enable your file browser to see hidden files and remove the temporary files before compiling the deb!

Data

Every file that has to be installed into the system goes into the debian folder. Let’s say that this is like the heart of our deb package. Consider this folder as the “/” mount point of your system, the main system folder or whatever you call it. Basically, you have to recreate into the debian folder the structure of the folders and files you wish to be installed into the system. So, let’s say that our package has to install the usplash-theme-opengeu-moonlight.so file into the /usr/lib/usplash/ folder, then, you’ll have to create or edit the files and folders status to look like this:

package-name/debian/usr/lib/usplash/usplash-theme-opengeu-moonlight.so

Installing the deb file will cause the usplash-theme-opengeu-moonlight.sot o be copied in the right place :)

You can of course add multiple files into the same package with multiple folder structures but remember that if you create a package containing the same files as another already installed package, and if this new package is not an upgrade to the other one, the installation will fail. apt-get will give you an error saying that the file is already contained in another package and therefore it cannot be overwritten.

Creating the real package

Now that we created the contents and the controls of the package, let’s build it so that a deb file is created for us automatically. Move yourself in the main folder of the application, in our example, it should be the “package-name” folder. Open a terminal here and run:

sudo dpkg-deb -build debian

You’ll be asked to install some extra packages the first time you use this command, just do it and then run the command again. A debian.deb file will be created in the dir you are in, just rename it to mirror your tastes, I suggest you to try and name it with the same name you mentioned in the control file, followed by the file version, for example: opengeu-artwork-usplash-3.0.deb and remember never to use spaces!

Conclusions

The file is now ready to be installed. In this way and knowing the files structure you can create any new deb from scratch or edit an existent one, no difference. Just use any control file as a template. Remember to keep your folder structure clean and that it is case sensitive and you’ll soon create nice deb packages :-)

Note: Above article is republished here with the permission of original author. Author and the original post can be found here


2 comments:

  1. Hi,

    How do I specify the permissions the file should take on the target machine?

    Thanks in advance,

    Jithin

    ReplyDelete
  2. you may clear your doubt from the original author

    http://thedarkmaster.wordpress.com/2008/05/24/how-to-create-manipulate-a-deb-file-of-a-compiled-application/

    ReplyDelete