天天看點

Linux Rpmbuild 包制作一、rpmbuild大緻步驟二、rpmbuild各目錄的作用三、rpmbuild安裝配置步驟四、執行個體說明五、rpmbuild指令參數詳解

Linux Rpmbuild 包制作

rpmbuild的原理,意義,具體參考官方文檔

<a href="http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/index.html">http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/index.html</a>

英文

中文

1.Set up the directory structure

建立目錄結構

2.Place the sources in the right directory

将源檔案放入正确目錄

3.Create a spec file that tells the rpmbuild command what to do

建立一個spec的檔案,編輯rpmbuild指令要做什麼

4.Build the source andbinary RPMs

建立源碼及二進制包

BUILD

The rpmbuild command builds software in this directory

RPMS

The rpmbuild command stores binary RPMs it reates in this irectory

SOURCES

You should put the sources for the application in this directory

SPECS

You should place the spec file for each RPM you plan to make in this directory

SRPMS

The rpmbuild command places source RPMs in this directory

在制作rpm包之前,需要安裝rpm-build這個軟體包

[root@test01 ~]# yum -y install rpm-build

增加一個普通使用者,不要以root身份來進行rpmbuild,危險操作,慎重,慎重!

[root@test01 ~]# useradd kuangl

[root@test01 ~]# su – kuangl

[kuangl@test01 software]$

mkdir -pv rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

mkdir: 已建立目錄 “rpmbuild”

mkdir: 已建立目錄 “rpmbuild/BUILD”

mkdir: 已建立目錄 “rpmbuild/RPMS”

mkdir: 已建立目錄 “rpmbuild/SOURCES”

mkdir: 已建立目錄 “rpmbuild/SPECS”

mkdir: 已建立目錄 “rpmbuild/SRPMS”

[kuangl@test01 software]$

echo "% _topdir/home/kuangl/rpmbuild" &gt;~/.rpmmacros

[kuangl@test01 software]$ rpmbuild --showrc|grep _topdir

-14: _builddir%{_topdir}/BUILD

-14: _rpmdir%{_topdir}/RPMS

-14: _sourcedir%{_topdir}/SOURCES

-14: _specdir%{_topdir}/SPECS

-14: _srcrpmdir%{_topdir}/SRPMS

-14: _topdir/home/admin/rpmbuild

檢視rpmbuild的内置變量

[kuangl@test01 software]$ rpmbuild --showrv

<a href="http://blog.51cto.com/attachment/201307/155600732.jpg" target="_blank"></a>

[kuangl@test01 SPECS]$ pwd

/home/kuangl/software/rpmbuild/SPECS

[kuangl@test01 SPECS]$ vim test.spec

随便建立個以spec結尾的檔案名,會出現以下資訊

Name:

Version:

Release:1%{?dist}

Summary:

Group:

License:

URL:

Source0:

BuildRoot:%(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

BuildRequires:

Requires:

%description

%prep

%setup -q

%build

%configure

make %{?_smp_mflags}

%install

rm -rf %{buildroot}

make install DESTDIR=%{buildroot}

%clean

%files

%defattr(-,root,root,-)

%doc

%changelog

變量

系統目錄

RPM_BUILD_DIR:

/$HOME/rpmbuild/BUILD

RPM_BUILD_ROOT:

/$HOME/rpmbuild/BUILDROOT

%{_sysconfdir}:

/etc

%{_sbindir}:

/usr/sbin

%{_bindir}:

/usr/bin

%{_datadir}:

/usr/share

%{_mandir}:

/usr/share/man

%{_libdir}:

/usr/lib64

%{_prefix}:

/usr

%{_localstatedir}:

/usr/var

%prep預處理腳本程式(prep)開始執行"

%setup

%build"編譯連接配接腳本程式(build)開始執行"

make

%install"安裝腳本程式(install)開始執行"

make install

%clean"建包結束後清理腳本程式(clean)開始執行"

%pre"安裝前執行腳本程式(pre)開始執行"

%post"安裝後執行腳本程式(post)開始執行"

%preun"解除安裝前執行腳本程式(preun)開始執行"

%postun"解除安裝後執行腳本程式(postun)開始執行"

%veryfiscript "軟體包校驗腳本程式(verifyscript)開始執行"

%triggerin -- xiuwu"軟體包安裝時觸發腳本程式(triggerin)開始執行"

%triggerun -- yuntaishan &lt; 2.0 "軟體包解除安裝前觸發腳本程式(triggerun)開始執行"

%triggerpostun – dapubu"軟體包解除安裝後觸發腳本程式(triggerpostun)開始執行"

%defattr (-,root,root)

%config /etc/funkey.def

%changelog更改日志

#

%define nginx_home %{_localstatedir}/cache/nginx

%define nginx_user nginx

%define nginx_group nginx

# 軟體包的内容概要

Summary: high performance web server

#軟體包的名稱

Name: nginx

#軟體包的版本

Version: 1.2.6

#軟體包的釋出實際版本号

Release: 1%{?dist}.ngx

#發行商或者打包組織的資訊

Vendor: nginx inc.

#軟體的首頁

URL: http://nginx.org/

#源代碼包,可以帶多個source0,source1等源,後面可以用%{source},%{source1}引用

Source0: http://nginx.org/download/%{name}-%{version}.tar.gz

Source1: logrotate

Source2: nginx.init

Source3: nginx.sysconf

Source4: nginx.conf

Source5: nginx.vh.default.conf

Source6: nginx.vh.example_ssl.conf

Source7: nginx.suse.init

#軟體授權方式

License: 2-clause BSD-like license

%if 0%{?suse_version}

#軟體分組

Group: Productivity/Networking/Web/Servers

%else

Group: System Environment/Daemons

%endif

#安裝或者編譯時使用的“虛拟目錄”

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

#需要依賴的軟體包

BuildRequires: zlib-devel

BuildRequires: pcre-devel

BuildRequires: perl

BuildRequires: libopenssl-devel

Requires(pre): pwdutils

BuildRequires: openssl-devel

Requires: initscripts &gt;= 8.36

Requires(pre): shadow-utils

Requires(post): chkconfig

#指明本軟體一些特定的功能,以便與其他rpm識别

Provides: webserver

#軟體的詳細說明

nginx [engine x] is a HTTP and reverse proxy server, as well as

a mail proxy server

%package debug

Summary: debug version of nginx

Requires: nginx

%description debug

not stripped version of nginx build with the debugging log support

#預處理腳本

#開始建構包

./configure \

--prefix=%{_sysconfdir}/nginx/ \

--sbin-path=%{_sbindir}/nginx \

--conf-path=%{_sysconfdir}/nginx/nginx.conf \

--error-log-path=%{_localstatedir}/log/nginx/error.log \

--http-log-path=%{_localstatedir}/log/nginx/access.log \

--pid-path=%{_localstatedir}/run/nginx.pid \

--lock-path=%{_localstatedir}/run/nginx.lock \

--http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \

--http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \

--http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \

--http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \

--http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \

--user=%{nginx_user} \

--group=%{nginx_group} \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_addition_module \

--with-http_sub_module \

--with-http_dav_module \

--with-http_gzip_static_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_stub_status_module \

--with-mail \

--with-mail_ssl_module \

--with-file-aio \

--with-debug \

--with-cc-opt="%{optflags} $(pcre-config --cflags)" \

$*

%{__mv} %{_builddir}/%{name}-%{version}/objs/nginx \

%{_builddir}/%{name}-%{version}/objs/nginx.debug

--with-http_flv_module \

--with-http_mp4_module \

--with-ipv6 \

#開手機把軟體安裝到虛拟的根目錄中

%{__rm} -rf $RPM_BUILD_ROOT

%{__make} DESTDIR=$RPM_BUILD_ROOT install

%{__mkdir} -p $RPM_BUILD_ROOT%{_datadir}/nginx

%{__mv} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/html $RPM_BUILD_ROOT%{_datadir}/nginx/

%{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/*.default

%{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/fastcgi.conf

%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/log/nginx

%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/run/nginx

%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/cache/nginx

%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d

%{__rm} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf

%{__install} -m 644 -p %{SOURCE4} \

$RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf

%{__install} -m 644 -p %{SOURCE5} \

$RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/default.conf

%{__install} -m 644 -p %{SOURCE6} \

$RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/example_ssl.conf

%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig

%{__install} -m 644 -p %{SOURCE3} \

$RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/nginx

# install SYSV init stuff

%{__mkdir} -p $RPM_BUILD_ROOT%{_initrddir}

%{__install} -m755 %{SOURCE7} \

$RPM_BUILD_ROOT%{_initrddir}/nginx

%{__install} -m755 %{SOURCE2} \

# install log rotation stuff

%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d

%{__install} -m 644 -p %{SOURCE1} \

$RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/nginx

%{__install} -m644 %{_builddir}/%{name}-%{version}/objs/nginx.debug \

$RPM_BUILD_ROOT%{_sbindir}/nginx.debug

#清理臨時檔案

#定義哪些檔案或者目錄會放入rpm中

%defattr(-,root,root)

%{_sbindir}/nginx

%dir %{_sysconfdir}/nginx

%dir %{_sysconfdir}/nginx/conf.d

%config(noreplace) %{_sysconfdir}/nginx/nginx.conf

%config(noreplace) %{_sysconfdir}/nginx/conf.d/default.conf

%config(noreplace) %{_sysconfdir}/nginx/conf.d/example_ssl.conf

%config(noreplace) %{_sysconfdir}/nginx/mime.types

%config(noreplace) %{_sysconfdir}/nginx/fastcgi_params

%config(noreplace) %{_sysconfdir}/nginx/scgi_params

%config(noreplace) %{_sysconfdir}/nginx/uwsgi_params

%config(noreplace) %{_sysconfdir}/nginx/koi-utf

%config(noreplace) %{_sysconfdir}/nginx/koi-win

%config(noreplace) %{_sysconfdir}/nginx/win-utf

%config(noreplace) %{_sysconfdir}/logrotate.d/nginx

%config(noreplace) %{_sysconfdir}/sysconfig/nginx

%{_initrddir}/nginx

%dir %{_datadir}/nginx

%dir %{_datadir}/nginx/html

%{_datadir}/nginx/html/*

%attr(0755,root,root) %dir %{_localstatedir}/cache/nginx

%attr(0755,root,root) %dir %{_localstatedir}/log/nginx

%files debug

#%attr(權限,屬主,屬組)

%attr(0755,root,root) %{_sbindir}/nginx.debug

#rpm安裝前執行的腳本

%pre

# Add the "nginx" user

getent group %{nginx_group} &gt;/dev/null || groupadd -r %{nginx_group}

getent passwd %{nginx_user} &gt;/dev/null || \

useradd -r -g %{nginx_group} -s /sbin/nologin \

-d %{nginx_home} -c "nginx user"%{nginx_user}

exit 0

#rpm安裝後執行的腳本

%post

# Register the nginx service

if [ $1 -eq 1 ]; then

/sbin/chkconfig --add nginx

# print site info

cat &lt;&lt;BANNER

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

Thanks for using NGINX!

Check out our community web site:

* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:

* http://www.nginx.com/support.html

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

BANNER

fi

#preunrpm解除安裝前執行的腳本

%preun

#$0代表解除安裝,$1代表安裝,$2代表更新

if [ $1 -eq 0 ]; then

/sbin/service nginx stop &gt; /dev/null 2&gt;&amp;1

/sbin/chkconfig --del nginx

#postrun 人拼命解除安裝後執行的腳本

%postun

if [ $1 -ge 1 ]; then

/sbin/service nginx upgrade &amp;&gt;/dev/null || :

#變更日志

* Tue Dec 11 2012 Sergey Budnevitch &lt;[email protected]&gt;

- 1.2.6

後面可使用%{name}的方式引用,具體指令需跟源包一緻

具體指令需跟源包一緻

可以帶多個source0,source1等源,後面可以用%{SOURCE0},%{SOURCE1}引用

Source8: nginx.vh.example.conf

考慮到多使用者的環境,一般定義為

該參數非常重要,因為在生成rpm的過程中,執行make install時就會把軟體安裝到上述的路徑中,在打包的時候,同樣依賴“虛拟目錄”為“根目錄”進行操作。

後面可使用$RPM_BUILD_ROOT 方式引用。

#該段内容為安裝前腳本程式。它在軟體包安裝之前執行,通常是檢測操作環境,建立有關目錄,清理多餘檔案等等,為軟體包的順利安裝做準備。本段很少使用,其段名格式為: %pre [子包選項]

%patch

此宏利用系統中的patch指令,來給指定的源程式包打更新檔,進而将程式更新。其使用格式為:

%patch [-P N] [-p N] [-b name] [-E]

注: []所括為可選項

(1) 當沒有任何選項時:

沒有任何選項時,該宏使用的是預設的更新檔檔案(第0個更新檔檔案),即由檔案頭Patch或Patch0域所定義的檔案

patch指令用了兩個選項:(有關patch指令用法,詳見其使用者手冊)

* -p :這個選項用于确定patch所要操作的檔案。它針對更新檔檔案頭部的檔案名,删除名字中指定數目個

斜杠(/)前面的所有字元,進而得到要操作的檔案名。

* -s :這個選項訓示patch在打更新檔過程中不輸出任何資訊,即使有錯誤發生。

(2) -P N :

使用此選項以訓示RPM使用第N個更新檔檔案(由檔案頭PatchN域定義)。

(3) -p N :

此選項與其參數是由%patch宏直接傳給patch指令的。請參見上面patch指令所用的-p選項的介紹。

(4) -b name :

當有多個patch指令操作同一個檔案時,patch會将原檔案換名儲存(其字尾變作.orig)

(5) -E :

此選項直接傳給patch指令,其作用是:如果一個檔案打完更新檔後内容為空(位元組數為0),則删除這個檔案。

此為編譯段,其内容為編譯腳本程式。該程式完成源程式的編譯和連接配接。一個最簡單的例子就是程式中僅有一個make指令。這适用于大部分情況,因為多數軟體均有自己的makefile,這樣通過make指令就可實作編譯與連接配接。如果沒有makefile的話,需要軟體包制作者自己在編譯段書寫上一系列的編譯連接配接指令。

此為安裝段,其内容是安裝腳本程式。該程式将已編譯連接配接好的執行程式或其它檔案存放到指定目錄下,這些程式或檔案供RPM打包時使用。一個最簡單的例子就是程式中僅用一個make install指令,進而完成安裝。這也需要相應的軟體有makefile維護檔案。沒有的話,軟體包制作者也得自己寫指令。

$RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/example.txt

%{__install} -m 644 -p %{SOURCE8} \

$RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/example.conf

#%config(noreplace) %{_sysconfdir}/nginx/conf.d/default.conf

%config(noreplace) %{_sysconfdir}/nginx/conf.d/example.conf

%config(noreplace) %{_sysconfdir}/nginx/conf.d/example.txt

該段内容為安裝後腳本程式。它在軟體包安裝完成之後執行,常用來建立符号連接配接,修改系統配置檔案,運作ldconfig程式等,以利軟體的正常運

[ -d "/opt/web/www" ]

[ "$?" != "0" ]&amp;&amp; mkdir -p /opt/web/www

/sbin/chkconfig nginx on

/sbin/service nginx start

#ngpassword is a tool for nginx to create password

cat &gt;/usr/sbin/ngpassword &lt;&lt;EOF

#!/usr/bin/perl

use strict;

my \$pw=\$ARGV[0] ;

print crypt(\$pw,\$pw)."\n";

EOF

chmod 755 /usr/sbin/ngpassword

#$1代表動作

0代表解除安裝

1代表安裝

2代表更新

#postrun rpm解除安裝後執行的腳本

* Thu Jan 31 2013change config file&lt;[email protected]&gt;

rpmbuild [options] [spec文檔|tarball包|源碼包]

[kuangl@test01 SPECS]$ rpmbuild -ba nginx.spec

選項

含義

-bp

檔案中安裝執行前的地方

-bc

執行spec的%pre和%build 段(準備并編譯)

-bi

執行spec中%pre,%build與%install(準備,編譯并安裝)

-bl

檢查spec中的%file段(檢視檔案是否齊全)

-ba

建立源碼與二進制包(常用)

-bb

隻建立二進制包(常用)

-bs

隻建立源碼包

-tp

對應-bp

-tc

對應-bc

-ti

對應-bi

-ta

對應-ba

-tb

對應-bb

-ts

對應-bs

--rebuild

建立二進制包,通-bb

其他的一些選項

--buildroot=DIRECTORY

确定以root目錄建立包

--clean

完成打包後清除BUILD下的檔案目錄

--nobuild

不進行%build的階段

--nodeps

不檢查建立包時的關聯檔案

--rmsource

完成打包後清除SOURCES

--rmspec

完成打包後清除SPEC

--target=CPU-VENDOR-OS

确定包的最終使用平台

本文轉自 kuangling 51CTO部落格,原文連結:http://blog.51cto.com/kling/1239571

繼續閱讀