Product SiteDocumentation Site

5.4. مدیریت بسته‌ها با استفاده از dpkg

dpkg is the base command for handling Debian 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. As such, it will fail if a dependency is not met. Tools such as apt and aptitude, on the contrary, will create a list of dependencies to install everything as automatically as possible.

5.4.1. نصب بسته‌ها

dpkg، فراتر از همه، ابزار مورد نیاز جهت نصب یک بسته دبیان موجود می‌باشد (چرا که خود نمی‌تواند دانلود کند). برای اینکار، از گزینه -i یا --install آن استفاده می‌کنیم.

مثال 5.2. نصب یک بسته با استفاده از dpkg

# dpkg -i man-db_2.8.5-2_amd64.deb
(Reading database ... 14913 files and directories currently installed.)
Preparing to unpack .../man-db_2.8.5-2_amd64.deb ...
Unpacking man-db (2.8.5-2) over (2.8.5-2) ...
Setting up man-db (2.8.5-2) ...
Updating database of manual pages ...
Processing triggers for mime-support (3.62) ...
We can see the different steps performed by dpkg; we know, thus, at what point any error may have occurred. The installation can also be effected in two stages: first unpacking, then configuration. apt takes advantage of this, limiting the number of calls to dpkg (since each call is costly, due to loading of the database in memory, especially the list of already installed files).

مثال 5.3. از حالت فشرده خارج کردن و پیکربندی به صورت جداگانه

# dpkg --unpack man-db_2.8.5-2_amd64.deb
(Reading database ... 14937 files and directories currently installed.)
Preparing to unpack man-db_2.8.5-2_amd64.deb ...
Unpacking man-db (2.8.5-2) over (2.8.5-2) ...
Processing triggers for mime-support (3.62) ...
# dpkg --configure man-db
Setting up man-db (2.8.5-2) ...
Updating database of manual pages ...
گاهی اوقات dpkg موفق به نصب یک بسته نمی‌شود و پیام خطایی باز می‌گرداند؛ اگر کاربر نخواهد این پیام را ببیند، تنها یک اخطار صادر می‌شود؛ به همین دلیل است که از نسخه‌های متفاوت گزینه --force-* استفاده می‌کنیم. دستور dpkg --force-help یا مستندات مربوط به آن، فهرستی از تمام گزینه‌های موجود را نمایش می‌دهد. متداول‌ترین خطا، که دیر یا زود با آن مواجه خواهید شد، وجود فایل مورد نظر روی سیستم است. زمانی که بسته شامل فایلی است که از قبل توسط بسته دیگری نصب شده است، dpkg از نصب آن خودداری می‌کند. آنگاه پیام‌های زیر صادر می‌شوند:
Unpacking libgdm (from .../libgdm_3.8.3-2_amd64.deb) ...
dpkg: error processing /var/cache/apt/archives/libgdm_3.8.3-2_amd64.deb (--unpack):
 trying to overwrite '/usr/bin/gdmflexiserver', which is also in package gdm3 3.4.1-9
در این مورد، اگر فکر می‌کنید که جایگزین کردن فایل تاثیر بسزایی روی سیستم نخواهد گذاشت (که معمولا می‌گذارد)، می‌توانید از گزینه --force-overwrite استفاده کنید که به dpkg می‌گوید فایل جدید را جایگزین قبلی کند و از این خطا جلوگیری نماید.
با توجه به اینکه گزینه‌های متفاوتی از --force-* وجود دارند، --force-overwrite معمولا از بقیه بیشتر مورد استفاده قرار می‌گیرد. این گزینه‌ها برای حالت‌های بسیار خاص موجود هستند و بهتر است تا جایی که می‌توانیم از آن‌ها استفاده نکنیم تا به قواعد استاندارد مکانیزم بسته‌بندی احترام بگذاریم. فراموش نکنید که، این قوانین منجر به پایداری و یکپارچکی سیستم شما می‌گردند.

5.4.2. حذف بسته

