如何在fedora中打RPM包
从最简单的脚本开始干起
RPM包是基于红帽系列的(Red Hat,Centos,Fedora)的软件安装包,它可以包含可执行文件、配置文件以及文档。
RPM包命名规范
RPM包的命名格式为<name>-<version>-<release>.<arch>.rpm,例如bdsync-0.11.1-1.el8.x86_64.rpm
环境的准备
在打包RPM包之前,需要安装一些工具:rpmdevtools、rpmlint
yum install rpmdevtools
yum install rpmlint
创建目录
使用rpmdev-setuptree来创建打包需要的目录
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
各个目录的作用我就直接搬运了:
- The BUILD directory is used during the build process of the RPM package. This is where the temporary files are stored, moved around, etc.
- The RPMS directory holds RPM packages built for different architectures and noarch if specified in .spec file or during the build.
- The SOURCES directory, as the name implies, holds sources. This can be a simple script, a complex C project that needs to be compiled, a pre-compiled program, etc. Usually, the sources are compressed as .tar.gz or .tgz files.
- The SPEC directory contains the .spec files. The .spec file defines how a package is built. More on that later.
- The SRPMS directory holds the .src.rpm packages. A Source RPM package doesn’t belong to an architecture or distribution. The actual .rpm package build is based on the .src.rpm package.
现在开始写一个helloword脚本
!/bin/sh
echo "Hello world"
把这个脚本放在目录hello-0.0.1中,然后使用命令打包
tar --create --file hello-0.0.1.tar.gz hello-0.0.1
把包hello-0.0.1.tar.gz放到刚刚创建的SOURCE目录下
下面是复杂的重头戏=>spec文件
我们可以在SPEC文件夹下创建hello.spec文件,命令为
rpmdev-newspec hello
这个文件是预先为我们写好的一个模板,我们需要在里面填写信息以及修改,最复杂的部分
目前,最简单的spec就是这样吧
name: hello
Version: 0.0.1
Release: 1%{?dist}
Summary: A simple hello world script
BuildArch: noarch
License: GPL
Source0: %{name}-%{version}.tar.gz
Requires: bash
%description
A demo RPM build
%prep
%setup -q
%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/%{_bindir}
cp %{name}.sh $RPM_BUILD_ROOT/%{_bindir}
%clean
rm -rf $RPM_BUILD_ROOT
%files
%{_bindir}/%{name}.sh
%changelog
* Sat Jun 18 2022 peter
-
使用rpmlint对spec文件进行检查
rpmlint hello.spec
构建rpm包
创建src.rpm包
rpmbuild -bs hello.spec
创建二进制rpm包
rpmbuild -bb hello.spec
安装rpm包
rpm -i hello-0.0.1-1.fc36.noarch.rpm
查看RPM包的信息
确认rpm包已经被安装了
rpm -qi hello
查看rpm包的%changelog部分
rpm -q hello --changelog
查看rpm包包含什么内容
rpm -ql hello
下面要学习一些更加深入的知识
上面的流程只是一个demo,这里想学习一些稍微深入的东西。
RPM的基础
当执行rpmbuild命令时,rpmbuild会读取.spec文件,并按照下面的步骤逐步完成构建。以%开头的表示预定义的宏
%prep阶段:读取位于%_sourcedir目录的源代码和 patch 。之后,解压源代码至%_builddir的子目录并应用所有 patch。%build阶段:编译位于%_builddir构建目录下的文件。通过执行类似"./configure && make"的命令实现。%install阶段:读取位于%_builddir构建目录下的文件并将其安装至%_buildrootdir目录。这些文件就是用户安装 RPM 后,最终得到的文件。注意一个奇怪的地方: 最终安装目录 不是 构建目录。通过执行类似"make install"的命令实现。%chech阶段:检查软件是否正常运行。通过执行类似"make test"的命令实现。很多软件包都不需要此步。bin阶段:读取位于%_buildrootdir最终安装目录下的文件,以便最终在%_rpmdir目录下创建 RPM 包。在该目录下,不同架构的 RPM 包会分别保存至不同子目录, “noarch” 目录保存适用于所有架构的 RPM 包。这些 RPM 文件就是用户最终安装的 RPM 包。src阶段:创建源码 RPM 包(简称 SRPM,以.src.rpm 作为后缀名),并保存至%_srcrpmdir目录。SRPM 包通常用于审核和升级软件包。
在上面的预定义宏中,宏对应的目录为
| 宏代码 | 名称 | 默认位置 | 用途 |
|---|---|---|---|
| %_specdir | Spec 文件目录 | ~/rpmbuild/SPECS | 保存 RPM 包配置(.spec)文件 |
| %_sourcedir | 源代码目录 | ~/rpmbuild/SOURCES | 保存源码包(如 .tar 包)和所有 patch 补丁 |
| %_builddir | 构建目录 | ~/rpmbuild/BUILD | 源码包被解压至此,并在该目录的子目录完成编译 |
| %_buildrootdir | 最终安装目录 | ~/rpmbuild/BUILDROOT | 保存 %install 阶段安装的文件 |
| %_rpmdir | 标准 RPM 包目录 | ~/rpmbuild/RPMS | 生成/保存二进制 RPM 包 |
| %_srcrpmdir | 源代码 RPM 包目录 | ~/rpmbuild/SRPMS | 生成/保存源码 RPM 包(SRPM) |
本文详细介绍了如何在Fedora系统中构建RPM包,从编写简单的Hello World脚本开始,涵盖RPM包命名规范、环境准备、创建目录、编写spec文件、使用rpmlint检查、构建与安装RPM包,以及查看RPM包信息等步骤。通过这一流程,读者可以学习到RPM包的基本构建原理和操作。
1385

被折叠的 条评论
为什么被折叠?



