Package Management (dpkg)

Package Management (dpkg)

A package in Linux terminology, is a software  (or application), and the package manager is responsible for package management. The package manager keeps track of all the installed softwares, it installs new softwares and allows old software to be removed. It is possible to manually download new softwares as well, but it is more common and convenient to use the built-in package management tools for this task.

Some packages may depend on other packages in order to work. These are  called as dependencies. (As an example a video cannot be started until additional graphics files are installed.)  If a package depends on other packages, the package manager will automatically find them and prepare them for installation. (This is one of the reason why  using package managers is easier than installing softwares manually.)

Installation and removal of software packages are handled by an application called dpkg.  dpkg is a software application at the center of Raspbian's package management system. It is a software tool that actually installs a software package from a .deb file.

There is another tool, called Advanced Packaging Tool (APT) in Raspbian, which is a real Package Manager.

APT is a frontend tool that makes using dpkg a lot easier. It is preconfigured with several repositories that contain every official package that is available for installation and it handles dependencies as well.

=====================================

dpkg - Debian Package

dpkg is the base command for handling Debian (and Raspbian) packages on the system. If you have .deb packages, it is dpkg that allows installation or analysis of their contents. But this program only has a partial view of the Debian universe: it knows what is installed on the system, and whatever it is given on the command line, but knows nothing of the other available packages and it cannot download packages. As such, it will fail if a dependency is not met. Tools such as apt, on the contrary, will create a list of dependencies to install everything as automatically as possible. So, dpkg should be seen as a system tool (backend), and apt as a tool closer to the user, which overcomes the limitations of dpkg. These tools work together, each one is suitable for its specific tasks

dpkg is the program that handles .deb files, like extracting, analyzing, and unpacking them. APT is a group of programs that allows the execution of higher-level modifications to the system: installing or removing a package (while keeping dependencies satisfied), updating the system, listing the available packages, etc.

=====================================

Debian packages - .deb files

A Debian package comes as a .deb file. The content of a .deb file is similar for each package, as it conatins these 3 files:

$ ar tv parted_1.4.24-4_i386.deb                                <-- it checks parted_1.4.24-4_i386.deb file with the tool ar
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

debian-binary: This states the version of the deb file format.
data.tar.gz: contains all the files that will be installed with their destination paths
control.tar.gz: contains all of the available meta-information (like the name and version of the package)

=====================================

Control file - control.tar.gz

It contains the meta-information (version and other details of the package), and if we check in detail (after unpacking it), we will see files similar to these:

-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. ("dpkg --verify" use it to check  if  installed files have been modified)
prerm and postinst: script for pre removal and post installation (more detail below at "Configuration Scripts")
control: it is the file that actually contains meta-information about the package

$ 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)
Suggests: parted-doc
....

=====================================

Package Meta-Information

The Debian (and the Raspbian) package contains not only the files for installation, but it describes its relationship with other packages (dependencies, conflicts, suggestions). It also provides scripts that enable package installation, removal, upgrades. These data are, within the package, what is called its “meta-information” (information about other information). This information can be found in the control file, which contains meta information about the package (like a package header).

Aftre installation it can be viewed by "apt-cache show <package>"

pi@raspberrypi:~ $ apt-cache show python
Package: python
Source: python-defaults
Version: 2.7.9-1
Architecture: armhf
Maintainer: Matthias Klose <doko@debian.org>
Installed-Size: 570
Pre-Depends: python-minimal (= 2.7.9-1)
Depends: python2.7 (>= 2.7.9-1~), libpython-stdlib (= 2.7.9-1)
Suggests: python-doc (= 2.7.9-1), python-tk (>= 2.7.9-1~)
Conflicts: python-central (<< 0.5.5)
Breaks: update-manager-core (<< 0.200.5-2)
Replaces: python-dev (<< 2.6.5-2)
Provides: python-ctypes, python-email, python-importlib, python-profiler, python-wsgiref

Dependencies:
The dependencies are defined in the Depends field. This lists the conditions to be met for the package to work correctly. This information is used by tools such as apt in order to install the required libraries with the correct versions. For each dependency, it is possible to restrict the range of versions. For example if we need the package libc6 in a version equal to or greater than “2.15”, this was written as Depends: libc6 (>=2.15)”.