Invoking dpkg with the -r or --remove option, followed by the name of a package, removes that package. This removal is, however, not complete: all of the configuration files, maintainer scripts, log files (system logs) and other user data handled by the package remain. That way disabling the program is easily done by uninstalling it, and it is still possible to quickly reinstall it with the same configuration. To completely remove everything associated with a package, use the -P or --purge option, followed by the package name.

مثال 5.4. حذف و پاکسازی بسته debian-cd

# dpkg -r debian-cd
(Reading database ... 15915 files and directories currently installed.)
Removing debian-cd (3.1.25) ...
# dpkg -P debian-cd
(Reading database ... 15394 files and directories currently installed.)
Purging configuration files for debian-cd (3.1.25) ...

5.4.3. فراخوانی پایگاه‌داده dpkg و جستجوی فایل‌های .deb

قبل از جمع‌بندی این قسمت، به مطالعه گزینه‌هایی از dpkg می‌پردازیم که برای کسب اطلاعات بیشتر از پایگاه‌داده داخلی آن به پرس و جو می‌پردازند. ابتدا نسخه بلند سپس کوتاه آورده می‌شود (هر دو به یک میزان پارامتر دریافت می‌کنند). --listfiles package (یا -L) فهرست فایل‌های نصب شده توسط بسته را نمایش می‌دهد؛ --search file (یا -S) به جستجوی بسته(ها) می‌پردازد که شامل فایل مورد نظر هستند؛ --status package (یا -s) فایل سرآیند یک بسته نصب شده را نمایش می‌دهد؛ --list (یا -l) فهرست بسته‌های نصب شده روی سیستم و وضعیت هر یک را نمایش می‌دهد؛ --contents file.db (یا -c) فهرستی از فایل‌های موجود در بسته را نمایش می‌دهد؛ --info file.db (یا -I) که فایل سرآیند این بسته دبیان را نمایش می‌دهد.

مثال 5.5. پرس و جوهای گوناگون با استفاده از dpkg

$ dpkg -L base-passwd
/.
/usr
/usr/sbin
/usr/sbin/update-passwd
/usr/share
/usr/share/base-passwd
/usr/share/base-passwd/group.master
/usr/share/base-passwd/passwd.master
/usr/share/doc
/usr/share/doc/base-passwd
/usr/share/doc/base-passwd/README
/usr/share/doc/base-passwd/changelog.gz
/usr/share/doc/base-passwd/copyright
/usr/share/doc/base-passwd/users-and-groups.html
/usr/share/doc/base-passwd/users-and-groups.txt.gz
/usr/share/doc-base
/usr/share/doc-base/users-and-groups
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/base-passwd
/usr/share/man
/usr/share/man/de
/usr/share/man/de/man8
/usr/share/man/de/man8/update-passwd.8.gz
/usr/share/man/es
/usr/share/man/es/man8
/usr/share/man/es/man8/update-passwd.8.gz
/usr/share/man/fr
/usr/share/man/fr/man8
/usr/share/man/fr/man8/update-passwd.8.gz
/usr/share/man/ja
/usr/share/man/ja/man8
/usr/share/man/ja/man8/update-passwd.8.gz
/usr/share/man/man8
/usr/share/man/man8/update-passwd.8.gz
/usr/share/man/pl
/usr/share/man/pl/man8
/usr/share/man/pl/man8/update-passwd.8.gz
/usr/share/man/ru
/usr/share/man/ru/man8
/usr/share/man/ru/man8/update-passwd.8.gz
$ dpkg -S /bin/date
coreutils: /bin/date
$ dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 15719
Maintainer: Michael Stone <mstone@debian.org>
Architecture: amd64
Multi-Arch: foreign
Version: 8.30-3
Pre-Depends: libacl1 (>= 2.2.23), libattr1 (>= 1:2.4.44), libc6 (>= 2.28), libselinux1 (>= 2.1.13)
Description: GNU core utilities
 This package contains the basic file, shell and text manipulation
 utilities which are expected to exist on every operating system.
 .
 Specifically, this package includes:
 arch base64 basename cat chcon chgrp chmod chown chroot cksum comm cp
 csplit cut date dd df dir dircolors dirname du echo env expand expr
 factor false flock fmt fold groups head hostid id install join link ln
 logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt
 od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm
 rmdir runcon sha*sum seq shred sleep sort split stat stty sum sync tac
 tail tee test timeout touch tr true truncate tsort tty uname unexpand
 uniq unlink users vdir wc who whoami yes
