当前位置:网站首页>如何创建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
-bbuild-aboth 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.
边栏推荐
- ora-01105 ora-03175
- The principle of NMS and its code realization
- matlab 采用描点法进行数据模拟和仿真
- MongoDB搭建及基础操作
- VOC格式数据集转COCO格式数据集
- Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 1)
- 行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
- Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
- 测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?
- Lattice PCIe Learning 1
猜你喜欢

安装oracle11的时候为什么会报这个问题

Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack

Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?

详细全面的postman接口测试实战教程

MongoDB construction and basic operations

JUC线程池(一): FutureTask使用

方法重写与Object类

4. PCIe 接口时序

(十七)51单片机——AD/DA转换

### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
随机推荐
Inter-process communication and inter-thread communication
阶段性测试完成后,你进行缺陷分析了么?
DHCP的工作过程
[Machine Learning] 21-day Challenge Study Notes (2)
OPENWIFI实践1:下载并编译SDRPi的HDL源码
Three handshake and four wave in tcp
oracle create user
新唐NUC980使用记录:在用户应用中使用GPIO
Pytorch usage and tricks
canvas Gaussian blur effect
2021年11月网络规划设计师上午题知识点(下)
JZ搜索引擎solr研究-从数据库创建索引
Why is this problem reported when installing oracle11
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
ORA-01105 ORA-03175
JWT简单介绍
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
VOC格式数据集转COCO格式数据集
ORA-00257