如何在CentOS/RedHat下,实现根据源码包创建rpm包

系统 Linux
有时你可能需要获取一个开源应用的源码包,但是可能没有无法通过该应用的RPM文件来安装到系统。在这种情况下,你既可以编译源码安装,也可以以源码包来创建一个rpm包文件,实现安装应用的功能。

如何在CentOS/RedHat下实现根据源码包创建rpm包

有时你可能需要获取一个开源应用的源码包,但是可能没有无法通过该应用的RPM文件来安装到系统。

在这种情况下,你既可以编译源码安装,也可以以源码包来创建一个rpm包文件,实现安装应用的功能。

同样存在一种情况即你想要创建一个你自己开发的自定义的RPM包。

这篇指南解释了如何提高源码来创建一个rpm包。

为了创建rpm包,你将需要源码包(通常为一个压缩文件,其内也包含了SPEC文件)。

SPEC文件包含以下说明:如何创建rpm包,包内包含的文件,安装的路径。

RPM将展现创建过程中的测试过程。

执行spec文件中的prep块中定义的系统命令和宏命令。

检测文件列表的内容

执行spec文件中的build块中定义的系统命令和宏命令。在这一步中文件列表中的宏命令也被执行。

创建二进制包文件

创建源码包文件

一旦RPM执行了上述步骤,将创建二进制的包文件和源码包文件。

二进制包文件包含所有安装或卸载包的额外信息的源文件。

通常安装包的所有选项都是有平台差异的。二进制包文件包含为一个特殊结构而编译的完整应用或函数库。源码包通常包含原始压缩的tar文件,spec文件和创建二进制包时相关的补丁。

让我们使用tar文件来看看如何创建一个简单的源和二进制RPM包。

如果你对rpm包比较陌生,你应该首先去理解在CentOS/Redhat上如何使用rpm命令安装,升级和卸载包。

1. 安装rpm-build包

为了创建基于spec文件的rpm文件,我们需要使用rpmbuild命令。

rpmbuild命令是rpm-build包的一部分。安装过程如下。

  1. # yum install rpm-build 

rpm-build依赖下面的几个包。如果你还未安装,就赶紧自动安装这些包。

  1. elfutils-libelf 
  2. rpm 
  3. rpm-libs 
  4. rpm-python 

 

2. RPM创建目录

rpm-build命令会在build阶段自动创建下列目录结构。

  1. # ls -lF /root/rpmbuild/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 BUILD/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 BUILDROOT/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 RPMS/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SOURCES/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SPECS/drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SRPMS/ 

注意:上述的目录结构对CentOS和Redhat是都适用。你也可以使用/usr/local/redhat命令,但是你想要在build阶段修改topdir参数。如果你使用的是SuSE企业版Linux,使用 /usr/src/packages目录。

如果你想使用你自己的命令结构来代替/root/rpmbuild,你可以使用下列选择:

使用-buildroot选择,并在rpmbuild阶段定义自定义的目录。

在rpmrc文件或rpmmacros文件中定义topdir参数

3. 下载源码tar文件

接着,下载你想要创建的包的源码tar文件,并保存在SOURCES目录下。

在这个例子中,我使用的是icecase开源软件的源码包,这时一个流多媒体服务器软件。但是这些步骤在创建其他rpm包时都是相同。你只需要下载你想要的软件的源码包即可。

  1. # cd /root/rpmbuild/SOURCES/# wget http://downloads.xiph.org/releases/icecast/icecast-2.3.3.tar.gz# ls -l-rw-r--r--. 1 root root 1161774 Jun 11  2012 icecast-2.3.3.tar.gz 

4. 创建spec文件

在这一步中,我们通过创建一个spec文件来定义创建过程。这个spec文件通常包含下列8个不同的块:

  • Preamble – preamable块包含了被创建包的信息和定义相关依赖。通常它是一些行,以标签信息开头,后接冒号,而后是一些说明信息。
  • %prep – 在这一个块中,我们将为创建过程准备软件。在此过程中,任何先前的创建都被删除,而源文件(. tar)文件被扩展,等等。 一个更关键的事情是要了解可用的有预定义宏命令,可以执行各种快捷选项来构建rpm包。当您尝试创建一个复杂的包时,您可能正在使用这个宏命令。在下面的示例中,我使用了一个名为% setup的宏,它删除了先前的创建,解压源文件并更改文件的权限。您还可以在% prep部分中使用sh脚本执行此操作,但是% setup宏使用预定义的sh脚本简化了流程。
  • %description – 描述块通常包含对包的描述信息
  • %build – 这个是真正执行build指令的块。通常是一个sh脚本。
  • %install – %install块也是作为sh脚本来执行的,就像%prep和%build块。这时安装阶段的步骤
  • %files –这个块包含了这个包的一系列文件。不在此块中的文件将不可用。要求是完整的路径,并且在此块中你可以设置文件的属性和权限。
  • %clean – 这个块是为了清理那些不是应用正常创建区域的其他文件。举个例子来说:如果应用在创建过程中在/tmp目录下创建了一个临时目录结构,它将不会被移除;但是如果在该块中定义了,那么在正确安装后临时目录将被移除。

下面是icecast.spec文件

  1. # cat /root/rpmbuild/SPECS/icecast.specName:           icecast 
  2. Version:        2.3.3Release:        0Summary:        Xiph Streaming media server that supports multiple formats. 
  3. Group:          Applications/Multimedia 
  4. License:        GPL 
  5. URL:            http://www.icecast.org/ 
  6. Vendor:         Xiph.org Foundation team@icecast.org 
  7. Source:         http://downloads.us.xiph.org/releases/icecast/%{name}-%{version}.tar.gz 
  8. Prefix:         %{_prefix} 
  9. Packager:   Karthik 
  10. BuildRoot:      %{_tmppath}/%{name}-root 
  11.  
  12. %description 
  13. Icecast is a streaming media server which currently supports Ogg Vorbisand MP3 audio streams. It can be used to create an Internet radio 
  14. station or a privately running jukebox and many things in between
  15. It is very versatile in that new formats can be added relatively 
  16. easily and supports open standards for commuincation and interaction. 
  17.  
  18. %prep 
  19. %setup -q -n %{name}-%{version} 
  20.  
  21. %build 
  22. CFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix} --mandir=%{_mandir} --sysconfdir=/etc 
  23.  
  24. make 
  25.  
  26. %install 
  27. "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT 
  28.  
  29. make DESTDIR=$RPM_BUILD_ROOT install 
  30. rm -rf $RPM_BUILD_ROOT%{_datadir}/doc/%{name
  31.  
  32. %clean 
  33. "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT 
  34.  
  35. %files 
  36. %defattr(-,root,root) 
  37. %doc README AUTHORS COPYING NEWS TODO ChangeLog 
  38. %doc doc/*.html 
  39. %doc doc/*.jpg 
  40. %doc doc/*.css 
  41. %config(noreplace) /etc/%{name}.xml 
  42. %{_bindir}/icecast 
  43. %{_prefix}/share/icecast/* 
  44.  
  45. %changelog 
  46.  
  47. In this file, under % prep section you may noticed the macro “%setup -q -n %{name}-%{version}”. This macro executes the following command in the background. 
  48.  
  49. cd /usr/src/redhat/BUILD 
  50. rm -rf icecast 
  51. gzip -dc /usr/src/redhat/SOURCES/icecast-2.3.3.tar.gz | tar -xvvf - 
  52. if [ $? -ne 0 ]; then 
  53.   exit $? 
  54. fi 
  55. cd icecast 
  56. cd /usr/src/redhat/BUILD/icecast 
  57. chown -R root.root . 
  58. chmod -R a+rX,g-w,o-w . 

 

在% build部分中,您将看到带有配置选项的CFLAGS,它定义了在RPM安装期间可以使用的选项和前缀选项,强制目录用于安装和sysconfig目录,系统文件需要在该目录下复制。

在这一行中,您将看到make实用程序,它决定需要编译的文件列表,并适当地编译它们。

在%安装部分中,“make install”的%安装下面的行用于从前面的步骤中编译的二进制文件,并安装或将它们复制到适当的位置,这样它们就可以被访问。

5. 使用rpmbuild命令创建rpm文件

一旦SPEC文件准备好,您就可以开始使用rpm - b命令来构建rpm了。b选项用于执行构建过程的所有阶段。如果您在这个阶段看到任何错误,那么您需要在再次尝试之前解决它。错误通常是库依赖项,您可以在需要时下载并安装它。

  1. # cd /root/rpmbuild/SPECS# rpmbuild -ba icecast.specExecuting(%prep): /bin/sh -e /var/tmp/rpm-tmp.Kohe4t 
  2. + umask 022 
  3. + cd /root/rpmbuild/BUILD 
  4. + cd /root/rpmbuild/BUILD 
  5. + rm -rf icecast-2.3.3 
  6. + /usr/bin/gzip -dc /root/rpmbuild/SOURCES/icecast-2.3.3.tar.gz 
  7. + /bin/tar -xf - 
  8. + STATUS=0 
  9. '[' 0 -ne 0 ']'+ cd icecast-2.3.3 
  10. + /bin/chmod -Rf a+rX,u+w,g-w,o-w . 
  11. + exit 0 
  12. Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.ynm7H7 
  13. + umask 022 
  14. + cd /root/rpmbuild/BUILD 
  15. + cd icecast-2.3.3 
  16. + CFLAGS='-O2 -g'+ ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc 
  17. checking for a BSD-compatible install... /usr/bin/install -c 
  18. checking whether build environment is sane... yes 
  19. checking for a thread-safe mkdir -p... /bin/mkdir -p 
  20. checking for gawk... gawk 
  21. checking whether make sets $(MAKE)... yes 
  22. checking whether to enable maintainer-specific portions of Makefiles... no 
  23. checking for gcc... gcc 
  24. .. 
  25. .. 
  26. .. 
  27. Wrote: /root/rpmbuild/SRPMS/icecast-2.3.3-0.src.rpm 
  28. Wrote: /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpm 
  29. Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.dzahrv 
  30. + umask 022 
  31. + cd /root/rpmbuild/BUILD 
  32. + cd icecast-2.3.3 
  33. '[' /root/rpmbuild/BUILDROOT/icecast-2.3.3-0.x86_64 '!=' / ']'+ rm -rf /root/rpmbuild/BUILDROOT/icecast-2.3.3-0.x86_64 
  34. + exit 0 

 

注意:如果你使用的是SuSE linux,如果rpmbuild工具不可用,试试使用“rpm -ba” 去创建rpm包。

在安装过程中,你可能会注意到如下错误信息:

错误1: XSLT配置文件未被找到

  1. checking for xslt-config... noconfigure: error: XSLT configuration could not be founderror: Bad exit status from /var/tmp/rpm-tmp.8J0ynG (%build) 
  2. RPM build errors: 
  3.     Bad exit status from /var/tmp/rpm-tmp.8J0ynG (%build) 

 

解决方法1: 安装libxstl-devel

  1. yum install libxstl-devel 

将同时安装如下依赖包:

  • libgcrypt
  • libgcrypt-devel
  • libgpg-error-devel

错误2: libvorbis错误

  1. checking for libvorbis... configure: error: must have Ogg Vorbis v1.0 or above installederror: Bad exit status from /var/tmp/rpm-tmp.m4Gk3f (%build) 

解决方法2: 安装libvorbis-devel

  1. yum install libvorbis-devel 

将同时安装如下依赖包:

  • libogg
  • libogg-devel
  • libvorbis

6. 验证源和二进制RPM包

一旦rpmbuild命令成功运行结束,你可以在下列命令下验证源码rpm包和二进制rpm包文件。

  1. # ls -l /root/rpmbuild/SRPMS/-rw-r--r-- 1 root root 1162483 Aug 25 15:46 icecast-2.3.3-0.src.rpm# ls -l /root/rpmbuild/RPMS/x86_64/-rw-r--r--. 1 root root 349181 Feb  4 12:54 icecast-2.3.3-0.x86_64.rpm7. Install the RPM File to Verify 

作为***的步骤,你可以安装二进制包来验证是否成功安装,以及所有的依赖都被满足。

  1. # rpm -ivvh /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpmD: ============== /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpmD: loading keyring from pubkeys in /var/lib/rpm/pubkeys/*.keyD: couldn't find any keys in /var/lib/rpm/pubkeys/*.keyD: loading keyring from rpmdbD: opening  db environment /var/lib/rpm cdb:mpool:joinenvD: opening  db index       /var/lib/rpm/Packages rdonly mode=0x0D:  read h#     210 Header sanity check: OKD: added key gpg-pubkey-c105b9de-4e0fd3a3 to keyringD: Using legacy gpg-pubkey(s) from rpmdbD: Expected size:       349181 = lead(96)+sigs(180)+pad(4)+data(348901)D:   Actual size:       349181D: ========== relocationsD:      added binary package [0]D: found 0 source and 1 binary packagesD: ========== +++ icecast-2.3.3-0 x86_64/linux 0x2.. 
  2. .. 

 

安装完成后可以验证是否成功。

 

  1. # rpm -qa icecasticecast-2.3.3-0.x86_64  

 

责任编辑:庞桂玉 来源: 运维派
相关推荐

2011-05-04 09:11:30

RPM包Linux

2018-07-19 09:20:47

LinuxArchLinuxRPM包

2014-08-19 09:33:29

FedoraCentOSRPM

2010-01-11 10:23:14

linuxrpm包

2010-06-02 13:05:14

Sendmail 安装

2010-02-22 18:35:01

2019-11-05 10:55:05

RPM包RPMLinux

2010-03-02 17:19:56

Linux telne

2017-04-08 19:45:27

Linux命令安装

2015-07-21 11:43:14

CentosRPM

2019-11-05 10:18:04

RPM包RPMLinux

2010-01-13 10:02:21

LinuxRPM软件包

2016-11-09 09:10:55

CentOSRPMFedora

2022-08-01 22:19:35

Ubuntu命令

2010-01-14 09:48:05

CentOS VSFT

2022-10-17 06:34:28

FedoraLinuxRPM Fusion

2010-07-01 17:52:41

UML包图

2013-11-25 17:08:49

Linux命令RPM选项

2010-01-14 13:17:42

CentOS安装

2015-03-23 13:15:55

yumRPM
点赞
收藏

51CTO技术栈公众号