RPM包快速構建教程

rpm打包需要特定的目錄及結構。查看rpm打包目錄 測試在CentOS6.3

rpm打包需要特定的目錄及結構。查看rpm打包目錄 測試在CentOS6.3

[root@localhost ~]#  rpm --showrc|grep _topdir

-14: _builddir  %{_topdir}/BUILD

-14: _buildrootdir      %{_topdir}/BUILDROOT

-14: _rpmdir    %{_topdir}/RPMS

-14: _sourcedir %{_topdir}/SOURCES

-14: _specdir   %{_topdir}/SPECS

-14: _srcrpmdir %{_topdir}/SRPMS

-14: _topdir    %{getenv:HOME}/rpmbuild

[root@localhost ~]# rpm --showrc|grep _usrsrc

-14: _usrsrc    %{_usr}/src

[root@localhost ~]# rpm --showrc|grep _usr

-14: _defaultdocdir     %{_usr}/share/doc

-14: _usr       /usr

-14: _usrsrc    %{_usr}/src

可以通過命令rpm --showrc查看實現代碼。另外直接通過 rpm --eval "%{macro}"來查看具體對應路徑。

比如我們要查看%{_bindir}的路徑,就可以使用命令rpm --eval "%{ _bindir}"來查看。

[root@localhost ~]# rpm --eval "%{_topdir}"

/root/rpmbuild

[root@localhost ~]# tree /root/rpmbuild

/root/rpmbuild

├── BUILD

│   └── hello-0.1

│       ├── debugfiles.list

│       ├── debuglinks.list

│       ├── debugsources.list

│       ├── hello

│       └── hello.c

├── BUILDROOT

├── RPMS

│   └── x86_64

│       ├── hello-0.1-1.x86_64.rpm

│       └── hello-debuginfo-0.1-1.x86_64.rpm

├── SOURCES

│   └── hello-0.1.tar.gz

├── SPECS

│   └── hello.spec

└── SRPMS

    └── hello-0.1-1.src.rpm

下面以hello world爲例,構建一個最小化打包過程。 首先需要寫一個SPEC文件hello.spce:

一般在這個目錄下  會自動出現的  

rpmdev-newspec leafpad
++++++++++++++++++++++++++++++++++

Summary:    hello world rpm package
Name:       hello
Version:    0.1
Release:    1
Source:     hello-0.1.tar.gz
License:    GPL
Packager:   test
Group:      Application


%description
This is a software for making your life more beautiful!

%prep
%setup -q

%build
gcc -o hello hello.c

%install
install -m 755 hello /usr/local/bin/hello

%files
/usr/local/bin/hello

放到上述SPECS目錄下。 然後一個源程序hello.c:

#include <stdio.h>int main()
{
    printf("Hello, World!\n");    return 0;
}
tar zcvf hello-0.1.tar.gz hello-0.1
mv hello-0.1.tar.gz  redhat/SOURCES
rpmbuild -ba hello.spec我們可以通過修改topdir宏的值來自定義打包路徑:
$ echo %_topdir $HOME/rpmbuild > ~/.rpmmacros
通過BuildRoot的值告訴rpmbuild,我們的構建根是builddir下的hello-root目錄。其中以%{}括起來的是RPM宏,_builddir代表~/rpmbuild/BUILD目錄;name代表spec文件開頭的Name字段值。

以下劃線開頭的builddir是系統RPM宏,我們可以通過rpm --showrc看到,可以在.rpmmacros中自定義。

RPM_BUILD_ROOT和前面的宏不同,這裏沒有{}括起來,是爲了在以後安裝生成的rpm時不至於也去尋找傳說中的構建根。

如果喜歡的話,可以修改Source字段如下:

實例  前提不缺依賴
1. 下載nginx源碼,直接運行命令:?12cd /rootwget http://nginx.org/download/nginx-1.7.1.tar.gz在拿到源碼包之後,解壓,並進入目錄:?12tar zxvf nginx-1.7.1.tar.gzcd nginx-1.7.12. 編寫SPEC文件文件名爲:nginx.spec?1234567891011121314151617181920212223242526272829Summary: High Performance Web ServerName: nginxVersion: 1.7.1Release: el5License: GPLGroup: Applications/ServerSource: http://nginx.org/download/nginx-%{version}.tar.gzURL: http://nginx.org/Distribution: LinuxPackager: yunjianfei <[email protected]>BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}%define srcdir /root/nginx-1.7.1%descriptionnginx [engine x] is a HTTP and reverse proxy server, as well as a mail proxy server%prep%buildcd %{srcdir}./configure --prefix=/usr/local/nginxmake -j8%installcd %{srcdir}make DESTDIR=%{buildroot} install%preunif [ -z "`ps aux | grep nginx | grep -v grep`" ];then killall nginx >/dev/null exit 0fi%files/usr/local/nginx3. 最後執行rpmbuild命令,打rpm包?1rpmbuild -bb nginx.spec

