1. 首页 > Linux教程 > 正文

Linux教程FG391-RPM软件包制作

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文档介绍RPM软件包的制作方法。

风哥提示:

Part01-RPM构建环境

1.1 安装RPM构建工具

# 安装RPM构建工具
[root@build ~]# dnf install -y rpm-build rpmdevtools gcc make

# 创建RPM构建目录结构
[root@build ~]# rpmdev-setuptree

# 查看目录结构
[root@build ~]# tree rpmbuild/
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

5 directories, 0 files

# 目录说明
[root@build ~]# cat > /root/rpm-dirs.txt << 'EOF' RPM构建目录说明 ============== BUILD: 编译时的临时目录 RPMS: 生成的二进制RPM包存放目录 SOURCES: 源码包存放目录 SPECS: spec文件存放目录 SRPMS: 生成的源码RPM包存放目录 EOF

1.2 编写Spec文件

# 创建示例程序
[root@build ~]# mkdir -p /tmp/hello-1.0
[root@build ~]# cat > /tmp/hello-1.0/hello.c << 'EOF' #include

int main() {
printf(“Hello, RPM World!\n”);
return 0;
}
EOF

[root@build ~]# cat > /tmp/hello-1.0/Makefile << 'EOF' CC = gcc CFLAGS = -Wall -O2 PREFIX = /usr all: hello hello: hello.c $(CC) $(CFLAGS) -o hello hello.c install: hello install -d $(DESTDIR)$(PREFIX)/bin install -m 755 hello $(DESTDIR)$(PREFIX)/bin/ clean: rm -f hello .PHONY: all install clean EOF # 创建源码包 [root@build ~]# cd /tmp [root@build tmp]# tar czf hello-1.0.tar.gz hello-1.0 [root@build tmp]# mv hello-1.0.tar.gz ~/rpmbuild/SOURCES/ # 编写Spec文件 [root@build ~]# cat > ~/rpmbuild/SPECS/hello.spec << 'EOF' Name: hello Version: 1.0 Release: 1%{?dist} Summary: A simple Hello World program License: GPL-3.0 URL: https://fgedu.net.cn/hello Source0: %{name}-%{version}.tar.gz BuildRequires: gcc Requires: glibc %description A simple Hello World program for demonstrating RPM packaging. %prep %setup -q %build make %{?_smp_mflags} %install rm -rf %{buildroot} make install DESTDIR=%{buildroot} %files %doc %{_bindir}/hello %changelog * Fri Apr 04 2026 Admin – 1.0-1
– Initial package
EOF

# 构建RPM包
[root@build ~]# rpmbuild -ba ~/rpmbuild/SPECS/hello.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.xxxxxx
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf hello-1.0
+ /usr/bin/gzip -dc /root/rpmbuild/SOURCES/hello-1.0.tar.gz
+ /usr/bin/tar -xof –
+ STATUS=0
+ ‘[‘ 0 -ne 0 ‘]’
+ cd hello-1.0
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ RPM_EC=0
++ jobs -p
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.xxxxxx
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd hello-1.0
+ make -j4
gcc -Wall -O2 -o hello hello.c
+ RPM_EC=0
++ jobs -p
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.xxxxxx
+ umask 022
+ cd /root/rpmbuild/BUILD
+ ‘[‘ /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64 ‘!=’ / ‘]’
+ rm -rf /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
++ dirname /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
+ mkdir -p /root/rpmbuild/BUILDROOT
+ mkdir /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
+ cd hello-1.0
+ rm -rf /root/rpmbuild/BUILDROOT/hello-1.0-1.更多视频教程www.fgedu.net.cnel9.x86_64
+ make install DESTDIR=/root/rpmbu更多学习教程公众号风哥教程itpux_comild/BUILDROOT/hello-1.0-1.el9.x86_64
install -d /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64/usr/bin
install -m 755 hello /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64/usr/bin/

+ ‘[‘ /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64 ‘!=’ / ‘]’
+ rm -rf /root/rpmbuild/BUIL学习交流加群风哥QQ113257174DROOT/hello-1.from PG视频:www.itpux.com0-1.el9.x86_64
++ dirname /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
+ cd /
+ chmod 0755 /root/rpmbuild/BUILDROOT
+ rm -rf /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
+ RPM_EC=0
++ jobs -p
+ exit 0

Provides: hello = 1.0-1.el9 hello(x86-64) = 1.0-1.el9
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Requires: glibc >= 2.34 libc.so.6()(64bit) libc.so.6(GLIBC_2.2.5)(64bit) rtld(GNU_HASH)

Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/hello-1.0-1.el9.x86_64
Wrote: /root/rpmbuild/SRPMS/hello-1.0-1.el9.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/hello-1.0-1.el9.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.xxxxxx
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd hello-1.0
+ RPM_EC=0
++ jobs -p
+ exit 0

# 查看生成的RPM包
[root@build ~]# ls -la ~/rpmbuild/RPMS/x86_64/
total 12
-rw-r–r–. 1 root root 12345 Apr 4 00:30:00 hello-1.0-1.el9.x86_64.rpm

# 查看RPM包信息
[root@build ~]# rpm -qpi ~/rpmbuild/RPMS/x86_64/hello-1.0-1.el9.x86_64.rpm
Name : hello
Version : 1.0
Release : 1.el9
Architecture: x86_64
Install Date: (not installed)
Group : Unspecified
Size : 12345
License : GPL-3.0
Signature : (none)
Source RPM : hello-1.0-1.el9.src.rpm
Build Date : Fri 04 Apr 2026 00:30:00 CST
Build Host : build.fgedu.net.cn
Relocations : (not relocatable)
URL : https://fgedu.net.cn/hello
Summary : A simple Hello World program
Description :
A simple Hello World program for demonstrating RPM packaging.

# 安装测试
[root@build ~]# dnf install -y ~/rpmbuild/RPMS/x86_64/hello-1.0-1.el9.x86_64.rpm
[root@学习交流加群风哥微信: itpux-combuild ~]# hello
Hello, RPM World!

风哥针对RPM打包建议:

  • 使用rpmdev-setuptree创建标准目录
  • Spec文件遵循标准格式
  • 添加必要的依赖关系
  • 包含变更日志
  • 测试安装和卸载

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息