1. 预备
yum -y install rpm-build rpmdevtools
2. 实例
2.1. 初始化RPM目录
[root@localhost ~]# rpmdev-setuptree
[root@localhost ~]# tree rpmbuild/
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
2.2. 源文件
创建hello
包目录
[root@localhost lab]# mkdir hello
编写源代码, 依然从"Hello world"开始
[root@localhost lab]# cat hello/hello.py
#!/bin/env python
print 'Hello world'
将hello
目录打包
[root@localhost lab]# tar zcvf hello.tar.gz hello/
2.3. 定制SPEC文件
通过rpmdev-newspec hello
生成spec模板文件, 在模板基础上修改
RPM包命名规范: 包名-版本号-发布次数-发行商-Linux平台-适合的硬件平台-包扩展名
- Name, Version, Release对应包名, 版本号, 发布次数
- BuildArch = 硬件平台, 如x86_64, i686, 其中noarch表示无硬件限制
- Source0 =
2.2章节
生成的tgz文件, 从命名可以看出, 支持多源文件:Source1, Source2… - BuildRequires = 编译前置条件, Requires = 软件运行前置条件
- %global = 全局变量, 注意: 全局变量使用
%
引用 (不是$
) - %prep, %build, %install定义编译的3个阶段: 前置步骤, 编译, 安装, 每个阶段的具体工作请参考
2.4章节
- %setup -q -n hello为内置语法, 本质还是shell脚本, 同样参考
2.4章节
- %files = 待映射(chroot)到服务器的目录和文件, 即将源文件安装到什么目录下
Name: hello
Version: 1.0
Release: 1%{?dist}
Summary: rpm example
License: GPLv3
Source0: hello.tar.gz
BuildRequires: python
Requires: python bash
BuildArch: x86_64
%global app_bin_dir /usr/local/bin
%global app_lib_dir /usr/local/lib/%{name}-%{version}
%global debug_package %{nil}
%description
show how to compile python code
%prep
%setup -q -n hello
%build
python -m compileall %{name}.py
%install
mkdir -p %{buildroot}/%{app_bin_dir}
mkdir -p %{buildroot}/%{app_lib_dir}
cat > %{buildroot}/%{app_bin_dir}/%{name} <<-EOF
#!/bin/bash
/usr/bin/python %{app_lib_dir}/%{name}.pyc
EOF
install -m 644 %{name}.py* %{buildroot}/%{app_lib_dir}/
%files
%defattr(-,lab,lab,-)
%dir %{app_lib_dir}
%{app_lib_dir}/*
%attr(700,-,-) %{app_bin_dir}/%{name}
2.4. 编译RPM
编译前, 先将源文件移动到~/rpmbuild/SOURCES
下
[root@localhost lab]# mv hello.tar.gz ~/rpmbuild/SOURCES
编译
[root@localhost lab]# rpmbuild -bb hello.spec
2.4.1. prep阶段日志
prep阶段, 读取SOURCES
目录源文件, 解压到BUILD
目录
%setup -q -n hello
本质是解压操作
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.UvUyxI
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf hello
+ /usr/bin/tar -xf -
+ /usr/bin/gzip -dc /root/rpmbuild/SOURCES/hello.tar.gz
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd hello
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
2.4.2. build阶段日志
build阶段工作目录还是BUILD
, 完成编译动作
如果只是shell脚本, 因不需要编译, 可以省略build步骤
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.g02H3n
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd hello
+ python -m compileall hello.py
Compiling hello.py ...
+ exit 0
2.4.3. install阶段日志
install阶段, 操作BUILD
目录文件, 将最终输出文件编排到BUILDROOT
目录
因采用chroot, BUILDROOT
目录结构与既定的安装目录几乎一致, 只是多了一层 /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64/
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.q905D3
+ umask 022
+ cd /root/rpmbuild/BUILD
+ '[' /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64 '!=' / ']'
+ rm -rf /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64
++ dirname /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64
+ mkdir -p /root/rpmbuild/BUILDROOT
+ mkdir /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64
+ cd hello
+ mkdir -p /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64//usr/local/bin
+ mkdir -p /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64//usr/local/lib/hello-1.0
+ cat
+ install -m 644 hello.py hello.pyc /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64//usr/local/lib/hello-1.0/
+ '[' x86_64 = noarch ']'
+ QA_CHECK_RPATHS=1
+ case "${QA_CHECK_RPATHS:-}" in
+ /usr/lib/rpm/check-rpaths
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: hello-1.0-1.el7.centos.x86_64
2.4.4. clean阶段日志
clean虽然没有声明, 但默认会执行, 清空BUILDROOT
目录
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.3yDqM5
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd hello
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/hello-1.0-1.el7.centos.x86_64
+ exit 0
2.5. 编译结果
编译的rpm包存放在RPMS目录
[root@localhost lab]# tree ~/rpmbuild/
/root/rpmbuild/
├── BUILD
│ └── hello
│ ├── hello.py
│ ├── hello.pyc
│ └── LICENSE
├── BUILDROOT
├── RPMS
│ └── x86_64
│ └── hello-1.0-1.el7.centos.x86_64.rpm
├── SOURCES
│ └── hello.tar.gz
├── SPECS
└── SRPMS
rpm包文件
[root@localhost ~]# rpm -ql hello
/usr/local/bin/hello
/usr/local/lib/hello-1.0
/usr/local/lib/hello-1.0/hello.py
/usr/local/lib/hello-1.0/hello.pyc
/usr/local/lib/hello-1.0/hello.pyo
执行/usr/local/bin/hello
命令
[root@localhost ~]# hello
Hello world