分类: LINUX
2009-03-03 14:48:06
cpio/rpm2cpio commands : How to extract an RPM package without installing it?
Most of you may know to how extract a tarballs and/or a zip files. Someone recently PM me with a question:
How do I extract an RPM package without installing it on my rhel5?
To be frank there is no direct RPM option available via rpm command to extract an RPM file. But there is a small nifty utility available called rpm2cpio. It Extract cpio archive from RPM Package Manager (RPM) package. With the following hack you will be able to extract an RPM file.
So rpm2cpio converts the .rpm file specified as a single argument to a cpio archive on standard out. If a - argument is given, an rpm stream is read from standard in.
Syntax is as follows:
rpm2cpio myrpmfile.rpm
rpm2cpio - < myrpmfile.rpm
rpm2cpio myrpmfile.rpm | cpio -idmv
Example:
Download an RPM file:
$ mkdir test
$ cd test
$ wget
Extract RPM file using rpm2cpio and cpio command:
$ rpm2cpio php-5.1.4-1.esp1.x86_64.rpm | cpio -idmvOutput:
/etc/httpd/conf.d/php.conf
./etc/php.d
./etc/php.ini
./usr/bin/php
./usr/bin/php-cgi
./usr/lib64/httpd/modules/libphp5.so
./usr/lib64/php
./usr/lib64/php/modules
....
.....
..
./var/lib/php/session
./var/www/icons/php.gif
19188 blocks
Output of rpm2cpio piped to cpio command (see how to use cpio) with following options:
* i: Restore archive
* d: Create leading directories where needed
* m: Retain previous file modification times when creating files
* v: Verbose i.e. display progress
Verify that you have extracted an RPM file in current directory:
$ ls Output:
etc php-5.1.4-1.esp1.x86_64.rpm usr var
This is useful if you want to extract configuration file or other file w/o installing an RPM file.
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates.
Related Linux / UNIX Tips:
* Download of the day: e tool to extract rpm, rar, zip and other formats under Linux
* How to extract files from ISO CD images in Linux
* RHEL / CentOS Support 4GB or more RAM ( memory )
* FreeBSD changing pkg_add package ftp site location
* Uninstall files installed from a source code tar-ball
------------------------------------------------------------------------------------------
cpio/rpm2cpio commands
To extract a cpio file:
$ cpio -iv < cpio_file
To list the contents of a cpio file:
$ cpio -itv < cpio_file
To create a cpio file with all files in the current directory:
$ ls | cpio -o > cpio_file
To extract all files from an RPM:
$ rpm2cpio RPM_file | cpio -idv
To extract individual file(s) from an RPM:
$ rpm2cpio RPM_file | cpio -id individual_file(s)
e.g. Extracting libcrypto.so.0.9.7a and libssl.so.0.9.7a from openssl-0.9.7a-2.i386.rpm:
$ rpm2cpio openssl-0.9.7a-2.i386.rpm | cpio -it egrep "libcrypto.so.0.9.7a|libssl.so.0.9.7a"
-rwxr-xr-x 1 root root 992092 Feb 27 12:10 ./lib/libcrypto.so.0.9.7a
-rwxr-xr-x 1 root root 216004 Feb 27 12:10 ./lib/libssl.so.0.9.7a
$ rpm2cpio openssl-0.9.7a-2.i386.rpm | cpio -idv ./lib/libssl.so.0.9.7a ./lib/libcrypto.so.0.9.7a
This will extract the two files from the RPM into a ./lib subdirectory.
To extract individual files from a Sun Flash Archive file:
1. Split the Flash Archive into the cookie, identification, and archive files.
$ flar split Flash_Archive_file
2. Extract the file(s) from the cpio archive.
e.g. extracting var/sadm/install/contents
$ cpio -idv var/sadm/install/contents < archive
Back to brandonhutchinson.com.
Using rpm2cpio
From time to time, you might find it necessary to extract one or more files from a package file. One way to do this would be to:
* Install the package
* Make a copy of the file(s) you need
* Erase the package
An easier way would be to use rpm2cpio.
rpm2cpio -- What does it do?
As the name implies, rpm2cpio takes an RPM package file and converts it to a cpio archive. Because it's written to be used primarily as a filter, there's not much to be specified. rpm2cpio takes only only one argument, and even that's optional!
The optional argument is the name of the package file to be converted. If there is no filename specified on the command line, rpm2cpio will simply read from standard input and convert that to a cpio archive. Let's give it a try:
# rpm2cpio logrotate-1.0-1.i386.rpm
0707020001a86a000081a4000000000000000000000001313118bb000002c200000008000
000030000000000000000000000190000e73eusr/man/man8/logrotate.8." logrotate
- log fi
le rotator
.TH rpm 8 "28 November 1995" "Red Hat Software" "Red Hat Linux"
.SH NAME
(We've just shown the first few lines of output.)
What on earth is all that stuff? Remember, rpm2cpio is written as a filter. It writes the cpio archive contained in the package file to standard output, which, if you've not redirected it somehow, is your screen. Here's a more reasonable example:
# rpm2cpio logrotate-1.0-1.i386.rpm > blah.cpio
# file blah.cpio
blah.cpio: ASCII cpio archive (SVR4 with CRC)
#
Here we've directed rpm2cpio to convert the logrotate package file. We've also redirected rpm2cpio's output to a file called blah.cpio. Next, using the file command, we find that the resulting file is indeed a true-blue cpio archive file. The following command is entirely equivalent to the one above and shows rpm2cpio's ability to read the package file from its standard input:
# cat logrotate-1.0-1.i386.rpm | rpm2cpio > blah.cpio
#
A more real-world example -- Listing the files in a package file
While there's nothing wrong with using rpm2cpio to actually create a cpio archive file, it's takes a few more steps and uses a bit more disk space than is strictly necessary. A somewhat cleaner approach would be to pipe rpm2cpio's output directly into cpio:
# rpm2cpio logrotate-1.0-1.i386.rpm | cpio -t
usr/man/man8/logrotate.8
usr/sbin/logrotate
14 blocks
#
In this example, we used the -t option to direct cpio to produce a ``table of contents'' of the archive created by rpm2cpio. This can make it much easier to get the right filename and path when you want to extract a file.
Extracting one or more files from a package file
Continuing the example above, let's extract the man page from the logrotate package. In the table of contents, we see that the full path is usr/man/man8/logrotate.8. All we need to do is to use the filename and path as shown below:
# rpm2cpio logrotate-1.0-1.i386.rpm |cpio -ivd usr/man/man8/logrotate.8
usr/man/man8/logrotate.8
14 blocks
#
In this case, the cpio options -i, -v, and -d direct cpio to:
* Extract one or more files from an archive.
* Display the names of any files processed, along with the size of the archive file, in 512-byte blocks.1
* Create any directories that precede the filename specified in the cpio command.
So where did the file end up? The last option (-d) to cpio provides a hint. Let's take a look:
# ls -al
total 5
-rw-rw-r-- 1 root root 3918 May 30 11:02 logrotate-1.0-1.i386.rpm
drwx------ 3 root root 1024 Jul 14 12:42 usr
# cd usr
# ls -al
total 1
drwx------ 3 root root 1024 Jul 14 12:42 man
# cd man
# ls -al
total 1
drwx------ 2 root root 1024 Jul 14 12:42 man8
# cd man8
# ls -al
total 1
-rw-r--r-- 1 root root 706 Jul 14 12:42 logrotate.8
# cat logrotate.8
.\" logrotate - log file rotator
.TH rpm 8 "28 November 1995" "Red Hat Software" "Red Hat Linux"
.SH NAME
logrotate \- log file rotator
.SH SYNOPSIS
\fBlogrotate\fP [configfiles]
.SH DESCRIPTION
\fBlogrotate\fP is a tool to prevent log files from growing without
Since the current directory didn't have a usr/man/man8/ path in it, the -d option caused cpio to create all the directories leading up to the logrotate.8 file in the current directory. Based on this, it's probably safest to use cpio outside the normal system directories unless you're comfortable with cpio, and you know what you're doing!