dpkg-tech.sgml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <!doctype debiandoc PUBLIC "-//DebianDoc//DTD DebianDoc//EN">
  2. <book>
  3. <title>dpkg technical manual</title>
  4. <author>Tom Lees <email>tom@lpsg.demon.co.uk</email></author>
  5. <version>$Id: dpkg-tech.sgml,v 1.3 2003/02/12 15:05:45 doogie Exp $</version>
  6. <abstract>
  7. This document describes the minimum necessary workings for the APT dselect
  8. replacement. It gives an overall specification of what its external interface
  9. must look like for compatibility, and also gives details of some internal
  10. quirks.
  11. </abstract>
  12. <copyright>
  13. Copyright &copy; Tom Lees, 1997.
  14. <p>
  15. APT and this document are free software; you can redistribute them and/or
  16. modify them under the terms of the GNU General Public License as published
  17. by the Free Software Foundation; either version 2 of the License, or (at your
  18. option) any later version.
  19. <p>
  20. For more details, on Debian GNU/Linux systems, see the file
  21. /usr/share/common-licenses/GPL for the full license.
  22. </copyright>
  23. <toc sect>
  24. <chapt>Quick summary of dpkg's external interface
  25. <sect id="control">Control files
  26. <p>
  27. The basic dpkg package control file supports the following major features:-
  28. <list>
  29. <item>5 types of dependencies:-
  30. <list>
  31. <item>Pre-Depends, which must be satisfied before a package may be
  32. unpacked
  33. <item>Depends, which must be satisfied before a package may be
  34. configured
  35. <item>Recommends, to specify a package which if not installed may
  36. severely limit the usefulness of the package
  37. <item>Suggests, to specify a package which may increase the
  38. productivity of the package
  39. <item>Conflicts, to specify a package which must NOT be installed
  40. in order for the package to be configured
  41. </list>
  42. Each of these dependencies can specify a version and a depedency on that
  43. version, for example "<= 0.5-1", "== 2.7.2-1", etc. The comparators available
  44. are:-
  45. <list>
  46. <item>"&lt;&lt;" - less than
  47. <item>"&lt;=" - less than or equal to
  48. <item>"&gt;&gt;" - greater than
  49. <item>"&gt;=" - greater than or equal to
  50. <item>"==" - equal to
  51. </list>
  52. <item>The concept of "virtual packages", which many other packages may provide,
  53. using the Provides mechanism. An example of this is the "httpd" virtual package,
  54. which all web servers should provide. Virtual package names may be used in
  55. dependency headers. However, current policy is that virtual packages do not
  56. support version numbers, so dependencies on virtual packages with versions
  57. will always fail.
  58. <item>Several other control fields, such as Package, Version, Description,
  59. Section, Priority, etc., which are mainly for classification purposes. The
  60. package name must consist entirely of lowercase characters, plus the characters
  61. '+', '-', and '.'. Fields can extend across multiple lines - on the second
  62. and subsequent lines, there is a space at the beginning instead of a field
  63. name and a ':'. Empty lines must consist of the text " .", which will be
  64. ignored, as will the initial space for other continuation lines. This feature
  65. is usually only used in the Description field.
  66. </list>
  67. <sect>The dpkg status area
  68. <p>
  69. The "dpkg status area" is the term used to refer to the directory where dpkg
  70. keeps its various status files (GNU would have you call it the dpkg shared
  71. state directory). This is always, on Debian systems, /var/lib/dpkg. However,
  72. the default directory name should not be hard-coded, but #define'd, so that
  73. alteration is possible (it is available via configure in dpkg 1.4.0.9 and
  74. above). Of course, in a library, code should be allowed to override the
  75. default directory, but the default should be part of the library (so that
  76. the user may change the dpkg admin dir simply by replacing the library).
  77. <p>
  78. Dpkg keeps a variety of files in its status area. These are discussed later
  79. on in this document, but a quick summary of the files is here:-
  80. <list>
  81. <item>available - this file contains a concatenation of control information
  82. from all the packages which dpkg knows about. This is updated using the dpkg
  83. commands "--update-avail &lt;file&gt;", "--merge-avail &lt;file&gt;", and
  84. "--clear-avail".
  85. <item>status - this file contains information on the following things for
  86. every package:-
  87. <list>
  88. <item>Whether it is installed, not installed, unpacked, removed,
  89. failed configuration, or half-installed (deconfigured in
  90. favour of another package).
  91. <item>Whether it is selected as install, hold, remove, or purge.
  92. <item>If it is "ok" (no installation problems), or "not-ok".
  93. <item>It usually also contains the section and priority (so that
  94. dselect may classify packages not in available)
  95. <item>For packages which did not initially appear in the "available"
  96. file when they were installed, the other control information
  97. for them.
  98. </list>
  99. <p>
  100. The exact format for the "Status:" field is:
  101. <example>
  102. Status: Want Flag Status
  103. </example>
  104. Where <var>Want</> may be one of <em>unknown</>, <em>install</>,
  105. <em>hold</>, <em>deinstall</>, <em>purge</>. <var>Flag</>
  106. may be one of <em>ok</>, <em>reinstreq</>, <em>hold</>,
  107. <em>hold-reinstreq</>.
  108. <var>Status</> may be one of <em>not-installed</>, <em>unpacked</>,
  109. <em>half-configured</>, <em>installed</>, <em>half-installed</>
  110. <em>config-files</>, <em>post-inst-failed</>, <em>removal-failed</>.
  111. The states are as follows:-
  112. <taglist>
  113. <tag>not-installed
  114. <item>No files are installed from the package, it has no config files
  115. left, it uninstalled cleanly if it ever was installed.
  116. <tag>unpacked
  117. <item>The basic files have been unpacked (and are listed in
  118. /var/lib/dpkg/info/[package].list. There are config files present,
  119. but the postinst script has _NOT_ been run.
  120. <tag>half-configured
  121. <item>The package was installed and unpacked, but the postinst script
  122. failed in some way.
  123. <tag>installed
  124. <item>All files for the package are installed, and the configuration
  125. was also successful.
  126. <tag>half-installed
  127. <item>An attempt was made to remove the packagem but there was a failure
  128. in the prerm script.
  129. <tag>config-files
  130. <item>The package was "removed", not "purged". The config files are left,
  131. but nothing else.
  132. <tag>post-inst-failed
  133. <item>Old name for half-configured. Do not use.
  134. <tag>removal-failed
  135. <item>Old name for half-installed. Do not use.
  136. </taglist>
  137. The two last items are only left in dpkg for compatibility - they are
  138. understood by it, but never written out in this form.
  139. <p>
  140. Please see the dpkg source code, <tt>lib/parshelp.c</tt>,
  141. <em>statusinfos</>, <em>eflaginfos</> and <em>wantinfos</> for more
  142. details.
  143. <item>info - this directory contains files from the control archive of every
  144. package currently installed. They are installed with a prefix of "&lt;packagename&gt;.".
  145. In addition to this, it also contains a file called &lt;package&gt;.list for every
  146. package, which contains a list of files. Note also that the control file is
  147. not copied into here; it is instead found as part of status or available.
  148. <item>methods - this directory is reserved for "method"-specific files - each
  149. "method" has a subdirectory underneath this directory (or at least, it can
  150. have). In addition, there is another subdirectory "mnt", where misc.
  151. filesystems (floppies, CDROMs, etc.) are mounted.
  152. <item>alternatives - directory used by the "update-alternatives" program. It
  153. contains one file for each "alternatives" interface, which contains information
  154. about all the needed symlinked files for each alternative.
  155. <item>diversions - file used by the "dpkg-divert" program. Each diversion takes
  156. three lines. The first is the package name (or ":" for user diversion), the
  157. second the original filename, and the third the diverted filename.
  158. <item>updates - directory used internally by dpkg. This is discussed later,
  159. in the section <ref id="updates">.
  160. <item>parts - temporary directory used by dpkg-split
  161. </list>
  162. <sect>The dpkg library files
  163. <p>
  164. These files are installed under /usr/lib/dpkg (usually), but
  165. /usr/local/lib/dpkg is also a possibility (as Debian policy dictates). Under
  166. this directory, there is a "methods" subdirectory. The methods subdirectory
  167. in turn contains any number of subdirectories for each general method
  168. processor (note that one set of method scripts can, and is, used for more than
  169. one of the methods listed under dselect).
  170. <p>
  171. The following files may be found in each of these subdirectories:-
  172. <list>
  173. <item>names - One line per method, two-digit priority to appear on menu
  174. at beginning, followed by a space, the name, and then another space and the
  175. short description.
  176. <item>desc.&lt;name&gt; - Contains the long description displayed by dselect
  177. when the cursor is put over the &lt;name&gt; method.
  178. <item>setup - Script or program which sets up the initial values to be used
  179. by this method. Called with first argument as the status area directory
  180. (/var/lib/dpkg), second argument as the name of the method (as in the directory
  181. name), and the third argument as the option (as in the names file).
  182. <item>install - Script/program called when the "install" option of dselect is
  183. run with this method. Same arguments as for setup.
  184. <item>update - Script/program called when the "update" option of dselect is
  185. run. Same arguments as for setup/install.
  186. </list>
  187. <sect>The "dpkg" command-line utility
  188. <sect1>"Documented" command-line interfaces
  189. <p>
  190. As yet unwritten. You can refer to the other manuals for now. See
  191. <manref name="dpkg" section="8">.
  192. <sect1>Environment variables which dpkg responds to
  193. <p>
  194. <list>
  195. <item>DPKG_NO_TSTP - if set to a non-null value, this variable causes dpkg to
  196. run a child shell process instead of sending itself a SIGTSTP, when the user
  197. selects to background the dpkg process when it asks about conffiles.
  198. <item>SHELL - used to determine which shell to run in the case when
  199. DPKG_NO_TSTP is set.
  200. <item>CC - used as the C compiler to call to determine the target architecture.
  201. The default is "gcc".
  202. <item>PATH - dpkg checks that it can find at least the following files in the
  203. path when it wants to run package installation scripts, and gives an error if
  204. it cannot find all of them:-
  205. <list>
  206. <item>ldconfig
  207. <item>start-stop-daemon
  208. <item>install-info
  209. <item>update-rc.d
  210. </list>
  211. </list>
  212. <sect1>Assertions
  213. <p>
  214. The dpkg utility itself is required for quite a number of packages, even if
  215. they have been installed with a tool totally separate from dpkg. The reason for
  216. this is that some packages, in their pre-installation scripts, check that your
  217. version of dpkg supports certain features. This was broken from the start, and
  218. it should have actually been a control file header "Dpkg-requires", or similar.
  219. What happens is that the configuration scripts will abort or continue according
  220. to the exit code of a call to dpkg, which will stop them from being wrongly
  221. configured.
  222. <p>
  223. These special command-line options, which simply return as true or false are
  224. all prefixed with "--assert-". Here is a list of them (without the prefix):-
  225. <list>
  226. <item>support-predepends - Returns success or failure according to whether
  227. a version of dpkg which supports predepends properly (1.1.0 or above) is
  228. installed, according to the database.
  229. <item>working-epoch - Return success or failure according to whether a version
  230. of dpkg which supports epochs in version properly (1.4.0.7 or above) is
  231. installed, according to the database.
  232. </list>
  233. <p>
  234. Both these options check the status database to see what version of the "dpkg"
  235. package is installed, and check it against a known working version.
  236. <sect1>--predep-package
  237. <p>
  238. This strange option is described as follows in the source code:
  239. <example>
  240. /* Print a single package which:
  241. * (a) is the target of one or more relevant predependencies.
  242. * (b) has itself no unsatisfied pre-dependencies.
  243. * If such a package is present output is the Packages file entry,
  244. * which can be massaged as appropriate.
  245. * Exit status:
  246. * 0 = a package printed, OK
  247. * 1 = no suitable package available
  248. * 2 = error
  249. */
  250. </example>
  251. <p>
  252. On further inspection of the source code, it appears that what is does is
  253. this:-
  254. <list>
  255. <item>Looks at the packages in the database which are selected as "install",
  256. and are installed.
  257. <item>It then looks at the Pre-Depends information for each of these packages
  258. from the available file. When it find a package for which any of the
  259. pre-dependencies are not satisfied, it breaks from the loop through the packages.
  260. <item>It then looks through the unsatisfied pre-dependencies, and looks for
  261. packages which would satisfy this pre-dependency, stopping on the first it
  262. finds. If it finds none, it bombs out with an error.
  263. <item>It then continues this for every dependency of the initial package.
  264. </list>
  265. Eventually, it writes out the record of all the packages to satisfy the
  266. pre-dependencies. This is used by the disk method to make sure that its
  267. dependency ordering is correct. What happens is that all pre-depending
  268. packages are first installed, then it runs dpkg -iGROEB on the directory,
  269. which installs in the order package files are found. Since pre-dependencies
  270. mean that a package may not even be unpacked unless they are satisfied, it is
  271. necessary to do this (usually, since all the package files are unpacked in one
  272. phase, the configured in another, this is not needed).
  273. <chapt>dpkg-deb and .deb file internals
  274. <p>
  275. This chapter describes the internals to the "dpkg-deb" tool, which is used
  276. by "dpkg" as a back-end. dpkg-deb has its own tar extraction functions, which
  277. is the source of many problems, as it does not support long filenames, using
  278. extension blocks.
  279. <sect>The .deb archive format
  280. <p>
  281. The main principal of the new-format Debian archive (I won't describe the old
  282. format - for that have a look at deb-old.5), is that the archive really is
  283. an archive - as used by "ar" and friends. However, dpkg-deb uses this format
  284. internally, rather than calling "ar". Inside this archive, there are usually
  285. the folowing members:-
  286. <list>
  287. <item>debian-binary
  288. <item>control.tar.gz
  289. <item>data.tar.gz
  290. </list>
  291. <p>
  292. The debian-binary member consists simply of the string "2.0", indicating the
  293. format version. control.tar.gz contains the control files (and scripts), and
  294. the data.tar.gz contains the actual files to populate the filesystem with.
  295. Both tarfiles extract straight into the current directory. Information on the
  296. tar formats can be found in the GNU tar info page. Since dpkg-deb calls
  297. "tar -cf" to build packages, the Debian packages use the GNU extensions.
  298. <sect>The dpkg-deb command-line
  299. <p>
  300. dpkg-deb documents itself thoroughly with its '--help' command-line option.
  301. However, I am including a reference to these for completeness. dpkg-deb
  302. supports the following options:-
  303. <list>
  304. <item>--build (-b) &lt;dir&gt; - builds a .deb archive, takes a directory which
  305. contains all the files as an argument. Note that the directory
  306. &lt;dir&gt;/DEBIAN will be packed separately into the control archive.
  307. <item>--contents (-c) &lt;debfile&gt; - Lists the contents of ther "data.tar.gz"
  308. member.
  309. <item>--control (-e) &lt;debfile&gt; - Extracts the control archive into a
  310. directory called DEBIAN. Alternatively, with another argument, it will extract
  311. it into a different directory.
  312. <item>--info (-I) &lt;debfile&gt; - Prints the contents of the "control" file
  313. in the control archive to stdout. Alternatively, giving it other arguments will
  314. cause it to print the contents of those files instead.
  315. <item>--field (-f) &lt;debfile&gt; &lt;field&gt; ... - Prints any number of
  316. fields from the "control" file. Giving it extra arguments limits the fields it
  317. prints to only those specified. With no command-line arguments other than a
  318. filename, it is equivalent to -I and just the .deb filename.
  319. <item>--extract (-x) &lt;debfile&gt; &lt;dir&gt; - Extracts the data archive
  320. of a debian package under the directory &lt;dir&gt;.
  321. <item>--vextract (-X) &lt;debfile&gt; &lt;dir&gt; - Same as --extract, except
  322. it is equivalent of giving tar the '-v' option - it prints the filenames as
  323. it extracts them.
  324. <item>--fsys-tarfile &lt;debfile&gt; - This option outputs a gunzip'd version
  325. of data.tar.gz to stdout.
  326. <item>--new - sets the archive format to be used to the new Debian format
  327. <item>--old - sets the archive format to be used to the old Debian format
  328. <item>--debug - Tells dpkg-deb to produce debugging output
  329. <item>--nocheck - Tells dpkg-deb not to check the sanity of the control file
  330. <item>--help (-h) - Gives a help message
  331. <item>--version - Shows the version number
  332. <item>--licence/--license (UK/US spellings) - Shows a brief outline of the GPL
  333. </list>
  334. <sect1>Internal checks used by dpkg-deb when building packages
  335. <p>
  336. Here is a list of the internal checks used by dpkg-deb when building packages.
  337. It is in the order they are done.
  338. <list>
  339. <item>First, the output Debian archive argument, if it is given, is checked
  340. using stat. If it is a directory, an internal flag is set. This check is only
  341. made if the archive name is specified explicitly on the command-line. If the
  342. argument was not given, the default is the directory name, with ".deb"
  343. appended.
  344. <item>Next, the control file is checked, unless the --nocheck flag was
  345. specified on the command-line. dpkg-deb will bomb out if the second argument
  346. to --build was a directory, and --nocheck was specified. Note that dpkg-deb
  347. will not be able to determine the name of the package in this case. In the
  348. control file, the following things are checked:-
  349. <list>
  350. <item>The package name is checked to see if it contains any invalid
  351. characters (see <ref id="control"> for this).
  352. <item>The priority field is checked to see if it uses standard values,
  353. and user-defined values are warned against. However, note that this
  354. check is now redundant, since the control file no longer contains
  355. the priority - the changes file now does this.
  356. <item>The control file fields are then checked against the standard
  357. list of fields which appear in control files, and any "user-defined"
  358. fields are reported as warnings.
  359. <item>dpkg-deb then checks that the control file contains a valid
  360. version number.
  361. </list>
  362. <item>After this, in the case where a directory was specified to build the
  363. .deb file in, the filename is created as "directory/pkg_ver.deb" or
  364. "directory/pkg_ver_arch.deb", depending on whether the control file contains
  365. an architecture field.
  366. <item>Next, dpkg-deb checks for the &lt;dir&gt;/DEBIAN directory. It complains
  367. if it doesn't exist, or if it has permissions &lt; 0755, or &gt; 0775.
  368. <item>It then checks that all the files in this subdir are either symlinks
  369. or plain files, and have permissions between 0555 and 0775.
  370. <item>The conffiles file is then checked to see if the filenames are too
  371. long. Warnings are produced for each that is. After this, it checks that
  372. the package provides initial copies of each of these conffiles, and that
  373. they are all plain files.
  374. </list>
  375. <chapt>dpkg internals
  376. <p>
  377. This chapter describes the internals of dpkg itself. Although the low-level
  378. formats are quite simple, what dpkg does in certain cases often does not
  379. make sense.
  380. <sect id="updates">Updates
  381. <p>
  382. This describes the /var/lib/dpkg/updates directory. The function of this
  383. directory is somewhat strange, and seems only to be used internally. A function
  384. called cleanupdates is called whenever the database is scanned. This function
  385. in turn uses <manref name="scandir" section="3">, to sort the files in this
  386. directory. Files who names do not consist entirely of digits are discarded.
  387. dpkg also causes a fatal error if any of the filenames are different lengths.
  388. <p>
  389. After having scanned the directory, dpkg in turn parses each file the same way
  390. it parses the status file (they are sorted by the scandir to be in numerical
  391. order). After having done this, it then writes the status information back
  392. to the "status" file, and removes all the "updates" files.
  393. <p>
  394. These files are created internally by dpkg's "checkpoint" function, and are
  395. cleaned up when dpkg exits cleanly.
  396. <p>
  397. Juding by the use of the updates directory I would call it a Journal. Inorder
  398. to effeciently ensure the complete integrity of the status file dpkg will
  399. "checkpoint" or journal all of it's activities in the updates directory. By
  400. merging the contents of the updates directory (in order!!) against the
  401. original status file it can get the precise current state of the system,
  402. even in the event of a system failure while dpkg is running.
  403. <p>
  404. The other option would be to sync-rewrite the status file after each
  405. operation, which would kill performance.
  406. <p>
  407. It is very important that any program that uses the status file abort if
  408. the updates directory is not empty! The user should be informed to run dpkg
  409. manually (what options though??) to correct the situation.
  410. <sect>What happens when dpkg reads the database
  411. <p>
  412. First, the status file is read. This gives dpkg an initial idea of the packages
  413. that are there. Next, the updates files are read in, overriding the status
  414. file, and if necessary, the status file is re-written, and updates files are
  415. removed. Finally, the available file is read. The available file is read
  416. with flags which preclude dpkg from updating any status information from it,
  417. though - installed version, etc., and is also told to record that the packages
  418. it reads this time are available, not installed.
  419. <p>
  420. More information on updates is given above.
  421. <sect>How dpkg compares version numbers
  422. <p>
  423. Version numbers consist of three parts: the epoch, the upstream version, and
  424. the Debian revision. Dpkg compares these parts in that order. If the epochs
  425. are different, it returns immediately, and so on.
  426. <p>
  427. However, the important part is how it compares the versions which are
  428. essentially stored as just strings. These are compared in two distinct parts:
  429. those consisting of numerical characters (which are evaluated, and then
  430. compared), and those consisting of other characters. When comparing
  431. non-numerical parts, they are compared as the character values (ASCII), but
  432. non-alphabetical characters are considered "greater than" alphabetical ones.
  433. Also note that longer strings (after excluding differences where numerical
  434. values are equal) are considered "greater than" shorter ones.
  435. <p>
  436. Here are a few examples of how these rules apply:-
  437. <example>
  438. 15 > 10
  439. 0010 == 10
  440. d.r > dsr
  441. 32.d.r == 0032.d.r
  442. d.rnr < d.rnrn
  443. </example>
  444. </book>