What are the operating modes of linux cpio

08-01-2023

Today, the editor will share with you the relevant knowledge points about the operating modes of linux cpio. I hope you have gained something after reading this article. Let's find out together.

Cpio three operating modes: 1. copy-out mode, used to package backup files, will copy the files to the archive package; 2. copy-in mode, used to restore files from the archive package file, can read the archive package from the standard input, read the files in the archive package or list the contents of the archive package; 3, copy-pass mode, used to copy the file from Copy one directory tree to another.

In linux, cpio is a data backup and recovery tool for creating cpio archives, extracting files from archives, or copying files from one directory to another . The generated archive file can be a disk file, or it can be written to data streaming media such as tape. cpio can not only process cpio archive files in binary and ASCII header data format, but also read and write archive files in various tar formats. For compatibility, cpio usually still creates archives in binary header data format unless otherwise specified.

cpio is similar to the tar command, which can copy and package files into cpio or tar format archive files, or copy files from archive files. The archive package file can be a file on a disk, or a device file such as a tape.

When extracting files from archive files, the cpio command can automatically identify which archive file is read, and can also read archive files with different byte orders created in different systems .

cpio has three operating modes, as shown in the table below:

< tbody>
cpio three operating modes
cpio three operation modes
operation modemode description
copy-out modeThis mode is used to package backup files.
cpio copies the files into the archive. The filenames to be packaged by cpio come from standard input, and it gets a list of filenames from standard input, one per line. Generally, pipes are used to redirect standard input to the output of another command. The most common way is to use the find command to generate the filename columntable, which is then piped to cpio, In this way, cpio can know where the data to be backed up comes from. You need to add the -depth option after find to reduce the trouble caused by entering directories without access rights.
cpio writes the archive package file to the standard output by default, so you need to use the redirection symbol > or >> to redirect the archive package data output to a file.
copy-in modeThis mode is used to restore files from archive files.
cpio reads the archive package from standard input, reads the files in the archive package or lists the contents of the archive package. cpio writes data read from the archive to standard output.
copy-pass modecpio copies files from one directory tree to another, it combines copy-in and copy-out operation, but without using the archive package. cpio reads from standard input a list of filenames to copy; the destination directory is given as a non-option command-line argument.

cpio supports the following archive formats: binary, old ASCII, new ASCII, crc, HPUX binary, HPUX old ASCII, old tar, and POSIX. 1 tar.

Some options of cpio can only be used in the corresponding operation mode, the commonly used command option format is as follows:

[root@initroot ~] # cpio -ovcB > [file|device] #backup [root@initroot ~]# cpio -ivcdu < [file|device] #Restore [root@initroot ~]# cpio -ivct < [file|device] #View

copy-out mode (backup) used options And parameters:

  • -o: output the data copy to a file or device

  • -B: let The default Blocks can be increased to 5120bytes, and the default is 512bytes! The advantage of this is that it can speed up the storage of large files (please refer to the concept of i-nodes)

copy-in mode(Restore) Options and parameters used:

  • -i: copy data from files or devices to the system

  • -d: Automatically create a directory! The data content backed up by cpio may not be in the same directory, so we must allow cpio to create a new directory when restoring, and the -d option is required at this time Help!

  • -u: Automatically overwrite older files with newer files!

  • -t: Required With the -i option, it can be used to "view" the content of files or devices created with cpio

General options and parameters:

  • -v: Allow the file name to be displayed on the screen during storage

  • -c: A newer portable format method Storage

cpio reads data from standard input and outputs processed data to standard output, so cpio needs to be used with pipes and redirection symbols.

Find all the files in the /boot directory and back them up to the /tmp/boot.cpio archive file:

[root@initroot ~]# cd / [root@initroot /]# find boot -print boot boot/grub boot/grub/gfxblacklist.txt boot/grub/unicode.pf2 boot/grub/locale ...Omit...

The find command can find all the files in the boot directory, including files and directories! Note that the boot here is a relative path, not an absolute path path!

[root@initroot /]# find boot | cpio -ocvB > /tmp/boot.cpio [root@initroot /]# ls -lh /tmp/boot.cpio -rw-r--r-- 1 peter peter 193M Feb 10 15:59 /tmp/boot.cpio [root@initroot ~]# file /tmp/boot.cpio /tmp/boot.cpio: ASCII cpio archive (pre-SVR4 or odc)

Use find boot to find out the file name, pass the output to cpio through the pipeline line |, and output the archived data to the /tmp/boot.cpio file through the redirection symbol >! The above find parameter boot It is a relative path, not an absolute path, and cpio will not do special processing for the path, and it will receive whatever data is given. So if an absolute path is used here, then the output of the find command is also an absolute path, and cpio receives an absolute path, so when copying a file from an archive, because it is an absolute path, the copied file will definitely be overwritten Delete the files in the original /boot directory! This is very dangerous! So when you use the find command to cooperate with cpio to package files, you must use a relative path.

Now unpack the archive package file /tmp/boot.cpio to the /root/ directory:

[root@initroot ~]# cd ~ [root@initroot ~]# cpio -idvc < /tmp/boot.cpio [root@initroot ~]# ls -al /root/boot

It can be seen that the contents of /root/boot and /boot are exactly the same!

cpio can completely back up system data to the tape drive:

[root@initroot ~]# find / | cpio -ocvB > /dev/st0

Restore data from tape drive:

[root@initroot ~]# cpio -idvc < /dev/st0

cpio can back up any file, including device files under /dev! It is a very important command!

cpio must cooperate with other programs, such as find to create files Name, so cpio has a very close relationship with pipeline commands and data flow redirection!

In fact, /boot/initramfs-xxx or /boot/initrd.img-xxx-generic files in the /boot directory Also a cpio archive package file.

We can unpack the file to the /tmp/initramfs directory to see what files are in this archive package:

[root@initroot ~ ]# file initrd.img-4.15.0-65-generic initrd.img-4.15.0-65-generic: ASCII cpio archive (SVR4 with no CRC) [root@initroot ~]# mkdir /tmp/initramfs [root@initroot ~]# cd /tmp/initramfs [root@initroot initramfs]# cpio -idv < /boot/initrd.img-4.15.0-65-generic . kernel kernel/x86 kernel/x86/microcode kernel/x86/microcode/AuthenticAMD.bin 56 blocks

Note that the -c option is removed here. If the -c option is added, cpio will display the following prompt:

< p>cpio: premature end of file

Remove the -c option to successfully decompress the archive package file!

The above is all the content of this article, what are the operating modes of linux cpio, thank you for reading! I believe that everyone has gained a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the Yisu cloud industry information channel.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us