Homepage: http://gnu.org/software/coreutils
$ dpkg -l 'b*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                       Version              Architecture Description
+++-====================-===============-===============-==================================================
un  backupninja          <none>          <none>          (no description available)
un  backuppc             <none>          <none>          (no description available)
un  baobab               <none>          <node>          (no description available)
un  base                 <none>          <none>          (no description available)
un  base-config          <none>          <none>          (no description available)
ii  base-files           11              amd64           Debian base system miscellaneous files
ii  base-passwd          3.5.46          amd64           Debian base system master password and group files
ii  bash                 5.0-4           amd64           GNU Bourne Again SHell
[..]
$ dpkg -c /var/cache/apt/archives/gnupg-utils_2.2.12-1_amd64.deb
drwxr-xr-x root/root         0 2018-12-15 02:17 ./
drwxr-xr-x root/root         0 2018-12-15 02:17 ./usr/
drwxr-xr-x root/root         0 2018-12-15 02:17 ./usr/bin/
-rwxr-xr-x root/root      3516 2018-12-15 02:17 ./usr/bin/gpg-zip
-rwxr-xr-x root/root    866256 2018-12-15 02:17 ./usr/bin/gpgcompose
-rwxr-xr-x root/root     30792 2018-12-15 02:17 ./usr/bin/gpgparsemail
-rwxr-xr-x root/root     84432 2018-12-15 02:17 ./usr/bin/gpgsplit
-rwxr-xr-x root/root    154952 2018-12-15 02:17 ./usr/bin/gpgtar
-rwxr-xr-x root/root    166568 2018-12-15 02:17 ./usr/bin/kbxutil
-rwxr-xr-x root/root      1081 2017-08-28 12:22 ./usr/bin/lspgpot
-rwxr-xr-x root/root      2194 2018-11-18 23:37 ./usr/bin/migrate-pubring-from-classic-gpg
-rwxr-xr-x root/root    121576 2018-12-15 02:17 ./usr/bin/symcryptrun
-rwxr-xr-x root/root     18424 2018-12-15 02:17 ./usr/bin/watchgnupg
drwxr-xr-x root/root         0 2018-12-15 02:17 ./usr/sbin/
-rwxr-xr-x root/root      3075 2018-12-15 02:17 ./usr/sbin/addgnupghome
-rwxr-xr-x root/root      2217 2018-12-15 02:17 ./usr/sbin/applygnupgdefaults
drwxr-xr-x root/root         0 2018-12-15 02:17 ./usr/share/
drwxr-xr-x root/root         0 2018-12-15 02:17 ./usr/share/doc/
[...]
$ dpkg -I /var/cache/apt/archives/gnupg-utils_2.2.12-1_amd64.deb
 new Debian package, version 2.0.
 size 857408 bytes: control archive=1844 bytes.
    1564 bytes,    32 lines      control              
    1804 bytes,    28 lines      md5sums              
 Package: gnupg-utils
 Source: gnupg2
 Version: 2.2.12-1
 Architecture: amd64
 Maintainer: Debian GnuPG Maintainers <pkg-gnupg-maint@lists.alioth.debian.org>
 Installed-Size: 1845
 Depends: libassuan0 (>= 2.0.1), libbz2-1.0, libc6 (>= 2.25), libgcrypt20 (>= 1.8.0), libgpg-error0 (>= 1.26-2~), libksba8 (>= 1.3.4), libreadline7 (>= 6.0), zlib1g (>= 1:1.1.4)
 Recommends: gpg, gpg-agent, gpgconf, gpgsm
 Breaks: gnupg (<< 2.1.21-4), gnupg-agent (<< 2.1.21-4)
 Replaces: gnupg (<< 2.1.21-4), gnupg-agent (<< 2.1.21-4)
 Section: utils
 Priority: optional
 Multi-Arch: foreign
 Homepage: https://www.gnupg.org/
 Description: GNU privacy guard - utility programs
  GnuPG is GNU's tool for secure communication and data storage.
  .
  This package contains several useful utilities for manipulating
  OpenPGP data and other related cryptographic elements.  It includes:
  .
   * addgnupghome -- create .gnupg home directories
   * applygnupgdefaults -- run gpgconf --apply-defaults for all users
   * gpgcompose -- an experimental tool for constructing arbitrary
                   sequences of OpenPGP packets (e.g. for testing)
   * gpgparsemail -- parse an e-mail message into annotated format
   * gpgsplit -- split a sequence of OpenPGP packets into files
   * gpgtar -- encrypt or sign files in an archive
   * kbxutil -- list, export, import Keybox data
   * lspgpot -- convert PGP ownertrust values to GnuPG
   * migrate-pubring-from-classic-gpg -- use only "modern" formats
   * symcryptrun -- use simple symmetric encryption tool in GnuPG framework
   * watchgnupg -- watch socket-based logs
