如何制作deb包

来源:互联网 发布:php彩票源码 编辑:程序博客网 时间:2024/06/10 21:20
下面用一个简单的程序例子,来讲解如何制作deb包。首先你需要一个deb包管理的系统,debian, ubuntu等。
这里我用的是nexenta. 这些系统默认装好了deb包制作需要的工具,如dpkg-dev, devscripts等。如果没有,你也可以在制作过程中用apt-get install <packagename>来手动安装。

1. 创建一个简单的源码包
aubrey@aubrey-nexenta:~/deb$ ls -l hellodeb/
total 2
-rw-r--r-- 1 aubrey staff 203 Feb 16 12:50 Makefile
-rw-r--r-- 1 aubrey staff  73 Feb 16 12:46 hellodeb.cC code与制作deb包关系不大,也不需要修改,我们主要看一下Makefile文件,我们在制作deb包的时候,这个文件是需要修改的。
PROG=hellodeb
CC=gcc
BINDIR=/usr/bin
INSTALL=cp

$(PROG): hellodeb.c
        $(CC) -o $(PROG) hellodeb.c

clean:
        rm -rf $(PROG)

install:
        $(INSTALL) $(PROG) $(BINDIR)

uninstall:
        rm -rf $(BINDIR)/$(PROG)2. 创建GPG key。GPG key在build包的时候需要用到,创建的方法参见GPG使用指南 。创建完后,检查一下:
aubrey@aubrey-nexenta:~/deb/hellodeb$ gpg --list-keys
/export/home/aubrey/.gnupg/pubring.gpg
--------------------------------------
pub   1024D/7F8F1E57 2008-01-29
uid                  Aubrey Li <aubreylee@gmail.com>
sub   2048g/6AF6581E 2008-01-29
3. 要开始对这个包进行deb化了。首先确保源代码目录绝对干净,为了让软件包能够正确地制作,必须把源代码目录的名字改成小写,并且符合<packagename>-<version>的形式。
aubrey@aubrey-nexenta:~/deb$ ls
hellodeb
aubrey@aubrey-nexenta:~/deb$ mv hellodeb/ hellodeb-1.0
aubrey@aubrey-nexenta:~/deb$ ls
hellodeb-1.04. 在正式deb化之前,我们先要export两个环境变量:
aubrey@aubrey-nexenta:~/deb$ export DEBEMAIL="aubreylee@gmail.com"
aubrey@aubrey-nexenta:~/deb$ export DEBFULLNAME="Aubrey Li"注意,这里的name和email必须和你生成GPG钥匙的时候完全一样。这两个变量值也会在deb包的changelog等多个文件里被用到。
5. 现在可以对源码包进行deb化了。
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0$ dh_make

Type of package: single binary, multiple binary, library, kernel module or cdbs?
 [s/m/l/k/b] s

Maintainer name : Aubrey Li
Email-Address   : aubreylee@gmail.com
Date            : Sat, 16 Feb 2008 13:19:46 +0800
Package Name    : hellodeb
Version         : 1.0
License         : blank
Type of Package : Single
Hit <enter> to confirm:
Done. Please edit the files in the debian/ subdirectory now. You should also
check that the hellodeb Makefiles install into $DESTDIR and not in / .
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0$
这里询问包的类型,这里我们是单个可执行文件,所以我选了s。
还有两个重要的提示:
  • Please edit the files in the debian/ subdirectory now.
  • You should also check that the hellodeb Makefiles install into $DESTDIR and not in / .
6. 我们先关注一下第二个提示,修改Makefile。这里主要是安装路径,修改如下:
PROG=hellodeb
CC=gcc
BINDIR=$(DESTDIR)/usr/bin
INSTALL=cp

$(PROG): hellodeb.c
        $(CC) -o $(PROG) hellodeb.c

clean:
        rm -rf $(PROG)

install:
        mkdir -p $(BINDIR)
        $(INSTALL) $(PROG) $(BINDIR)

uninstall:
        rm -rf $(BINDIR)/$(PROG)第一个修改是为了在build包的时候能够把需要的文件安装到正确的目录,从而正确的包含在生成的deb包中。
第二个修改是因为修改后的BINDIR变量的目录并不存在,所以需要手动创建。

