Linux rpmbuild 使用介绍。CentOS 采用 RPM 管理安装的包,开发 OpenStack 后,需要将 python 代码包装成完整的包,共安装,本文介绍 OpenStack RPM 制作方法。
安装基础环境
yum install rpm-build make gcc -y
使用说明
初始化打包目录
$ rpmdev-setuptree
$ tree rpmbuild/
rpmbuild/
├── BUILD //打包过程中的工作目录
├── RPMS //存放生成的二进制包
├── SOURCES //打包资源,源码补丁文件
├── SPECS //spec文档
└── SRPMS //存放生成的源码包
注意:
- 默认位置通过配置文件:
~/.rpmmacros
中变量 _topdir
控制
rpmbild 命令
# rpmbuild --help
Usage: rpmbuild [OPTION...]
Build options with [ <specfile> | <tarball> | <source package> ]:
-bp build through %prep (unpack sources and apply patches) from <specfile>
-bc build through %build (%prep, then compile) from <specfile>
-bi build through %install (%prep, %build, then install) from <specfile>
-bl verify %files section from <specfile>
-ba build source and binary packages from <specfile>
-bb build binary package only from <specfile>
-bs build source package only from <specfile>
-tp build through %prep (unpack sources and apply patches) from <tarball>
-tc build through %build (%prep, then compile) from <tarball>
-ti build through %install (%prep, %build, then install) from <tarball>
-ta build source and binary packages from <tarball>
-tb build binary package only from <tarball>
-ts build source package only from <tarball>
--rebuild build binary package from <source package>
--recompile build through %install (%prep, %build, then install) from <source package>
--buildroot=DIRECTORY override build root
--clean remove build tree when done
--nobuild do not execute any stages of the build
--nodeps do not verify build dependencies
--nodirtokens generate package header(s) compatible with (legacy) rpm v3 packaging
--noclean do not execute %clean stage of the build
--nocheck do not execute %check stage of the build
--rmsource remove sources when done
--rmspec remove specfile when done
--short-circuit skip straight to specified stage (only for c,i)
--target=CPU-VENDOR-OS override target platform
Common options for all rpm modes and executables:
-D, --define='MACRO EXPR' define MACRO with value EXPR
--undefine=MACRO undefine MACRO
-E, --eval='EXPR' print macro expansion of EXPR
--macros=<FILE:...> read <FILE:...> instead of default file(s)
--noplugins don't enable any plugins
--nodigest don't verify package digest(s)
--nosignature don't verify package signature(s)
--rcfile=<FILE:...> read <FILE:...> instead of default file(s)
-r, --root=ROOT use ROOT as top level directory (default: "/")
--dbpath=DIRECTORY use database in DIRECTORY
--querytags display known query tags
--showrc display final rpmrc and macro configuration
--quiet provide less detailed output
-v, --verbose provide more detailed output
--version print the version of rpm being used
Options implemented via popt alias/exec:
--with=<option> enable configure <option> for build
--without=<option> disable configure <option> for build
--buildpolicy=<policy> set buildroot <policy> (e.g. compress man pages)
--sign generate GPG signature (deprecated, use command rpmsign instead)
Help options:
-?, --help Show this help message
--usage Display brief usage message
若包名包含 .centos/.fc25
等,可以通过 --define="dist .el7"
定制RPM包名称。
rpmbuild -bb xxx.spec --define="dist .el7"
也可以在/etc/rpm/macros.dist
中指定:
[root@xiexianbin-cn ~]# cat /etc/rpm/macros.dist
# dist macros.
%centos_ver 7
%centos 7
%rhel 7
%dist .el7.centos
%el7 1
[root@xiexianbin-cn ~]#
注:打包时各种宏定义:/usr/lib/rpm/macros
打包
如果想发布rpm格式的源码包或者是二进制包,就要使用rpmbuild工具(rpm最新打包工具)。如果我们已经根据本地源码包的成功编译安装而写了 spec文件(该文件要以.spec结束),那我们就可以建立一个打包环境,也就是目录树的建立,一般是在/usr/src/redhat/目录下建立5 个目录。它门分别是BUILD、SOURCE、SPEC、SRPM、RPM。其中BUILD目录用来存放打包过程中的源文件,SOURCE用来存放打包是 要用到的源文件和patch,SPEC用来存放spec文件,SRPM、RPM分别存放打包生成的rpm格式的源文件和二进制文件。当然我们可以根据需要 来选用不同的参数打包文件,笔者总结如下3条。
只生成二进制格式的rpm包
rpmbuild -bb xxx.spec
用此命令生成软件包,执行后屏幕将显示如下信息:(每行开头为行号)
1 Executing: %prep 2 + umask 022 3 + cd /usr/src/dist/BUILD 4 + exit 0 5 Executing: %build 6 + umask 022 7 + cd /usr/src/dist/BUILD
生成的文件会在刚才建立的RPM目录下存在。
只生成src格式的rpm包
rpmbuild -bs xxx.spec
生成的文件会在刚才建立的SRPM目录下存在。
只生成完整的源文件
rpmbuild -bp xxx.spec
源文件存在目录BUILD下。读者朋友可能对这个命令不太明白,这个命令的作用就是把tar包解开然后把所有的补丁文件合并而生成一个完整的具最新功能的源文件。
完全打包
rpmbuild -ba xxx.spec
产生以上3个过程分别生成的包。存放在相应的目录下。
OpenStack rpm 打包示例
下载 .src.rpm 包
wget https://repos.fedorapeople.org/repos/openstack/EOL/openstack-grizzly/epel-6/openstack-quantum-2013.1.4-4.el6.src.rpm
使用 rpm -ivh 解压源码包
rpm -ivh openstack-quantum-2013.1.4-4.el6.src.rpm (--root=xxx 指定路径)
或
rpmbuild --rebuild openstack-nova-15.0.2-1.el7.src.rpm
解压的结果放在 /~/rpmbuild/
下面。
检查解压文件 (optional)
cd /root/rpmbuild/SPECS/
ls openstack-quantum.spec
修改源代码(optional)
使用 rpmbuild 打包
注:如果没有rpmbuild命令,请从CentOS光盘镜像安装: yum install rpm-build
如果有相关依赖包未安装,一并安装:yum install python2-devel, yum install dos2unix
rpmbuild -ba openstack-quantum.spec (--define "_topdir xxx/root/rpmbuild" 指定buildroot)
cbs.centos.org 介绍
CentOS 官方RPM 发布地址: http://cbs.centos.org/koji/
,我们可以在这里下载到完整的rpm
、src
(该包中包含打包所需的spec
文件)。
PS: OpenStack 官方 RPM spcs 仓库: https://github.com/openstack/rpm-packaging
问题
指定到/usr/lib/rpm/check-files时,若配置文件有错误,可能报错,可以配置/usr/lib/rpm/macros
:
#%__check_files %{_rpmconfigdir}/check-files %{buildroot}