RPM 套件是 Red Hat 系軟體發佈標準格式。本文示範從原始碼目錄建立 test-1.0 簡單套件的完整步驟:建立目錄樹、編寫 Shell 腳本與 README、產生 .tar.gz 源碼包、撰寫 .spec 規範檔、rpmbuild 編譯生成 .rpm、安裝驗證。

cd /usr/src/redhat
mkdir -p test-1.0
cd test-1.0
echo 'echo this is a test' > test.sh
chmod +x test.sh
echo 'just launch the script' > README
# 一般用戶操作
mkdir -p ~/rpmbuild/{SOURCES,SPECS}
tar -czf ~/rpmbuild/SOURCES/test-1.0.tar.gz test-1.0
# ~/rpmbuild/SPECS/test.spec
Summary: Simple test package
Name: test
Version: 1.0
Release: 1%{?dist}
License: GPL
Source0: %{name}-%{version}.tar.gz
%description
A simple test RPM package.
%prep
%setup -q
%build
# 無編譯步驟
%install
rm -rf $RPM_BUILD_ROOT
install -D -m 0755 test.sh $RPM_BUILD_ROOT/usr/bin/test.sh
install -D -m 0644 README $RPM_BUILD_ROOT/usr/share/doc/test-1.0/README
%files
/usr/bin/test.sh
/usr/share/doc/test-1.0/README
%changelog
* Mon Jan 01 2024 Builder - 1.0-1
- Initial package
rpmbuild -ba ~/rpmbuild/SPECS/test.spec
# 產出:~/rpmbuild/RPMS/x86_64/test-1.0-1.el7.x86_64.rpm
# ~/rpmbuild/SRPMS/test-1.0-1.el7.src.rpm
sudo rpm -ivh ~/rpmbuild/RPMS/x86_64/test-1.0-1.el7.x86_64.rpm
test.sh # 應輸出 "this is a test"
rpm -ql test # 列出安裝檔案
rpm -e test # 移除套件
%pre/%post/%preun/%postun:安裝/移除前後鉤子。%config(noreplace):標記設定檔,升級不覆蓋用戶修改。Requires:、Provides:、Obsoletes::相依性控制。rpmlint 檢查 Spec 常見錯誤。掌握 RPM 打包流程,即可將內部工具、腳本、編譯好二進制檔標準化分發至所有 RHEL/CentOS/Fedora 主機。