CentOS 7.3系統下如何製作一個rpm檔案?,centosrpm
我們以一個通用的磁碟檢測軟體sas3ircu為例,來看看如何將一個binary檔案打包成一個rpm檔案,然後再運行 rpm -ivh xxx.rpm 將此rpm檔案安裝到系統中。
使用的作業系統是CentOS 7.3. 步驟如下:
Step 1. 安裝3個必要的rpm包製作軟體
yum install rpm-build yum install rpmrebuild yum install rpmdevtools
Step 2. 運行命令,在目前的目錄下產生rpmbuild目錄
這個目錄就是將來rpm-build命令在打包rpm時所需要的目錄。
rpmdev-setuptree
rpmbuild目錄的內部結構如下:
rpmbuild/├── BUILD├── RPMS├── SOURCES├── SPECS└── SRPMS
Step 3. 寫一個makefile
DESTDIR= dirname=$(DESTDIR)/usr/bin install : sas3ircu test -d $(dirname) || mkdir -p $(dirname) cp sas3ircu $(DESTDIR)/usr/bin/sas3ircu .PHONY : clean clean : rm -f sas3ircu
Step 4. 寫一個spec檔案
Name: sas3ircu Version: 16.0.0 Release: 1 Summary: sas3ircu is a disk check utility Group: Applications/Engineering License: N/A Source0: %{name}-%{version}.tar.gz %description This is a disk check utility. %define debug_package %{nil} %prep %setup %build make %install echo ${RPM_BUILD_ROOT} make DESTDIR=${RPM_BUILD_ROOT} install %files %defattr(-,root,root) /usr/bin/%(name)
Step 5. 查看現在rpmbuild裡的目錄結構
. ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES │ └── sas3ircu-16.0.0 │ ├── makefile │ └── sas3ircu ├── SPECS │ └── sas3ircu.spec └── SRPMS └── sas3ircu-16.0.0-1.src.rpm
Step 6. 將SOURCES檔案夾下的內容打包成 sas3ircu-16.0.0.tar.gz
cd SOURCEStar -cf sas3ircu-16.0.0.tar sas3ircu-16.0.0/gzip sas3ircu-16.0.0.tarrm -rf sas3ircu-16.0.0
現在的rpmbuild的目錄結構是這樣的:
. ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES │ └── sas3ircu-16.0.0.tar.gz ├── SPECS │ └── sas3ircu.spec └── SRPMS
Step 7. 做rpm包
rpmbuild -bb ./rpmbuild/SPECS/sas3ircu.spec
注意,有源檔案的情況下使用-ba選項;沒有源檔案只有二進位檔案,可以使用-bb選項。
該命令的執行返回如下:
[root@host-72 ~]# rpmbuild -bb ./rpmbuild/SPECS/sas3ircu.spec sh: name: command not found Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.XhQG2P + umask 022 + cd /root/rpmbuild/BUILD + cd /root/rpmbuild/BUILD + rm -rf sas3ircu-16.0.0 + /usr/bin/gzip -dc /root/rpmbuild/SOURCES/sas3ircu-16.0.0.tar.gz + /usr/bin/tar -xvvf - drwxrwxr-x finix/finix 0 2018-02-09 21:36 sas3ircu-16.0.0/ -rw-rw-r-- finix/finix 182 2018-02-09 21:31 sas3ircu-16.0.0/makefile -rwxr-xr-x finix/finix 660560 2018-02-09 15:36 sas3ircu-16.0.0/sas3ircu + STATUS=0 + '[' 0 -ne 0 ']' + cd sas3ircu-16.0.0 + /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w . + exit 0 Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.oAnm6z + umask 022 + cd /root/rpmbuild/BUILD + cd sas3ircu-16.0.0 + make test -d /usr/bin || mkdir -p /usr/bin cp sas3ircu /usr/bin/sas3ircu + exit 0 Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.FCXhbk + umask 022 + cd /root/rpmbuild/BUILD + '[' /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 '!=' / ']' + rm -rf /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 ++ dirname /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 + mkdir -p /root/rpmbuild/BUILDROOT + mkdir /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 + cd sas3ircu-16.0.0 + echo /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 + make DESTDIR=/root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 install test -d /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64/usr/bin || mkdir -p /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64/usr/bin cp sas3ircu /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64/usr/bin/sas3ircu + /usr/lib/rpm/check-buildroot + /usr/lib/rpm/redhat/brp-compress + /usr/lib/rpm/redhat/brp-strip /usr/bin/strip + /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump + /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip + /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1 + /usr/lib/rpm/redhat/brp-python-hardlink + /usr/lib/rpm/redhat/brp-java-repack-jars Processing files: sas3ircu-16.0.0-1.x86_64 warning: File listed twice: /usr/bin Provides: sas3ircu = 16.0.0-1 sas3ircu(x86-64) = 16.0.0-1 Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 Wrote: /root/rpmbuild/RPMS/x86_64/sas3ircu-16.0.0-1.x86_64.rpm Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.kKDpcQ + umask 022 + cd /root/rpmbuild/BUILD + cd sas3ircu-16.0.0 + /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/sas3ircu-16.0.0-1.x86_64 + exit 0
Step 8. 查看rpm包是否產生
[root@host-72 ~]# tree rpmbuild/rpmbuild/├── BUILD│ └── sas3ircu-16.0.0│ ├── makefile│ └── sas3ircu├── BUILDROOT├── RPMS│ └── x86_64│ └── sas3ircu-16.0.0-1.x86_64.rpm├── SOURCES│ └── sas3ircu-16.0.0.tar.gz├── SPECS│ └── sas3ircu.spec└── SRPMS
Step 9. 驗證產生的rpm包是否能夠被安裝
首先,要刪除已經被copy到/usr/bin目錄下的sas3ircu檔案: rm -f /usr/bin/sas3ircu
然後,安裝rpm檔案:
[root@host-72 ~]# rpm -ivh ./rpmbuild/RPMS/x86_64/sas3ircu-16.0.0-1.x86_64.rpm Preparing... ################################# [100%] package sas3ircu-16.0.0-1.x86_64 is already installed file / from install of sas3ircu-16.0.0-1.x86_64 conflicts with file from package filesystem-3.2-21.el7.x86_64 file /usr/bin from install of sas3ircu-16.0.0-1.x86_64 conflicts with file from package filesystem-3.2-21.el7.x86_64
此時,發現了如上的錯誤。怎麼辦呢?Google.
Step 10. 解決衝突
rpmrebuild -pe ./rpmbuild/RPMS/x86_64/sas3ircu-16.0.0-1.x86_64.rpm
此命令運行後,會開啟一個編輯器(比如vi),讓你編輯該SPEC內容,其中會有如下的一句,將該句刪除,再退出即可:
# Delete this line %dir %attr(0755, root, root) "/usr/bin"
Step 11. 再次驗證
先刪除在步驟9中已經安裝的rpm的殘餘部分;然後重複步驟9,驗證該rpm檔案是否能夠被成功安裝。
[root@host-72 ~]# rpm -e sas3ircu [root@host-72 ~]# rpm -ivh ./rpmbuild/RPMS/x86_64/sas3ircu-16.0.0-1.x86_64.rpm Preparing... ################################# [100%] Updating / installing... 1:sas3ircu-16.0.0-1 ################################# [100%] [root@host-72 ~]# ls -l /usr/bin/sas3ircu -rwxr-xr-x 1 root root 658632 Feb 9 21:42 /usr/bin/sas3ircu
至此,證明安裝成功,該rpm可用。
最後,關於SPEC檔案的文法還是比較複雜的。如有需要,還要自己多加研究。