Using dpkg-deb to remake a package

statmonkey

Vsido has some great tools that might be unknown to many users.  One of these hidden gems is dpkg-deb. What dpkg-deb does is allows you to create your own packaged version of an application that already exists as a deb.  It's very simple to use and for anyone wishing to have more control over not just what is on your system but how it actually works it offers some real value.  I will be using dmenu as an example since I recently did this for myself and I hope that it gives you some idea of the functionality of this tool.  Before I start a hat tip to VastOne for starting me on this path, it's been very interesting and enlightening.

Why would you do this?

If, for example there were some incidental changes you made to some package that the Debian devs had not seen fit to include or if you don't like the structure that a package installs by default.  Maybe you want to add features, move file locations, etc.

How technical do you need to be?


Not very, assuming you aren't trying to rebuild essential packages. A basic understanding of file structures, command lines, text editors and you are on your way.

What is not covered here?

I am not going to cover building a deb from source, this is only about repackaging.  Debian offers a great many ways to skin the cat and this is just one of them.  If you are interested in others (most of which are already in our favorite distro) a quick search will present you with some of the options.

What do I need?

Not much. I am pretty sure dpkg-deb is installed by default or by Vsido-welcome in the package building section. If not you can use apt-get to grab it.  So fire up your terminal and play along.

What are the steps?

   Make your project file structure
       Download the deb you want to repackage
   Dis-assemble/extract the deb package
   Copy in your changes/adjustments
   Adjust the version number
   Build your package
   Remove the old package
   Install the new
   Enjoy your customized Linux

What is assumed?

It's going to be assumed that you have made modifications to the way the package functions already.  What I mean by that is this: Let's say you made modifications to dmenu, for example adding in a script you like or altering dmenu_run in some way.  You don't want to use the Debian version because it doesn't consider these changes and you would like to keep them with you in the future and you want to be able to package suckless-tools quickly if they get updated.  You don't have to build it from scratch again, once your modifications are made you are ready to go.

Set Up Your Structure
Download the deb you want to alter and put it in the top-level folder where you are going to repackage. In our case http://ftp.us.debian.org/debian/pool/main/s/suckless-tools/suckless-tools_38-2_amd64.deb
and I put it in the folder project. Decide where you want the project and (you can see the structure of the folders below the commands)
   mkdir project
Now move to the /project folder.
   cd ~/project
and we can start the real work.
   mkdir -p extract/DEBIAN
   mkdir build
Your structure should be as follows:
   project
      extract
      extract/Debian
      build
      
Extract Your Package
Once that is in place we can extract the packages:
   dpkg-deb -x suckless-tools_38-2_amd64.deb  extract/      #our package
   dpkg-deb -e suckless-tools_38-2_amd64.deb  extract/
   
What you will now have is a full file structure showing all the "pieces of a deb package". If you look in the DEBIAN folder you will see the information for control that discusses maintainer, etc. If you look in the overall extract folder you will see where the actual application will install itself.  In this case:
   usr/bin
   usr/share #primarily documentation
This makes (for consistency and understanding) the path look like this:
        project
             extract
                 DEBIAN
                 usr
                     bin
                     share

Apply Your Changes

All well and good but now we need to apply our changes.  This means removing the files we are replacing or moving them were we want them, or even adding to them. For our example we want to replace dmenu_run and add a file dmenu_myscript.  So simply remove dmenu_run from the folder /project/usr/bin in the packaging structure.  You can add or change structure here as well.  As an example we'll put them both in /usr/local/sbin since their our scripts now.  We need to create that structure.  So move to the usr folder in extract

   cd extract/usr
   mkdir -p usr/local/bin

and copy your files into that folder. The only thing you need to be sure of is you get all the changes you made where you want them.  The package program will assume that everything you want to do is in the right place.  This is regardless of what you want to do. For example: If you made changes to dmenu like applying a patch, you would swap out your version for the Debian version in the folder bin or you could even move it to a different folder to be installed there.  Whatever path you set under extract will be the path under the root folder (/) when installed on your system.

Document Your Changes and Keep Track
I would also point out that there must be a file "control" in the folder extract/DEBIAN or dpkg-deb will fail. I am pointing this out because ... well the latest version of suckless-tools is put togther incorrectly and has control in the root folder.  I only caught it after a build failure.

I would suggest that you edit the control file in the DEBIAN folder and change the version number to something other than the Debian original. For example suckless-tools is 38-2 on Debian and in the line "Version"I changed it to 38-2-1 to keep them straight. So open the file control that you find in the folder DEBIAN under extract with your favorite text editor and change the line that starts
   Version: 38-2 #just change 38-2 to what you want, e.g. 38-2-1
If you look you will see a place for notes down below.  If you would like to make some comments so you remember what you did, this is the place.  When you are done save the file and move on.

Build Your Package

Now we just move back to our top level folder and build it
   cd /project
   dpkg-deb -b extract/ build/
   
When it completes,check your build directory you should see your new package called suckless-tools_38-2-1_amd64.deb

Install Your Package
Now just uninstall suckless-tools if you haven't already and run
s dpkg -i suckless-tools_38-2-1_amd64.deb
to install your personalized version.  I would suggest holding the suckless-tools package as well to keep it from being overwritten by apt.

Congratulations you have a little deb file all your very own :)

VastOne

Absolutely stunning How To!

This is most impressive, and I intend to use it to create the packages that will eventually fill and be the VSIDO repo

Well done statman, well done!
VSIDO      VSIDO Change Blog    

    I dev VSIDO

statmonkey

Thank you, glad you found it of some value.  When I get a chance I will post something on building debs from code and/patching as well. I am working on a patch for systemd and sysv to cure some little issues that are driving me nuts.  Once I have the patches done I will run through it again to make sure I know what I am doing and then write it up.