1

2

cd /root

wget http://nginx.org/download/nginx-1.7.1.tar.gz

在拿到源碼包之後,解壓,並進入目錄:

1

2

tar zxvf nginx-1.7.1.tar.gz

cd nginx-1.7.1

2. 編寫SPEC文件

文件名爲:nginx.spec

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Summary: High Performance Web Server

Name: nginx

Version: 1.7.1

Release: el5

License: GPL

Group: Applications/Server

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

URL: http://nginx.org/

Distribution: Linux

Packager: test

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

%define srcdir /root/nginx-1.7.1

%description

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

%prep

%build

cd %{srcdir}

./configure --prefix=/usr/local/nginx

make -j8

%install

cd %{srcdir}

make DESTDIR=%{buildroot} install

%preun

if [ -z "`ps aux | grep nginx | grep -v grep`" ];then

 killall nginx >/dev/null

 exit 0

fi

%files

/usr/local/nginx

3. 最後執行rpmbuild命令,打rpm包

1

rpmbuild -bb nginx.spec

spec腳本包括很多關鍵字,主要有:
引用
Name: 軟件包的名稱,後面可使用%{name}的方式引用

Summary: 軟件包的內容概要

Version: 軟件的實際版本號,例如:1.0.1等,後面可使用%{version}引用

Release: 發佈序列號,例如:1linuxing等,標明第幾次打包,後面可使用%{release}引用

Group: 軟件分組,建議使用標準分組

License: 軟件授權方式,通常就是GPL

Source: 源代碼包,可以帶多個用Source1、Source2等源,後面也可以用%{source1}、%{source2}引用

BuildRoot: 這個是安裝或編譯時使用的“虛擬目錄”,考慮到多用戶的環境,一般定義爲:
%{_tmppath}/%{name}-%{version}-%{release}-root
或
%{_tmppath}/%{name}-%{version}-%{release}-buildroot-%(%{__id_u} -n}
該參數非常重要,因爲在生成rpm的過程中,執行make install時就會把軟件安裝到上述的路徑中,在打包的時候,同樣依賴“虛擬目錄”爲“根目錄”進行操作。
後面可使用$RPM_BUILD_ROOT 方式引用。

URL: 軟件的主頁

Vendor: 發行商或打包組織的信息,例如RedFlag Co,Ltd

Disstribution: 發行版標識

Patch: 補丁源碼,可使用Patch1、Patch2等標識多個補丁,使用%patch0或%{patch0}引用

Prefix: %{_prefix} 這個主要是爲了解決今後安裝rpm包時,並不一定把軟件安裝到rpm中打包的目錄的情況。這樣,必須在這裏定義該標識,並在編寫%install腳本的時候引用,才能實現rpm安裝時重新指定位置的功能

Prefix: %{_sysconfdir} 這個原因和上面的一樣,但由於%{_prefix}指/usr,而對於其他的文件,例如/etc下的配置文件,則需要用%{_sysconfdir}標識

Build Arch: 指編譯的目標處理器架構,noarch標識不指定,但通常都是以/usr/lib/rpm/marcros中的內容爲默認值

Requires: 該rpm包所依賴的軟件包名稱,可以用>=或<=表示大於或小於某一特定版本,例如:
libpng-devel >= 1.0.20 zlib 
※“>=”號兩邊需用空格隔開,而不同軟件名稱也用空格分開
還有例如PreReq、Requires(pre)、Requires(post)、Requires(preun)、Requires(postun)、BuildRequires等都是針對不同階段的依賴指定 

Provides: 指明本軟件一些特定的功能,以便其他rpm識別

Packager: 打包者的信息

%description 軟件的詳細說明


假如需要在 rpmbuild 生成軟件包, 在安裝時候忽略依賴關係
請在 spec 文件中添加下面參數
AutoReqProv: no


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章