RPM: Difference between revisions

From Leo's Notes
This page was last edited on 30 December 2021, at 21:18.
No edit summary
m Leo moved page RPM Tips & Tricks to RPM: Generic information on RPM
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
To list installed packages:
== Common RPM Tasks ==
rpm -qa
Below are the common tasks I do with RPM.


To look for a specific package by name:
=== List installed packages ===
rpm -qi <package name>
{{highlight|lang=terminal|code=
# rpm -qa
}}


To determine the origin of a file:
Format output using {{code|--queryformat}}.
rpm -qf <path to file>
{{highlight|lang=terminal|code=
# rpm -qa --queryformat="%{name}\n"
}}
 
=== Look for a specific package by name ===
{{highlight|lang=terminal|code=
# rpm -qi <package name>
}}
 
=== Determine the origin of a file ===
{{highlight|lang=terminal|code=
# rpm -qf <path to file>
}}


eg:
eg:
# rpm -qf /etc/hosts
{{highlight|lang=terminal|code=
setup-2.8.14-10.el6.noarch
# rpm -qf /etc/hosts
setup-2.8.14-10.el6.noarch
}}
 
=== Extracting a rpm package ===
Go into a directory and extract it using cpio. This will dump the files in the current directory, so make a directory to avoid having your files clobbered.
 
{{highlight|lang=terminal|code=
# mkdir package && cd package
# rpm2cpio ~/custom-package.rpm {{!}} cpio -idmv
}}
 
=== Viewing / Editing rpm package scripts ===
Use the {{code|rpmrebuild}} utility to generate a .spec file from an existing RPM package. The spec file that gets generated will contain any scripts that may be bundled with rpm which you can modify.
 
{{highlight|lang=terminal|code=
# rpmrebuild -e -p some-package.rpm
## This should open your editor with the .spec file that was generated.
## Save and quit when done to have the option to rebuild the rpm.
}}
 
=== Remove a package with a broken uninstall script ===
{{highlight|lang=terminal|code=
# rpm -e --noscripts <package>
}}
 
== Building RPM ==
RPMs are built using a .spec specification file.
 
