打包前的准备&需要了解的信息

系统环境:openEuler 20.03 LTS SP3

依赖工具:rpmbuild rpmdevtools

yum install rpmbuild rpmdevtools -y

软件源码(这里拿GNU的helloworld程序作为示例):http://ftp.gnu.org/gnu/hello/hello-2.12.tar.gz

SPEC阶段和目录的对应关系(RPM 包的构建 - 实例 - Michael翔 - 博客园 (cnblogs.com))

阶段读取的目录写入的目录具体动作
%prep%\_sourcedir%\_builddir读取位于 %\_sourcedir 目录的源代码和 patch 。之后,解压源代码至 %\_builddir 的子目录并应用所有 patch。
%build%\_builddir%\_builddir编译位于 %\_builddir 构建目录下的文件。通过执行类似 ./configure && make 的命令实现。
%install%\_builddir%\_buildrootdir读取位于 %\_builddir 构建目录下的文件并将其安装至 %\_buildrootdir 目录。这些文件就是用户安装 RPM 后,最终得到的文件。注意一个奇怪的地方: 最终安装目录 不是 构建目录。通过执行类似 make install 的命令实现。
%check%\_builddir%\_builddir检查软件是否正常运行。通过执行类似 make test 的命令实现。很多软件包都不需要此步。
bin%\_buildrootdir%\_rpmdir读取位于 %\_buildrootdir 最终安装目录下的文件,以便最终在 %\_rpmdir 目录下创建 RPM 包。在该目录下,不同架构的 RPM 包会分别保存至不同子目录, noarch 目录保存适用于所有架构的 RPM 包。这些 RPM 文件就是用户最终安装的 RPM 包。
src%\_sourcedir%\_srcrpmdir创建源码 RPM 包(简称 SRPM,以.src.rpm 作为后缀名),并保存至 %\_srcrpmdir 目录。SRPM 包通常用于审核和升级软件包。

打包工具的使用

通常,我们不使用root用户进行这一系列的操作,所以我们可以在系统中添加一个rpmbuilder用户用来进行打包相关操作,具体命令如下:

useradd rpmbuilder
passwd rpmbuilder #set your pwd
su - rpmbuilder
创建工作目录

如果提示command not found
执行yum install tree nano vim -y即可。

[rpmbuilder@VM ~]# rpmdev-setuptree
[rpmbuilder@VM ~]# tree rpmbuild
rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

此命令会在$HOME里创建工作目录。

软件编译构建

我们需要先了解该软件的构建方式,获取源码解压后我们可以看到以下文件列表:

 title=

image-20220420161345660

可见,存在configure文件,可以用 ./configure; make; make install 方式编译安装。

构建时,需要使源码文件位于$HOME/rpmbuild/SOURCES/文件夹内。

spec文件的格式与编写

nano是Linux下一个优秀的文本编辑器,极易上手,部分操作如下:
打开文件:

nano filename

Ctrl+X->Y->Enter 保存三步骤
Ctrl+W 查找相关字符串

spec文件一般以name-version.spec命名,其部分内容如下(spec文件内不应有注释):

[rpmbuilder@VM ~]# rpmdev-newspec -o hello-2.12.spec #利用工具新建spec文件
[rpmbuilder@VM ~]# nano hello-2.12.spec
[rpmbuilder@VM ~]# cat hello-2.12.spec
Name:           hello
Version:        2.12
Release:        1%{?dist}
Summary:        Hello World!

License:        GPL v3+
URL:            http://ftp.gnu.org/gnu/hello
Source0:        http://ftp.gnu.org/gnu/hello/%{name}-%{version}.tar.gz

BuildRequires:  gettext #编译需求软件包
Requires(post): info #安装需求软件包
Requires(preun): info

%description
The "Hello World" program, done with all bells and whistles of a proper FOSS
project, including configuration, build, internationalization, help files, etc.

%description -l zh_CN #根据系统语言显示,如果没有对应语言,则默认英文
"Hello World" 程序, 包含 FOSS 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.

%prep
%setup -q #进行解压操作及编译之前的一些操作都可以写在这里

%build
%configure
%make_build

%install
make install DESTDIR=%{buildroot} #install到$HOME/rpmbuild/BUILDROOT/%{name}-%{version}.%{arch}目录
%find_lang %{name} #本地化的一些操作
rm -f %{buildroot}/%{_infodir}/dir #执行简单的清理

%clean #编译后执行清理

%pre #安装前

%post #安装后
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :

%preun #卸载执行前
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi

%postun #卸载执行后

%files -f %{name}.lang
%doc AUTHORS ChangeLog NEWS README THANKS TODO #将上述文件复制至doc文件夹里
%license COPYING #将上述文件复制至相应license文件夹里
%{_mandir}/man1/hello.1.* #主要文件 %{_mandir}为宏定义 位于%{buildroot}目录下/usr/share/man/
%{_infodir}/hello.info.* #/usr/share/info/
%{_bindir}/hello #/usr/bin/

%changelog
* Wed Apr 20 2022 root
- 

类似的 还有 %{\\_includedir} %{\\_libdir}

%file标记可以将%{buildroot}目录下的文件安装至任意目录。

重要: 不要%{\_bindir}直接写到%file标记下,这样安装时没问题,卸载软件包时会将/usr/bin/里的文件全部删除。

打包相关操作

编写好spec文件后,可以开始进行打包操作:

[rpmbuilder@VM ~]# rpmbuild -bb hello-2.12.spec #只生成源码格式的rpm包

命令执行完成后 可以安装测试效果
[rpmbuilder@VM ~]# sudo rpm -ivh $HOME/rpmbuild/RPMS/aarch64/hello-2.12-1.aarch64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:hello-2.12-1                     ################################# [100%]

如果提示rpmbuilder用户不在sudoer file里,root添加里添加一下即可或是切换至root用户安装。

安装完成后测试各个功能是否正常:

[rpmbuilder@VM ~]# hello
Hello, world!
[rpmbuilder@VM ~]# hello -t
hello, world
[rpmbuilder@VM ~]# hello --traditional
hello, world
[rpmbuilder@VM ~]# hello -g
hello: option requires an argument -- 'g'
Try 'hello --help' for more information.
[rpmbuilder@VM ~]# hello -g "Hello,openEuler!"
Hello,openEuler!
[rpmbuilder@VM ~]# hello --version
hello (GNU Hello) 2.12.1-6fe9
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Karl Berry, Sami Kerola, Jim Meyering,
and Reuben Thomas.
[rpmbuilder@VM ~]# rpm -qi hello
Name        : hello
Version     : 2.12
Release     : 1
Architecture: aarch64
Install Date: Wed 20 Apr 2022 04:43:20 PM CST
Group       : Unspecified
Size        : 210280
License     : GPL v3+
Signature   : (none)
Source RPM  : hello-2.12-1.src.rpm
Build Date  : Wed 20 Apr 2022 04:36:34 PM CST
Build Host  : ecs-20ed
URL         : http://ftp.gnu.org/gnu/hello
Summary     : Hello World!
Description :
The "Hello World" program, done with all bells and whistles of a proper FOSS
project, including configuration, build, internationalization, help files, etc.
configure、make、make install 命令的区别

RPM 包的构建 - 实例 - Michael翔 - 博客园 (cnblogs.com)

最后修改:2024 年 04 月 16 日
如果觉得我的文章对你有用,请随意赞赏