Friday, June 11, 2010

Create Debian and Ubuntu or Redhat and Centos packages

How to Create a Debian/Ubuntu package (.deb) manually

This post describes a low-level, hacky and quick approach with little automatic validation and instrumentation.
This approach is useful for packages which don't require compilation or complicated runtime configuration.
To start learning the more consistent, heavy-weight, validated approaches, fetch some source packages with apt-get source, and have a look at their debian subdirectories.
The merit of our approach is simplicity (you have to understand only a few basic concepts to create your first package) and speed.

A debian package is basically a tar archive, and the essense of creating a debian package is that you

1.create a directory structure in a directory called "debian"
2.you populate the subdirectories of "debian" with files (binaries, configuration files, installation scripts, ...)
3.add some 'control' files that contain information about the package itself
4.pack this "debian" directory in an "archive" ; the deb package
the "debian" directory

create a directory, named "debian" in an appropriate location, eg your in home directory
inside this "debian" directory, create a directorie called "DEBIAN" : This is called the "controll area" and will contain "controll files", files that contain information about the package itself
Then, think of "debian" directory as the root of the filesystem your package will be installed on, and create subdirectories acoordingly. So, id you have a package that will be installed in /usr/sbin, create a directory debian/usr/sbin
eg if your package should install a shell script in /usr/bin, copy myprogram.sh to .../debian/usr/bin/myprogram.sh

The file itself is called 'control', and contains a discription of the package, to build the package,
run dpkg-deb --build debian

