linux deb 安装包制作,linux/ubuntu中制作deb安裝包

linux/ubuntu中制作deb安裝包

由於要制作一個在arm平台上運行的xorg-server-1.12.4版本的安裝包,所以需要學習如何制作deb安裝包。這里以一個非常小的工程為例,記錄制作的過程。

首先需要一個deb包管理系統,如debian、ubuntu等,這里我使用的是ubuntu14.04LTS,這些系統默認安裝了deb包制作所需的工具,沒有的話后期也可以通過sudo apt-get install來安裝。

1 創建一個簡單的源碼包

ls -l

total 24

-rw-rw-r– 1 mountzf mountzf 73 Jul 8 14:05 helloworld.c

-rw-rw-r– 1 mountzf mountzf 323 Jul 8 14:32 Makefile#include

int main()

{

printf("Hello World!\n");

return 0;

}

C code的內容與deb包的制作關系不大,這里還是先主要看一下makefile的內容,在制作deb包的時候,makfile是需要修改的。# Sample makefile.

PROG=helloworld

CC=gcc

BINDIR=/usr/bin

INSTALL=cp

# Compile commands.

$(PROG):helloworld.c

$(CC) -o $(PROG) helloworld.c

# make clean command.

clean:

rm -rf $(PROG)

# make install command.

install:

$(INSTALL) $(PROG) $(BINDIR)

# make uninstall command.

uninstall:

rm -rf $(BINDIR)/$(PROG)

2 創建GPG key

GPG key在build包的時候需要用到,ubuntu系統中默認已經安裝gpg工具,可以gpg --help查看使用方法。這里gpg --gen-key,然后按照提示依次進行即可。由於我是在虛擬機中運行ubuntu,生成密鑰時遇到如下問題:

Not enough random bytes available. Please do some other work to give

the OS a chance to collect more entropy! (Need 288 more bytes)

不要慌,翻閱論壇后發現很多人都說如下命令針對此問題有效:sudo aptitude install haveged

安裝完成haveged之后,順利生成公鑰和私鑰,創建完成之后檢查一下:

gpg –list-keys

/home/mountzf/.gnupg/pubring.gpg

--------------------------------

pub 2048R/306A7521 2016-07-08

uid mountzf

sub 2048R/17D974A5 2016-07-08

3 環境准備

在對這個源碼包進行deb化之前,首先要確保源代碼目錄絕對干凈,為了讓軟件包能夠正確地制作,需要把源代碼目錄更改為“小寫字母-版本號”格式。同時需要export兩個環境變量。

~/makeDeb$ ls -l

total 4

drwxrwxr-x 2 mountzf mountzf 4096 Jul 8 14:32 helloworld-0.1export DEBEMAIL="xxx@xx.xx"

export DEBFULLNAME="xxxx"

注意此處的郵箱和密碼必須和你在生成gpg key的時候完全一致,這兩個變量的值也會在changelog等多處文件中用到。

4 對源碼包進行deb化

如果系統沒有安裝dh-make工具包,需要執行sudo apt-get install dh-make命令進行安裝。dh_make --createorig命令生成制作deb包所需的默認信息並在上一層目錄中生成helloworld_0.1.orig.tar.xz源碼壓縮包(沒有源碼壓縮包的話dh_make將不能成功執行)。

mountzf@mountzf:~/makeDeb/helloworld-0.1$ dh_make –createorig

Type of package: single binary, indep binary, multiple binary, library, kernel module, kernel patch?

[s/i/m/l/k/n] s

Maintainer name : mountzf

Email-Address : xxxx@xxx.xxx

Date : Fri, 08 Jul 2016 15:44:24 +0800

Package Name : helloworld

Version : 0.1

License : blank

Type of Package : Single

Hit to confirm:

Done. Please edit the files in the debian/ subdirectory now. You should also

check that the helloworld Makefiles install into $DESTDIR and not in / .

這里詢問安裝包類型,我么們這里是單個可執行文件,所以我選擇了s。同時系統給出了兩個提示信息,我們發現當前目錄中多了一個debian目錄,同時目錄中有些內容需要編輯。

changelog control docs helloworld.default.ex init.d.ex manpage.sgml.ex menu.ex postrm.ex prerm.ex README.source source

compat copyright helloworld.cron.d.ex helloworld.doc-base.EX manpage.1.ex manpage.xml.ex postinst.ex preinst.ex README.Debian rules watch.ex

這個目錄下面的文件很多,不能一一解釋。這里列舉幾個重要的,也是絕大部分軟件必須的:

control文件: 聲明很多重要的變量,dpkg通過這些變量來管理軟件包

copyright文件: 不用說,版權信息,相當重要

changelog文件: 這是一個必需文件,包含軟件版本號,修訂號,發行版和優先級。

rules文件: 這實際上是另外一個Makefile腳本,用來給dpkg-buildpackage用的.

compat文件: 這個文件留着是有用的

dirs文件:這個文件指出我們需要的但是在缺省情況下不會自動創建的目錄

其中control文件我們可能需要修改。刪掉后綴是 .ex 和 .EX 的文件,不然如果你沒編輯它們並重命名為不帶 .ex/.EX 后綴的同名文件,在打包完成的 lintian 檢查時會有一條錯誤叫做:Debin 幫助文件被打包進了 .debian.tar.gzrm -rf *.ex

rm -rf *.EX

至於 .ex 和 .EX 是干嘛的,看下面這個列表:

emacsen 開頭的是針對基於 emacs 這個 IDE 的名為 emacsen 的編輯器的安裝/刪除/啟動腳本。是打包類似 emacsen 插件這樣的程序才會用到的。但是 emacsen 這個項目 2007 年就不更新了。於是無用。

