RPM构建-简单实例

 

本文主要通过一个简单的例子来演示制作rpm包的过程。

将小程序pybin.py搬运到/usr/bin目录下,程序代码如下:pybin.py

#!/usr/bin/python
print "Hello world, I am pybin."

一、准备

制作rpm包主要用到rpmbuild工具,rpmdevtools套件包含了rpmbuild等软件,同时又提供了很多有用的脚本,安装:

yum install rpmdevtools

spec配置检查工具

yum install rpmlint

二、构建目录结构

rpmdev-setuptree是rpmdevtools中带的初始化环境脚本,执行后会在用户主目录下创建rpmbuild目录

[zht@zht-dev ~]$ rpmdev-setuptree 
[zht@zht-dev ~]$ tree rpmbuild
rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

5 directories, 0 files

三、准备要打包的文件

将源代码(理想情况下应为一个 tar.gz压缩文件)复制到 SOURCES 目录中。如果有必要,重命名 tar压缩文件,以包含应用程序的版本号,便于与其他文件区分开。约定的命名格式为{包名}-{版本号}.tar.gz

[zht@zht-dev SOURCES]$ pwd
/home/zht/rpmbuild/SOURCES
[zht@zht-dev SOURCES]$ ls
pybin-0.0.1
[zht@zht-dev SOURCES]$ tar -czf pybin-0.0.1.tar.gz pybin-0.0.1/
[zht@zht-dev SOURCES]$ rm -rf pybin-0.0.1
[zht@zht-dev SOURCES]$ ls
pybin-0.0.1.tar.gz
[zht@zht-dev SOURCES]$ 

新增配置文件pybin.conf

[DEFAULT]
debug = True
[root@zht-dev SOURCES]# ls
pybin-0.0.1.tar.gz  pybin.conf
[root@zht-dev SOURCES]# 

四、Spec文件

在SPEC目录通过以下命令初始化spec文件:

[zht@zht-dev SPECS]$ pwd
/home/zht/rpmbuild/SPECS
[zht@zht-dev SPECS]$ ls
[zht@zht-dev SPECS]$ rpmdev-newspec pybin.spec
pybin.spec created; type python, rpm version >= 4.11.
[zht@zht-dev SPECS]$ ls
pybin.spec
[zht@zht-dev SPECS]$ 

编辑pybin.spec文件,示例如下:

%define debug_package %{nil}
%define workdir /usr/bin

Name:           pybin
Version:        0.0.1
Release:        1%{?dist}
Summary:        This is a python script.

License:        GPL
URL:            changhungtao.github.io
Source0:        %{name}-%{version}.tar.gz
Source1:        %{name}.conf

#BuildArch:      
BuildRequires:  python-devel

%description
This is a python script.

%prep
%setup -q


%build


%install
rm -rf $RPM_BUILD_ROOT
install -d -m 755 $RPM_BUILD_ROOT/%{workdir}
install -p -D -m 755 pybin.py $RPM_BUILD_ROOT/%{workdir}
install -d -m 755 $RPM_BUILD_ROOT/%{_sysconfdir}/%{name}
install -p -D -m 666 %{SOURCE1} $RPM_BUILD_ROOT/%{_sysconfdir}/%{name}/%{name}.conf 


%clean
rm -rf $RPM_BUILD_ROOT/
 

%files
%doc
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%defattr(0755,root,root)
%{workdir}/pybin.py


%changelog

编辑完成后的rpmbuild目录如下:

[zht@zht-dev rpmbuild]$ pwd
/home/zht/rpmbuild
[zht@zht-dev rpmbuild]$ tree
.
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│   ├── pybin-0.0.1.tar.gz
│   └── pybin.conf
├── SPECS
│   └── pybin.spec
└── SRPMS

6 directories, 3 files

五、编译RPM

通过指定spec文件编译RPM包,命令如下:

[zht@zht-dev SPECS]$ pwd
/home/zht/rpmbuild/SPECS
[zht@zht-dev SPECS]$ ls
pybin.spec
[zht@zht-dev SPECS]$ rpmbuild -bb pybin.spec 
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.MU63Kp
+ umask 022
+ cd /home/zht/rpmbuild/BUILD
+ cd /home/zht/rpmbuild/BUILD
+ rm -rf pybin-0.0.1
+ /usr/bin/gzip -dc /home/zht/rpmbuild/SOURCES/pybin-0.0.1.tar.gz
+ /usr/bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd pybin-0.0.1
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.3DLK7G
+ umask 022
+ cd /home/zht/rpmbuild/BUILD
+ cd pybin-0.0.1
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.3LuJvY
+ umask 022
+ cd /home/zht/rpmbuild/BUILD
+ '[' /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64 '!=' / ']'
+ rm -rf /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64
++ dirname /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64
+ mkdir -p /home/zht/rpmbuild/BUILDROOT
+ mkdir /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64
+ cd pybin-0.0.1
+ rm -rf /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64
+ install -d -m 755 /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64//usr/bin
+ install -p -D -m 755 pybin.py /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64//usr/bin
+ install -d -m 755 /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64//etc/pybin
+ install -p -D -m 666 /home/zht/rpmbuild/SOURCES/pybin.conf /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64//etc/pybin/pybin.conf
+ '[' '%{buildarch}' = 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: pybin-0.0.1-1.el7.centos.x86_64
Provides: config(pybin) = 0.0.1-1.el7.centos pybin = 0.0.1-1.el7.centos pybin(x86-64) = 0.0.1-1.el7.centos
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires: /usr/bin/python
Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64
Wrote: /home/zht/rpmbuild/RPMS/x86_64/pybin-0.0.1-1.el7.centos.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.cT8x0x
+ umask 022
+ cd /home/zht/rpmbuild/BUILD
+ cd pybin-0.0.1
+ rm -rf /home/zht/rpmbuild/BUILDROOT/pybin-0.0.1-1.el7.centos.x86_64/
+ exit 0
[zht@zht-dev SPECS]$ 

查看编译后的文件夹:

[zht@zht-dev rpmbuild]$ pwd
/home/zht/rpmbuild
[zht@zht-dev rpmbuild]$ tree
.
├── BUILD
│   └── pybin-0.0.1
│       └── pybin.py
├── BUILDROOT
├── RPMS
│   └── x86_64
│       └── pybin-0.0.1-1.el7.centos.x86_64.rpm
├── SOURCES
│   ├── pybin-0.0.1.tar.gz
│   └── pybin.conf
├── SPECS
│   └── pybin.spec
└── SRPMS

8 directories, 5 files
[zht@zht-dev rpmbuild]$ 

RPMS/x86_64/pybin-0.0.1-1.el7.centos.x86_64.rpm即为编译出来的rpm包,pybin为包名,0.0.1为版本号,1.el7为释放号,centos.x86_64为架构号。

六、测试RPM

[root@zht-dev rpmbuild]# rpm -q pybin
package pybin is not installed
[root@zht-dev rpmbuild]# 
[root@zht-dev rpmbuild]# rpm -ivh RPMS/x86_64/pybin-0.0.1-1.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:pybin-0.0.1-1.el7.centos         ################################# [100%]
[root@zht-dev rpmbuild]# whereis pybin
pybin: /usr/bin/pybin.py /etc/pybin
[root@zht-dev rpmbuild]# ls -al /usr/bin/pybin.py 
-rwxr-xr-x 1 root root 51 May 31 15:26 /usr/bin/pybin.py
[root@zht-dev rpmbuild]# ls -al /etc/pybin/pybin.conf 
-rw-rw-rw- 1 root root 24 May 31 15:39 /etc/pybin/pybin.conf
[root@zht-dev rpmbuild]# rpm -q pybin
pybin-0.0.1-1.el7.centos.x86_64
[root@zht-dev rpmbuild]# pybin.py 
Hello world, I am pybin.
[root@zht-dev rpmbuild]# rpm -e pybin 
[root@zht-dev rpmbuild]# rpm -q pybin
package pybin is not installed
[root@zht-dev rpmbuild]# ls -al /usr/bin/pybin.py     
ls: cannot access /usr/bin/pybin.py: No such file or directory
[root@zht-dev rpmbuild]# ls -al /etc/pybin/pybin.conf 
ls: cannot access /etc/pybin/pybin.conf: No such file or directory
[root@zht-dev rpmbuild]#