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


你可能感兴趣的:(RPM包快速构建教程)