Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5709696
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: LINUX

2006-08-18 10:09:13

Linux 发行版使用的打包格式主要有两种:Redhat Package Manager (RPM) 和 Debian (DEB)。两者的用途差不多,而具体的方法不同。总的来说,两者都是“增强的”文件档案格式。这些包格式提供的增强包括:版本号注释、应用程序对其他应 用程序或库的依赖性、打包工具的可读性描述、管理安装、升级和卸载的打包工具通用机制。

在 DEB 文件中,嵌套的配置文件 control 包含大多数包元数据。而 RPM 文件中则是 spec。以这两种格式创建包的完整细节超出了本教程的范围,这里仅作基本介绍。



回页首


DEB 包使用存档工具和 tar 的近亲 ar(或某种使用 ar 的更高级的工具)创建。因此可使用 ar 查看 .deb 文件的内容。通常使用更高级的工具如 dpkgdpkg-debapt-get 与 DEB 包打交道。比如:


% ar tv unzip_5.51-2ubuntu1.1_i386.deb
rw-r--r-- 0/0 4 Aug 1 07:23 2005 debian-binary
rw-r--r-- 0/0 1007 Aug 1 07:23 2005 control.tar.gz
rw-r--r-- 0/0 133475 Aug 1 07:23 2005 data.tar.gz

文件 debian-binary 只包括 DEB 版本号(目前为 2.0)。档案 data.tar.gz 包含实际的应用程序文件 —— 可执行文件、文档、手册、配置文件,等等。

档案 control.tar.gz 从打包的角度来看最有趣。我们来看看这个 DEB 包:


% tar tvfz control.tar.gz
drwxr-xr-x root/root 0 2005-08-01 07:23:43 ./
-rw-r--r-- root/root 970 2005-08-01 07:23:43 ./md5sums
-rw-r--r-- root/root 593 2005-08-01 07:23:43 ./control

与预料的一样,md5sums 包含所有分发文件的加密散列值用于验证。元数据包含在 control 文件中。多数情况下,可能会发现或希望在 control.tar.gz 中包含名为 postinstprerm 的脚本,分别用于在安装后和删除前执行某些步骤。



回页首


安装脚本可以做 shell 脚本能够做的任何事情(看看现有包中的一些例子就知道了)。但是这些脚本是可选的,常常不需要或者没有。.deb 包需要的是 control 文件。该文件的格式包括各种元数据字段,最好用一个例子来说明:


% cat control
Package: unzip
Version: 5.51-2ubuntu1.1
Section: utils
Priority: optional
Architecture: i386
Depends: libc6 (>= 2.3.2.ds1-4)
Suggests: zip
Conflicts: unzip-crypt (<< 5.41)
Replaces: unzip-crypt (<< 5.41)
Installed-Size: 308
Maintainer: Santiago Vila
Description: De-archiver for .zip files
InfoZIP's unzip program. With the exception of multi-volume archives
(ie, .ZIP files that are split across several disks using PKZIP's /& option),
this can handle any file produced either by PKZIP, or the corresponding
InfoZIP zip program.
.
This version supports encryption.

基本上,除了自定义的数据值以外,您的 control 文件应该和这个差不多。对非 CPU 专用包,无论是脚本、纯文档或源代码,都使用 Architecture: all



回页首


创建 DEB 包要使用工具 dpkg-deb。我们不可能讨论打包的所有细节,但基本的思路是,创建一个工作目录 ./debian/,然后在运行 dpkg-deb 之前将所有需要的内容放入其中。可能还需要设置文件的权限以便适应安装时需要的状态。比如:


% mkdir -p ./debian/usr/bin/
% cp foo-util ./debian/usr/bin # copy executable/script
% mkdir -p ./debian/usr/share/man/man1
% cp foo-util.1 ./debian/usr/share/man/man1 # copy the manpage
% gzip --best ./debian/usr/share/man/man1/foo-util.1
% find ./debian -type d | xarg chmod 755 # set dir permissions
% mkdir -p ./debian/DEBIAN
% cp control ./debian/DEBIAN # first create a matching 'control'
% dpkg-deb --build debian # create the archive
% mv debian.deb foo-util_1.3-1all.deb # rename to final package name



回页首


上一节中可以看到,./debian/ 下的本地目录结构是为了和预期的安装结果相匹配。要创建一个好的包,还有几点值得注意。

  • 通常应该创建一个 ./debian/usr/share/doc/foo-util/copyright(根据包名调整)文件作为发行版的一部分。
  • 建立 ./debian/usr/share/doc/foo-util/changelog.gz 和 ./debian/usr/share/doc/foo-utils/changelog.Debian.gz 这两个文件也是不错的做法。
  • 工具 lintian 可以检查 DEB 包中有问题的特性。lintian 提出的问题不是每个都必须修改,但如果希望分发的范围更广,最好修正所有这些问题。
  • 工具 fakeroot 可以帮助正确的所有者进行打包。通常目标希望工具作为 root 安装,而不是恰好生成包的某个用户(lintian 会对此提出警告)。可使用如下的命令:

    % fakeroot dpkg-deb --build debian



回页首


创建包时,RPM 采取了和 DEB 略有不同的策略。它的配置文件称为 spec 而不是 control,而且 spec 文件的内容也比 control 文件多。安装前后、卸载前和安装本身需要的所有步骤的细节都包含在 spec 配置内嵌的脚本文件中。事实上,spec 格式甚至提供了宏和通用动作。

如果需要创建 RPM 包,可使用 rpm -b 工具程序。比如:


% rpm -ba foo-util-1.3.spec # perform all build steps

包的构建过程不依赖于 DEB 中那些特殊命名的目录,而是更复杂的 spec 文件中的指令。



回页首


RPM 中的主要元数据和 DEB 中的非常类似。比如,foo-util-1.3.spec 可能包含如下所示的内容:


# spec file for foo-util 1.3
Summary: A utility that fully foos
Name: foo-util
Version: 1.3
Release: 1
Copyright: GPL
Group: Application/Misc
Source: ftp://example.com/foo-util.tgz
URL:
Distribution: MyLinux
Vendor: Acme Systems
Packager: John Doe

%description
The foo-util program is an advanced fooer that combines the
capabilities of OneTwo's foo-tool and those in the GPL bar-util.



回页首


RPM spec 文件中的一些段可以包含一些很小的 shell 脚本。其中包括:

  • %prep:为准备构建而要执行的步骤,比如清除以前的(部分)版本。通常下面的宏很有用:

    %prep
    %setup

  • %build:实际构建工具的步骤。如果使用 make 工具,可能如下所示:

    %build
    make

  • %install:安装工具的步骤。如果使用 make,如下所示:

    %install
    make install

  • %files必须 包含属于该包的文件列表。即便 Makefile 可能使用这些文件,包管理器程序(rpm)也不会知道它们,除非将它们包含在这里:

    %files
    %doc README
    /usr/bin/foo-util
    /usr/share/man/man1/foo-util.1
阅读(2420) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~