Introduction to rpm package management

Posted by dekeb55 on Sat, 15 Feb 2020 11:28:04 +0100

API: application program interface

ABI: application binary interface

The ABI file of linux system is in ELF format

The ABI file of windows system is in the form of exe and msi

System level development language: c/c++

Works: httpd, vsftpd, nginx

Application level development: java/python/php

java works: hadoop, hbase

python works: openstack

It's very hard to install the software in linux. It's compiled and installed in different source code and installed in binary system

  • Compilation and installation: a compilation environment is required, such as gcc.

    Source code: binary format (executable, library file, configuration file, help file) under the target system is organized into one or more "package" files

  • Binary installation (the source code has been compiled into a program that can be executed directly)

Package manager

  • Debian: dpt,dpkg. Filename suffix is ". deb"
  • redhat: rpm. Filename suffix is ". RPM"
  • S.U.S.E: rpm. Filename suffix is ". RPM"
  • Gentoo: ports
  • ArchLinux

Source code file name interpretation: name version.tar.gz

  • version:major.minor.release

    Major: major version number. When there is a big change, the major version number will be updated

    Minor: add some small functions and update the minor version number

    release: fix bug, update this number

RPM file name interpretation: name version release.arch.rpm

  • version:major.minor.release (same as source code)

  • Release: the release number of the rpm package.

    Although the source code is the same, but the packaging method is different, update this number

    release.OS: applicable operating system. 2.el7.i386.rmp

  • arch: cpu architecture. i386 stands for 32-bit system; x64(amd64) stands for 64 bit system

    ppc (power pc); noarch (Application of all architectures)

  • Example: redis-3.0.2-1.centos7.x64.rpm

    redis is name; 3.0.2 is version; 1 is the distribution number of rpm package; centos7 is the applicable operating system; x64 is the cpu architecture

Package composition

Many programs are made up of main programs and many parts. Some users only want to use main programs, while some users use main programs and other parts.

So in order to achieve on-demand installation, the package is divided into many sub packages. The installation package of the main program is the main package; the installation package of other parts is the sub package.

  • Main package name: name version release.arch.rpm

  • Name of the subpackage: name function version release.arch.rpm

    function: devel, utils, libs, etc. It's actually the name of the part.

Dependency relationship

The philosophy of linux is that programs should be as small as possible, and multiple small programs should be formed to complete complex functions. Therefore, when installing A, A depends on B, and so on.

In order to automatically install dependencies, a front-end tool is produced.

Front end tools: automatically resolving dependencies

  • yum: rhel, the front-end tool of rpm package manager on centos system class
  • Apt get (APT cache): a front-end tool of deb package manager
  • zypper: a front-end tool for suse's rpm package manager
  • dnf: the front-end tool of rpm package manager on centos8, fedora 22 +

Package manager

Function: the application program with compile number is packaged into one or several packages, so as to realize convenient installation, upgrade, uninstall, query and other management operations.

1. List of package components (each package is implemented separately)

  • List of documents
  • Scripts to run when installing or uninstalling

2. Public database: stored in / var/lib/rpm directory

  • Name and version of the package
  • Dependency relationship
  • Function description
  • File path and check code information of each file generated by installation
  • Wait
# ls /var/lib/rpm
Basenames     __db.001  __db.003  Group       Name          Packages     Requirename  Sigmd5
Conflictname  __db.002  Dirnames  Installtid  Obsoletename  Providename  Sha1header   Triggername

Group: after grouping the packages, you can manage groups, install and uninstall them in group units.

Sigmd5: check code

Trigger name: trigger name

Conflictname: package has version conflict

Access to packages

1. CD or official file server (or image site) of system distribution

2. Official website for developing this program

For example, the official website of nginx

3. Third party organization

Be sure to verify the acquired package to prevent the acquired package from being modified by others.

Does the official package have the corresponding MD5 verification? Use the MD5 verifier to verify the downloaded package and generate the MD5 verification code. If it is the same as the official package, it means that the package has not been modified and can be used safely.

rpm command management package on centos

General options:

  • -v: Show details
  • -vv: more detailed

Management includes: installation, upgrade, uninstall, query and verification, and database maintenance