Important text files are: ( It's all depends on our requirements needs to customize... )
DEBIAN/control: all meta-information: package name, size, version number, dependencies, short description, long description
DEBIAN/md5sums: md5sum values for all regular files in the package
DEBIAN/postinst: executable script which will be run by dpkg -i after dependencies are installed, and files of this package are extracted
DEBIAN/preinst: executable script which runs prior of post install Scirpt
DEBIAN/prerm: pre-removal Script
DEBIAN/postrm: post-removal Script
DEBIAN/Control file – Contains description of the packagename, version,dependencies, description
DEBIAN/Conffiles - is a text file which list all configuration files that the package contains. The files should be referred to by their absolute path

The order in which the package works is as follows


1. The pre-removal script will execute
2. Then the pre-install script will execute
3. Then the post-Removal Script will execute
4. Then the post-Install script will execute

Recommeded: Some useful/Informative Links Dig more...
http://linuxtrove.com/wp/?p=78
http://www.linuxfordevices.com/c/a/Linux-For-Devices-Articles/How-to-make-deb-packages/
http://tldp.org/HOWTO/html_single/Debian-Binary-Package-Building-HOWTO/
http://blog.boxedice.com/2010/02/05/how-to-create-a-debian-deb-package/
http://ptspts.blogspot.com/2010/02/how-to-create-debianubuntu-package-deb.html
http://users.telenet.be/mydotcom/howto/linux/package.htm


Little Theory about Deb packages ...






What is a Debian package?

Packages generally contain all of the files necessary to implement a set of related commands or features. There are two types of Debian packages:
Binary packages, which contain executables, configuration files, man/info pages, copyright information, and other documentation. These packages are distributed in a Debian-specific archive format
Source packages, which consist of a .dsc file describing the source package (including the names of the following files), a .orig.tar.gz file that contains the original unmodified source in gzip-compressed tar format and usually a .diff.gz file that contains the Debian-specific changes to the original source.

What is the format of a Debian binary package?

A Debian "package", or a Debian archive file, contains the executable files, libraries, and documentation associated with a particular suite of program or set of related programs. Normally, a Debian archive file has a filename that ends in .deb.

Why are Debian package file names so long?

The Debian binary package file names conform to the following convention: _-_.deb

The Package field gives the package name. This is the name by which the package can be manipulated by the package tools, and usually similar to but not necessarily the same as the first component string in the Debian archive file name.

The Version field gives both the upstream developer's version number and (in the last component) the revision level of the Debian package of this program.

The Architecture field specifies the chip for which this particular binary was compiled.

The Depends field gives a list of packages that have to be installed in order to install this package successfully.

The Installed-Size indicates how much disk space the installed package will consume. This is intended to be used by installation front-ends in order to show whether there is enough disk space available to install the program.

The Section line gives the "section" where this Debian package is stored at the Debian FTP sites.

The Priority indicates how important is this package for installation, so that semi-intelligent software like dselect or aptitude can sort the package into a category of e.g. Essential, Required, Important, Standard, Optional, or Extra.

The Maintainer field gives the e-mail address of the person who is currently responsible for maintaining this package.
The Description field gives a brief summary of the package's features.

What is a Debian conffile?

Conffiles is a list of configuration files (usually placed in /etc) that the package management system will not overwrite when the package is upgraded. This ensures that local values for the contents of these files will be preserved, and is a critical feature enabling the in-place upgrade of packages on a running system.
To determine exactly which files are preserved during an upgrade, run:
dpkg --status package

And look under "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 sd-agent will remove all installed package files except for those listed in the DEBIAN/conffiles file. Here is what ours looks like:
/etc/sd-agent/config.cfg
/etc/init.d/sd-agent
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 sd-agent

What is a Debian preinst, postinst, prerm, and postrm script?

These files are executable scripts which are automatically run before or after a package is installed. Along with a file named control, all of these files are part of the "control" section of a Debian archive file.
The individual files are:
preinst
This script executes before that package will be unpacked from its Debian archive (".deb") file. Many 'preinst' scripts stop services for packages which are being upgraded until their installation or upgrade is completed (following the successful execution of the 'postinst' script).
postinst
This script typically completes any required configuration of the package foo once foo has been unpacked from its Debian archive (".deb") file. Often, 'postinst' scripts ask the user for input, and/or warn the user that if he accepts default values, he should remember to go back and re-configure that package as the situation warrants. Many 'postinst' scripts then execute any commands necessary to start or restart a service once a new package has been installed or upgraded.
prerm
This script typically stops any daemons which are associated with a package. It is executed before the removal of files associated with the package.
postrm
This script typically modifies links or other files associated with foo, and/or removes files created by the package.

What is meant by saying that a package Depends, Recommends, Suggests, Conflicts, Replaces or Provides another package?

The Debian package system has a range of package "dependencies" which are designed to indicate (in a single flag) the level at which Program A can operate independently of the existence of Program B on a given system:
Package A depends on Package B if B absolutely must be installed in order to run A. In some cases, A depends not only on B, but on a version of B. In this case, the version dependency is usually a lower limit, in the sense that A depends on any version of B more recent than some specified version.
Package A recommends Package B, if the package maintainer judges that most users would not want A without also having the functionality provided by B.
Package A suggests Package B if B contains files that are related to (and usually enhance) the functionality of A.
Package A conflicts with Package B when A will not operate if B is installed on the system. Most often, conflicts are cases where A contains files which are an improvement over those in B. "Conflicts" are often combined with "replaces".
Package A replaces Package B when files installed by B are removed and (in some cases) over-written by files in A.
Package A provides Package B when all of the files and functionality of B are incorporated into A. This mechanism provides a way for users with constrained disk space to get only that part of package A which they really need.

What is meant by Pre-Depends?

"Pre-Depends" is a special dependency. In the case of most packages, dpkg will unpack its archive file (i.e., its .deb file) independently of whether or not the files on which it depends exist on the system. Simplistically, unpacking means that dpkg will extract the files from the archive file that were meant to be installed on your file system, and put them in place. If those packages depend on the existence of some other packages on your system, dpkg will refuse to complete the installation (by executing its "configure" action) until the other packages are installed.
However, for some packages, dpkg will refuse even to unpack them until certain dependencies are resolved. Such packages are said to "Pre-depend" on the presence of some other packages.  

No comments: