Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1331186
  • 博文数量: 206
  • 博客积分: 10571
  • 博客等级: 上将
  • 技术积分: 2610
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-30 11:50
文章分类
文章存档

2014年(1)

2013年(4)

2012年(18)

2010年(14)

2009年(31)

2008年(3)

2007年(135)

分类: LINUX

2007-04-30 18:35:58

构建 Debian 软件包

etony C.F.AN v0.0.1, 2006-6-29


本文档主要简单介绍几种构建 Debian 软件包的方法.


声明

  • 版本信息:

本文档的最新版本将张贴于: LinuxSir.Org论坛 ;

也可以从 获取.

  • 反馈:

所有评论, 错误报告, 其他信息以及批评, 请邮寄到 ;

或在LinuxSir.Org论坛 张贴.

  • 版权信息:

本文档的版权(c)2006-2007 归 etony C.F.AN 所有.

转载请注明源自 .

软件包介绍

在Debian中存在两种类型的软件包:

  • 源代码包

源代码包, 包括一个描述源代码包的 .dsc 文件, 一个包含 gzip-tar 归档压缩格式的未经修改源码的 .orig.tar.gz 文件, 一个包含对源代码作 Debian 特有修改的 .diff.gz 文件. 可以使用 dpkg-source 打包和解压 debian 源码文档. 详见联机手册.

  • 二进制包

二进制包以 .deb 扩展名来表示,这些文件通常称为DEB文件,其中包含可执行文件,文档,配置文件和版权信息及其它一些东西.可以使用 Debian 的 dpkg 工具解包(安装).但除去版权信息(和 changelog.Debian 文件)不讲,二进制包也可以是空的,这种软件包作为过渡包或关联包(也称虚拟包),它们的唯一的作用是用于满足依赖性.

在本文中我们将主要讲述 .deb 包的构建,源代码包的只会有少量舍己

软件包命名约定

Debian软件包命名遵循下列约定: <软件包名称>_<版本>-_<平台>.deb例如

nano_1.3.10-2_i386.deb

 

        软件包名称: nano

        版本: 1.3.10

        修订号: 2

        平台: i386

文件的结构

参阅

修改现有 .deb 文件

比如 nano(1.3.10) 需要满足以下依赖关系:

$ dpkg-deb -f nano_1.3.10-2_i386.deb depends            //depends 为对应查询段

 

libc6 (>= 2.3.5-1), libncursesw5 (>= 5.4-5)

当系统不能满足依赖关系, 比如不能满足里来版本, 可以通过下边的方法处理, 当然这种情况下安装的nano并不一定运行正常, 这种方法可以应用于某些软件的变通安装.

$ mkdir nano //创建nano目录

 

$ dpkg-deb -e ./nano_1.3.10-2_i386.deb ./nano/DEBIAN    //解压控制文件

 

$ dpkg-deb -x ./nano_1.3.10-2_i386.deb ./nano           //解压程序文件

 

$ vi ./nano/DEBIAN/control                                //修改控制信息

 

$ dpkg-deb -b nano nano_1.3.10-2_ill.deb                //重新打包

注:下载 .deb 包的方法

  • 方法一

运行命令

$apt-get install -d packagename

$aptitude install -d packagename

将下载 .deb 文件到 //var/cache/apt/archives/ 目录

如果管理系统数据库中的对应版本不低于源上的版本将不会下载

  • 方法二

运行命令:

$aptitude download packagename

将直接执行下载操作, 将 .deb 文件下载到当前目录.

从当前系统构建 .deb 包

Debian软件包: dpkg-repack 可以用来重新构建已经安装到系统中的软件包为 .deb 文件. 并包含已经修改了的配置文件. 详细新息参阅:

对于某些需要修改配置后进行大面积安装的软件包, 可以是用 dpkg-repack 重新打包, 然后分发.

运行 dpkg-repack 需要 root 权限,也可以是用 fakeroot 取代 root 权限。例如:

# aptitude install dpkg-repack

 

$ fakeroot -u dpkg-repack nano

dpkg-deb: building package `nano' in `./nano_1.3.11-2_i386.deb'.

 

user@debian:~$ fakeroot -u dpkg-repack apt

dpkg-deb: building package `apt' in `./apt_0.5.22_i386.deb'.

可以使用 dpkg-deb 进行验证

$ dpkg-deb  -I ./nano_1.3.11-2_i386.deb

 new debian package, version 2.0.

 size 256488 bytes: control archive= 2749 bytes.

      12 bytes,     1 lines      conffiles           

     857 bytes,    22 lines      control             

    3529 bytes,    51 lines      md5sums             

     613 bytes,    21 lines   *  postinst             #!/bin/sh

     160 bytes,     5 lines   *  postrm               #!/bin/sh

     379 bytes,    20 lines   *  preinst              #!/bin/sh

     288 bytes,    14 lines   *  prerm                #!/bin/sh

 Package: nano

 Priority: important

 Section: editors

 Installed-Size: 1452

 Maintainer: Jordi Mallach

 Architecture: i386

 Version: 1.3.11-2

 Replaces: pico

 Provides: editor

 Depends: libc6 (>= 2.3.5-1), libncursesw5 (>= 5.4-5)

 Suggests: spell

 Conflicts: nano-tiny (<= 1.0.0-1), pico

 Conffiles:

  /etc/nanorc 0f0e96f9c7e5ffe2338e79c9aacb9b9f

 Description: free Pico clone with some new features

  GNU nano is a free replacement for Pico, the default Pine editor. Pine is

  copyrighted under a restrictive licence, that makes it unsuitable for

  Debian's main section. GNU nano is an effort to provide a Pico-like

  editor, but also includes some features that were missing in the original,

  such as 'search and replace', 'goto line' or internationalization support. .

  (Repackaged on Fri, 26 May 2006 22:17:52 +0800 by dpkg-repack.)

 Architecture: i386

 

tony@tony:~/doc/build_package$

 

$dpkg-deb -c ./nano_1.3.11-2_i386.deb

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./etc/

-rw-r--r-- root/root     13611 2006-04-06 06:22:03 ./etc/nanorc

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./usr/

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./usr/share/

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./usr/share/doc/

drwxr-xr-x root/root         0 2006-05-26 22:17:52 ./usr/share/doc/nano/

-rw-r--r-- root/root      3419 2002-07-19 09:08:58 ./usr/share/doc/nano/BUGS.gz

-rw-r--r-- root/root      1847 2005-08-30 02:29:02 ./usr/share/doc/nano/AUTHORS

-rw-r--r-- root/root    102030 2006-03-30 14:34:43 ./usr/share/doc/nano/changelog.gz

-rw-r--r-- root/root      2338 2005-03-20 05:33:13 ./usr/share/doc/nano/README

-rw-r--r-- root/root      3234 2006-03-17 01:57:28 ./usr/share/doc/nano/THANKS

-rw-r--r-- root/root      2206 2005-06-08 10:28:06 ./usr/share/doc/nano/TODO

-rw-r--r-- root/root      1066 2003-03-24 21:09:26 ./usr/share/doc/nano/UPGRADE

 

.....

使用源提供的源代码构建 .deb 包

  • 下载源码包

APT提供了一套简便的方法帮你获得发布版中众多程序的源代码以及创建一个.deb所需的所有文件。

首先, 在 /etc/apt/sources.list 文件中加入 deb-src 条目. 详细参阅

然后, 运行命令

# apt-get update

# aptitude update

就可以用下面的命令下载源码包:

# apt-get source packagename

# aptitude source packagename

通常会下载三个文件:.orig.tar.gz,.dsc 和 .diff.gz。对于 Debian 专用的软件包,最后一个文件不会下载,第一个文件的文件名中没有“orig”项。

dpkg-source 读取 .dsc 文件信息,将源码包解包到 packagename-version 目录,下载下来的源码包中有一个 debian/ 目录,里面是创建 .deb 包所需的文件。

例如: $ apt-get source nano

Reading package lists... Done

Building dependency tree... Done

Need to get 1199kB of source archives.

Get:1 testing/main nano 1.3.11-2 (dsc) [706B]

Get:2 testing/main nano 1.3.11-2 (tar) [1173kB]      

Get:3 testing/main nano 1.3.11-2 (diff) [25.4kB]     

Fetched 1199kB in 3m46s (5287B/s)                                             

... ...

dpkg-source: extracting nano in nano-1.3.11

dpkg-source: unpacking nano_1.3.11.orig.tar.gz

dpkg-source: applying ./nano_1.3.11-2.diff.gz

做相应调整后, 在源代码的根目录 nano-1.3.11 下运行:

# dpkg-buildpackage -rfakeroot -uc -b

将代码编译成为软件包.

想要下载的源码包自动编译成软件包,只需在命令行中加上 -b,例如:

# apt-get -b source packagename

请在下载包所在的目录中运行上面的命令。

# dpkg -i file.deb

apt-get的source命令与它的其它命令有所不同,普通用户权限就可以运行source命令,指定文件被下载到用户调用 apt-source package 命令时所处的目录中。

  • 编译源码包所需的软件包

通常,编译源码包时要用到某些头文件和共享库,所有的源码包的管理文件 (control file) 中都有一个域 "Build-Depends:", 域中指出了编译该源码包需要哪些附加包。

如果你仅仅想检查一下编译当前包需要哪些相关软件包,可以使用 apt-cache show 命令的一个变种,它将显示软件包信息中有关 Build-Depends 的行。

$ apt-cache showsrc nano|grep Build-Depends 

Build-Depends: dpkg-dev (>= 1.13.9), libncurses5-dev, libncursesw5-dev, libslang2-dev, debhelper (>= 5.0.22), dpatch

APT 提供了一个简单的方法下载这些附加包,你只需运行 apt-get build-dep package, 其中 package 就是你打算编译的源码包名称。例如:

# apt-get build-dep nano

Reading package lists... Done

Building dependency tree... Done

The following NEW packages will be installed:

  build-essential dpatch g++ libncursesw5-dev libslang2-dev

0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.

Need to get 986kB of archives.

After unpacking 2806kB of additional disk space will be used.

Do you want to continue [Y/n]?

这些用于 nano 正确编译指定源码包的附加包将会被安装。注意这个命令不能用来搜索软件的源码包,你得另外运行 apt-get source 下载源码包。

对现有文件打包:

  • 创建工作目录

构建模拟根目录, 比如 fakeroot :

$ mkdir fakeroot

  • 部署文件

在 fakeroot 下构建模拟文件系统, 将欲打包文件放置到对应目录

形如:

tony@tony:~$ tree fakeroot/

fakeroot/

|-- DEBIAN

|   |-- control

|   `-- preinst

`-- usr

    `-- share

        `-- doc

            `-- securing-debian-howto.zh-cn

                |-- ap-bind-chuser.zh-cn.html

                |-- ap-bridge-fw.zh-cn.html

                |-- ap-checklist.zh-cn.html

... ...

  • 创建配置文件

在 fakeroot 下创建DEBIAN目录用于放置控制文件比如:

control preinst postinst prerm postrm 等, 只有 control 是必须的, 其他为可选.

可以在 /var/lib/dpkg/info 目录下找到这些脚本的范例

比如

tony@tony:~/fakeroot/DEBIAN$ cat control

 

Package: securing-debian-howto.zh-cn

Priority: extra

Section: doc

Maintainer: tony

Architecture: all

Version: 3.0

Description: Securing Debian Manual

This manual describes the security of the Debian GNU/Linux operating system and within the Debian project. It covers some of the common tasks involved in setting up a secure user and network environment, gives information on the security tools available, steps to take before and after a compromise and also describes how security is enforced in Debian by the security team. The document includes a step by step hardening guide and within the appendix there is detailed information on how to setup an intrusion detection system and a bridge firewall with Debian GNU/Linux.

 

tony@tony:~/fakeroot/DEBIAN$ cat preinst

#! /bin/sh

 

echo "."

echo "."

 

echo "Welcome to install Securing Debian HOWTO. "

echo "press any key to continue... "

read

exit 0

  • 生成 .deb 包

在 fakeroot 目录外执行:

$ dpkg -b fakeroot securing-debian-howto-zh-cn.deb

 

dpkg-deb: building package `securing-debian-howto.zh-cn' in `securing-debian-howto-zh-cn.deb'.

将会在当前目录下生成 securing-debian-howto-zh-cn.deb 包

  • 检查包内容

可是用以下命令检查:

$ dpkg -c securing-debian-howto-zh-cn.deb

 

$ dpkg -I securing-debian-howto-zh-cn.deb

源代码构建

这次以 leafpad 为例, leafpad 的当前版本为 0.8.9

  • 下载程序源代码

·                      #wget

  • 创建工作目录

·                      # mkdir leafpad 

·                       

·                      #mv leafpad-0.8.9.tar.gz leafpad/

·                       

·                      #cd leafpad

·                       

·                      #tar zxvf leafpad-0.8.9.tar.gz

·                       

·                      #cd leafpad-0.8.9

  • 安装打包环境

运行一下命令,安装编译 leafpad 所需的环境

# apt-get install dh-make dpkg-dev g++ patch make libc6-dev fakeroot libgtk2.0-dev cdbs debhelper pkg-config gnome-pkg-tools

  • Debian 初始化

·                      # dh_make -e etony@tom.com -f ../leafpad-0.8.9.tar.gz

·                       

·                      Type of package: single binary, multiple binary, library, kernel module or cdbs?

·                       [s/m/l/k/b] s

·                       

·                      Maintainer name : tony

·                      Email-Address   : etony@tom.com

·                      Date            : Sun, 28 May 2006 22:38:08 +0800

·                      Package Name    : leafpad

·                      Version         : 0.8.9

·                      License         : blank

·                      Type of Package : Single

·                      Hit to confirm:

·                      Done. Please edit the files in the debian/ subdirectory now. leafpad

·                      uses a configure script, so you probably don't have to edit the Makefiles.

  • 构建配置文件

根据需求修改 debian/ 目录下的内容

修改 changelog, control,

以下为自动生成的 changelog, control 配置文件框架

/leafpad/leafpad-0.8.9/debian$ cat control

Source: leafpad

Section: unknown

Priority: optional

Maintainer: tony

Build-Depends: debhelper (>= 4.0.0), autotools-dev

Standards-Version: 3.6.2

 

Package: leafpad

Architecture: any

Depends: ${shlibs:Depends}, ${misc:Depends}

Description:

 

 

/leafpad/leafpad-0.8.9/debian$ cat changelog

leafpad (0.8.9-1) unstable; urgency=low

 

  * Initial release Closes: #nnnn  (nnnn is the bug number of your ITP)

 

 -- tony   Sun, 28 May 2006 22:38:08 +0800

如有需要自己,根据示例文件 *.ex 生成 postinst, postrm, prerm 等脚本文件

  • 构建 .deb 包

·                      # dpkg-buildpackage -rfakeroot -uc -b

如果您已经多次运行了上边的命令, 也可以使用下边的命令快速构建临时 .deb 包:

#fakeroot debian/rules binary

将会在其上一级目录生成 leafpad_0.8.9-1_i386.deb

参考文档

 

阅读(2128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~