欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何制作一个rpm文件

程序员文章站 2022-05-29 11:08:54
...

我们以一个通用的磁盘检测软件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 SOURCES
tar -cf sas3ircu-16.0.0.tar sas3ircu-16.0.0/
gzip sas3ircu-16.0.0.tar
rm -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选项。

该命令的执行返回如下:

    [[email protected]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包是否生成

[[email protected] ~]# 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文件:

    [[email protected]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文件的语法还是比较复杂的。如有需要,还要自己多加研究。

相关标签: rpm