#####################################################
##如有轉載,請務必保留本文連結及版權資訊
##歡迎廣大運維同仁一起交流linux/unix網站運維技術!
##QQ:335623998
##E-mail:[email protected]
如何定制RPM包
<a href="http://dreamway.blog.51cto.com/1281816/1110822">http://dreamway.blog.51cto.com/1281816/1110822</a>
前言:此文以生産案例應用通過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
2、建構前的準備
建立rpmbuild所需的目錄結構,通常安裝好的Cent0S5.8系統已經建立好目錄結構,如下:
# ls /usr/src
debug kernels redhat
# ls /usr/src/redhat/
BUILD RPMS SOURCES SPECS SRPMS
root使用者制作RPM包使用/usr/src/redhat/下的目錄即可
如果使用普通使用者,則手工建立好目錄即可,指令如下
# 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為例)
# 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目錄階段
%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 || :
%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 <[email protected]>
- updated to 1.4.22
…………略………………
* Thu Oct 16 2003 Simon Matter <[email protected]>
- initial build
3、擷取spec檔案方法(三種方法)
3.1 方法1:通過src.rpm包擷取
下載下傳&安裝相應的src.rpm包
# 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%]
這裡的“安裝”是指把xxx.src.rpm中的tar.gz、patches、xxx.spec等檔案分别輸出到/usr/src/redhat/的SOURCES、SPECS等子目錄中
檢視生成的檔案
# ls /usr/src/redhat/{SOURCES,SPECS}
/usr/src/redhat/SOURCES:
haproxy-1.2.3.tar.gz
/usr/src/redhat/SPECS:
haproxy.spec
找到spec檔案,進行備份、修改
# cd /usr/src/redhat/SPECS
# cp haproxy.spec haproxy.spec_bak$(date +%F)
# ls
haproxy.spec haproxy.spec_bak2013-01-06
3.2 方法2:建立spec檔案
再目錄SPECS下,建立一個新的spec檔案,根據spec檔案文法規範編寫,這個難度較高,适用于自己開發的軟體。
3.3 方法3:從源碼包中擷取已有spec檔案 (推薦的方法)
# 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/
将spec放入目錄SPECS
# cp haproxy.spec /usr/src/redhat/SPECS/
備份
# cp haproxy.spec haproxy.spec.$(date +%F)
本文采用此方法擷取spec
4、編輯spec
# 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
5、下載下傳HAProxy源碼包
下載下傳源碼包并放入SOURCES目錄中
# 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
6、建立RPM 包
通過rpmbuild指令來解析SPEC檔案生成對應的RPM包
6.1 确認rpmbuild 已經安裝
# yum install rpm-build -y
6.2 安裝依賴包
在build rpm包的系統上安裝pcre-devel
# yum -y install pcre-devel
6.3開始build rpm:
# rpmbuild -v -ba SPECS/haproxy.spec
6.4 看到如下内容即建立過程,紅色内容為build包位置
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.44698
+ umask 022
+ cd /usr/src/redhat/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ 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
+ 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 即建立過程錯誤
7、檢驗RPM包
7.1 使用 Mock 和 Koji 去測試 RPM 包
7.2 列出RPM軟體包内的檔案資訊
# 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
7.3 列出RPM軟體包的描述資訊
# 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 :
through the use of HTTP cookies
7.4 rpm方式安裝驗證
[root@study01 tools]# rpm -ivh haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm
Preparing... ########################################### [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
8、将定制的RPM加入yum倉庫
所需工具createrepo 、yum-arch
詳見很早以前寫的yum server搭建文章
8.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/
8.2 建立repodata 資料庫
包含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
8.3 RPM包分析
在目錄下産生 heaers 目錄
# yum-arch -l /opt/ftp/pub/os/Linux/Centos/x86_64/Centos5.8/RPMS.hexun
haproxy-1.4.22-20130106_hexun_as5.x86_64.rpm headers repodata
注意:
每添加新的軟體包,都需要執行此兩個指令使其生效。
8.4 yum 用戶端安裝定制版HAProxy
8.4.1 配置yum
添加定制軟體包yum倉庫
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
Setting up Remove Process
---> 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
Erasing : haproxy 1/1
Removed: #已經成功解除安裝
haproxy.x86_64 0:1.4.22-20130106_hexun_as5
Complete!
9、制作RPM報錯記錄
9.1 build包報錯找不到源碼包
# 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
9.2 build時報依賴關系錯誤
# rpmbuild -v -ba SPECS/haproxy.spec
error: Failed build dependencies:
pcre-devel is needed by haproxy-1.4.22-20130106_hexun_as5.x86_64
解決:在build rpm包的系統上安裝pcre-devel
# yum -y install pcre-devel
10、制作RPM包涉及到的指令用法
10.1 指令rpmbuild
用法:
Usage: rpmbuild [ <specfile> | <tarball> | <source package> ]
常用參數:
-bb 隻編譯二進制rpm包
-bs 隻編譯源碼rpm包(src.rpm)
-ba 同時編譯二進制和源碼rpm包(src.rpm)
-bp執行到%prep段,解開tar包然後把所有的更新檔檔案合并而生成一個完整的具最新功能的源檔案
10.2 指令rpm
rpm 指令以前的linux版本較常用,但是解決軟體包依賴關系就比較麻煩,不如yum便捷。
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表示保持檔案的更新時間。
10.3 指令 yum
目前yum指令在安裝軟體包時很常用,這裡不再一一列舉用法參數。
11、參考資料
RPM 打包技術與典型 SPEC 檔案分析
<a href="http://www.ibm.com/developerworks/cn/linux/l-rpm/?S_TACT=105AGX52&S_CMP=tech-51CTO" target="_blank">http://www.ibm.com/developerworks/cn/linux/l-rpm/?S_TACT=105AGX52&S_CMP=tech-51CTO</a>
常用的rpm參數組合
<a href="http://www.xdays.info/rpm%E5%8C%85%E5%88%B6%E4%BD%9C.html#file" target="_blank">http://www.xdays.info/rpm%E5%8C%85%E5%88%B6%E4%BD%9C.html#file</a>
RPM使用筆記
<a href="http://rdc.taobao.com/blog/cs/?p=43#sec-7" target="_blank">http://rdc.taobao.com/blog/cs/?p=43#sec-7</a>
How to create an RPM package/zh-cn
<a href="https://fedoraproject.org/wiki/How_to_create_an_RPM_package/zh-cn#.E6.96.B0.E5.BB.BA.E4.B8.80.E4.B8.AA.E7.A9.BA.E7.99.BD.E7.9A.84_.spec_.E6.96.87.E4.BB.B6" target="_blank">https://fedoraproject.org/wiki/How_to_create_an_RPM_package/zh-cn#.E6.96.B0.E5.BB.BA.E4.B8.80.E4.B8.AA.E7.A9.BA.E7.99.BD.E7.9A.84_.spec_.E6.96.87.E4.BB.B6</a>
Fedora 新軟體維護者指南
<a href="https://fedoraproject.org/wiki/Fedora_%E6%96%B0%E8%BD%AF%E4%BB%B6%E7%BB%B4%E6%8A%A4%E8%80%85%E6%8C%87%E5%8D%97?rd=Fedora%E9%8F%82%E6%9D%BF%EE%9A%9C%E6%B5%A0%E5%89%81%E6%B7%AE%E9%8E%B6%E3%82%88%E2%82%AC%E5%91%AE%E5%AF%9A%E9%8D%97?#.E4.BD.BF.E7.94.A8_Mock_.E5.92.8C_Koji_.E5.8E.BB.E6.B5.8B.E8.AF.95_RPM_.E5.8C.85" target="_blank">https://fedoraproject.org/wiki/Fedora_%E6%96%B0%E8%BD%AF%E4%BB%B6%E7%BB%B4%E6%8A%A4%E8%80%85%E6%8C%87%E5%8D%97?rd=Fedora%E9%8F%82%E6%9D%BF%EE%9A%9C%E6%B5%A0%E5%89%81%E6%B7%AE%E9%8E%B6%E3%82%88%E2%82%AC%E5%91%AE%E5%AF%9A%E9%8D%97?#.E4.BD.BF.E7.94.A8_Mock_.E5.92.8C_Koji_.E5.8E.BB.E6.B5.8B.E8.AF.95_RPM_.E5.8C.85</a>
本文轉自pandazhai 51CTO部落格,原文連結:http://blog.51cto.com/dreamway/1110874