Install: RPM {- i| -- install} [install options] package file

  • Example: rpm -ivh Packagename

    # rpm -ivh  zsh-5.0.2-33.el7.x86_64.rpm
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zsh-5.0.2-33.el7                 ################################# [100%]
  • Show installation progress: - h

    Input ා each ා represents 2% progress

  • test installation: - test

    Check for conflicts:

    Display: error: Failed dependencies, indicating that the dependency is not installed, which makes the package unable to be installed.

    # rpm -ivh --test php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64.rpm
    warning: php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
    error: Failed dependencies:
            libcrypto.so.1.1()(64bit) is needed by php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64
            libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) is needed by php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64
            libssl.so.1.1()(64bit) is needed by php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64
            libssl.so.1.1(OPENSSL_1_1_0)(64bit) is needed by php-common-7.3.5-3.module_el8.1.0+252+0d4e049c.x86_64
    
  • Ignore dependency and force installation: - nodes

    It is not recommended to use. The disposition depends on the help document.

  • Reinstallation: - replacepkgs

    Purpose: when the configuration file used by the program is changed to be bad and cannot be repaired to the state after installation, delete the file first, and then use -- replacepkgs to reinstall, the configuration file will be repaired to the state after installation. Note that if you do not delete this file, even if you use -- replacepkgs, there is no job, and the original configuration file will be preserved.

    # rpm -ivh --replacepkgs zsh-5.0.2-33.el7.x86_64.rpm
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zsh-5.0.2-33.el7                 ################################# [100%]
    
  • Scripts during installation and uninstallation: there are 4 scripts

    • Script to run before installation starts: preinstall. No way to run this script: - nopre
    • Script to run after installation: postingstall. No way to run this script: - nopost
    • Script to run before uninstall starts: preinstall. No way to run this script: - noprun
    • Script to run after uninstall is complete: postuninstall. No way to run this script: - nopostun
    • None of the four scripts run: - noscripts
  • Do not check package integrity during installation: - nodigest

    Don't verify package or header digests when reading.

    # rpm -ivh --nodigest zsh-5.0.2-33.el7.x86_64.rpm
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zsh-5.0.2-33.el7                 ################################# [100%]
  • Do not check the signature information of the package or the validity of the source during installation: -- nosignature

    Don't verify package or header signatures when reading.

    # rpm -ivh --nosignature zsh-5.0.2-33.el7.x86_64.rpm
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zsh-5.0.2-33.el7                 ################################# [100%]

Upgrade: use the same options as installation

  • Install or upgrade: RPM {- u| -- upgrade} [install options] package \ u file

    # rpm -Uvh --nodigest zsh-5.0.2-33.el7.x86_64.rpm
    Preparing...                          ################################# [100%]
            package zsh-5.0.2-33.el7.x86_64 is already installed
  • You can only upgrade: RPM {- f| -- freshen} [install options] package \ file

    # rpm -Fvh --nodigest zsh-5.0.2-33.el7.x86_64.rpm
  • Options specific to upgrade

    • --oldpackage: degraded. There is a problem with the new version, so you need to downgrade back to the original version
    • --Force: ignore dependency and force upgrade
  • Be careful:

    • Do not upgrade the kernel. linux supports the coexistence of multiple kernels, so install the new kernel directly.
    • If the configuration file has been modified, the new version will not overwrite the original file, but will be provided after renaming the new file to filename.rpmnew.

Uninstall: RPM {- e| -- erase} [-- allmatches] [-- justdb] [-- nodes] [-- nocripts] [-- test] package| name

When uninstalling, package name is specified. When installing / upgrading, file name is specified

# rpm -e zsh
# rpm -ql zsh
package zsh is not installed
  • Uninstall all versions of package ﹣ Name: -- allmatches
  • Ignore dependencies: - nodes

Query: RPM {- q| -- Query} [select options] [Query options]

select-options:

  • View all packages installed: - a, -- all

    # rpm -qa
  • View which package a file belongs to: - f file

    # rpm -qf /usr/share/doc/zsh-5.0.2
    zsh-5.0.2-33.el7.x86_64

