runsisi's

technical notes

rpm 基础操作

2019-02-01 runsisi#rpm

在 RHEL/CentOS 环境下,我们常见的 rpm 相关的工具主要有两个:rpm 和 yum,这里简单介绍一下日常的一些操作。

列出当前系统所有已安装的 rpm 包:

~$ rpm -qa | grep nbd
nbd-3.11-1.1.el7.x86_64
rbd-nbd-12.2.2-16.55.g3edebbb.el7.x86_64

查询 rpm 包的基本信息:

~$ rpm -qi nbd
Name        : nbd
Version     : 3.11
Release     : 1.1.el7
Architecture: x86_64
Install Date: Tue 19 Jun 2018 03:42:44 PM CST
Group       : Unspecified
Size        : 153137
License     : GPL+
Signature   : (none)
Source RPM  : nbd-3.11-1.1.el7.src.rpm
Build Date  : Tue 22 May 2018 05:39:11 PM CST
Build Host  : localhost
Relocations : (not relocatable)
URL         : http://nbd.sourceforge.net
Summary     : Network Block Device user-space tools (TCP version)
Description :
Tools for the Linux Kernel's network block device, allowing you to use
remote block devices over a TCP/IP network.

查询 rpm 包的历史安装、移除信息:

~$ sudo yum history info nbd
Loaded plugins: fastestmirror, priorities
Transaction ID : 88
Begin time     : Fri Jun  1 02:52:10 2018
Begin rpmdb    : 560:a0c9108e28969f97131a1e3dde3d7bdef8e82641
End time       :            02:52:11 2018 (1 seconds)
End rpmdb      : 561:81e82131bef6a22c084eeca81cdbf1e08aa50cc0
User           : System <unset>
Return-Code    : Success
Command Line   : -y --disablerepo=* --enablerepo=TFG_DEPEND_PACKAGE --exclude=grub2,repair-system --setopt=protected_multilib=false install nbd
Transaction performed with:
Loading mirror speeds from cached hostfile
    Updated       rpm-4.11.1-16.1.el7.x86_64                    ?
    Updated       yum-3.4.3-118.1.el7.noarch                    ?
    Updated       yum-plugin-fastestmirror-1.1.31-24.el7.noarch @anaconda/rawhide
Packages Altered:
    Install nbd-3.11-1.1.el7.x86_64 @TFG_DEPEND_PACKAGE
history info

列出 rpm 包的内容:

~$ rpm -ql ceph-selinux
/usr/share/man/man8/ceph_selinux.8.gz
/usr/share/selinux/devel/include/contrib/ceph.if
/usr/share/selinux/packages/ceph.pp

列出 rpm 包的安装、卸载脚本:

~$ rpm -q --scripts ceph-selinux
postinstall scriptlet (using /bin/sh):
# backup file_contexts before update
. /etc/selinux/config
FILE_CONTEXT=/etc/selinux/${SELINUXTYPE}/contexts/files/file_contexts
cp ${FILE_CONTEXT} ${FILE_CONTEXT}.pre

# Install the policy
/usr/sbin/semodule -i /usr/share/selinux/packages/ceph.pp
...

列出 rpm 包的配置文件:

~$ rpm -q --configfiles mlnx-ofa_kernel-4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0.x86_64
/etc/infiniband/connectx.conf
/etc/infiniband/openib.conf
/etc/modprobe.d/ib_ipoib.conf
/etc/modprobe.d/mlnx.conf

查找 rpm 的依赖:

~$ rpm -q --requires ceph-selinux
/bin/sh
/bin/sh
ceph-base = 2:12.2.9-2.22.g06cf88e.el7
ceph-base = 2:12.2.9-2.22.g06cf88e.el7
gawk
libselinux-utils
policycoreutils
policycoreutils
policycoreutils
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
selinux-policy-base >= 3.13.1-60.el7
rpmlib(PayloadIsXz) <= 5.2-1

查询 rpm 包的 obsoletes 信息:

~$ rpm -q --obsoletes mlnx-ofa_kernel-4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0.x86_64
kernel-ib
compat-rdma
rdma
rdma-core < 41mlnx1-1
rdma-core-devel < 41mlnx1-1
mlnx-en
mlnx_en
mlnx-en-utils
kmod-mlnx-en
mlnx-en-kmp-default
mlnx-en-kmp-xen
mlnx-en-kmp-trace
mlnx-en-doc
mlnx-en-debuginfo
mlnx-en-sources

查询 rpm 包的 provides 信息:

~$ rpm -q --provides mlnx-ofa_kernel-4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0.x86_64
config(mlnx-ofa_kernel) = 4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0
mlnx-ofa_kernel = 4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0
mlnx-ofa_kernel(x86-64) = 4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0
rdma
rdma-core = 41mlnx1-1
rdma-core-devel = 41mlnx1-1

查询 rpm 包的 conflicts 信息:

~$ rpm -q --conflicts mlnx-ofa_kernel-4.1-OFED.4.1.1.0.2.1.gc22af88.rhel7u0.x86_64

根据文件查找已安装的 rpm 包:

~$ rpm -qf /usr/share/selinux/packages/ceph.pp
ceph-selinux-12.2.9-2.22.g06cf88e.el7.x86_64

解压 rpm 包:

~$ rpm2cpio ceph-selinux-12.2.2-16.60.gf3f1a60.el7.x86_64.rpm | cpio -div
./usr/share/man/man8/ceph_selinux.8.gz
./usr/share/selinux/devel/include/contrib/ceph.if
./usr/share/selinux/packages/ceph.pp
202 blocks
~$ ls
ceph-selinux-12.2.2-16.60.gf3f1a60.el7.x86_64.rpm  usr
~$ tree
.
├── ceph-selinux-12.2.2-16.60.gf3f1a60.el7.x86_64.rpm
└── usr
    └── share
        ├── man
        │   └── man8
        │       └── ceph_selinux.8.gz
        └── selinux
            ├── devel
            │   └── include
            │       └── contrib
            │           └── ceph.if
            └── packages
                └── ceph.pp

9 directories, 4 files

安装 rpm 包的两种方式:

~$ rpm -i xxx.rpm
~$ rpm -U xxx.rpm

其中 -i 不会检测版本的升级信息,如果系统已经存在了一个其它版本的包,则 -i 不会覆盖安装,如果两个版本的文件在同一个文件夹,则会报冲突,如果两个版本的文件在不同的文件夹,则在系统上会存在两个版本(安装内核一般用这种方式);-U 会检测当前是否升级安装,它会将前一个版本删除或覆盖,它会考虑 rpm spec 中定义的 Obsoletes/Conflicts 指令。

强制安装 rpm 包:

~$ sudo rpm -Uvh --force --nodeps xxx.rpm

其中 --force 忽略文件冲突,--nodeps 忽略缺失的依赖。

上述 rpm -qi, rpm -ql, rpm -q --scripts, rpm -qR 都可以增加 -p 选项指定本地 rpm 包进行查询。

展开 rpm spec 文件中的宏:

~$ rpmspec --parse xxx.spec

查看 rpm 宏定义:

~$ rpm --eval %systemd_post

if [ $1 -eq 1 ] ; then
        # Initial installation
        systemctl preset  >/dev/null 2>&1 || :
fi

参考资料

How to Use ‘Yum History’ to Find Out Installed or Removed Packages Info

https://www.tecmint.com/view-yum-history-to-find-packages-info/