In CentOS 7.3, how does one create an rpm file ?, Centosrpm
Take a general disk detection software sas3ircu as an example to see how to package a binary file into an rpm file and then run rpm-ivh xxx. rpm installs the rpm file to the system.
CentOS 7.3 is used. The steps are as follows:
Step 1. install three necessary rpm package production software
yum install rpm-build yum install rpmrebuild yum install rpmdevtools
Step 2. Run the command to generate the rpmbuild directory in the current directory
This directory is the directory required by the rpm-build command in the future during rpm packaging.
rpmdev-setuptree
The internal structure of the rpmbuild directory is as follows:
rpmbuild/├── BUILD├── RPMS├── SOURCES├── SPECS└── SRPMS
Step 3. Write a 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. Write a spec File
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. view the directory structure in rpmbuild
. ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES │ └── sas3ircu-16.0.0 │ ├── makefile │ └── sas3ircu ├── SPECS │ └── sas3ircu.spec └── SRPMS └── sas3ircu-16.0.0-1.src.rpm
Step 6. Package the content in the SOURCES folder into a 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
The directory structure of rpmbuild is as follows:
. ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES │ └── sas3ircu-16.0.0.tar.gz ├── SPECS │ └── sas3ircu.spec └── SRPMS
Step 7. Create an rpm package
rpmbuild -bb ./rpmbuild/SPECS/sas3ircu.spec
Note that the-ba option is used when a source file exists. If no source file exists, only the binary file is used. You can use the-bb option.
The Command Execution returns the following:
[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. Check whether the rpm package is generated
[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. Verify that the generated rpm package can be installed
First, delete the sas3ircu file that has been copied to the/usr/bin directory: rm-f/usr/bin/sas3ircu
Then, install the rpm file:
[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
The above error is found. What should we do? Google.
Step 10. resolve the conflict
rpmrebuild -pe ./rpmbuild/RPMS/x86_64/sas3ircu-16.0.0-1.x86_64.rpm
After this command is run, an editor (such as vi) will be opened for you to edit the SPEC content. The following sentence will be displayed: Delete the sentence and exit:
# Delete this line %dir %attr(0755, root, root) "/usr/bin"
Step 11. Verify again
First, delete the remaining rpm parts that have been installed in Step 9. Then, repeat Step 9 to verify that the rpm file can be successfully installed.
[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
This indicates that the installation is successful and the rpm is available.
Finally, the syntax for SPEC files is complex. If necessary, you need to study more on your own.