#######################################################如有转载,请务必保留本文链接及版权信息##欢迎广大运维同仁一起交流linux/unix网站运维技术!##QQ:335623998##E-mail:335623998@qq.com
##博客: http://dreamway.blog.51cto.com/
##weibo:http://weibo.com/zhaixiangpan
#####################################################如何定制RPM包http://dreamway.blog.51cto.com/1281816/1110822前言:此文以生产案例应用通过HAProxy1.4.22源码包为例定制生成rpm包,并加入内部YUM Server仓库,便于以后的软件包安装1、制作RPM包linux系统环境# cat /etc/redhat-release
CentOS release 5.8 (Final)
# uname -r
2.6.18-308.el5
# uname -m
x86_64
# ls /usr/src
debug kernels redhat
# ls /usr/src/redhat/
BUILD RPMS SOURCES SPECS SRPMS
# mkdir -p /home/zxp/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
RPMS: 制作好的二进制包的输出位置。
SOURCES: 源码的位置。
SPECS: spec文件存放的位置。
SRPMS: 制作好的源码RPM包的输出位置,该包安装时仍需要先编译。3、SPEC文件详解构建一个标准的 RPM 包,就需要在目录SPECS下创建.spec文件,里面包含即将被安装的软件的所有详细信息。然后对这个文本在系统中执行rpmbuild命令,系统会按照.spec内容设置的步骤自动生成最终的 RPM 包。 3.1 SPEC文件 语法详解(以HAProxy为例)
# cat haproxy.spec
Summary: HA-Proxy is a TCP/HTTP reverse proxy for high availability environments #软件摘要信息
Name: haproxy #软件名称
Version: 1.4.22 #软件版本
Release: 20130106_hexun_as5 #软件分支版本
License: GPL #软件版权
Group: System Environment/Daemons #软件所属分类
URL: http://haproxy.1wt.eu/ #软件主页
Source0: http://haproxy.1wt.eu/download/1.4/src/%{name}-%{version}.tar.gz #源码位置
BuildRoot: %{_tmppath}/%{name}-%{version}-root #安装目录
BuildRequires: pcre-devel #编译依赖软件包
Requires: /sbin/chkconfig, /sbin/service #安装依赖软件包
%description #软件详细的描述信息
HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high
availability environments. Indeed, it can:
- route HTTP requests depending on statically assigned cookies
- spread the load among several servers while assuring server persistence
through the use of HTTP cookies
- switch to backup servers in the event a main one fails
- accept connections to special ports dedicated to service monitoring
- stop accepting connections without breaking existing ones
- add/modify/delete HTTP headers both ways
- block requests matching a particular pattern
It needs very little resource. Its event-driven architecture allows it to easily
handle thousands of simultaneous connections on hundreds of instances without
risking the system's stability.
# %prep定义了构建前要做的准备,通常是%setup定义如何解包
%prep
%setup -q
# We don't want any perl dependecies in this RPM:
%define __perl_requires /bin/true #定义不使用perl依赖关系
%build #编译源码命令,通常是./configure && make,根据具体包安装方法而定
%{__make} ARCH=%{_target_cpu} TARGET=linux26
%install #安装阶段
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}
%{__install} -d %{buildroot}%{_sbindir}
%{__install} -d %{buildroot}%{_sysconfdir}/rc.d/init.d
%{__install} -d %{buildroot}%{_sysconfdir}/%{name}
%{__install} -d %{buildroot}%{_mandir}/man1/
%{__install} -s %{name} %{buildroot}%{_sbindir}/
%{__install} -c -m 644 examples/%{name}.cfg %{buildroot}%{_sysconfdir}/%{name}/
%{__install} -c -m 755 examples/%{name}.init %{buildroot}%{_sysconfdir}/rc.d/init.d/%{name}
%{__install} -c -m 755 doc/%{name}.1 %{buildroot}%{_mandir}/man1/
%clean #清理BUILD目录阶段
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}
%post #安装后制定的脚本
/sbin/chkconfig --add %{name}
%preun #卸载前执行的脚本
if [ $1 = 0 ]; then
/sbin/service %{name} stop >/dev/null 2>&1 || :
/sbin/chkconfig --del %{name}
fi
%postun #卸载后执行的脚本
if [ "$1" -ge "1" ]; then
/sbin/service %{name} condrestart >/dev/null 2>&1 || :
fi
%files #文件列表,主要是设置安装rpm包后的文件、目录属性
%defattr(-,root,root) #定义默认属性
%doc CHANGELOG TODO examples/*.cfg doc/haproxy-en.txt doc/haproxy-fr.txt doc/architecture.txt doc/configuration.txt #指定软件文档
%doc %{_mandir}/man1/%{name}.1* #指定man帮助文档
%attr(0755,root,root) %{_sbindir}/%{name} #单独指定目录属性
%dir %{_sysconfdir}/%{name} #定义软件安装目录
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.cfg #单独指定文件属性
%attr(0755,root,root) %config %{_sysconfdir}/rc.d/init.d/%{name} #单独指定文件属性
%changelog #软件升级日志
* Tue Aug 14 2012 Willy Tarreau <w@1wt.eu>
- updated to 1.4.22
…………略………………
* Thu Oct 16 2003 Simon Matter <simon.matter@invoca.ch>
- initial build
# cd SRPMS/
# wget http://1wt.eu/tools/haproxy/src/devel/haproxy-1.2.3-1.src.rpm
# rpm -ivh haproxy-1.2.3-1.src.rpm
1:haproxy ########################################### [100%]
# ls /usr/src/redhat/{SOURCES,SPECS}
/usr/src/redhat/SOURCES:
haproxy-1.2.3.tar.gz
/usr/src/redhat/SPECS:
haproxy.spec
# cd /usr/src/redhat/SPECS
# cd /usr/src/redhat/SPECS
# cp haproxy.spec haproxy.spec_bak$(date +%F)
# ls
haproxy.spec haproxy.spec_bak2013-01-06
# tar zxf haproxy-1.4.22.tar.gz
# cd haproxy-1.4.22
# ll haproxy.spec
-rw-rw-r-- 1 root root 7442 Aug 14 15:09 haproxy.spec
#cd haproxy-1.4.22/examples/
# ls
haproxy.spec
# cp haproxy.spec /usr/src/redhat/SPECS/
# cp haproxy.spec haproxy.spec.$(date +%F)
# pwd /usr/src/redhat/SPECS
# vim haproxy.spec
[root@study02 SPECS]# diff haproxy.spec haproxy.spec.2013-01-07
4c4
< Release: 20130106_hexun_as5
---
> Release: 1
36c36
< %{__make} ARCH=%{_target_cpu} TARGET=linux26
---
> %{__make} USE_PCRE=1 DEBUG="" ARCH=%{_target_cpu} TARGET=linux26
# pwd
/usr/src/redhat/SOURCES
# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
# ls haproxy-1.4.22.tar.gz
# yum install rpm-build -y
# yum -y install pcre-devel
# rpmbuild -v -ba SPECS/haproxy.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.44698
+ umask 022
+ cd /usr/src/redhat/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /usr/src/redhat/BUILD
+ rm -rf haproxy-1.4.22
+ /bin/gzip -dc /usr/src/redhat/SOURCES/haproxy-1.4.22.tar.gz
+ tar -xf -
+ STATUS=0
…………略………………
Wrote: /usr/src/redhat/SRPMS/haproxy-1.4.22-20130106_hexun_as5.src.rpm
Wrote: /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
Wrote: /usr/src/redhat/RPMS/x86_64/haproxy-debuginfo-1.4.22-20130106_hexun_as5.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.75459
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd haproxy-1.4.22
+ '[' /var/tmp/haproxy-1.4.22-root '!=' / ']'
+ /bin/rm -rf /var/tmp/haproxy-1.4.22-root
+ exit 0 #注意输出status为0 即创建过程错误
# rpm -qpl /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.4.22
/usr/share/doc/haproxy-1.4.22/CHANGELOG
/usr/share/doc/haproxy-1.4.22/TODO
/usr/share/doc/haproxy-1.4.22/acl-content-sw.cfg
/usr/share/doc/haproxy-1.4.22/architecture.txt
/usr/share/doc/haproxy-1.4.22/auth.cfg
/usr/share/doc/haproxy-1.4.22/build.cfg
/usr/share/doc/haproxy-1.4.22/configuration.txt
/usr/share/doc/haproxy-1.4.22/content-sw-sample.cfg
/usr/share/doc/haproxy-1.4.22/cttproxy-src.cfg
/usr/share/doc/haproxy-1.4.22/examples.cfg
/usr/share/doc/haproxy-1.4.22/haproxy-en.txt
/usr/share/doc/haproxy-1.4.22/haproxy-fr.txt
/usr/share/doc/haproxy-1.4.22/haproxy.cfg
/usr/share/doc/haproxy-1.4.22/option-http_proxy.cfg
/usr/share/doc/haproxy-1.4.22/tarpit.cfg
/usr/share/doc/haproxy-1.4.22/test-section-kw.cfg
/usr/share/doc/haproxy-1.4.22/url-switching.cfg
/usr/share/man/man1/haproxy.1.gz
# rpm -qpi /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
Name : haproxy Relocations: (not relocatable)
Version : 1.4.22 Vendor: (none)
Release : 20130106_hexun_as5 Build Date: Mon 07 Jan 2013 09:36:21 AM CST
Install Date: (not installed) Build Host: study02
Group : System Environment/Daemons Source RPM: haproxy-1.4.22-20130106_hexun_as5.src.rpm
Size : 1275757 License: GPL
Signature : (none)
URL : http://haproxy.1wt.eu/
Summary : HA-Proxy is a TCP/HTTP reverse proxy for high availability environments
Description :
HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high
availability environments. Indeed, it can:
- route HTTP requests depending on statically assigned cookies
- spread the load among several servers while assuring server persistence
through the use of HTTP cookies
- switch to backup servers in the event a main one fails
- accept connections to special ports dedicated to service monitoring
- stop accepting connections without breaking existing ones
- add/modify/delete HTTP headers both ways
- block requests matching a particular pattern
It needs very little resource. Its event-driven architecture allows it to easily
handle thousands of simultaneous connections on hundreds of instances without
risking the system's stability.
[root@study01 tools]# rpm -ivh haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
Preparing... ########################################### [100%]
1:haproxy ########################################### [100%]
[root@study01 tools]# ls
haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
[root@study01 tools]# ls /etc/haproxy/ #安装目录及主配置文件
haproxy.cfg
[root@study01 tools]# ll /etc/rc.d/init.d/haproxy #服务启动文件
-rwxr-xr-x 1 root root 2553 Jan 7 09:36 /etc/rc.d/init.d/haproxy
[root@study01 tools]# ll /etc/init.d/haproxy
-rwxr-xr-x 1 root root 2553 Jan 7 09:36 /etc/init.d/haproxy
[root@study01 tools]# rpm -qf /etc/init.d/haproxy #查看软件所属软件包
haproxy-1.4.22-20130106_hexun_as5
至此定制RPM包已经制作完毕,接下来我们就可以把常用的应用软件源码包都制作为rpm包放入yum仓库,以后部署软件就很便捷了。
8、将定制的RPM加入yum仓库所需工具createrepo 、yum-arch详见很早以前写的yum server搭建文章《CentOS基于 http服务 搭建yum 源 服务器》http://bbs.linuxtone.org/thread-4439-1-1.html8.1 在yum server上配置[root@hxinstall 5ASU8]# cd /opt/ftp/pub/os/Linux/Centos/x86_64/Centos5.8/
[root@hxinstall Centos5.8]# ls
dvd install
# cd RPMS.hexun/
# ls
haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
[root@hxinstall RPMS.hexun]# createrepo /opt/ftp/pub/os/Linux/Centos/x86_64/Centos5.8/RPMS.hexun
1/1 - haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
Saving Primary metadata
Saving file lists metadata
Saving other metadata
# yum-arch -l /opt/ftp/pub/os/Linux/Centos/x86_64/Centos5.8/RPMS.hexun
# ls
haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm headers repodata
cat << EOF >> /etc/yum.repos.d/CentOS-Base.repo
[hexun]
name= hexun rpm
baseurl=http://10.0.251.154/pub/os/Linux/Centos/x86_64/Centos5.8/RPMS.hexun
gpgcheck=0
EOF
8.4.2 YUM安装自定制HAProxy 软件包
# yum clean all
# yum install haproxy -y
Loaded plugins: fastestmirror
Determining fastest mirrors
……略…………
hexun | 951 B 00:00
hexun/primary | 1.2 kB 00:00
hexun 1/1
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.4.22-20130106_hexun_as5 set to be updated
………略………
Running Transaction
Installing : haproxy 1/1
Installed: #yum已经安装成功
haproxy.x86_64 0:1.4.22-20130106_hexun_as5
8.4.3 卸载定制版软件包
# yum remove haproxy -y
Loaded plugins: fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.4.22-20130106_hexun_as5 set to be erased
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================
Package Arch Version Repository Size
===================================================================================
Removing:
haproxy x86_64 1.4.22-20130106_hexun_as5 installed 1.2 M
Transaction Summary
===================================================================================
Remove 1 Package(s)
Reinstall 0 Package(s)
Downgrade 0 Package(s)
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : haproxy 1/1
Removed: #已经成功卸载
haproxy.x86_64 0:1.4.22-20130106_hexun_as5
Complete!
# rpmbuild -v -ba SPECS/haproxy.spec
error: File /usr/src/redhat/SOURCES/haproxy-1.4.22.tar.gz: No such file or directory
# cd /usr/src/redhat/SOURCES/
[root@study02 SOURCES]# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
# rpmbuild -v -ba SPECS/haproxy.spec
error: Failed build dependencies:
pcre-devel is needed by haproxy-1.4.22-20130106_hexun_as5.x86_64
# yum -y install pcre-devel
Usage: rpmbuild [ <specfile> | <tarball> | <source package> ]
Usage: rpm [OPTION...]
常用的rpm参数组合:
(1)rpm -qx file_name, x={f,i,l,a,p...}, file_name可以是命令名、动态库名称、配置文件名等等。
使用此命令的前提:相应的rpm包已经安装。
rpm -qf file:查询文件file是哪个rpm包中的;rpm -qf `which your_cmd`, rpm -qf `locate file_name`
rpm -qi rpm_name:查看指定rpm包安装的详细信息;
rpm -ql installed_rpm_name:列出已经安装的rpm包中包含了哪些文件及他们的安装路径。如rpm -ql iptraf
用以下选项与 -q 连用来指明要查询哪些软件包的信息。这些选项被称之为“软件包指定选项”:
-a 查询所有已安装的软件包。
-f <file> 将查询包含有文件 <file>的软件包。
-p <packagefile> 查询软件包文件名为 <packagefile>的包。
有几种方式来指定查询软件包时所显示的信息。 以下选项可通过读取rpm包头部的办法显示rpm包的信息,这样的选项被称作“信息选择选项”:
-i 显示软件包信息,如描述、发行号、大小、编译日期、安装日期、硬件平台、以及其它一些各类信息。
-l 列出软件包中包含的文件。(列出已经安装的rpm包中包含了哪些文件及他们的安装路径)
-s 显示软件包中所有文件的状态。
-d 列出被标注为文档的文件 (如,man 手册、 info 信息、README,等等) 。
-c 列出被标注为配置文件的文件。这些文件是需要在安装完毕后加以定制的,如 (sendmail.cf, passwd, inittab, 等) 。
如果要在执行上述选项的同时,显示文件列表, 可以同时使用 -v 命令行选项,就能得出与 ls -l 格式类似的输出。
(2)查看未安装的rpm/src.rpm包中包含的文件列表
- 在本地暂时只能使用(4)中提供的方法;
- 通过rpmfind.net等网站进行查询;
(3)rpm -ivh xxx.rpm:重新安装;(和-Uvh相比,建议用-ivh)
rpm -ivh --relocate /=/tmp/test/ xxx.rpm
(4)rpm2cpio xxx.rpm/xxx.src.rpm:将rpm解压为cpio归档;
rpm2cpio xxx.rpm/xxx.src.rpm | cpio -idmv (rpm2cpio xxx.rpm | cpio --extract --make-directories)
参数-i(或--extract)表示提取文件; v表示指示执行进程;-d(或--make-directory)表示根据包中文件原来的路径建立目录;m表示保持文件的更新时间。
https://github.com/jordansissel/fpm/wiki