sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Backups
module list

cpio

copy-in/copy-out (cpio) is much more versatile than tar, but it is more difficult to use. It is also necessary to understand how cpio works before you create an archive so that you can easily restore files from it. cpio is not as well supported in linux as is tar, and, when you archive using cpio on earlier RedHat versions, it outputs a lot of warning messages that can be alarming. This appears to simply be sloppiness on the part of the programmers who ported it, and appears to be fixed in later releases.

Creating and restoring a simple archive

cpio -o (copy-out) reads a list of things to archive from standard input and creates the archive on standard output. Each item that is to be placed in the archive must be listed on a separate line on standard input. Thus standard input is normally a list prepared by another program or the output of find:

$ echo "log/proc.out
> log/sum.out" | cpio -o > archive1.cpio
cpio: log/proc.out: truncating inode number
cpio: log/sum.out: truncating inode number
35 blocks
$

The above example illustrates creating an archive (archive1.cpio) that contains two files: log/proc.out and log/sum.out. Note that [bogus] error messages. (For some reason cpio keeps track of the inode numbers in the archive, but the cpio archive format evidently is limited to an inode number with shorter length than is used on this system, hence the error. Since inode numbers are never restored, the error is meaningless.)

We will now remove the log directory and try to restore the archive using cpio -i (copy-in). This mode reads the archive from standarad input and restores the items listed on the command-line (or everything, by default):

$ rm -rf log
[gboyd@linux ~]$ cpio -i < archive1.cpio
cpio: log/proc.out: No such file or directory
cpio: log/sum.out: No such file or directory
35 blocks
$

The restore was unsuccessful, but the reason has nothing to do with the archive: neither file could be restored because the log directory was missing (the log directory is the 'No such file or directory' that cpio is complaining about.) If we created an empty log directory first, things would go much better.

$ mkdir log
$ cpio -i < archive1.cpio
35 blocks
$ ls log
proc.out  sum.out
$

This small problem could have been avoided if we would have used find to create the archive in the first place:

$ find log | cpio -o > archive1.cpio
35 blocks
$ rm -rf log
$ cpio -i < archive1.cpio
35 blocks
$ ls -R log
log:
proc.out  sum.out
$

The reason for this difference is seen by examing what, exactly, is on the archive. We can do this with the 'table-of-contents' mode of cpio: cpio -i -t

$ rm -rf log
$ cpio -i -t < archive1.cpio
log
log/proc.out
log/sum.out
35 blocks
$

The difference is that find placed a directory entry on the archive. When cpio restored the archive, the directory entry on the archive told it to create the directory! Hence to create a cpio of a directory recursively, you should use find dir instead of find dir -type f.

We will add one more illustration to show you the second major difference between cpio and tar. Here we will simply try to restore the archive again:

$ cpio -i  < archive1.cpio
cpio: log/proc.out not created: newer or same age version exists
cpio: log/sum.out not created: newer or same age version exists
35 blocks
$

cpio refuses to overwrite a file unless the file on the archive is newer! 

In summary, we have illustrated three 'features' of cpio:

Each of these last three 'features' may be toggled by use of an option when the archive is restored, as we will see.

Now that we see the issues, lets quickly go through the syntax of copy-in:

Restoring an archive (copy -in):

cpio -i [ -d ] [ -u ] [ -m ] [ list of paths to restore ] < archive

-d    create directories as needed

-u    unconditionally overwrite files (don't worry if the existing file is newer)

-m    restore file modification times

The list of paths to restore tells cpio what to restore. The default is all the data on the archive.

Objects that use cpio format

As we indicated in a previous section, RPM files use a modified cpio format. You can extract the cpio archive from an RPM file using rpm2cpio:

$ rpm2cpio kompozer-0.7.10-i386.rpm | cpio -it
./
./usr
./usr/bin
./usr/bin/kompozer
./usr/bin/kompozer-config
./usr/lib
./usr/lib/kompozer
./usr/lib/kompozer/TestGtkEmbed
...

You can also examine the contents of an initrd file using cpio:

$ file /boot/initrd-2.6.9-55.EL.img
/boot/initrd-2.6.9-55.EL.img: gzip compressed data, from Unix, max compression
$ gunzip < /boot/initrd-2.6.9-55.EL.img | cpio -it
.
dev
dev/tty4
dev/console
dev/null
dev/tty1
...


Prev This page was made entirely with free software on linux:  
Kompozer
and Openoffice.org    
Next

Copyright 2010 Greg Boyd - All Rights Reserved.
Document made with Nvu