一、rpmbuild命令的安裝
yum install rpm-build
二、用法
rpmbuild -bb XXXX.spec或者rpmbuild -ba XXX.tar.gz
三、目錄概述
rpmbuild在運行后會在用戶目錄下生成一個rpmbuild的文件夾:
[root@localhost rpmbuild]# ls ~/rpmbuild/ BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
有四個目錄:
BUILD:你要打包的文件將會在這里編譯;
BUILDROOT:在虛擬安裝(make install)的目錄;
RPMS:存放生成的二進制的rpm包,也就是我們普通用的那種RPM包;
SOURCES:你要編譯的源碼包會被copy到這里;
SPECS:你執行的spec文件會被copy到這里;
SRPMS:存放的是rpm源碼包,源碼包里只有一個文件,就是你的XXXX.tar.gz
四、SPEC文件的編寫
vim一個空文件,可以看到rpmbuild已經幫我們生成了一個格式:

[root@localhost packages]# vi aaa.spec Name: Version: Release: 1%{?dist} Summary: Group: License: URL: Source0: BuildRequires: Requires: %description %prep %setup -q %build %configure make %{?_smp_mflags} %install make install DESTDIR=%{buildroot} %files %doc %changelog
相對應的關鍵字介紹如下:
Name:軟件包的名稱 %{name} Version:軟件的實際版本號 Release:發布序列號 %{release} 比如:moosefs-3.0.81-1.tar.gz Name:moosefs; 版本號:3.0.81 Release:1 Summary:軟件包的內容概要 Group:軟件分組,建議使用標准分組 License:軟件授權方式 URL:軟件的主頁 Source0:源代碼包地址,多個源可以用"%{source1}"、"%{source1}"等引用 Requires:該rpm包所依賴的軟件包名稱,可以用>=或<=表示大於或小於某一特定版本 PreReq、Requires(pre)、Requires(post)、Requires(preun)、Requires(postun)、BuildRequires等都是針對不同打包階段的依賴指定的 %description:軟件的詳細說明 %prep:表示要開始編譯軟件了,通常包括%setup和%patch兩個命令 %setup -q: 解壓源文件程序 %patch:把補丁放到源碼中 %build表示開始構建 %configure 相當於"./configure",也可以指定參數 make %{?_smp_mflags} 就是"make" %install:表示開始安裝(其實是安裝到虛擬目錄,一般我們指定是BUILDROOT) make install DESTDIR=%{buildroot}就是"make install" %files:用來指定要把那些文件打包到rpm包中 %defattr(644,root,root,755)用來設定文件的默認權限 %doc:這個非常重要,只有寫在這下面的文件,才會被打包到rpm中去 如果目錄不對都會提醒你File not found by glob: /root/rpmbuild/BUILDROOT/XXXXX 其實在打包的時候%doc可以先不用寫,執行過后到 /root/rpmbuild/BUILDROOT/目錄下看都安裝了什么,再寫;
%changelog:變更日志,有些時候回提醒你前面加"*"
下面是我給fuse-2.9.7寫的spec文件:

%define _relname .devel Summary: File System in Userspace (FUSE) utilities Name: fuse Version: 2.9.7 Release: 1%{?_reasename} License: commercial Group: System Environment/Base Source0: %{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) Requires: kernel >= 2.6.14 %description libfuse %prep %setup -q %clean rm -rf $RPM_BUILD_ROOT %build %configure make %install make install DESTDIR=$RPM_BUILD_ROOT %files %defattr(644,root,root,755) %doc %{_includedir}/* %{_includedir}/fuse/* %{_libdir}/* %{_libdir}/pkgconfig/* %{_bindir}/* %{_mandir}/man1/* %{_mandir}/man8/* /sbin/* %{_sysconfdir}/init.d/* %{_sysconfdir}/udev/* %{_sysconfdir}/udev/rules.d/* %changelog *Mon Feb 13 2006 Peter Lemenkov <lemenkov newmail ru> - 2.9.1-1 *Proper udev rule *Mon Feb 13 2006 Peter Lemenkov <lemenkov newmail ru> - 2.9.1-1 *Added missing requires
五、打包之后的疑問和解決方法
1.那么打包之后的文件是不是我們想要的呢?
答:可以用"rpm --prefix"去安裝,結果:error: package fuse is not relocatable,用rpm命令查看包信息,原來被禁掉了。
[root@localhost x86_64]# rpm -ivh fuse-2.9.7-1.x86_64.rpm --prefix=/usr/local/fuse3 error: package fuse is not relocatable [root@localhost x86_64]# rpm -qpi fuse-2.9.7-1.x86_64.rpm Name : fuse Version : 2.9.7 Release : 1 Architecture: x86_64 Install Date: (not installed) Group : System Environment/Base Size : 833112 License : commercial Signature : (none) Source RPM : fuse-2.9.7-1.src.rpm Build Date : Wed Aug 10 20:06:19 2016 Build Host : localhost Relocations : (not relocatable) Summary : File System in Userspace (FUSE) utilities Description : libfuse
2.怎么樣不安裝rpm就知道里面有什么文件呢?
#先看我們之前說的SRPM中打的源碼包是什么樣子的:
[root@localhost SRPMS]# pwd /root/rpmbuild/SRPMS [root@localhost SRPMS]# rpm -qlp fuse-2.9.7-1.src.rpm fuse-2.9.7.tar.gz libfuse.spec #再看我們最終想要的二進制的rpm包是神樣子的: [root@localhost x86_64]# pwd /root/rpmbuild/RPMS/x86_64 [root@localhost x86_64]# rpm -qlp fuse-2.9.7-1.x86_64.rpm /etc/init.d/fuse /etc/udev/rules.d /etc/udev/rules.d/99-fuse.rules /sbin/mount.fuse /usr/bin/fusermount /usr/bin/ulockmgr_server /usr/include/fuse
3.怎么樣不安裝rpm包就可以提取rpm包里的文件:
http://blog.chinaunix.net/uid-33787-id-3331183.html
4.怎樣確定系統多出來的命令就是我們剛裝上的呢?
[root@localhost packages]# rpm -qi fuse-libs Name : fuse-libs Version : 2.9.2 Release : 6.el7 Architecture: x86_64 Install Date: Wed Aug 10 10:14:39 2016 Group : System Environment/Libraries Size : 293074 License : LGPLv2+ Signature : RSA/SHA256, Wed Nov 25 22:30:48 2015, Key ID 24c6a8a7f4a80eb5 Source RPM : fuse-2.9.2-6.el7.src.rpm Build Date : Fri Nov 20 13:49:56 2015