[..]

5.4.4. فایل لاگ dpkg

dpkg مجموعه تمام رخدادهای خود را درون فایل /var/log/dpkg.log ذخیره می‌کند. این فایل تمام عملیات انجام شده توسط dpkg را به همراه جزئیات نمایش می‌دهد. در کنار ایجاد روشی جهت ردیابی عملکرد dpkg، این فایل تاریخچه‌ای از عملیات انجام شده روی سیستم را ذخیره می‌کند: به راحتی می‌توان تشخیص داد در هر لحظه چه بسته‌ای اضافه یا حذف شده است و این اطلاعات به درک و بررسی اتفاقات اخیر روی سیستم کمک شایانی می‌کند. به علاوه، تمام نسخه‌ها نیز نگهداری می‌شوند که این امر فرآیند بررسی اطلاعات موجود را با فایل changelog.Debian.gz برای بسته مورد نظر یا حتی گزارش‌های آنلاین باگ، تسهیل می‌نماید.

5.4.5. پشتیبانی از چند-معماری

تمام بسته‌های دبیان دارای یک فیلد Architecture در فایل کنترلی خود هستند. این فیلد می‌تواند شامل “all” (برای بسته‌های مستقل از یک معماری خاص) یا نام معماری‌های مورد نظر (مانند “armhf”، “amd64” و ...) باشد. در مورد آخر، dpkg تنها در صورتی اقدام به نصب بسته می‌کند که معماری آن با معماری سیستم موجود که توسط دستور dpkg --print-architecture مشخص می‌شود، یکسان باشد.
این محدودیت تضمین می‌کند که کاربران به اشتباه از یک باینری معماری دیگر استفاده نکنند. همه چیز خوب پیش می‌رود به جز (برخی) رایانه‌هایی که قادر به اجرای باینری‌های معماری‌های متفاوت هستند، خواه به صورت پیش‌فرض (یک سیستم “amd64“ می‌تواند باینری‌های “i386“ را اجرا کند) یا با استفاده از شبیه‌سازها.

5.4.5.1. فعال‌سازی چند-معماری

قابلیت پشتیبانی از چند معماری در dpkg به کاربران امکان تعریف “معماری‌های خارجی” که می‌توانند روی سیستم نصب شوند را می‌دهد. اینکار به راحتی با دستور dpkg--add-architecture که در ادامه می‌بینیم، انجام می‌شود. همچنین دستور متناظر با آن dpkg--remove-architecture است که پشتیبانی از یک معماری خارجی را متوقف می‌سازد، اما تنها زمانی قابل استفاده است که هیچ بسته‌ای از این معماری روی سیستم نصب نباشد.
# dpkg --print-architecture
amd64
# dpkg --print-foreign-architectures
# dpkg -i gcc-8-base_8.3.0-6_armhf.deb
dpkg: error processing archive gcc-8-base_8.3.0-6_armhf.deb (--install):
 package architecture (armhf) does not match system (amd64)
