定制RPM包并加入内部Yum Server
#####################################################
##如有转载,请务必保留本文链接及版权信息
##欢迎广大运维同仁一起交流linux/unix网站运维技术!
##QQ:335623998
##E-mail:335623998@qq.com
#####################################################
前言:此文以通过HAProxy1.4.22源码包为例定制生成rpm包,并加入内部YUM Server仓库,便于以后的软件包安装
1、制作RPM包linux系统环境
  1. # cat /etc/redhat-release  

  2. CentOS release 5.8 (Final)  

  3. # uname -r  

  4. 2.6.18-308.el5  

  5. # uname -m  

  6. x86_64

2、构建前的准备
创建rpmbuild所需的目录结构,通常安装好的Cent0S5.8系统已经创建好目录结构,如下:
  1. # ls /usr/src  

  2. debug kernels redhat  

  3. # ls /usr/src/redhat/  

  4. BUILD RPMS SOURCES SPECS SRPMS

root用户制作RPM包使用/usr/src/redhat/下的目录即可
如果使用普通用户,则手工创建好目录即可,命令如下
  1. # mkdir -p  /home/zxp/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

目录说明
BUILD:   用于编译软件包时,源码的临时存放空间。
RPMS:   制作好的二进制包的输出位置。
SOURCES:  源码的位置。
SPECS:   spec文件存放的位置。
SRPMS:   制作好的源码RPM包的输出位置,该包安装时仍需要先编译。
3、SPEC文件详解
构建一个标准的 RPM 包,就需要在目录SPECS下创建.spec文件,里面包含即将被安装的软件的所有详细信息。然后对这个文本在系统中执行rpmbuild命令,系统会按照.spec内容设置的步骤自动生成最终的 RPM 包。
3.1 SPEC文件 语法详解(以HAProxy为例)
  1. # cat haproxy.spec  

  2. Summary: HA-Proxy is a TCP/HTTP reverse proxy for high availability environments        #软件摘要信息  

  3. Name: haproxy       #软件名称  

  4. Version: 1.4.22     #软件版本  

  5. Release: 20130106_hexun_as5  #软件分支版本  

  6. License: GPL                  #软件版权  

  7. Group: System Environment/Daemons   #软件所属分类  

  8. URL: http://haproxy.1wt.eu/         #软件主页  

  9. Source0: http://haproxy.1wt.eu/download/1.4/src/%{name}-%{version}.tar.gz  #源码位置  

  10. BuildRoot: %{_tmppath}/%{name}-%{version}-root  #安装目录  

  11. BuildRequires: pcre-devel                 #编译依赖软件包  

  12. Requires: /sbin/chkconfig, /sbin/service  #安装依赖软件包  

  13. %description  #软件详细的描述信息  

  14. HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high  

  15. availability environments. Indeed, it can:  

  16. - route HTTP requests depending on statically assigned cookies  

  17. - spread the load among several servers while assuring server persistence  

  18.  through the use of HTTP cookies  

  19. - switch to backup servers in the event a main one fails  

  20. - accept connections to special ports dedicated to service monitoring  

  21. - stop accepting connections without breaking existing ones  

  22. - add/modify/delete HTTP headers both ways  

  23. - block requests matching a particular pattern  

  24. It needs very little resource. Its event-driven architecture allows it to easily  

  25. handle thousands of simultaneous connections on hundreds of instances without  

  26. risking the system's stability.  

  27. # %prep定义了构建前要做的准备,通常是%setup定义如何解包  

  28. %prep        

  29. %setup -q  

  30. # We don't want any perl dependecies in this RPM:  

  31. %define __perl_requires /bin/true #定义不使用perl依赖关系  

  32. %build #编译源码命令,通常是./configure && make,根据具体包安装方法而定  

  33. %{__make} ARCH=%{_target_cpu} TARGET=linux26  

  34. %install  #安装阶段  

  35. [ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}  

  36. %{__install} -d %{buildroot}%{_sbindir}  

  37. %{__install} -d %{buildroot}%{_sysconfdir}/rc.d/init.d  

  38. %{__install} -d %{buildroot}%{_sysconfdir}/%{name}  

  39. %{__install} -d %{buildroot}%{_mandir}/man1/  

  40. %{__install} -s %{name} %{buildroot}%{_sbindir}/  

  41. %{__install} -c -m 644 examples/%{name}.cfg %{buildroot}%{_sysconfdir}/%{name}/  

  42. %{__install} -c -m 755 examples/%{name}.init %{buildroot}%{_sysconfdir}/rc.d/init.d/%{name}  

  43. %{__install} -c -m 755 doc/%{name}.1 %{buildroot}%{_mandir}/man1/  

  44. %clean  #清理BUILD目录阶段  

  45. [ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}  

  46. %post   #安装后制定的脚本  

  47. /sbin/chkconfig --add %{name}

  48. %preun  #卸载前执行的脚本  

  49. if [ $1 = 0 ]; then

  50.  /sbin/service %{name} stop >/dev/null 2>&1 || :  

  51.  /sbin/chkconfig --del %{name}

  52. fi  

  53. %postun  #卸载后执行的脚本  

  54. if [ "$1" -ge "1" ]; then

  55.  /sbin/service %{name} condrestart >/dev/null 2>&1 || :  

  56. fi  

  57. %files  #文件列表,主要是设置安装rpm包后的文件、目录属性  

  58. %defattr(-,root,root)  #定义默认属性  

  59. %doc CHANGELOG TODO examples/*.cfg doc/haproxy-en.txt doc/haproxy-fr.txt doc/architecture.txt doc/configuration.txt #指定软件文档  

  60. %doc %{_mandir}/man1/%{name}.1*           #指定man帮助文档  

  61. %attr(0755,root,root) %{_sbindir}/%{name}  #单独指定目录属性  

  62. %dir %{_sysconfdir}/%{name}                #定义软件安装目录  

  63. %attr(0644,root,root) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.cfg   #单独指定文件属性  

  64. %attr(0755,root,root) %config %{_sysconfdir}/rc.d/init.d/%{name} #单独指定文件属性  

  65. %changelog    #软件升级日志  

  66. * Tue Aug 14 2012 Willy Tarreau <w@1wt.eu>  

  67. - updated to 1.4.22  

  68. …………略………………  

  69. * Thu Oct 16 2003 Simon Matter <simon.matter@invoca.ch>  

  70. - initial build  

3、获取spec文件方法(三种方法)
3.1 方法1:通过src.rpm包获取
下载&安装相应的src.rpm包
  1. # cd SRPMS/  

  2. # wget http://1wt.eu/tools/haproxy/src/devel/haproxy-1.2.3-1.src.rpm  

  3. # rpm -ivh haproxy-1.2.3-1.src.rpm  

  4.   1:haproxy                ########################################### [100%]  

 这里的“安装”是指把xxx.src.rpm中的tar.gz、patches、xxx.spec等文件分别输出到/usr/src/redhat/的SOURCES、SPECS等子目录中
查看生成的文件
  1. # ls /usr/src/redhat/{SOURCES,SPECS}  

  2. /usr/src/redhat/SOURCES:  

  3. haproxy-1.2.3.tar.gz  

  4. /usr/src/redhat/SPECS:  

  5. haproxy.spec  

找到spec文件,进行备份、修改
  1. # cd /usr/src/redhat/SPECS  

  2. # cd /usr/src/redhat/SPECS  

  3. # cp haproxy.spec haproxy.spec_bak$(date +%F)  

  4. # ls  

  5. haproxy.spec  haproxy.spec_bak2013-01-06  

3.2 方法2:新建spec文件
再目录SPECS下,创建一个新的spec文件,根据spec文件语法规范编写,这个难度较高,适用于自己开发的软件。
3.3 方法3:从源码包中获取已有spec文件 推荐的方法
  1. # tar zxf haproxy-1.4.22.tar.gz  

  2. # cd haproxy-1.4.22  

  3. # ll haproxy.spec    

  4. -rw-rw-r-- 1 root root 7442 Aug 14 15:09 haproxy.spec  

  5. #cd haproxy-1.4.22/examples/  

  6. # ls  

  7. haproxy.spec  

将spec放入目录SPECS
  1. # cp haproxy.spec  /usr/src/redhat/SPECS/

备份
  1. # cp haproxy.spec  haproxy.spec.$(date +%F)

本文采用此方法获取spec
4、编辑spec
  1. # pwd /usr/src/redhat/SPECS  

  2. # vim haproxy.spec  

修改前后内容对比(后面为原文件)
  1. [root@study02 SPECS]# diff haproxy.spec haproxy.spec.2013-01-07  

  2. 4c4  

  3. < Release: 20130106_hexun_as5  

  4. ---

  5. > Release: 1  

  6. 36c36  

  7. < %{__make} ARCH=%{_target_cpu} TARGET=linux26  

  8. ---

  9. > %{__make} USE_PCRE=1 DEBUG="" ARCH=%{_target_cpu} TARGET=linux26  

5、下载HAProxy源码包
下载源码包并放入SOURCES目录中
  1. # pwd  

  2. /usr/src/redhat/SOURCES  

  3. # wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz  

  4. # ls haproxy-1.4.22.tar.gz  

6、创建RPM 包
通过rpmbuild命令来解析SPEC文件生成对应的RPM包
6.1 确认rpmbuild 已经安装
  1. # yum install rpm-build -y

6.2 安装依赖包
在build rpm包的系统上安装pcre-devel
  1. # yum -y  install pcre-devel

6.3开始build rpm:
  1. # rpmbuild  -v -ba SPECS/haproxy.spec

6.4 看到如下内容即创建过程,红色内容为build包位置
  1. Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.44698  

  2. + umask 022  

  3. + cd /usr/src/redhat/BUILD  

  4. + LANG=C  

  5. + export LANG  

  6. + unset DISPLAY  

  7. + cd /usr/src/redhat/BUILD  

  8. + rm -rf haproxy-1.4.22  

  9. + /bin/gzip -dc /usr/src/redhat/SOURCES/haproxy-1.4.22.tar.gz  

  10. + tar -xf -  

  11. + STATUS=0  

  12. …………略………………  

  13. Wrote: /usr/src/redhat/SRPMS/haproxy-1.4.22-20130106_hexun_as5.src.rpm  

  14. Wrote: /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm  

  15. Wrote: /usr/src/redhat/RPMS/x86_64/haproxy-debuginfo-1.4.22-20130106_hexun_as5.x86_64.rpm  

  16. Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.75459  

  17. + umask 022  

  18. + cd /usr/src/redhat/BUILD  

  19. + cd haproxy-1.4.22  

  20. + '[' /var/tmp/haproxy-1.4.22-root '!=' / ']'

  21. + /bin/rm -rf /var/tmp/haproxy-1.4.22-root  

  22. + exit 0  #注意输出status为0 即创建过程错误

7、检验RPM包
7.1 使用 Mock 和 Koji 去测试 RPM 包
7.2 列出RPM软件包内的文件信息
  1. # rpm -qpl   /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm  

  2. /etc/haproxy  

  3. /etc/haproxy/haproxy.cfg  

  4. /etc/rc.d/init.d/haproxy  

  5. /usr/sbin/haproxy  

  6. /usr/share/doc/haproxy-1.4.22  

  7. /usr/share/doc/haproxy-1.4.22/CHANGELOG  

  8. /usr/share/doc/haproxy-1.4.22/TODO  

  9. /usr/share/doc/haproxy-1.4.22/acl-content-sw.cfg  

  10. /usr/share/doc/haproxy-1.4.22/architecture.txt  

  11. /usr/share/doc/haproxy-1.4.22/auth.cfg  

  12. /usr/share/doc/haproxy-1.4.22/build.cfg  

  13. /usr/share/doc/haproxy-1.4.22/configuration.txt  

  14. /usr/share/doc/haproxy-1.4.22/content-sw-sample.cfg  

  15. /usr/share/doc/haproxy-1.4.22/cttproxy-src.cfg  

  16. /usr/share/doc/haproxy-1.4.22/examples.cfg  

  17. /usr/share/doc/haproxy-1.4.22/haproxy-en.txt  

  18. /usr/share/doc/haproxy-1.4.22/haproxy-fr.txt  

  19. /usr/share/doc/haproxy-1.4.22/haproxy.cfg  

  20. /usr/share/doc/haproxy-1.4.22/option-http_proxy.cfg  

  21. /usr/share/doc/haproxy-1.4.22/tarpit.cfg  

  22. /usr/share/doc/haproxy-1.4.22/test-section-kw.cfg  

  23. /usr/share/doc/haproxy-1.4.22/url-switching.cfg  

  24. /usr/share/man/man1/haproxy.1.gz

7.3 列出RPM软件包的描述信息
  1. # rpm -qpi   /usr/src/redhat/RPMS/x86_64/haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm  

  2. Name        : haproxy                      Relocations: (not relocatable)  

  3. Version     : 1.4.22                            Vendor: (none)  

  4. Release     : 20130106_hexun_as5            Build Date: Mon 07 Jan 2013 09:36:21 AM CST  

  5. Install Date: (not installed)               Build Host: study02  

  6. Group       : System Environment/Daemons    Source RPM: haproxy-1.4.22-20130106_hexun_as5.src.rpm  

  7. Size        : 1275757                          License: GPL  

  8. Signature   : (none)  

  9. URL         : http://haproxy.1wt.eu/  

  10. Summary     : HA-Proxy is a TCP/HTTP reverse proxy for high availability environments  

  11. Description :  

  12. HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high  

  13. availability environments. Indeed, it can:  

  14. - route HTTP requests depending on statically assigned cookies  

  15. - spread the load among several servers while assuring server persistence  

  16. through the use of HTTP cookies  

  17. - switch to backup servers in the event a main one fails  

  18. - accept connections to special ports dedicated to service monitoring  

  19. - stop accepting connections without breaking existing ones  

  20. - add/modify/delete HTTP headers both ways  

  21. - block requests matching a particular pattern  

  22. It needs very little resource. Its event-driven architecture allows it to easily  

  23. handle thousands of simultaneous connections on hundreds of instances without  

  24. risking the system's stability.  

7.4 rpm方式安装验证
  1. [root@study01 tools]# rpm -ivh haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm  

  2. Preparing...                ########################################### [100%]  

  3.   1:haproxy                ########################################### [100%]  

  4. [root@study01 tools]# ls  

  5. haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm  

  6. [root@study01 tools]# ls /etc/haproxy/ #安装目录及主配置文件  

  7. haproxy.cfg  

  8. [root@study01 tools]# ll /etc/rc.d/init.d/haproxy  #服务启动文件  

  9. -rwxr-xr-x 1 root root 2553 Jan 7 09:36 /etc/rc.d/init.d/haproxy  

  10. [root@study01 tools]# ll /etc/init.d/haproxy  

  11. -rwxr-xr-x 1 root root 2553 Jan 7 09:36 /etc/init.d/haproxy  

  12. [root@study01 tools]# rpm -qf /etc/init.d/haproxy  #查看软件所属软件包  

  13. haproxy-1.4.22-20130106_hexun_as5  

至此定制RPM包已经制作完毕,接下来我们就可以把常用的应用软件源码包都制作为rpm包放入yum仓库,以后部署软件就很便捷了。

  续文,请移步至《将定制RPM包加入内部Yum Sever》