本文出自:Aubrey Li


下面用一個簡單的程序例子,來講解如何製作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.c
C 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.0
4. 在正式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 -sa
build過程中需要輸入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#
最後需要說明的是,Nexenta目前正在基於Ubuntu/dapper的deb包做移植,想學習deb包製作的,對OpenSolaris和Nexenta感興趣的,歡迎參與到Nexenta的開發中來。:)
arrow
arrow
    全站熱搜

    正義的胖虎 發表在 痞客邦 留言(0) 人氣()