Linux 之旅【番外】:SRPM
图源:pexels
SRPM的使用
在Linux 之旅 22:使用RPM与YUM安装软件中我们讨论了RPM如何使用,但并没有说明SRPM应该如何使用,这里算是做一个收尾。
安装
与RPM不同,使用SRPM时所用的命令是rpmbuild
。
比较重要和常用的参数有:
--rebuild
:将SRMP软件包进行编译并打包,会产生一个RMP软件包。--recompile
:将SRMP软件包进行编译、打包后安装。
如果相关操作执行成功,将会自动删除编译过程中产生的中间数据。
下面通过一个示例来说明:
[root@xyz rpmbuild]# cd /tmp
[root@xyz tmp]# wget -4 http://vault.centos.org/7.1.1503/updates/Source/SPackages/ntp-4.2.6p5-19.el7.centos.1.src.rpm
--2021-09-21 17:38:29-- http://vault.centos.org/7.1.1503/updates/Source/SPackages/ntp-4.2.6p5-19.el7.centos.1.src.rpm
正在解析主机 vault.centos.org (vault.centos.org)... 54.186.51.210
正在连接 vault.centos.org (vault.centos.org)|54.186.51.210|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 301 Moved Permanently
位置:https://vault.centos.org/7.1.1503/updates/Source/SPackages/ntp-4.2.6p5-19.el7.centos.1.src.rpm [跟随至新的 URL]
--2021-09-21 17:38:29-- https://vault.centos.org/7.1.1503/updates/Source/SPackages/ntp-4.2.6p5-19.el7.centos.1.src.rpm
正在连接 vault.centos.org (vault.centos.org)|54.186.51.210|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:4285870 (4.1M) [application/x-rpm]
正在保存至: “ntp-4.2.6p5-19.el7.centos.1.src.rpm”
100%[===========================================================================================>] 4,285,870 1.14MB/s 用时 3.6s
2021-09-21 17:38:34 (1.14 MB/s) - 已保存 “ntp-4.2.6p5-19.el7.centos.1.src.rpm” [4285870/4285870])
[root@xyz tmp]# rpmbuild --rebuild ntp-4.2.6p5-19.el7.centos.1.src.rpm
正在安装 ntp-4.2.6p5-19.el7.centos.1.src.rpm
警告:用户mockbuild 不存在 - 使用root
警告:群组mockbuild 不存在 - 使用root
...省略
警告:用户mockbuild 不存在 - 使用root
警告:群组mockbuild 不存在 - 使用root
错误:构建依赖失败:
libcap-devel 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
libedit-devel 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
perl-HTML-Parser 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
pps-tools-devel 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
autogen 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
autogen-libopts-devel 被 ntp-4.2.6p5-19.el7.1.x86_64 需要
这里是我从CentOS官网上挑选的一个小工具,用来演示。第一次构建出现错误,提示缺少相关工具,安装相应工具后再次尝试:
[root@xyz tmp]# yum install libcap-devel libedit-devel perl-HTML-Parser pps-tools-devel autogen autogen-libopts-devel -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
...省略
[root@xyz tmp]# rpmbuild --rebuild ntp-4.2.6p5-19.el7.centos.1.src.rpm
正在安装 ntp-4.2.6p5-19.el7.centos.1.src.rpm
警告:用户mockbuild 不存在 - 使用root
警告:群组mockbuild 不存在 - 使用root
警告:用户mockbuild 不存在 - 使用root
...省略
执行(%clean): /bin/sh -e /var/tmp/rpm-tmp.BUxGYy
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd ntp-4.2.6p5
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/ntp-4.2.6p5-19.el7.centos.1.x86_64
+ exit 0
执行(--clean): /bin/sh -e /var/tmp/rpm-tmp.5Cix7t
+ umask 022
+ cd /root/rpmbuild/BUILD
+ rm -rf ntp-4.2.6p5
+ exit 0
可以看到,成功使用SRPM软件包中的源码生成了编译后的RPM软件包。
通常来说我们并不需要使用SRPM来重新编译以生成RPM包,但是如果厂商提供的RPM包无法满足我们的需要,比如我们要修改某个配置或者源码以满足特定的需要,就需要下载SRPM包后,在修改相应的配置文件或源码后执行上边的操作。
SRPM包相关目录
SRPM包在使用过程中会使用下面的目录:
需要注意的是SRPM使用的目录是与当前用户相关的,这里均以
root
用户为准。
/root/rpmbuild/
BUILD
:编译软件时产生的中间文件。BUILDROOT
:软件的工程目录。RPMS
:编译产生的RPM包。SOURCES
:存放软件源码。SPECS
:存放软件的打包配置文件(打RPM包时候会用到的配置文件)。SRPMS
:SRPM包。
此外,如果在编译过程中出现错误,会将错误信息写入/tmp
中,可以使用该错误日志进行debug。
下面解包SRPM软件包。因为之前使用rpmbuild
相关命令在解压编译完成后会自动删除相关的源码以及.spec
文件,所以我们这里要使用rpm -i
命令来解包SRPM
,该操作对于SRPM软件包来说只会将其中的源码和SPEC文件解压到当前目录,而并不会执行任何安装操作(这与对待RPM软件包完全不同):
[root@xyz rpmbuild]# cd ~
[root@xyz ~]# cp /tmp/ntp-4.2.6p5-19.el7.centos.1.src.rpm .
[root@xyz ~]# rpm -ivh ntp-4.2.6p5-19.el7.centos.1.src.rpm
正在升级/安装...
1:ntp-4.2.6p5-19.el7.centos.1 ################################# [100%]
警告:用户mockbuild 不存在 - 使用root
警告:群组mockbuild 不存在 - 使用root
警告:用户mockbuild 不存在 - 使用root
...省略
[root@xyz ~]# rpm -q ntp
未安装软件包 ntp
[root@xyz ~]# ll ./rpmbuild/SPECS/
总用量 152
-rw-rw-r--. 1 root root 41422 6月 24 2015 ntp.spec
-rw-rw-r--. 1 root root 110562 11月 16 2020 python3.spec
[root@xyz ~]# ll ./rpmbuild/SOURCES/*ntp*
-rw-rw-r--. 1 root root 559 6月 24 2015 ./rpmbuild/SOURCES/ntp-4.2.4p7-getprecision.patch
-rw-rw-r--. 1 root root 661 6月 24 2015 ./rpmbuild/SOURCES/ntp-4.2.6p1-cmsgalign.patch
-rw-rw-r--. 1 root root 765 6月 24 2015 ./rpmbuild/SOURCES/ntp-4.2.6p1-linkfastmath.patch
-rw-rw-r--. 1 root root 391 6月 24 2015 ./rpmbuild/SOURCES/ntp-4.2.6p1-retcode.patch
-rw-rw-r--. 1 root root 12358 6月 24 2015 ./rpmbuild/SOURCES/ntp-4.2.6p1-sleep.patch
...省略
-rw-rw-r--. 1 root root 260 6月 24 2015 ./rpmbuild/SOURCES/ntp-wait.service
-rw-rw-r--. 1 root root 324 6月 24 2015 ./rpmbuild/SOURCES/sntp.service
-rw-rw-r--. 1 root root 58 6月 24 2015 ./rpmbuild/SOURCES/sntp.sysconfig
打包配置文件
之前说过SPECS
目录下存放的是软件的打包配置文件,下面我们来看该文件的内容:
[root@xyz ~]# cat ./rpmbuild/SPECS/ntp.spec
Summary: The NTP daemon and utilities
Name: ntp
Version: 4.2.6p5
Release: 19%{
?dist}.1
# primary license (COPYRIGHT) : MIT
# ElectricFence/ (not used) : GPLv2
# kernel/sys/ppsclock.h (not used) : BSD with advertising
# include/ntif.h (not used) : BSD
# include/rsa_md5.h : BSD with advertising
# include/ntp_rfc2553.h : BSD with advertising
# lib/isc/commandline.c (not used) : BSD with advertising
# lib/isc/inet_aton.c (not used) : BSD with advertising
# lib/isc/strtoul.c (not used) : BSD with advertising
# lib/isc/unix/file.c : BSD with advertising
# lib/isc/inet_aton.c (not used) : BSD with advertising
# libntp/mktime.c : BSD with advertising
# libntp/ntp_random.c : BSD with advertising
# libntp/memmove.c : BSD with advertising
# libntp/ntp_rfc2553.c : BSD with advertising
# libntp/adjtimex.c (not used) : BSD
# libparse/ : BSD
# ntpd/refclock_jjy.c: MIT
# ntpd/refclock_oncore.c : BEERWARE License (aka, Public Domain)
# ntpd/refclock_palisade.c : BSD with advertising
# ntpd/refclock_jupiter.c : BSD with advertising
# ntpd/refclock_mx4200.c : BSD with advertising
# ntpd/refclock_palisade.h : BSD with advertising
# ntpstat-0.2/ : GPLv2
# sntp/libopts/ (not used) : BSD or GPLv3+
# util/ansi2knr.c (not used) : GPL+
License: (MIT and BSD and BSD with advertising) and GPLv2
Group: System Environment/Daemons
Source0: http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-%{
version}.tar.gz
Source1: ntp.conf
Source2: ntp.keys
Source4: ntpd.sysconfig
# http://people.redhat.com/rkeech/#ntpstat
Source5: ntpstat-0.2.tgz
S