Errors were encountered while processing:
 gcc-8-base_8.3.0-6_armhf.deb
# dpkg --add-architecture armhf
# dpkg --add-architecture armel
# dpkg --print-foreign-architectures
armhf
armel
# dpkg -i gcc-8-base_8.3.0-6_armhf.deb
(Reading database ... 14319 files and directories currently installed.)
Preparing to unpack gcc-8-base_8.3.0-6_armhf.deb ...
Unpacking gcc-8-base:armhf (8.3.0-6) ...
Setting up gcc-8-base:armhf (8.3.0-6) ...
# dpkg --remove-architecture armhf
dpkg: error: cannot remove architecture 'armhf' currently in use by the database
# dpkg --remove-architecture armel
# dpkg --print-foreign-architectures
armhf

5.4.5.2. تغییرات مربوط به چند-معماری

To make multi-arch actually useful and usable, libraries had to be repackaged and moved to an architecture-specific directory so that multiple copies (targeting different architectures) can be installed alongside. Such updated packages contain the “Multi-Arch: same” header field to tell the packaging system that the various architectures of the package can be safely co-installed (and that those packages can only satisfy dependencies of packages of the same architecture). The most important libraries have been converted since the introduction of multi-arch in Debian 7 Wheezy, but there are many libraries that will likely never be converted unless someone specifically requests it (through a bug report for example).
$ dpkg -s gcc-8-base
dpkg-query: error: --status needs a valid package name but 'gcc-8-base' is not: ambiguous package name 'gcc-8-base' with more than one installed instance

Use --help for help about querying packages.
$ dpkg -s gcc-8-base:amd64 gcc-8-base:armhf | grep ^Multi
Multi-Arch: same
Multi-Arch: same
$ dpkg -L libgcc1:amd64 |grep .so
/lib/x86_64-linux-gnu/libgcc_s.so.1
$ dpkg -S /usr/share/doc/gcc-8-base/copyright
gcc-8-base:amd64, gcc-8-base:armhf: /usr/share/doc/gcc-8-base/copyright
شایان ذکر است که بسته‌های Multi-Arch: same باید در انتهای نام خود، معماری را ذکر کنند تا به تنهایی قابل تشخیص از سایر بسته‌ها گردند. همچنین احتمال به اشتراک‌گذاری فایل‌ها از یک نمونه دیگر از آن بسته وجود دارد؛ dpkg اطمینان می‌یابد که تمام بسته‌ها زمانی که به اشتراک گذاشته می‌شوند فایل‌های یکسان و بیت به بیت دارند. در انتها، تمام نمونه‌های یک بسته باید از یک نسخه تبعیت کنند. همچنین باید با یکدیگر بروزرسانی گردند.
پشتیبانی از چند-معماری همراه با چالش‌های جالبی در رابطه با شیوه مدیریت وابستگی‌ها است. رعایت یک وابستگی نیازمند این است که یا بسته به عنوان “Multi-Arch: foreign” نشانه‌گذاری شده باشد یا معماری آن با معماری وابستگی‌اش یکسان باشد (در این فرآیند، بسته‌هایی که مستقل از یک معماری هستند به عنوان بسته‌های سازگار با معماری سیستم در نظر گرفته می‌شوند). همچنین یک وابستگی می‌تواند به منظور اجرا در سیستم، ضعیف گردد، با استفاده از package:any، اما بسته‌های خارجی تنها در حالتی می‌توانند این وابستگی را رعایت کنند که به عنوان “Multi-Arch: allowed” نشانه‌گذاری شده باشند.