query-options:

  • Check the changelog of rpm package (not source code): -- changelog

    # rpm -q --changelog zsh
  • Check which files are generated after the rpm package is installed: - l

    # rpm -ql zsh
  • Check the version number, size and package group of this package: - i

    # rpm -qi zsh
  • Profile used to query package: - c

    # rpm -qc bash
    /etc/skel/.bash_logout
    /etc/skel/.bash_profile
    /etc/skel/.bashrc
  • Help files provided by query package: - d

    # rpm -qd bash 
    /usr/share/doc/bash-4.2.46/COPYING
    /usr/share/info/bash.info.gz
    /usr/share/man/man1/..1.gz
    /usr/share/man/man1/:.1.gz
    /usr/share/man/man1/[.1.gz
    ...
  • capabilities provided by query package: -- supplies

    # rpm -qd bash | less
    [root@localhost ~]# rpm -q --provides zsh
    config(zsh) = 5.0.2-33.el7
    zsh = 5.0.2-33.el7
    zsh(x86-64) = 5.0.2-33.el7
    # rpm -q --provides bash
    /bin/bash
    /bin/sh
    bash = 4.2.46-31.el7
    bash(x86-64) = 4.2.46-31.el7
    config(bash) = 4.2.46-31.el7

    According to the name of capability, query which package this capability is provided by: -- whatprovides

    # rpm -q --provides bash
    /bin/bash
    /bin/sh
    bash = 4.2.46-31.el7
    bash(x86-64) = 4.2.46-31.el7
    config(bash) = 4.2.46-31.el7
    # rpm -q --whatprovides bash
    bash-4.2.46-31.el7.x86_64
    # rpm -q --whatprovides 'config(bash)'#Note: it needs to be enclosed in quotation marks because of the brackets.
    bash-4.2.46-31.el7.x86_64
    

    According to the name of capability, query which packages the capability depends on: -- whatrequires

    No packages depend on zsh, but many depend on bash.

    There are n multi package dependencies for sending a message: libc.so.6()(64bit)

    # rpm -q --whatrequires zsh
    no package requires zsh
    # rpm -q --whatrequires bash
    bash-completion-2.1-6.el7.noarch
    dracut-033-554.el7.x86_64
    initscripts-9.49.46-1.el7.x86_64
    lvm2-2.02.180-8.el7.x86_64
    autofs-5.0.7-99.el7.x86_64
    jline-1.0-8.el7.noarch
    rsyslog-8.24.0-34.el7.x86_64
    PackageKit-command-not-found-1.1.10-1.el7.centos.x86_64
    kpatch-0.6.1-1.el7.noarch
    # rpm -q --whatrequires 'libc.so.6()(64bit)'
  • See which capabilities a package depends on: - R

    See

    # rpm -qR bash
    /bin/sh
    config(bash) = 4.2.46-31.el7
    libc.so.6()(64bit)
    libc.so.6(GLIBC_2.11)(64bit)
    libc.so.6(GLIBC_2.14)(64bit)
    libc.so.6(GLIBC_2.15)(64bit)
    libc.so.6(GLIBC_2.2.5)(64bit)
    libc.so.6(GLIBC_2.3)(64bit)
    libc.so.6(GLIBC_2.3.4)(64bit)
    libc.so.6(GLIBC_2.4)(64bit)
    libc.so.6(GLIBC_2.8)(64bit)
    libdl.so.2()(64bit)
    libdl.so.2(GLIBC_2.2.5)(64bit)
    libtinfo.so.5()(64bit)
    rpmlib(BuiltinLuaScripts) <= 4.2.2-1
    rpmlib(CompressedFileNames) <= 3.0.4-1
    rpmlib(FileDigests) <= 4.6.0-1
    rpmlib(PayloadFilesHavePrefix) <= 4.0-1
    rtld(GNU_HASH)
    rpmlib(PayloadIsXz) <= 5.2-1
  • Query scripts in the package: -- scripts

    Found that zsh package contains: postinstall, preinstall, postuninstall

    # rpm -q --scripts zsh
    postinstall scriptlet (using /bin/sh):
    if [ ! -f /etc/shells ] ; then
        echo "/bin/zsh" > /etc/shells
    else
        grep -q "^/bin/zsh$" /etc/shells || echo "/bin/zsh" >> /etc/shells
    fi
    
    if [ -f /usr/share/info/zsh.info.gz ]; then
    # This is needed so that --excludedocs works.
    /sbin/install-info /usr/share/info/zsh.info.gz /usr/share/info/dir \
      --entry="* zsh: (zsh).                        An enhanced bourne shell."
    fi
    
    :
    preuninstall scriptlet (using /bin/sh):
    if [ "$1" = 0 ] ; then
        if [ -f /usr/share/info/zsh.info.gz ]; then
        # This is needed so that --excludedocs works.
        /sbin/install-info --delete /usr/share/info/zsh.info.gz /usr/share/info/dir \
          --entry="* zsh: (zsh).                    An enhanced bourne shell."
        fi
    fi
    :
    postuninstall scriptlet (using /bin/sh):
    if [ "$1" = 0 ] ; then
        if [ -f /etc/shells ] ; then
            TmpFile=`/bin/mktemp /tmp/.zshrpmXXXXXX`
            grep -v '^/bin/zsh$' /etc/shells > $TmpFile
            cp -f $TmpFile /etc/shells
            rm -f $TmpFile
        fi
    fi
  • Query the information of the installed package. The above options are all applicable, but you can't use package [name]. You need to use package [file]

    # rpm -ql zsh
    package zsh is not installed
    # rpm -qpl zsh-5.0.2-33.el7.x86_64.rpm
    /bin/zsh
    /etc/skel/.zshrc
    /etc/zlogin
    /etc/zlogout
    ...
    # rpm -qpR zsh-5.0.2-33.el7.x86_64.rpm
    # rpm -qp --scripts zsh-5.0.2-33.el7.x86_64.rpm

Verification: RPM {- v| -- verify} [select options] [verify options]

# rpm -V zsh#No echo means no one tampered with it
# rpm -ql zsh | less
# file /etc/zlogin
/etc/zlogin: ASCII text
# emacs -nw /etc/zlogin#Modify it on purpose
# rpm -V zsh#Verify again, echo the following information
S.5....T.  c /etc/zlogin

Interpretation of verification results:

  • S file size changed
  • M Mode has changed.
  • The content of the 5 file has changed, which makes the MD5 verification different from the previous one
  • D device's primary / secondary device number does not match
  • L readLink(2) path mismatch
  • U is the main transformer
  • Group G changed
  • T mTime has changed
  • P caPabilities changed

Verify package integrity and source validity

Where is the public key? It is usually in the organization CA, but there is a public key on the system CD-ROM. when the system is installed, the public key on the CD-ROM will be stored in the / etc / PKI / RPM GPG / directory.

# ls /etc/pki/rpm-gpg/
RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Debug-7  RPM-GPG-KEY-CentOS-Testing-7

Import public key: ා rpm --import file

# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

After the public key is imported, the integrity and source validity of the package will be automatically verified at the same time of installation

Manual verification of rmp package: provided that the public key has been imported

# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
# rpm -K zsh-5.0.2-33.el7.x86_64.rpm
zsh-5.0.2-33.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

Where to find the public key of a third-party organization (such as epel) and download it on epel's official website.

rpm database

Think about where the information is read when rpm queries and validations are performed?

When the rpm package is installed, the package details will be inserted into the local database, so that future query and verification operations can be realized.

Database path of local RPM package: / var/lib/rpm

# ls /var/lib/rpm
Basenames     __db.001  __db.003  Group       Name          Packages     Requirename  Sigmd5
Conflictname  __db.002  Dirnames  Installtid  Obsoletename  Providename  Sha1header   Triggername

So rpm database is too important. If it is damaged, it needs to be rebuilt.

Database Reconstruction: rpm {--initdb|--rebuilddb} [-v][--dbpath DIRECTORY] [--root DIRECTORY]

centos6 get help: man rpm

centos7 get help: man rpmdb

Create database from 0: - initdb

According to the rpm header file retained in the system, build a new database: -- rebuild DB

Create / update database at specified path: - dbpath DIRECTORY

# rpm --initdb --dbpath=/tmp/rpmdb
# ls /tmp/rpmdb/
Basenames     __db.001  __db.003  Group       Name          Packages     Requirename  Sigmd5
Conflictname  __db.002  Dirnames  Installtid  Obsoletename  Providename  Sha1header   Triggername
# rpm --rebuilddb --dbpath=/tmp/rpmdb
# ls /tmp/rpmdb/
Basenames     Dirnames  Installtid  Obsoletename  Providename  Sha1header  Triggername
Conflictname  Group     Name        Packages      Requirename  Sigmd5

QQ group of mutual learning in c/c + +: 877684253

My wechat: xiaoshitou5854

Topics: Linux RPM Database CentOS PHP