Archive

Archive for January 24th, 2009

Build Own deb Package

January 24th, 2009 No comments

About Deb package

Normal Debian packages get a proper source package, including a debian/rules file which automates the steps involved in creating the binary package. Here we just show how to package a simple shell script or binary executable into a small binary package.

From the dpkg-deb man page: “dpkg-deb packs, unpacks and provides information about Debian archives. .deb files can also be manipulated with ar and tar alone if necessary. Use dpkg to install and remove packages from your system.”

.deb package are just the tar archives but with a proper structural format of  files. You can see and extract any deb package with any archive manager tool.

You might find lots of example .deb files in directory ‘/var/cache/apt/archives/‘. With ‘dpkg-deb -I somepackage.deb‘ you might get a general overview of what this package offers in particular. ‘dpkg-deb -c somepackage.deb‘ lists all files which will be installed.

List content of the .deb file with ‘ar tv somepackage.deb‘. Use the ‘x’ option to extract the files.

Package Structure:

$ ar tv parted_1.4.24-4_i386.deb
rw-r--r-- 0/0      4 Mar 28 13:46 2002 debian-binary
rw-r--r-- 0/0   1386 Mar 28 13:46 2002 control.tar.gz
rw-r--r-- 0/0  39772 Mar 28 13:46 2002 data.tar.gz

Now we can start to extract all files including the content of the tar files.

Debian Binary:

The content of this file is “2.0\n”. This states the version of the deb file format. For 2.0 all other lines get ignored.

data.tar.gz

The ‘data.tar.gz’ file contains all the files that will be installed with their destination paths:

drwxr-xr-x root/root      0 2002-03-28 13:44:57 ./
drwxr-xr-x root/root      0 2002-03-28 13:44:49 ./sbin/
-rwxr-xr-x root/root  31656 2002-03-28 13:44:49 ./sbin/parted
drwxr-xr-x root/root      0 2002-03-28 13:44:38 ./usr/
drwxr-xr-x root/root      0 2002-03-28 13:44:41 ./usr/share/
drwxr-xr-x root/root      0 2002-03-28 13:44:38 ./usr/share/man/
drwxr-xr-x root/root      0 2002-03-28 13:44:52 ./usr/share/man/man8/
-rw-r--r-- root/root   1608 2002-03-28 13:44:37 ./usr/share/man/man8/parted.8.gz
drwxr-xr-x root/root      0 2002-03-28 13:44:41 ./usr/share/doc/
drwxr-xr-x root/root      0 2002-03-28 13:44:52 ./usr/share/doc/parted/
-rw-r--r-- root/root   1880 2002-03-07 14:20:08 ./usr/share/doc/parted/README
-rw-r--r-- root/root   1347 2002-02-27 01:40:50 ./usr/share/doc/parted/copyright
-rw-r--r-- root/root   6444 2002-03-28 13:37:33 ./usr/share/doc/parted/changelog
-rw-r--r-- root/root  15523 2002-03-28 02:36:43 ./usr/share/doc/parted/changelog.gz

It must be the last file in the deb archive.

control.tar.gz

This file has the contents like listed below.

-rw-r--r--    1 root     root         1336 Mar 28  2002 control
-rw-r--r--    1 root     root          388 Mar 28  2002 md5sums
-rwxr-xr-x    1 root     root          253 Mar 28  2002 postinst
-rwxr-xr-x    1 root     root          194 Mar 28  2002 prerm

‘md5sums’ contains for each file in data.tar.gz the md5sum. In our example the content looks like this:

1d15dcfb6bb23751f76a2b7b844d3c57  sbin/parted
4eb9cc2e192f1b997cf13ff0b921af74  usr/share/man/man8/parted.8.gz
2f356768104a09092e26a6abb012c95e  usr/share/doc/parted/README.Debian
a6259bd193f8f150c171c88df2158e3e  usr/share/doc/parted/copyright
7f8078127a689d647586420184fc3953  usr/share/doc/parted/changelog.Debian.gz
98f217a3bf8a7407d66fd6ac8c5589b7  usr/share/doc/parted/changelog.gz

‘prerm’ and ‘postinst’ seem to take care of removing old documentation files and adding a link from doc to share/doc.

$ cat postinst
#!/bin/sh
set -e
# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
  if [ -d /usr/doc -a ! -e /usr/doc/parted -a -d /usr/share/doc/parted ]; then
    ln -sf ../share/doc/parted /usr/doc/parted
  fi
fi
# End automatically added section
$ cat prerm
#!/bin/sh
set -e
# Automatically added by dh_installdocs
if [ \( "$1" = "upgrade" -o "$1" = "remove" \) -a -L /usr/doc/parted ]; then
  rm -f /usr/doc/parted
fi
# End automatically added section

And finally the most interesting file:

$ cat control
Package: parted
Version: 1.4.24-4
Section: admin
Priority: optional
Architecture: i386
Depends: e2fsprogs (>= 1.27-2), libc6 (>= 2.2.4-4), libncurses5 (>= \
5.2.20020112a-1), libparted1.4 (>= 1.4.13+14pre1), libreadline4 (>= \
4.2a-4), libuuid1
Suggests: parted-doc
Conflicts: fsresize
Replaces: fsresize
Installed-Size: 76
Maintainer: Timshel Knoll <timshel@debian.org>
Description: The GNU Parted disk partition resizing program
 GNU Parted is a program that allows you to create, destroy,
 resize, move and copy hard disk partitions. This is useful
 for creating space for new operating systems, reorganizing
 disk usage, and copying data to new hard disks.
 .
 This package contains the Parted binary and manual page.
 .
 Parted currently supports DOS, Mac, Sun, BSD, GPT and PC98
 disklabels/partition tables, as well as a 'loop' (raw disk)
 type which allows use on RAID/LVM. Filesystems supported are
 ext2, ext3, FAT (FAT16 and FAT32) and linux-swap. Parted can
 also detect HFS (Mac OS), JFS, NTFS, ReiserFS, UFS and XFS
 filesystems, but cannot create/remove/resize/check these
 filesystems yet.
 .
 The nature of this software means that any bugs could cause
 massive data loss. While there are no known bugs at the moment,
 they could exist, so please back up all important files before
 running it, and do so at your own risk.

Further information about the control file can be obtained via ‘man 5 deb-control’.

Below is the example script with intergrate all the above steps together. You can change the script as per your needs.

#!/bin/bash
# This script should be run from within the directory with the .jpg files.
# First, create the empty tree under a new "build" directory
# (the name "build" is completely arbitrary here)
mkdir -p build/usr/share/newwallpapers/
# Put the jpg images in the newwallpapers directory
cp -v *.jpg build/usr/share/newwallpapers/
# move into the build directory, make a docs directory
cd build
mkdir -p usr/share/doc/your-package-name
# write the README file
cat > usr/share/doc/your-package-name/README <<END
This package provides latest desktop wallpapers for Ubuntu desktop.
It moves the original wallpapers aside using dpkg-divert, and
restores them upon uninstallation.
END
# write the control file
cat > control <<END
Package: your-package-name
Version: 0.1
Section: user/themes
Priority: optional
Architecture: all
Installed-Size: `du -ks usr|cut -f 1`
Maintainer: Your-Name-Here <someone@example.com>
Description: New desktop wallpapers, with auto-backup of the originals
 This package contains a set of jpg files. This
 version will preserve your current wallpapers using dpkg-divert and
 restore them upon uninstallation.
 END
# write the pre-installation script
cat > preinst <<END
#!/bin/sh
if [ "$1" = install ]; then
    for f in newhill.jpg get_started.jpg new_era.jpg this_world.jpg heaven.jpg weather.jpg new_lesson.jpg old_building.jpg wemm.jpg coinage.jpg genesis.jpg aslan.jpg googleworld.jpg vcustomer.jpg hacktheworld.jpg manhack.jpg warlock.jpg sicker.jpg; do
        dpkg-divert --package your-package-name --rename --add /usr/share/newwallpaperss/$f
    done
fi
END
# write the post-removal script
cat > postrm <<END
#!/bin/sh
if [ "$1" = remove ]; then
    for f in newhill.jpg get_started.jpg new_era.jpg this_world.jpg heaven.jpg weather.jpg new_lesson.jpg old_building.jpg wemm.jpg coinage.jpg genesis.jpg aslan.jpg googleworld.jpg vcustomer.jpg hacktheworld.jpg manhack.jpg warlock.jpg sicker.jpg; do
        dpkg-divert --package your-package-name --rename --remove /usr/share/newwallpapers/$f
    done
fi
END
# Setting this environment variable fixes Apple's modified GNU tar so that
# it won't make dot-underscore AppleDouble files. Google it for details...
export COPY_EXTENDED_ATTRIBUTES_DISABLE=1
# create the data tarball
# (the tar options "czvf" mean create, zip, verbose, and filename.)
tar czvf data.tar.gz usr/share/newwallpapers/ usr/share/doc/
# create the control tarball
tar czvf control.tar.gz control preinst postrm
# create the debian-binary file
echo 2.0 > debian-binary
# create the ar (deb) archive
ar -r your-package-name.deb debian-binary control.tar.gz data.tar.gz
# move the new deb up a directory
mv your-package-name.deb ..
# remove the tarballs, and cd back up to where we started
rm data.tar.gz control.tar.gz
cd ..

After running this, should have a your-package-name.deb next to your jpg files in the current directory. You can delete the build sub-directory, and try installing your new package.

All the credit goes to http://tldp.org/HOWTO/html_single/Debian-Binary-Package-Building-HOWTO/ and http://synthesize.us/HOWTO_make_a_deb_archive_without_dpkg.

Categories: Linux Tags: