当前位置:网站首页>如何创建rpm包
如何创建rpm包
2022-08-05 01:10:00 【oneslide】
本文展示如何将脚本打包成rpm格式的文件
什么是RPM包?
RPM是Red Hat Package Manager的简称,rpm包可以在redhat系列的系统轻松安装,更新,卸载。包括Fedora, CentOS, RHEL, etc.
RPM包使用.rpm
扩展名,是一个压缩包,包含
- 二进制文件(nmap, stat, xattr, ssh, sshd, and so on)
- 配置文件 (sshd.conf, updatedb.conf, logrotate.conf, etc.)
- 文档 (README, TODO, AUTHOR, etc)
rpm包的名字一般格式:
<name>-<version>-<release>.<arch>.rpm
例如:
bdsync-0.11.1-1.x86_64.rpm
一些包也包含rpm的适用平台:
bdsync-0.11.1-1.el8.x86_64.rpm
如何创建rpm包
1. 安装rpm开发工具
yum install -y rpmdevtools rpmlint
2. 创建rpm包开发目录
创建rpm包的开发目录,位置一般在$HOME/rpmbuild
:
$ rpmdev-setuptree
开发目录结构及作用如下:
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
- BUILD 目录用于存放rpm构建过程中产生的临时文件
- RPMS 目录存放成果rpm包
- SOURCES 目录存放源码。 可以是简单的脚本,需要编译的复杂的C项目。源码的格式通常是
.tar.gz
或者.tgz
- SPEC 目录存放.spec文件, .spec文件用于定义如何打包rpm
- SPRPMS 存放.src.rpm包,源码包不属于任何架构或者Linux发行版。 实际的.rpm包是基于.src.rpm包构建的。
创建一个简单的rpm包
- 创建一个简单的脚本
$ cat << EOF >> hello.sh #!/bin/sh echo "Hello world" EOF
- 创建源码包
mkdir hello-0.0.1
mv hello.sh hello-0.0.1
tar --create --file hello-0.0.1.tar.gz hello-0.0.1
mv hello-0.0.1.tar.gz $HOME/rpmbuild/SOURCES
- 创建spec文件
如下命令生成模板spec
rpmdev-newspec hello
现在的目录机构如下所示:
/root/rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│ └── hello-0.0.1.tar.gz
├── SPECS
│ └── hello.spec
└── SRPMS
- hello.spec
Name: hello
Version: 0.0.1
Release: 1%{
?dist}
Summary: A simple hello world script
BuildArch: noarch # noarch代表任何芯片架构或者发行版都可以安装
License: GPL
Source0: %{
name}-%{
version}.tar.gz # 源码包名字,此处为hello-0.0.1.tar.gz
Requires: bash # 此软件安装的前提是需要bash,多个的话用","分隔,比如gcc,gcc-c++
%description
A demo RPM build
%prep
%setup -q # 这一步是解压SOURCES目录的源码包
%install
rm -rf $RPM_BUILD_ROOT # RPM_BUILD_ROOT是构建当前目录,宏常量
mkdir -p $RPM_BUILD_ROOT/%{
_bindir}
cp %{
name}.sh $RPM_BUILD_ROOT/%{
_bindir} # 拷贝到rpm包的%{_bindir}的目录下,%{_bindir}为宏常量,通常为/usr/bin
%clean
rm -rf $RPM_BUILD_ROOT
%files
%{
_bindir}/%{
name}.sh # /usr/bin/hello.sh 相当于把rpm包里的hello.sh放到/usr/bin下
%changelog
* Sun Nov 18 2020 Valentin Bajrami <[email protected]> - 0.0.1
- First version being packaged
宏常量的查看方式
$ rpm --eval '%{_bindir}'
/usr/bin
Other useful macros:
其他常用宏常量:
- %{name} name of the package (as defined in the Name: field)
- %{version} version of the package (as defined in the Version: field)
- %{_datadir} shared data (/usr/sbin by default)
- %{_sysconfdir} configuration directory (/etc by default)
- 检测spec文件有效性
$ rpmlint ~/rpmbuild/SPECS/hello.spec
SPECS/hello.spec: W: no-%build-section
SPECS/hello.spec: W: invalid-url Source0: hello-0.0.1.tar.gz
0 packages and 1 specfiles checked; 0 errors, 2 warnings.
- 构建rpm包
rpmbuild -ba hello.spec
-b
build-a
both source and binary packages
- 测试安装包
cd ~/rpmbuild/RPMS
rpm -ivh hello-0.0.1.rpm
检测rpm是否成功:
rpm -qi hello
Troubleshoting
- 缺少so文件
在一些场景下,.so
文件包含于rpm包中,但是运行rpm -ivh
时出现,如下报错:
$ rpm -ivh hello.rpm
error: Failed dependencies:
lib3ds-2.so()(64bit) is needed by cips-22.05.06-1.el7.x86_64
libAltova.so()(64bit) is needed by cips-22.05.06-1.el7.x86_64
libAltovaXML.so()(64bit) is needed by cips-22.05.06-1.el7.x86_64
libGLEW.so.2.1()(64bit) is needed by cips-22.05.06-1.el7.x86_64
这是因为rpmbuild过程中会自动检测软件所需的.so
依赖,但是仅检测通过rpm方式安装的。假如.so
文件在rpm里,安装可以使用--nodeps
选项来跳过检测, 这并不会影响软件正常运行。
rpm -ivh --nodeps *.rpm
Reference List
- https://www.cnblogs.com/dissipate/p/13050646.html
- https://www.thegeekstuff.com/2015/02/rpm-build-package-example/#:~:text=To%20build%20an%20rpm%20file%20based%20on%20the,rpm-build%20package.%20Install%20it%20as%20shown%20show%20below.
边栏推荐
- 3. pcie.v file
- If capturable=False, state_steps should not be CUDA tensors
- ORA-01105 ORA-03175
- [Machine Learning] 21-day Challenge Study Notes (2)
- 2022 Nioke Multi-School Training Session H Question H Take the Elevator
- The principle of NMS and its code realization
- 多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
- Matlab uses plotting method for data simulation and simulation
- Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
- Zombie and orphan processes
猜你喜欢
随机推荐
Bit rate vs. resolution, which one is more important?
oracle create user
新唐NUC980使用记录:在用户应用中使用GPIO
第十四天&postman
Software testing interview questions: What stages should a complete set of tests consist of?
Software Testing Interview Questions: About Automated Testing Tools?
PCIe Core Configuration
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
(17) 51 MCU - AD/DA conversion
软件基础的理论
10年测试经验,在35岁的生理年龄面前,一文不值
ora-00604 ora-02429
GCC: compile-time library path and runtime library path
软件测试技术之最有效的七大性能测试技术
GCC: Shield dependencies between dynamic libraries
Getting Started with Kubernetes Networking
LiveVideoStackCon 2022 上海站明日开幕!
SAP ERP和ORACLE ERP的区别是哪些?
Method Overriding and Object Class
day14--postman接口测试