Some directives to note:
* Summary defines what the package is for
* Description describes what the package does.
* prep section unpacks source and applies patches
* build section builds any source code. The make command usually uses DESTDIR=%{buildroot}.
* install section uses the buildroot macro ({{code|%{buildroot}}} to generate a temporary buildroot location.
* files section defines all files to be included. Files can be marked as 'config', 'doc', or 'license'. Configs and docs labeld as such can be displayed using {{code|rpm -qc}} or {{code|rpm -qd}} respectively. License files are placed in {{code|/usr/share/licenses}}.
 
The Buildroot directory is like a chroot environment of the target system.
 
=== Generating a Spec File ===
A specification file can be generated using the {{code|rpmdev-newspec}} command.
 
{{highlight|lang=terminal|code=
# rpmdev-newspec new-package
}}
 
=== Creating 32bit Dependency ===
In your RPM.spec file, have the following in your {{code|Requires}} line:
 
Requires: nss-pam-ldapd(x86-32)
 
'''Not:'''
Requires: nss-pam-ldapd.i686
 
=== Checking for Install/Upgrade/Remove in Install Scripts ===
To check whether a .rpm file is being installed, upgraded, or removed in the pre/post install scripts, check {{code|$1}} against the table below:
{| class="wikitable"
! $1 =
! 0
! 1
! 2
|-
| %pre
|
| Install
| Upgrade
|-
| %post
|
| Install
| Upgrade
|-
| %preun
| Uninstall
| Upgrade
|
|-
| %postun
| Uninstall
| Upgrade
|
|}
 
Example usage:
{{highlight|lang=bash|code=
%pre
 
if [ $1 -eq 1 ] ; then
  # Initial install
fi
}}
 
=== Useful Flags ===
When packaging .jar files, the .spec file should contain:
 
{{highlight|lang=text|code=
%define debug_package %{nil}
%define __jar_repack 0
}}
 
Binary stripping is done by rpmbuild automatically. To prevent this, add:
 
{{highlight|lang=text|code=
%define __os_install_post %{nil}
}}
 
=== Building ===
Use the {{code|rpmbuild}} command to build a RPM file using an existing .spec file.
 
The default topdir directory is {{code|~/rpmbuild}}. This can be changed by passing in {{code|--define "_topdir `pwd`"}}.
 
Packages are then signed using the {{code|rpmsign}} command. For example, {{code|rpmsign --addsign something.rpm}}.
 
== Troubleshooting ==
 
=== Warning: RPMDB altered outside of yum. ===
If you ever get the error:
Warning: RPMDB altered outside of yum.
 
... it probably means that you ran {{code|rpm}}. To fix this warning, clear {{code|yum}}'s cache and force a rebuild by running:
{{highlight|lang=terminal|code=
# yum clean all
# yum makecache
}}


[[Category:Linux]]
{{Navbox Linux}}[[Category:Linux]]
[[Category:PackageManagement]]
[[Category:TipsnTricks]]

Latest revision as of 21:18, 30 December 2021

Common RPM Tasks

Below are the common tasks I do with RPM.

List installed packages

# rpm -qa

Format output using --queryformat.

# rpm -qa --queryformat="%{name}\n"

Look for a specific package by name

# rpm -qi <package name>

Determine the origin of a file

# rpm -qf <path to file>

eg:

# rpm -qf /etc/hosts
setup-2.8.14-10.el6.noarch

Extracting a rpm package

Go into a directory and extract it using cpio. This will dump the files in the current directory, so make a directory to avoid having your files clobbered.

# mkdir package && cd package
# rpm2cpio ~/custom-package.rpm | cpio -idmv

Viewing / Editing rpm package scripts

Use the rpmrebuild utility to generate a .spec file from an existing RPM package. The spec file that gets generated will contain any scripts that may be bundled with rpm which you can modify.

# rpmrebuild -e -p some-package.rpm
## This should open your editor with the .spec file that was generated.
## Save and quit when done to have the option to rebuild the rpm.

Remove a package with a broken uninstall script

# rpm -e --noscripts <package>

Building RPM

RPMs are built using a .spec specification file.

Some directives to note:

  • Summary defines what the package is for
  • Description describes what the package does.
  • prep section unpacks source and applies patches
  • build section builds any source code. The make command usually uses DESTDIR=%{buildroot}.
  • install section uses the buildroot macro (%{buildroot} to generate a temporary buildroot location.
  • files section defines all files to be included. Files can be marked as 'config', 'doc', or 'license'. Configs and docs labeld as such can be displayed using rpm -qc or rpm -qd respectively. License files are placed in /usr/share/licenses.

The Buildroot directory is like a chroot environment of the target system.

Generating a Spec File

A specification file can be generated using the rpmdev-newspec command.

# rpmdev-newspec new-package

Creating 32bit Dependency

In your RPM.spec file, have the following in your Requires line:

Requires: nss-pam-ldapd(x86-32)

Not:

Requires: nss-pam-ldapd.i686

Checking for Install/Upgrade/Remove in Install Scripts

To check whether a .rpm file is being installed, upgraded, or removed in the pre/post install scripts, check $1 against the table below:

$1 = 0 1 2
%pre Install Upgrade
%post Install Upgrade
%preun Uninstall Upgrade
%postun Uninstall Upgrade

Example usage:

%pre

if [ $1 -eq 1 ] ; then
  # Initial install
fi

Useful Flags

When packaging .jar files, the .spec file should contain:

%define debug_package %{nil}
%define __jar_repack 0

Binary stripping is done by rpmbuild automatically. To prevent this, add:

%define __os_install_post %{nil}

Building

Use the rpmbuild command to build a RPM file using an existing .spec file.

The default topdir directory is ~/rpmbuild. This can be changed by passing in --define "_topdir `pwd`".

Packages are then signed using the rpmsign command. For example, rpmsign --addsign something.rpm.

Troubleshooting

Warning: RPMDB altered outside of yum.

If you ever get the error:

Warning: RPMDB altered outside of yum.

... it probably means that you ran rpm. To fix this warning, clear yum's cache and force a rebuild by running:

# yum clean all
# yum makecache