Every cpio entry is a header, the path name (with its NUL) and the file’s data. The
four header formats below are told apart by their magic. This page follows the
cpio(5) description in libarchive and the odc format in POSIX’s
pax.
Old binary header
The original PWB/Unix format: thirteen 16-bit words, 26 bytes. The magic is
070707 octal (0x71C7) as a 16-bit number, so it reads
C7 71 when written little-endian and 71 C7 big-endian; a
reader uses it to learn the byte order of the whole header. The 32-bit fields are
two 16-bit words, most significant word first, each word in that byte order (on a
little-endian machine 0x12345678 is stored 34 12 78 56).
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 2 | h_magic | 070707 octal. |
2 | 2 | h_dev | Device holding the file (used with h_ino to spot hard links). |
4 | 2 | h_ino | Inode number. |
6 | 2 | h_mode | File type and permissions; see mode. |
8 | 2 | h_uid | Owner’s user ID. |
10 | 2 | h_gid | Owner’s group ID. |
12 | 2 | h_nlink | Number of links. |
14 | 2 | h_majmin | Device number, for device files. |
16 | 4 | h_mtime | Modification time, seconds since 1970. |
20 | 2 | h_namesize | Length of the path name including its NUL. |
22 | 4 | h_filesize | Size of the data. |
Padding: if h_namesize is odd, one NUL follows the name, and a NUL
follows the data when needed so the next header starts at an even offset. In the
original PWB format the size was limited to 16 MiB; the later “new binary” form of the
same header treats it as a signed 32-bit number (2 GiB). User, group, device and inode
numbers are limited to 16 bits.
odc (POSIX portable ASCII) header
76 bytes of ASCII text: every field is octal digits with leading zeros. POSIX.1
defines it for pax -x cpio.
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 6 | c_magic | 070707. |
6 | 6 | c_dev | Device. |
12 | 6 | c_ino | Inode number. |
18 | 6 | c_mode | Type and permissions. |
24 | 6 | c_uid | User ID. |
30 | 6 | c_gid | Group ID. |
36 | 6 | c_nlink | Number of links. |
42 | 6 | c_rdev | Device number, for device files. |
48 | 11 | c_mtime | Modification time. |
59 | 6 | c_namesize | Name length including the NUL. |
65 | 11 | c_filesize | Data size. |
No padding after the name or the data. Six octal digits allow 18-bit IDs and device and inode numbers; eleven allow files up to 8 GiB.
newc (SVR4) and crc headers
110 bytes of ASCII text: every field after the magic is 8 hexadecimal digits. This is the format of Linux initramfs images and of RPM payloads.
| Offset | Size | Name | Description |
|---|---|---|---|
0 | 6 | c_magic | 070701 (newc) or 070702 (crc). |
6 | 8 | c_ino | Inode number. |
14 | 8 | c_mode | Type and permissions. |
22 | 8 | c_uid | User ID. |
30 | 8 | c_gid | Group ID. |
38 | 8 | c_nlink | Number of links. |
46 | 8 | c_mtime | Modification time, seconds since 1970. |
54 | 8 | c_filesize | Data size; up to 4 GiB. |
62 | 8 | c_devmajor | Major number of the device holding the file. |
70 | 8 | c_devminor | Its minor number. |
78 | 8 | c_rdevmajor | Major number, for device files. |
86 | 8 | c_rdevminor | Minor number, for device files. |
94 | 8 | c_namesize | Name length including the NUL. |
102 | 8 | c_check | newc: zero, ignored. crc: the checksum of the data. |
Padding: NULs after the name so that header and name together are a multiple of 4 bytes, and NULs after the data to the next multiple of 4.
The crc checksum
Despite the name it is not a CRC: it is the sum of every byte of the file’s data as unsigned numbers, keeping the low 32 bits.
Mode
| Octal | Meaning |
|---|---|
0170000 | Mask for the file type |
0140000 | Socket |
0120000 | Symbolic link; the data is the link target |
0100000 | Regular file |
0060000 | Block device |
0040000 | Directory |
0020000 | Character device |
0010000 | FIFO (named pipe) |
0004000 | Set user ID |
0002000 | Set group ID |
0001000 | Sticky bit |
0000777 | Read, write and execute for owner, group and others |
In the old PWB binary format, 0100000 was IALLOC and
0010000 ILARG, inode flags with no meaning to cpio; the
later formats reuse those bits for the file types above.
Hard links and the trailer
In the binary and odc formats, every hard link carries a full copy of the data. In newc and crc, entries with the same device and inode are links to one file, and the data is stored once: the other entries have a size of zero. libarchive’s description says the data goes with the first entry; GNU cpio writes it with the last, so a reader should accept either.
The archive ends with an entry named TRAILER!!!, with no data. Writers
then pad the archive with NULs to a whole block (512 bytes, or 5,120 for the
traditional cpio block size). A Linux initramfs may have several
archives one after another, some compressed.
Other variants exist: HP-UX stored device numbers differently, and Solaris adds special entries for ACLs and extended attributes. Neither is described publicly in detail. No cpio format stores user or group names.
Example
A newc header for a 5-byte file hi, mode 0100644:
070701 c_magic
00000001 c_ino
000081A4 c_mode 0100644, regular file rw-r--r--
000003E8 c_uid 1000
000003E8 c_gid 1000
00000001 c_nlink
5F5E1000 c_mtime
00000005 c_filesize
00000008 00000001 c_devmajor, c_devminor
00000000 00000000 c_rdevmajor, c_rdevminor
00000003 c_namesize ("hi" and NUL)
00000000 c_check
hi\0 \0\0\0 the name, its NUL, then padding
The fields are written one after another with no spaces; they are split into lines here only for reading. Header and name are 113 bytes, so three NULs bring them to 116, a multiple of 4. The 5 data bytes are followed by three NULs to reach the next multiple of 4.