[Linux] Convert and Extract RPM Packages to cpio Format with rpm2cpio

目次

Overview

The rpm2cpio command converts RPM package files (.rpm), used in Red Hat-based distributions, into a “cpio format” data stream that can be extracted without installation. This command is typically used with the cpio command via a pipe (|) to list package contents or extract specific files. It is extremely useful for retrieving a single configuration file that was accidentally deleted or for verifying contents before installing a package.

Specifications (Arguments and Options)

Syntax

rpm2cpio [RPM_file_path]

Note: rpm2cpio itself does not have complex options. You control its behavior (extraction, listing, etc.) using the options of the cpio command that receives the output.

Main Arguments

  • RPM_file_path: Specifies the source RPM file. If omitted or set to -, it reads from standard input.

Basic Usage

You can display a list of files included in an RPM package. This provides results similar to the rpm -qpL command but can be used in environments where the rpm command itself is unavailable, such as when using a rescue disk on another OS.

# List package contents (without extracting)
rpm2cpio nginx-1.24.0-1.el9.x86_64.rpm | cpio -t

Example output:

./etc/nginx/nginx.conf
./usr/sbin/nginx
./usr/share/man/man8/nginx.8.gz
./usr/share/nginx/html/index.html
450 blocks

Practical Commands

Extracting All Package Contents

This command extracts the RPM file into the current directory. It combines the -i (extract) and -d (create directories) options of cpio.

# Create and move to a working directory
mkdir work_dir && cd work_dir

# Extract package contents into the current directory
rpm2cpio ../nginx-1.24.0-1.el9.x86_64.rpm | cpio -id

Extracting from Standard Input

This method uses the < redirection to load the RPM file. The behavior is the same as above, but it is useful for scripts where file paths are stored in variables.

# Extract via standard input (using - or no argument)
rpm2cpio - < ../nginx-1.24.0-1.el9.x86_64.rpm | cpio -id

Customization Tips

  • Extracting Specific Files: To retrieve only a specific configuration file instead of the whole package, pass a filename pattern to cpio.Bash# Extract only nginx.conf rpm2cpio nginx-pkg.rpm | cpio -id "*nginx.conf"
  • Verbose Output (-v): Add -v to the cpio command to see a real-time list of files being processed.Bashrpm2cpio target-pkg.rpm | cpio -idv

Important Notes

  • Not Registered in System Database: Files extracted this way are not recorded in the RPM database (used by rpm -qa, etc.). Since they are placed as simple file copies, they are not managed by the package system for updates or removals.
  • Extracts to Current Directory: By default, files are extracted relative to your current location. Running this in the root directory (/) risks overwriting system files, so always use a temporary working directory.
  • Requires cpio Command: rpm2cpio only converts data and sends it to standard output. It will not work if the cpio command is not installed on your system.

Advanced Usage

Inspecting Remote RPM Packages Without Downloading

By combining this with curl or wget, you can check the contents of an RPM file over the network without saving it locally.

# Fetch a remote RPM file and list its contents via pipe
curl -L https://example.com/repo/packages/monitor-tool.rpm | rpm2cpio - | cpio -t

Summary

The rpm2cpio tool is key to treating RPM packages as simple archives. Unlike a formal system installation using rpm -ivh or dnf install, it allows you to rescue specific files with minimal impact. Remember the standard phrase “| cpio -id” for extraction to make the most of this utility during troubleshooting and verification tasks.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次