Recommends and Suggests:
The Recommends and Suggests fields describe dependencies that are not compulsory, but can improve the fuctionality of the package. You should always install the “recommended” packages, unless you know exactly why you do not need them. Conversely, it is not necessary to install “suggested” packages unless you know why you need them.

Conflicts: 
The Conflicts field indicates when a package cannot be installed simultaneously with another. The most common reasons for this are that both packages include a file of the same name, or provide the same service on the same TCP port, or would hinder each other’s operation. dpkg will refuse to install a package if it triggers a conflict with an already installed package, except if the new package specifies that it will “replace” the installed package, in which case dpkg will choose to replace the old package with the new one. apt always follows your instructions: if you choose to install a new package, it will automatically offer to uninstall the package that poses a problem.

Breaks:
The Breaks field shows, that the installation of a package will “break” another package. dpkg will refuse to install a package that breaks an already installed package, and apt will try to resolve the problem by updating the package that would be broken, to a newer compatible version.

Replaces:
The Replaces field indicates that the package contains files that are also present in another package, and it is OK to replace them. Without this specification, dpkg fails, stating that it can not overwrite the files of another package (technically, it is possible to force it to do so with the --force-overwrite option, but that is not considered standard operation). If all of the files in an installed package have been replaced, the package is considered to be removed. This field also encourages dpkg to remove the replaced package where there is a conflict.

=====================================

Configuration Scripts

In addition to the control file, the control.tar.gz archive for each Debian package may contain a number of scripts. These scripts are called by dpkg at different stages in the processing of a package. All of the configuration scripts for installed packages are stored in the /var/lib/dpkg/info/ directory (like prerm, postinst...). In general, the preinst script is executed prior to installation of the package, while the postinst follows it. Likewise, prerm is invoked before removal of a package and postrm afterwards. An update of a package is equivalent to removal of the previous version and installation of the new one.

/var/lib/dpkg/info/ also includes a file with the .list extension for each package, containing the list of files that belong to that package. The /var/lib/dpkg/status file contains the status of each package and the information from the control file is also replicated there.


For an update, dpkg calls
1. prerm of the old package
2. preinst of the new package
3. Unpacking the new package. If a file already exists, that file is removed, but a backup copy is temporarily available
4. postrm of the old package
5. dpkg updates all internal data (file list, configuration scripts, etc.) and removes the backups of the replaced files.
   (This is the point of no return: dpkg is no longer able to return to the previous state.)
6. dpkg will update the configuration files
7. finally, dpkg configures the package by executing postinst of the new package


Package Removal
1. dpkg calls prerm of the package.
2. dpkg removes all of the package’s files, with the exception of the configuration files and configuration scripts
3. dpkg executes postrm of the package (all of the configuration scripts, except postrm, are removed)
   (If the user has not used the “purge” option, the process stops here.)
4. For a complete purge (dpkg --purge or dpkg -P), the configuration files are also deleted (after postrm for purge)

(When a Debian package is removed, the configuration files are kept, so re-installation is still available. To remove all data associated with a package, it is necessary to “purge” the package with the command, dpkg -P package,)

=====================================

/var/log/dpkg.log             log file for all the actions of dpkg
/usr/share/doc/<package>      this folder conatins documentation for each package
/var/lib/dpkg/info/           configuration scripts for installed packages (prerm, postinst)
/var/lib/dpkg/status          status of each package + data from control file
dpkg --get-selections         list installed packages

apt-cache show <package>      shows meta information about a package (conflicts, dependencies...)


dpkg --verify                 check if these files have been modified since their installation
dpkg -i <package>             installs an already available  Debian package, (which is a .deb file, It cannot download anything).
dpkg -r <package>             removes a package, but all config files remain (so reinstall is possible with same config)
dpkg -P <package>             remove package and it s configuration files (purge)


dpkg -l                       displays the list of packages known to the system and their installation status
dpkg -L <package>             lists the files installed by a package
dpkg -S /bin/date             finds the package(s) containing the given file
dpkg -s <package>             displays the headers of an installed package (status)

1 comment:

  1. I really love your site.. Excellent colors & theme. Did you develop this website yourself? Please reply back as I'm looking to create my own personal blog and would love to learn where you got this from or what the theme is named. Appreciate it!

    ReplyDelete