yum-rpm-pkg

Introduction

YUM is the primary package management tool for installing, updating, removing, and managing software packages in Red Hat Enterprise Linux. YUM performs dependency resolution when installing, updating, and removing software packages.

RPM is a popular package management tool in Red Hat Enterprise Linux-based distros. Using RPM, you can install, uninstall, and query individual software packages. Still, it cannot manage dependency resolution like YUM. RPM does provide you useful output, including a list of required packages. An RPM package consists of an archive of files and metadata. Metadata includes helper scripts, file attributes, and information about packages.

Yum

Yum is much more useful than rpm as it resolves dependencies of the package and installs them all!, First yum download meta data of packages from enalbed repo, rpm is downloaded only when it’s installing. any another command like search, list only check the meta data.

Useful command

conf

  • /etc/yum.conf: global conf
  • /etc/yum.repos.d/: each repos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# show all repos, disabled and enabled
$ yum repolist all

# yum cache
$ yum clean all
$ yum makecache

# show packages from local cache, no auto cache update
$ yum list # installed and not
$ yum list libvirt # installed and not of given package
$ yum list installed # installed only
$ yum list installed libvirt # installed only of given package
# shell-style glob
$ yum list mysql* # installed and not pkg with prefix mysql

$ yum install yum-utils # can trigger cache update

# show latest version of ipvsadm(will trigger cache update) of repo
$ repoquery ipvsadm

# show files of given pkg from local cache and will trigger cache update
# bash can be installed or not(list files of given packages)
$ repoquery --list bash
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
/usr/bin/alias
/usr/bin/bash
/usr/bin/bashbug
...

mysql++.x86_64 3.1.0-12.el7 epel # not install available from epel repo
ipvsadm.x86_64 1.27-8.el7 @base # installed from base repo: @ indicate installed

# check all packages contains ipvs in its name(package name)
$ yum search ipvs # wildcard searching

$ yum info ipvsadm # show basic info of a package installed or not, no auto cache update
$ yum provides /etc/fstab # show which package(installed or not) has such file from local cache, no auto cache update
$ yum provides rm # show which package(installed or not) has rm binary from local cache, no auto cache update

# Install, will trigger cache update
$ yum install -y ipvsadm
$ yum remove ipvsadm

$ yum grouplist
$ yum groupinfo "Development tools" # show what packages in this group
$ yum groupinstall -y "Development Tools" # install group of packages
$ yum groupremove

######################## Download file of given rpm #########################
$ yumdownloader ipvsadm # save at current dir(no matter it's installed or not)

$ yum install --downloadonly ipvsadm # saved at /var/cache/yum/x86_64/
$ yum install --downloadonly --downloaddir=/tmp ipvsadm
# if packet is already installed
$ yum reinstall --downloadonly --downloaddir=/tmp ipvsadm

$ yumdownloader --source ipvsadm # download ipvsadm-xxx-src.rpm
######################## Download file of given rpm #########################

$ yum history # show what operations happened
$ yum history undo <id> # undo an operation

#####################upgrade and downgrade####################################
# list all possible version of given packet from local cache
$ yum provides ipvsadm

# check current installed version
$ rpm -qa | grep ipvsadm

# upgrade to latest or specific version
# NOTE for upgrade, package must be installed before!!!
$ yum upgrade libvirt
Package(s) libvirt available, but not installed.

# upgrade does not run daemon-reload and service restart!!!
$ yum upgrade -y ipvsadm # can trigger local cache update
$ yum upgrade -y ipvsadm-1.27-7.el7.x86_64 # can trigger local cache update

# yum upgrade return 0 (OK) !!!
# if packet does not exist
# or upgrade to specific version or latest

$ yum upgrade -y no-exist-package
Loaded plugins: fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
47 packages excluded due to repository priority protections
No Match for argument: no-exist-package
No package no-exist-package available.
No packages marked for update
$ echo $?

# downgrade to previous version or specific version
# NOTE for upgrade, package must be installed before!!!
$ yum downgrade ipvsadm
$ yum downgrade ipvsadm-1.27-7.el7.x86_64
#####################upgrade and downgrade####################################

RPM

Name Format of RPM name-version-release.os.arch., example: bash-4.3.2-5.el6.x86_64.rpm

  • name: package name
  • version: package version
  • release: identifier of RPM itself, not related to package
  • os: operation system
  • arch:i686、x86_64、amd64、ppc(power-pc)、noarch

RPM repo

epel: extended packages provided by RedHat, Maintained by Fedora.

Useful command for rpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Install
$ rpm -ivh xx.rpm
$ rpm -ivh https://xxx.rpm

# rebuild xx.rpm from source, then install it
$ rpm -rebuild *.src.rpm
$ rpm -ivh /usr/src/dist/RPMS/xx.rpm

# Query
$ rpm -qa # check all installed packages
$ rpm -q ipvsadm # check if ipvsadm installed or not

$ rpm -qi ipvsadm # check basic info of this installed rpm

# show files of given pkg, NOTE this requires ipvsadm must be installed!!!
$ rpm -qlv ipvsadm # check all installed files of given installed rpm
# show files of given local rpm
$ rpm -qlp ./ipvsadm.x86_64 0:1.27-8.el7

# preinstall scriptlet – this will run before a package is installed
# postinstall scriptlet – this will run after a package is installed
# preuninstall scriptlet – this will run before a package is uninstalled
# postuninstall scriptlet – this will run after a package is uninstalled

# check scripts of the installed rpm, post/pre etc
$ rpm -q --scripts libvirt
preinstall scriptlet (using /bin/sh):
# 'libvirt' group is just to allow password-less polkit access to
# libvirtd. The uid number is irrelevant, so we use dynamic allocation
# described at the above link.
getent group libvirt >/dev/null || groupadd -r libvirt

exit 0
postinstall scriptlet (using /bin/sh):


if [ $1 -eq 1 ] ; then
# Initial installation
systemctl preset virtlockd.socket virtlogd.socket libvirtd.service >/dev/null 2>&1 || :
fi

preuninstall scriptlet (using /bin/sh):

if [ $1 -eq 0 ] ; then
# Package removal, not upgrade
systemctl --no-reload disable libvirtd.service virtlogd.socket virtlogd.service virtlockd.socket virtlockd.service > /dev/null 2>&1 || :
systemctl stop libvirtd.service virtlogd.socket virtlogd.service virtlockd.socket virtlockd.service > /dev/null 2>&1 || :
fi

postuninstall scriptlet (using /bin/sh):
/bin/systemctl daemon-reload >/dev/null 2>&1 || :
if [ $1 -ge 1 ] ; then
/bin/systemctl reload-or-try-restart virtlockd.service >/dev/null 2>&1 || :
/bin/systemctl reload-or-try-restart virtlogd.service >/dev/null 2>&1 || :
/bin/systemctl try-restart libvirtd.service >/dev/null 2>&1 || :
fi


# In upgrade scenario we must explicitly enable virtlockd/virtlogd
# sockets, if libvirtd is already enabled and start them if
# libvirtd is running, otherwise you'll get failures to start
# guests

$ rpm -qf /usr/sbin/ipvsadm # check which rpm this file belongs to

# Uninstall
$ rpm -e ipvsadm

# Extract rpm files
$ rpm2cpio ./packagecloud-test-1.1-1.x86_64.rpm | cpio -idmv

############################ For uninstalled package ###########################################################
# -p, --package PACKAGE_FILE
# Query an (uninstalled) package PACKAGE_FILE. The PACKAGE_FILE may be specified as an ftp or http style URL
############################ For uninstalled package ###########################################################
# show files of given rpm
$ rpm -qlp ./ipvsadm.x86_64 0:1.27-8.el7

# Rebuild rpm from installed files then deploy it with rebuild rpm
# edit some conf files then rebuild it
$ rpmrebuild ipvsadm