7. 然后我们要看一下debian这个生成的目录了
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0/debian$ ls
README.Debian  control    docs                hellodeb-default.ex   manpage.sgml.ex  postrm.ex   watch.ex
changelog      copyright  emacsen-install.ex  hellodeb.doc-base.EX  manpage.xml.ex   preinst.ex
compat         cron.d.ex  emacsen-remove.ex   init.d.ex             menu.ex          prerm.ex
conffiles.ex   dirs       emacsen-startup.ex  manpage.1.ex          postinst.ex      rules这个目录下面的文件很多,不能一一解释。这里列举几个重要的,也是绝大部分软件必须的:
  • control文件: 声明很多重要的变量,dpkg通过这些变量来管理软件包
  • copyright文件: 不用说,版权信息,相当重要
  • changelog文件: 这是一个必需文件,包含软件版本号,修订号,发行版和优先级。
  • rules文件: 这实际上是另外一个Makefile脚本,用来给dpkg-buildpackage用的.
  • compat文件: 这个文件留着是有用的
  • dirs文件:这个文件指出我们需要的但是在缺省情况下不会自动创建的目录
我删除掉其他文件,debian目录现在如下:
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0/debian$ ls
changelog  compat  control  copyright  dirs  rules注意,除了compat文件,其他文件都是需要修改的,根据你自己的软件包的情况。

8. 好了,所有的准备工作都就绪了。我们可以build软件包。dpkg-buildpackage有一项dh_testroot的检查,你必须用root来运行这个命令,或者用fakeroot(需要安装fakeroot包)
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0$ dpkg-buildpackage -rfakeroot -sa或者切换到root,注意,你要确保你保持切换后仍旧保持当前用户的路径,否则在build包的时候会找不到GPG key。
aubrey@aubrey-nexenta:~/deb/hellodeb-1.0$ sudo -s
Password:
root@aubrey-nexenta:~/deb/hellodeb-1.0# dpkg-buildpackage -sabuild过程中需要输入GPG密匙的密码,两次,其他就没什么事情可做了。

9. 创建完成后,在该目录的上级目录应该得到如下几个文件:
  • hellodeb_1.0-1.tar.gz: 源码包
  • hellodeb_1.0-1.dsc: 源代码总结,根据control文件创建,包含GPG签名
  • hellodeb_1.0-1_solaris-i386.deb: 完整的二进制包,可用dpkg管理
  • hellodeb_1.0-1_solaris-i386.changes: 供dput使用
10. 最后作一下检查和安装工作.
root@aubrey-nexenta:~/deb# dpkg-deb -c hellodeb_1.0-1_solaris-i386.deb
drwxr-xr-x root/root         0 2008-02-16 13:56:12 ./
drwxr-xr-x root/root         0 2008-02-16 13:56:10 ./usr/
drwxr-xr-x root/root         0 2008-02-16 13:56:10 ./usr/share/
drwxr-xr-x root/root         0 2008-02-16 13:56:10 ./usr/share/doc/
drwxr-xr-x root/root         0 2008-02-16 13:56:12 ./usr/share/doc/hellodeb/
-rw-r--r-- root/root       246 2008-02-16 13:19:48 ./usr/share/doc/hellodeb/copyright
-rw-r--r-- root/root       191 2008-02-16 13:19:48 ./usr/share/doc/hellodeb/changelog.Debian.gz
drwxr-xr-x root/root         0 2008-02-16 13:56:11 ./usr/bin/
-rwxr-xr-x root/root      3436 2008-02-16 13:56:11 ./usr/bin/hellodeb
root@aubrey-nexenta:~/deb# dpkg -i hellodeb_1.0-1_solaris-i386.deb
Selecting previously deselected package hellodeb.
(Reading database ... 31630 files and directories currently installed.)
Unpacking hellodeb (from hellodeb_1.0-1_solaris-i386.deb) ...
Setting up hellodeb (1.0-1) ...
root@aubrey-nexenta:~/deb# which hellodeb
/usr/bin/hellodeb
root@aubrey-nexenta:~/deb# hellodeb
hello deb
root@aubrey-nexenta:~/deb# dpkg -r hellodeb
(Reading database ... 31632 files and directories currently installed.)
Removing hellodeb ...
root@aubrey-nexenta:~/deb# which hellodeb
root@aubrey-nexenta:~/deb#


if like this that you meet

Could not find hello_1.0.orig.tar.gz
Either specify an alternate file to use with -f,
or add --createorig to create one.

do like this :
#dh_make --createorig

all is ok now








参考:http://www.linuxsir.org/bbs/thread322285.html
http://sunwill.blog.ccidnet.com/blog.php?do=showone&itemid=175703&typ=blog
http://www.debian-administration.org/articles/488
http://www.debian.org/doc/maint-guide/

http://www.quietearth.us/articles/2006/08/16/Building-deb-package-from-source           //good site

原创粉丝点击