RPM包构建、测试与文件共享全解析
立即解锁
发布时间: 2025-09-11 01:35:54 阅读量: 345 订阅数: 22 AIGC 

# RPM 包构建、测试与文件共享全解析
## 1. RPM 包构建基础
### 1.1 各部分功能概述
RPM 包构建涉及多个部分,每个部分都有其特定功能:
- **%description**:用于描述 RPM 包,可长可短,示例如下:
```plaintext
%description
A collection of utility scripts for testing RPM creation.
```
- **%prep**:构建过程中首个执行的脚本,用于准备构建目录,将所需文件复制到相应位置。示例脚本如下:
```plaintext
%prep
#########################################################################
# Create the build tree and copy the files from the development
# directories into the build tree.
#########################################################################
echo "BUILDROOT = $RPM_BUILD_ROOT"
mkdir -p $RPM_BUILD_ROOT/usr/local/bin/
mkdir -p $RPM_BUILD_ROOT/usr/local/share/utils
cp /home/student/development/utils/scripts/* $RPM_BUILD_ROOT/usr/local/bin
cp /home/student/development/utils/license/* $RPM_BUILD_ROOT/usr/local/share/utils
cp /home/student/development/utils/spec/* $RPM_BUILD_ROOT/usr/local/share/utils
exit
```
- **%files**:定义要安装的文件及其在目录树中的位置,还可指定文件属性、所有者和所属组。示例如下:
```plaintext
%files
%attr(0744, root, root) /usr/local/bin/*
%attr(0644, root, root) /usr/local/share/utils/*
```
- **%pre**:可放置在 RPM 安装文件前需要运行的脚本,示例中该部分为空。
- **%post**:安装文件后运行的脚本,可执行创建文件、运行系统命令、重启服务等操作。示例脚本如下:
```plaintext
%post
######################################################################
# Set up MOTD scripts
######################################################################
cd /etc
# Save the old MOTD if it exists
if [ -e motd ]
then
cp motd motd.orig
fi
# If not there already, Add link to create_motd to cron.daily
cd /etc/cron.daily
if [ ! -e create_motd ]
then
ln -s /usr/local/bin/create_motd
fi
# create the MOTD for the first time
/usr/local/bin/mymotd > /etc/motd
```
- **%postun**:RPM 包卸载后运行的脚本,用于清理 %post 部分创建的文件或链接。示例脚本如下:
```plaintext
%postun
# remove installed files and links
rm /etc/cron.daily/create_motd
# Restore the original MOTD if it was backed up
if [ -e /etc/motd.orig ]
then
mv -f /etc/motd.orig /etc/motd
fi
```
- **%clean**:RPM 构建过程结束后执行的清理脚本,用于删除构建目录。示例如下:
```plaintext
%clean
rm -rf $RPM_BUILD_ROOT/usr/local/bin
rm -rf $RPM_BUILD_ROOT/usr/local/share/utils
```
- **%changelog**:可选部分,记录 RPM 及其包含文件的更改历史。示例如下:
```plaintext
%changelog
* Wed Aug 29 2018 Your Name <[email protected]>
- The original package includes several useful scripts. it is
primarily intended to be used to illustrate the process of
building an RPM.
```
### 1.2 构建 RPM 包步骤
构建 RPM 包的具体步骤如下:
1. 确保 spec 文件位于 rpmbuild 树的 SPECS 目录中,可创建链接方便编辑:
```plaintext
[student@studentvm1 ~]# cd ~/rpmbuild/SPECS/
[student@studentvm1 ~]# ln -s ~/development/spec/utils.spec ; ll
total 0
lrwxrwxrwx 1 student student 41 Aug 31 11:43 utils.spec -> /home/student/development/spec/utils.spec
```
2. 运行以下命令构建 RPM 包:
```plaintext
[student@studentvm1 ~]# rpmbuild --target noarch -bb utils.spec
```
3. 检查构建结果,验证新 RPM 是否存在于 `~/rpmbuild/RPMS/noarch` 目录中:
```plaintext
[student@studentvm1 SPECS]$ cd ~/rpmbuild/RPMS/noarch/ ; ll
total 24
-rw-rw-r-- 1 student student 24372 Aug 31 11:45 utils-1.0.0-1.noarch.rpm
```
### 1.3 RPM 包构建流程
```mermaid
graph LR
A[准备 spec 文件] --> B[创建链接到 SPECS 目录]
B --> C[运行 rpmbuild 命令构建]
C --> D[检查构建结果]
```
##
0
0
复制全文
相关推荐










