rpmbuild 底层原理

临文乍了了,彻卷兀若无。这篇文章主要讲述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用户)
  • ??yum install rpm-build ????[root@oe-20-03-LTS ~]# rpmbuild --help Usage: rpmbuild [OPTION...]Build options with [ < specfile> | < tarball> | < source package> ]: -bpbuild through %prep (unpack sources and apply patches) from < specfile> -bcbuild through %build (%prep, then compile) from < specfile> -bibuild through %install (%prep, %build, then install) from < specfile> -blverify %files section from < specfile> -babuild source and binary packages from < specfile> -bbbuild binary package only from < specfile> -bsbuild source package only from < specfile> -rpbuild through %prep (unpack sources and apply patches) from < source package> -rcbuild through %build (%prep, then compile) from < source package> -ribuild through %install (%prep, %build, then install) from < source package> -rlverify %files section from < source package> -rabuild source and binary packages from < source package> -rbbuild binary package only from < source package> -rsbuild source package only from < source package> -tpbuild through %prep (unpack sources and apply patches) from < tarball> -tcbuild through %build (%prep, then compile) from < tarball> -tibuild through %install (%prep, %build, then install) from < tarball> -tabuild source and binary packages from < tarball> -tbbuild binary package only from < tarball> -tsbuild source package only from < tarball> --rebuildbuild binary package from < source package> --recompilebuild through %install (%prep, %build, then install) from < source package> --buildroot=DIRECTORYoverride build root --build-in-placerun build in current directory --cleanremove build tree when done --nobuilddo not execute any stages of the build --nodepsdo not verify build dependencies --nodirtokensgenerate package header(s) compatible with (legacy) rpm v3 packaging --nocleando not execute %clean stage of the build --noprepdo not execute %prep stage of the build --nocheckdo not execute %check stage of the build --rmsourceremove sources when done --rmspecremove specfile when done --short-circuitskip straight to specified stage (only for c,i) --target=CPU-VENDOR-OSoverride target platformCommon options for all rpm modes and executables: -D, --define=MACRO EXPRdefine MACRO with value EXPR --undefine=MACROundefine MACRO -E, --eval=EXPRprint macro expansion of EXPR --target=CPU-VENDOR-OSSpecify target platform --macros=< FILE:...> read < FILE:...> instead of default file(s) --nopluginsdont enable any plugins --nodigestdont verify package digest(s) --nosignaturedont verify package signature(s) --rcfile=< FILE:...> read < FILE:...> instead of default file(s) -r, --root=ROOTuse ROOT as top level directory (default: "/") --dbpath=DIRECTORYuse database in DIRECTORY --querytagsdisplay known query tags --showrcdisplay final rpmrc and macro configuration --quietprovide less detailed output -v, --verboseprovide more detailed output --versionprint the version of rpm being usedOptions 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) --signgenerate GPG signature (deprecated, use command rpmsign instead) --nodebuginfodo not generate debuginfo for this packageHelp options: -?, --helpShow this help message --usageDisplay 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 oss : Linux install arch: aarch64 install os: Linux compatible archs: aarch64 aarch64_ilp32 noarch compatible oss: LinuxRPMRC VALUES: archcolor: 2 optflags: -O2 -gFeatures 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-1Macro path: /usr/lib/rpm/macros:/usr/lib/rpm/macros.d/macros.*:/usr/lib/rpm/platform/%_target/macros:/usr/lib/rpm/fileattrs/*.attr:/usr/lib/rpm/redhat/macros:/etc/rpm/macros.*:/etc/rpm/macros:/etc/rpm/%_target/macros:~/.rpmmacros======================== -13: __7zip/usr/bin/7za -13: ___build_args-e -13: ___build_cmd%?_sudo:%_sudo %?_remsh:%_remsh %_remhost %?_remsudo:%_remsudo %?_remchroot:%_remchroot %_remroot %___build_shell %___build_args -13: ___build_postexit $? -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: __aclocalaclocal -13: __arar -13: __arch_install_post%nil -13: __asas -13: __autoconf autoconf -13: __autoheaderautoheader -13: __automake automake -13: __awkgawk -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_terminate_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: __ccgcc -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: __cppgcc -E -13: __cxxg++ ??vi .rpmmacros
    %_top_dir /…
    mkdir -pv rpmbuild/BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS
    rpmbuild --showrc | grep _topdir

  • BUILD 源代码解压后放的位置,只需要提供这个目录
  • RPMS 制作完成后的rpm包存放目录,里面还有子目录i386,交叉编译,noarch没有平台限制
  • SOURCES 原材料,tar.gz、补丁、配置文件
  • SPECS 存放spec文件,nginx.spec
  • SRPMS src格式的rpm包,src.rpm官方提供

  • Place the sources in the right directory
  • Create a spec file that tells the rpmbuild command what to do.spec格式文件rpm -qi httpdrrdtoolintroduction section
  • prep section 准备阶段 (不可少)
  • build section
  • install section 安装阶段(不可少)
  • clean section
  • 脚本段:
    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


  • ??%clean??
  • ??%__rm -rf %buildroot??

  • 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
你懂的:??b站快速通道??


【rpmbuild 底层原理】


    推荐阅读