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" >~/.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 < 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 >= 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} >/dev/null || groupadd -r %{nginx_group}
getent passwd %{nginx_user} >/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 <<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 > /dev/null 2>&1
/sbin/chkconfig --del nginx
#postrun 人拼命卸载后执行的脚本
%postun
if [ $1 -ge 1 ]; then
/sbin/service nginx upgrade &>/dev/null || :
#变更日志
* Tue Dec 11 2012 Sergey Budnevitch <[email protected]>
- 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" ]&& mkdir -p /opt/web/www
/sbin/chkconfig nginx on
/sbin/service nginx start
#ngpassword is a tool for nginx to create password
cat >/usr/sbin/ngpassword <<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<[email protected]>
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