RPM(Redhat Package Manager)是用于Redhat、CentOS、Fedora等Linux 分发版(distribution)的常见的软件包管理器。rpm工具可以用来制作源码安装包和二进制安装包。本文档提供一个示例来说明如何制作一个rpm二进制包。
1. 准备安装打包需要的程序:yum install rpm-build rpmdevtools
2. 安装一个简单的hello.sh脚本-
首先我们需要准备需要打包的程序hello.sh
#!/bin/bash echo "Hello World!"
-
编写SPEC文档hello.spec
注意:我们的spec文档的名字要和安装程序的名称相同
Name: hello Version: 2.1 Release: 1 Summary: The "Hello World" script Summary(zh_CN): GNU "Hello World" 程序 License: GPLv3+ %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 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等. %install mkdir -p %{buildroot}/usr/local/bin install -m 755 -t %{buildroot}/usr/local/bin /root/dw/rpmTest/hello.sh %files /usr/local/bin/hello.shspec文档说明:
-
表头包含安装包的基本信息,表头项说明如下(* 标识必须包含此项)
- Name *:软件包的名称
- Version *:软件版本号
- Release *:同一版本软件的发布版本号
- Summary *:软件的简短介绍
- License *:软件的授权模式
- Summary(zh_CN):中文版本的简短介绍
-
内容包含以下项目
- %description
软件的完整介绍,与Summary字段不通,完成内容介绍可以写多行。 - %prep(没有使用)
解压源码的过程。 - %build (没有使用)
编译源码的过程。 - %install
安装脚本。将需要安装的文件拷贝到%{buildroot}目录。%{buildroot}目录相当于安装系统的根目录。 - %files
需要打包的文件列表,注意这里的文件路径是以${buildroot}制定的安装后的根目录。可以使用通配符,如/etc/keepalived/*。注意:使用通配符时,如果安装目录有文件与安装包中冲突,安装失败。 - %changelog(没有使用)
软件的变化记录。 - scriptlets 安装卸载时执行脚本(没有使用)
- 在软体包安装之前 (%pre) 或之后 (%post) 执行
- 在软体包卸载之前 (%preun) 或之后 (%postun) 执行
- 在事务开始 (%pretrans) 或结束 (%posttrans) 时执行
- %description
-
执行rpm-build -bb hello.spec生成二进制rpm包,默认情况下会在当前用户生成目录~/rpmbuild
tree rpmbuild/ rpmbuild/ ├── BUILD ├── BUILDROOT ├── RPMS │ └── x86_64 │ └── hello-2.1-1.ky10.x86_64.rpm ├── SOURCES ├── SPECS └── SRPMS
**建议定义_topdir宏,将安装包生成到制定目录。**下面的命令生成rpm包到当前目录:
rpmbuild -bb hello.spec --define "_topdir $PWD/rpmbuild"4. 自定义rpm包名称
默认安装包名称定义在/usr/lib/rpm/macros文件中:
%_rpmfilename %{_build_name_fmt}
%_build_name_fmt %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
在hello.spec的第一行修改%_rpmfilename宏进行自定义包名称安装
%define _rpmfilename hello.rpm
再次执行打包命令,查看rpmbuild/RPMS目录生成自定义rpm包。
tree rpmbuild/ rpmbuild/ ├── BUILD ├── BUILDROOT ├── RPMS │ └── hello.rpm ├── SOURCES ├── SPECS └── SRPMS5. 将rpm包中的程序添加systemd`自启动服务
生成systemd自启动服务需要编写hello.service文件,本文并不做hello.service文件格式的具体说明,只是说明打包流程。通过修改hello.spec文件就可以将hello.service添加到systemd自启动服务中:
- %install阶段生成/etc/systemd/system/hello.service
- %post阶段在安装后启动hello.service服务
- %preun阶段在卸载前停止并禁用hello.service服务
hello.service文件如下:
[Unit] [Install] WantedBy=multi-user.target [Service] ExecStart=/usr/local/bin/hello.sh Restart=always RestartSec=5 StandardOutput=syslog StandardError=syslog SyslogIdentifier=%n
修改后的hello.spec文件如下
%define _rpmfilename hello.rpm
Name: hello
Version: 2.1
Release: 1
Summary: The "Hello World" script
Summary(zh_CN): GNU "Hello World" 程序
License: GPLv3+
%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 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.
%install
mkdir -p %{buildroot}/usr/local/bin
install -m 755 -t %{buildroot}/usr/local/bin /root/dw/rpmTest/hello.sh
# install the systemd unit file to buildroot.
mkdir -p %{buildroot}/etc/systemd/system
install -t %{buildroot}/etc/systemd/system /root/dw/rpmTest/hello.service
%files
/usr/local/bin/hello.sh
/etc/systemd/system/hello.service
%post
systemctl enable hello.service
systemctl start hello.service
%preun
systemctl stop hello.service
systemctl disable hello.service
安装生成的rpm包,并查看hello.service服务
rpm -ivh rpmbuild/RPMS/hello.rpm systemctl status hello ● hello.service Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: disabled) Active: activating (auto-restart) since Tue 2021-10-12 15:16:37 CST; 80ms ago Process: 288322 ExecStart=/usr/local/bin/hello.sh (code=exited, status=0/SUCCESS) Main PID: 288322 (code=exited, status=0/SUCCESS)
卸载hello包,执行了停止和移除服务命令
rpm -e hello Removed /etc/systemd/system/multi-user.target.wants/hello.service.参考
- RPM 打包︰由一竅不通到動手濫用 (一)
- RPM 打包︰由一竅不通到動手濫用 (二)
- RPM 打包︰由一竅不通到動手濫用 (三)
- RPM打包原理、示例、详解及备查
- CentOS 如何打 RPM 包



