rpmbuild 底层原理

Posted 闭关苦炼内功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rpmbuild 底层原理相关的知识,希望对你有一定的参考价值。

Preparing to build RPMs

The main tasks in building RPMs are:

  • Planning what you want to build

    • 应用软件
    • 二进制程序、程序库
    • 一堆系统配置文件
    • 文档包
    • 创建一个二进制RPM或者是一个src.rpm包
      • tar.gz,spec
      • 手动去编译
  • Gathering the software to package

  • Patching the software as needed

  • Creating a reproducible build of the software

  • Planning for upgrades

  • Outlining any dependencies(Spec文件)

  • Building the RPMs

    • Set up the directory structure(禁止使用root用户)

      • BUILD 源代码解压后放的位置,只需要提供这个目录
      • RPMS 制作完成后的rpm包存放目录,里面还有子目录i386,交叉编译,noarch没有平台限制
      • SOURCES 原材料,tar.gz、补丁、配置文件
      • SPECS 存放spec文件,nginx.spec
      • SRPMS src格式的rpm包,src.rpm官方提供
      yum install rpm-build
      
      [root@oe-20-03-LTS ~]# rpmbuild --help
      Usage: rpmbuild [OPTION...]
      
      Build options with [ <specfile> | <tarball> | <source package> ]:
            -bp                        build through %prep (unpack sources and apply patches) from <specfile>
            -bc                        build through %build (%prep, then compile) from <specfile>
            -bi                        build through %install (%prep, %build, then install) from <specfile>
            -bl                        verify %files section from <specfile>
            -ba                        build source and binary packages from <specfile>
            -bb                        build binary package only from <specfile>
            -bs                        build source package only from <specfile>
            -rp                        build through %prep (unpack sources and apply patches) from <source
                                       package>
            -rc                        build through %build (%prep, then compile) from <source package>
            -ri                        build through %install (%prep, %build, then install) from <source package>
            -rl                        verify %files section from <source package>
            -ra                        build source and binary packages from <source package>
            -rb                        build binary package only from <source package>
            -rs                        build source package only from <source package>
            -tp                        build through %prep (unpack sources and apply patches) from <tarball>
            -tc                        build through %build (%prep, then compile) from <tarball>
            -ti                        build through %install (%prep, %build, then install) from <tarball>
            -ta                        build source and binary packages from <tarball>
            -tb                        build binary package only from <tarball>
            -ts                        build source package only from <tarball>
            --rebuild                  build binary package from <source package>
            --recompile                build through %install (%prep, %build, then install) from <source package>
            --buildroot=DIRECTORY      override build root
            --build-in-place           run build in current directory
            --clean                    remove build tree when done
            --nobuild                  do not execute any stages of the build
            --nodeps                   do not verify build dependencies
            --nodirtokens              generate package header(s) compatible with (legacy) rpm v3 packaging
            --noclean                  do not execute %clean stage of the build
            --noprep                   do not execute %prep stage of the build
            --nocheck                  do not execute %check stage of the build
            --rmsource                 remove sources when done
            --rmspec                   remove specfile when done
            --short-circuit            skip straight to specified stage (only for c,i)
            --target=CPU-VENDOR-OS     override target platform
      
      Common options for all rpm modes and executables:
        -D, --define='MACRO EXPR'      define MACRO with value EXPR
            --undefine=MACRO           undefine MACRO
        -E, --eval='EXPR'              print macro expansion of EXPR
            --target=CPU-VENDOR-OS     Specify target platform
            --macros=<FILE:...>        read <FILE:...> instead of default file(s)
            --noplugins                don't enable any plugins
            --nodigest                 don't verify package digest(s)
            --nosignature              don't verify package signature(s)
            --rcfile=<FILE:...>        read <FILE:...> instead of default file(s)
        -r, --root=ROOT                use ROOT as top level directory (default: "/")
            --dbpath=DIRECTORY         use database in DIRECTORY
            --querytags                display known query tags
            --showrc                   display final rpmrc and macro configuration
            --quiet                    provide less detailed output
        -v, --verbose                  provide more detailed output
            --version                  print the version of rpm being used
      
      Options implemented via popt alias/exec:
            --with=<option>            enable configure <option> for build
            --without=<option>         disable configure <option> for build
            --buildpolicy=<policy>     set buildroot <policy> (e.g. compress man pages)
            --sign                     generate GPG signature (deprecated, use command rpmsign instead)
            --nodebuginfo              do not generate debuginfo for this package
      
      Help options:
        -?, --help                     Show this help message
            --usage                    Display brief usage message
      [root@oe-20-03-LTS ~]#
      

      rpm宏查看 rpmbuild --showrc

      [root@oe-20-03-LTS ~]# rpmbuild --showrc
      ARCHITECTURE AND OS:
      build arch            : aarch64
      compatible build archs: aarch64 noarch
      build os              : Linux
      compatible build os's : Linux
      install arch          : aarch64
      install os            : Linux
      compatible archs      : aarch64 aarch64_ilp32 noarch
      compatible os's       : Linux
      
      RPMRC VALUES:
      archcolor             : 2
      optflags              : -O2 -g
      
      Features supported by rpmlib:
          rpmlib(BuiltinLuaScripts) = 4.2.2-1
          rpmlib(CompressedFileNames) = 3.0.4-1
          rpmlib(ConcurrentAccess) = 4.1-1
          rpmlib(ExplicitPackageProvide) = 4.0-1
          rpmlib(FileCaps) = 4.6.1-1
          rpmlib(FileDigests) = 4.6.0-1
          rpmlib(HeaderLoadSortsTags) = 4.0.1-1
          rpmlib(LargeFiles) = 4.12.0-1
          rpmlib(PartialHardlinkSets) = 4.0.4-1
          rpmlib(PayloadFilesHavePrefix) = 4.0-1
          rpmlib(PayloadIsBzip2) = 3.0.5-1
          rpmlib(PayloadIsLzma) = 4.4.2-1
          rpmlib(PayloadIsXz) = 5.2-1
          rpmlib(PayloadIsZstd) = 5.4.18-1
          rpmlib(RichDependencies) = 4.12.0-1
          rpmlib(ScriptletExpansion) = 4.9.0-1
          rpmlib(ScriptletInterpreterArgs) = 4.0.3-1
          rpmlib(TildeInVersions) = 4.10.0-1
          rpmlib(VersionedDependencies) = 3.0.3-1
      
      Macro path: /usr/lib/rpm/macros:/usr/lib/rpm/macros.d/macros.*:/usr/lib/rpm/platform/%{_target}/macros:/usr/l                                                           ib/rpm/fileattrs/*.attr:/usr/lib/rpm/redhat/macros:/etc/rpm/macros.*:/etc/rpm/macros:/etc/rpm/%{_target}/macr                                                           os:~/.rpmmacros
      
      ========================
      -13: __7zip     /usr/bin/7za
      -13: ___build_args      -e
      -13: ___build_cmd       %{?_sudo:%{_sudo} }%{?_remsh:%{_remsh} %{_remhost} }%{?_remsudo:%{_remsudo} }%{?_remc                                                           hroot:%{_remchroot} %{_remroot} }%{___build_shell} %{___build_args}
      -13: ___build_post      exit $?
      -13: ___build_pre
        RPM_SOURCE_DIR="%{u2p:%{_sourcedir}}"
        RPM_BUILD_DIR="%{u2p:%{_builddir}}"
        RPM_OPT_FLAGS="%{optflags}"
        RPM_LD_FLAGS="%{?__global_ldflags}"
        RPM_ARCH="%{_arch}"
        RPM_OS="%{_os}"
        export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_LD_FLAGS RPM_ARCH RPM_OS
        RPM_DOC_DIR="%{_docdir}"
        export RPM_DOC_DIR
        RPM_PACKAGE_NAME="%{NAME}"
        RPM_PACKAGE_VERSION="%{VERSION}"
        RPM_PACKAGE_RELEASE="%{RELEASE}"
        export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE
        LANG=C
        export LANG
        unset CDPATH DISPLAY ||:
        %{?buildroot:RPM_BUILD_ROOT="%{u2p:%{buildroot}}"
        export RPM_BUILD_ROOT}
        %{?_javaclasspath:CLASSPATH="%{_javaclasspath}"
        export CLASSPATH}
        PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:%{_libdir}/pkgconfig:%{_datadir}/pkgconfig"
        export PKG_CONFIG_PATH
        CONFIG_SITE=${CONFIG_SITE:-NONE}
        export CONFIG_SITE
      
        %{verbose:set -x}%{!verbose:exec > /dev/null}
        umask 022
        cd "%{u2p:%{_builddir}}"
      -13: ___build_shell     %{?_buildshell:%{_buildshell}}%{!?_buildshell:/bin/sh}
      -13: ___build_template  #!%{___build_shell}
      %{___build_pre}
      %{nil}
      -13: __aclocal  aclocal
      -13: __ar       ar
      -13: __arch_install_post        %{nil}
      -13: __as       as
      -13: __autoconf autoconf
      -13: __autoheader       autoheader
      -13: __automake automake
      -13: __awk      gawk
      -13: __brp_boot_efi_times       /usr/lib/rpm/brp-boot-efi-times
      -13: __brp_compress     %{_rpmconfigdir}/brp-compress
      -13: __brp_java_gcjcompile      %{_rpmconfigdir}/brp-java-bytecompile
      -13: __brp_python_bytecompile   %{_rpmconfigdir}/brp-python-bytecompile "" "%{?_python_bytecompile_errors_ter                                                           minate_build}" "%{?_python_bytecompile_extra}"
      -13: __brp_strip        %{_rpmconfigdir}/brp-strip %{__strip}
      -13: __brp_strip_comment_note   %{_rpmconfigdir}/brp-strip-comment-note %{__strip} %{__objdump}
      -13: __brp_strip_shared %{_rpmconfigdir}/brp-strip-shared
      -13: __brp_strip_static_archive %{_rpmconfigdir}/brp-strip-static-archive %{__strip}
      -13: __bzip2    /usr/bin/bzip2
      -13: __bzr      /usr/bin/bzr
      -13: __cat      /usr/bin/cat
      -13: __cc       gcc
      -13: __check_files      %{_rpmconfigdir}/check-files %{buildroot}
      -13: __chgrp    /usr/bin/chgrp
      -13: __chmod    /usr/bin/chmod
      -13: __chown    /usr/bin/chown
      -13: __cmake    /usr/bin/cmake
      -13: __cmake_path       ^(%{_libdir}|%{_datadir})/cmake/.*/.*(Config.cmake|-config.cmake)$
      -13: __cmake_provides   %{_rpmconfigdir}/cmake.prov
      -13: __cmake_requires   %{_rpmconfigdir}/cmake.req
      -13: __cp       /usr/bin/cp
      -13: __cpio     /usr/bin/cpio
      -13: __cpp      gcc -E
      -13: __cxx      g++
      

      vi .rpmmacros

      %{_top_dir} /…

      mkdir -pv rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

      rpmbuild --showrc | grep _topdir

    • Place the sources in the right directory

    • Create a spec file that tells the rpmbuild command what to do

      .spec格式文件

      rpm -qi httpd

      rrdtool

      introduction section

      • Summary:

      • Name: 名字

      • Version: 版本2.4.2,不能使用横线

      • Release: 1%{?dist} 宏,制作者,rpm发型号

      • License: GPLv2,版权信息,正确版权信息

      • Group: 属于哪个组,事先定义好的,/usr/share/doc/rpm-4.4.2.3/GROUPS

      • URL: 制作者,从哪可以下载到,获取到,源码包下载路径

      • Packager: 制作者,制作者邮件地址

      • Vendor: 提供者,公司,开发者

      • Source: 下载链接找到这个软件包,绝对不是去下载

      • BuildRoot:

        • SOURCES目录源码包

        • .spec写好文件

        • BUILD目录

        • BUILDROOT目录当做根来用,编译好的程序安装到这个目录

          %{_tmppath}/%{name}-%{version}-%{release}-root

      • BuildRequires: 制作过程依赖哪些软件包

        用于定义依赖关系的,制作过程依赖哪些软件安装包的

      • %description: 描述信息

        多个自然段空行隔开

      prep section 准备阶段 (不可少)

      • %prep
      • %setup自己解压缩

      build section

      • %build
      • %{__make} %{?__smp_mflags}

      install section 安装阶段(不可少)

      • %install

        %{__rm} -rf %{buildroot}

        %{__make} install DESTDIR="%{buildroot}"

        %find_lang %{name}

      clean section

      • %clean
      • %{__rm} -rf %{buildroot}

      脚本段:

      httpd

      %pre

      %post

      %preun

      %postun

      files section 文件段(files段 列出的文件必须要一一对应BUILDROOT目录下的每一个文件,不能多也不能少)

      %files -f %{name}.lang
      %defattr(-, root, root, 0755)
      %doc API CHANGES COPYING CREDITS README axelrc.example
      %doc %{_mandir}/main/axel.l*
      %doc %{_mandir}/*/main/axel.l*
      %config %{_sysconfdir}/axelrc
      /usr/local/bin/axel
      %attr(0755, root, root) /etc/rc.d/init.d/nginx
      

      change log section 日志段,修改日志,改变日志段

      %changelog
      * Wed Apr 11 2012 xxx.com <1@admin.com> - 1.0.14-1
      - 注释 Initial version
      
      
    • Build the source and binary RPMs

      rpmbuild command

      -bp 执行到 %prep

      -bi 执行到 %install

      -bc 执行到 %build

      -bb 执行到 制作二进制格式的rpm包

      -bs 执行到 制作源码格式的rpm包

      -ba 执行到 all,二进制格式的、源码格式的都制作

      -bl 执行到 制作完成后,检测哪些文件在BUILDROOT目录下存在安装生成,但是却在制作rpm过程中在files段中没有给它包含到rpm包中,会帮忙检查报错,安装了却没包含rpm包会报错,包含进rpm包却没安装生成也会报错,双方必须要一一对应。

  • Testing the RPMs

rpmbuild -bp nginx.spec
如果报缺少依赖关系,yum给它装上

执行build阶段
rpmbuild -bc nginx.spec

执行install安装阶段
rpmbuild -bi nginx.spec
安装报错,没有文件,自己建立没有文件

制作所有包
执行build all安装阶段
rpmbuild -ba nginx.spec
到RPMS目录


每一个RPM包都有一种capability能力

自身名字、提供的文件

编译依赖、安装依赖

如果有src.rpm包,直接 rpmbuild --rebuild 去制作,不用root用户

展开src.rpm包

rpm2cpio nginx-1.0.14-3.src.rpm | cpio -id

寻找rpm包的两个常用站点

rpmfind.net

rpmpone.com

https://www.bilibili.com/video/BV1ai4y1N7gp?p=2

以上是关于rpmbuild 底层原理的主要内容,如果未能解决你的问题,请参考以下文章

js根据类名获取元素的底层原理

如何让 rpmbuild 下载特定 .spec 的所有源?

linux之使用rpmbuild打rpm包

图解 Google V8 # 05:函数表达式的底层工作机制

rpmbuild 实践

rpmbuild打包出错处理