initd.ex 是啟動腳本,你的軟件要開機自啟動,才需要去編輯並重命名它,比如輸入法。否則無用。

cron.d.ex 定時服務。除非你的軟件有服務,並需要按周期運行。否則無用。同理 default.ex 也是這樣。

doc-base.EX 是 deb 系專用的。大概意思是告訴系統各種不同格式的幫助手冊在哪里可以找得到。你在乎手冊嗎?

manpage.*.ex 是具體的各個格式的幫助手冊。

post*.ex 跟 RPM 系的 post 一樣。postinst 管安裝,postrm 管刪除。一般是庫文件會用。而且這個無需你去干預,改名去掉 .ex 就可以,dh_installdeb 會自動幫你填寫。

watch.ex 是蝶變專有的,意思是關注某個 url,有變化就郵件通知你來升級。跟 OBS 的 auto check 差不多。除了更新狂誰都用不到。而且里面是用正則表達式寫的,要是不懂就算了吧。

接着要刪除 README*。rm -rf README*

其中 README.debian 是類似於 RPM 的 changelog 這樣的一個存在。比如你加了補丁,改動了什么,寫進去。 README.source 是描述源代碼是否滿足蝶變策略的文件,我們打的包都不通過蝶變官方分發,所以不用管這個。

5 修改Makefile

根據第二個提示,我們不應該將該程序安裝至/根目錄中,而應該在$DESTDIR,修改Makefile如下:BINDIR=$(DESTDIR)/usr/bin

# make install command.

install:

mkdir -p $(BINDIR)

$(INSTALL) $(PROG) $(BINDIR)

第一處改動是為了在build包的時候把文件安裝到正確的目錄,第二處修改是$(DESTDIR)/usr/local/bin並不存在,所以在安裝之前需要創建安裝目錄。

注意:Debian要求可執行文件不能安裝在/usr/local目錄下,因此如果BINDIR設為/usr/local/bin的話,build的時候會出錯而不能繼續進行。

6 build軟件包

基本上所有工作都准備就緒了,下一步我們就可以build軟件包了。利用dpkg-buildpackage -rfakeroot命令。

gpg: WARNING: unsafe ownership on configuration file /home/mountzf/.gnupg/gpg.conf

dpkg-buildpackage: warning: failed to sign .dsc and .changes file

切換到root賬戶,再運如上命令即可。sudo su

dpkg-buildpackage -rfakeroot

此時源碼已經打包完畢,生成了deb安裝包helloworld_0.1-1_amd64.deb和.tar.gz壓縮包。查看一下生成的文件:

root@mountzf:~# which helloworld

/usr/local/bin/helloworld

root@mountzf:/home/mountzf/makeDeb# ls -l

total 72

drwxrwxr-x 3 mountzf mountzf 4096 Jul 8 16:22 helloworld-0.1

-rw-r–r– 1 root root 1504 Jul 8 16:22 helloworld_0.1-1_amd64.changes

-rw-r–r– 1 root root 2194 Jul 8 16:22 helloworld_0.1-1_amd64.deb

-rw-rw-r– 1 mountzf mountzf 9441 Jul 8 16:22 helloworld_0.1-1.debian.tar.gz

-rw-rw-r– 1 mountzf mountzf 824 Jul 8 16:22 helloworld_0.1-1.dsc

-rw-rw-r– 1 mountzf mountzf 464 Jul 8 15:44 helloworld_0.1.orig.tar.xz

7 檢查與安裝

最后,做一下檢查和安裝工作。

root@mountzf:/home/mountzf/makeDeb# dpkg-deb -c helloworld_0.1-1_amd64.deb

drwxr-xr-x root/root 0 2016-07-08 17:15 ./

drwxr-xr-x root/root 0 2016-07-08 17:15 ./usr/

drwxr-xr-x root/root 0 2016-07-08 17:15 ./usr/bin/

-rwxr-xr-x root/root 6128 2016-07-08 17:15 ./usr/bin/helloworld

drwxr-xr-x root/root 0 2016-07-08 17:15 ./usr/share/

drwxr-xr-x root/root 0 2016-07-08 17:15 ./usr/share/doc/

drwxr-xr-x root/root 0 2016-07-08 17:15 ./usr/share/doc/helloworld/

-rw-r–r– root/root 1676 2016-07-08 17:05 ./usr/share/doc/helloworld/copyright

-rw-r–r– root/root 180 2016-07-08 17:05 ./usr/share/doc/helloworld/changelog.Debian.gz

dpkg -i helloworld_0.1-1_amd64.deb安裝所制作的deb安裝包

root@mountzf:/home/mountzf/makeDeb# which helloworld

root@mountzf:/home/mountzf/makeDeb# dpkg -i helloworld_0.1-1_amd64.deb

(Reading database … 95541 files and directories currently installed.)

Preparing to unpack helloworld_0.1-1_amd64.deb …

Unpacking helloworld (0.1-1) over (0.1-1) …

Setting up helloworld (0.1-1) …

root@mountzf:/home/mountzf/makeDeb# which helloworld

/usr/bin/helloworld

dpkg -r helloworld 卸載剛才安裝的deb安裝包

root@mountzf:/home/mountzf/makeDeb# dpkg -r helloworld

(Reading database … 95541 files and directories currently installed.)

Removing helloworld (0.1-1) …

root@mountzf:/home/mountzf/makeDeb# which helloworld

root@mountzf:/home/mountzf/makeDeb#

至此,通過一個小的項目實例,驗證了利用dh_make和dpkg-buildpackage制作deb安裝包的步驟。但是這只是編譯和本機架構相同的deb安裝包,要在pc上編譯適用於arm的安裝包,這種方法好像沒有成功。后面將繼續學習如何利用pc編譯適用於arm的deb安裝包。

祝楓

2016年7月8日於深圳

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值