# with editing ipvsadm.spec prompt, then rebuild
$ rpmrebuild -e ipvsadm

rpm scriptlets

When scriptlets are called, they will be supplied with an argument. This argument, accessed via $1 (for shell scripts) is the number of packages of this name which will be left on the system when the action completes. So for the common case of install, upgrade, and uninstall we have:

install upgrade uninstall
%pretrans $1 == 1 $1 == 2 (N/A)
%pre $1 == 1 $1 == 2 (N/A)
%post $1 == 1 $1 == 2 (N/A)
%preun (N/A) $1 == 1 $1 == 0
%postun (N/A) $1 == 1 $1 == 0
%posttrans $1 == 1 $1 == 1 (N/A)

Note: even some scripts in rpm(built-in, no such files after installation) come from .spec, there is NO spec file in xxx.rpm but xxx.src.rpm.

Build rpm

Refer to RPM guideline

FAQ?

does service auto start after rpm -i xx.rpm?

It depends on the xx.rpm, as RPM allows to add scripts, hence you can do this there, but most of time, service is not started automatically after installation.

show all services with systemctl

1
2
3
4
5
6
7
8
# show all active systemd service
$ systemctl --type=service

# show all active/inactive systemd service
$ systemctl --type=service --all
# enable means start at boot time

$ systemctl enable htg

yum upgrade vs yum install

  • yum upgrade needs the package installed before
  • yum upgrade does NOT touch conf file, it remains what changed, actually, this depends on rpm.spec file

    with noreplace, if it exists, no replace

1
2
3
4
5
6
7
8
9
10
11
$ cat libvirtd.spec
...
%config(noreplace) %{_sysconfdir}/sysconfig/libvirtd
%config(noreplace) %{_sysconfdir}/sysconfig/virtlogd
%config(noreplace) %{_sysconfdir}/sysconfig/virtlockd
%config(noreplace) %{_sysconfdir}/libvirt/libvirtd.conf
%config(noreplace) %{_sysconfdir}/libvirt/virtlogd.conf
%config(noreplace) %{_sysconfdir}/libvirt/virtlockd.conf
%config(noreplace) %{_prefix}/lib/sysctl.d/60-libvirtd.conf

%config(noreplace) %{_sysconfdir}/logrotate.d/libvirtd
  • yum install copy conf rom rpm and backup existing conf suffixed with xx.rpmsave(like libivrtd.conf.rpmsave)

list rpm installation date

1
2
3
4
5
6
7
$rpm -qa --last
libreport-filesystem-2.1.11-38.el7.centos.x86_64 Wed 11 Sep 2019 05:22:55 PM CST
grub2-common-2.02-0.64.el7.centos.noarch Wed 11 Sep 2019 05:22:55 PM CST
filesystem-3.2-21.el7.x86_64 Wed 11 Sep 2019 05:22:55 PM CST
centos-release-7-4.1708.el7.centos.x86_64 Wed 11 Sep 2019 05:22:55 PM CST
libgcc-4.8.5-16.el7.x86_64 Wed 11 Sep 2019 05:22:54 PM CST
microcode_ctl-2.1-22.2.el7.centos.x86_64 Wed 11 Sep 2019 09:33:20 AM CST

prevent rpmbuild to strip symbol

You can add %global __os_install_post %{nil} at the top line of xx.spec file.

remove multiple package with wildcard

1
2
3
4
5
# remove dependency as well
$ yum remove 'libvirt*'

# only remove package, their dependency is kept
$rpm -e $(rpm -qa 'libvirt*')

rpm command hangs

rpm may be waiting for a lock to be freed.

1
2
3
4
5
6
$ ps -ef | grep rpm 
$ kill -9 $output_above
$/bin/rm -rf /var/lib/rpm/__db.0*

# Then try your rpm command again. If it still doesn't work, repeat as above
# but then also run rpm --rebuilddb before trying your rpm command again.