buffer-format.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. =======================
  2. initramfs buffer format
  3. =======================
  4. Al Viro, H. Peter Anvin
  5. With kernel 2.5.x, the old "initial ramdisk" protocol was complemented
  6. with an "initial ramfs" protocol. The initramfs content is passed
  7. using the same memory buffer protocol used by initrd, but the content
  8. is different. The initramfs buffer contains an archive which is
  9. expanded into a ramfs filesystem; this document details the initramfs
  10. buffer format.
  11. The initramfs buffer format is based around the "newc" or "crc" CPIO
  12. formats, and can be created with the cpio(1) utility. The cpio
  13. archive can be compressed using gzip(1), or any other algorithm provided
  14. via CONFIG_DECOMPRESS_*. One valid version of an initramfs buffer is
  15. thus a single .cpio.gz file.
  16. The full format of the initramfs buffer is defined by the following
  17. grammar, where::
  18. * is used to indicate "0 or more occurrences of"
  19. (|) indicates alternatives
  20. + indicates concatenation
  21. GZIP() indicates gzip compression of the operand
  22. BZIP2() indicates bzip2 compression of the operand
  23. LZMA() indicates lzma compression of the operand
  24. XZ() indicates xz compression of the operand
  25. LZO() indicates lzo compression of the operand
  26. LZ4() indicates lz4 compression of the operand
  27. ZSTD() indicates zstd compression of the operand
  28. ALGN(n) means padding with null bytes to an n-byte boundary
  29. initramfs := ("\0" | cpio_archive | cpio_compressed_archive)*
  30. cpio_compressed_archive := (GZIP(cpio_archive) | BZIP2(cpio_archive)
  31. | LZMA(cpio_archive) | XZ(cpio_archive) | LZO(cpio_archive)
  32. | LZ4(cpio_archive) | ZSTD(cpio_archive))
  33. cpio_archive := cpio_file* + (<nothing> | cpio_trailer)
  34. cpio_file := ALGN(4) + cpio_header + filename + "\0" + ALGN(4) + data
  35. cpio_trailer := ALGN(4) + cpio_header + "TRAILER!!!\0" + ALGN(4)
  36. In human terms, the initramfs buffer contains a collection of
  37. compressed and/or uncompressed cpio archives (in the "newc" or "crc"
  38. formats); arbitrary amounts zero bytes (for padding) can be added
  39. between members.
  40. The cpio "TRAILER!!!" entry (cpio end-of-archive) is optional, but is
  41. not ignored; see "handling of hard links" below.
  42. The structure of the cpio_header is as follows (all fields contain
  43. hexadecimal ASCII numbers fully padded with '0' on the left to the
  44. full width of the field, for example, the integer 4780 is represented
  45. by the ASCII string "000012ac"):
  46. ============= ================== ==============================================
  47. Field name Field size Meaning
  48. ============= ================== ==============================================
  49. c_magic 6 bytes The string "070701" or "070702"
  50. c_ino 8 bytes File inode number
  51. c_mode 8 bytes File mode and permissions
  52. c_uid 8 bytes File uid
  53. c_gid 8 bytes File gid
  54. c_nlink 8 bytes Number of links
  55. c_mtime 8 bytes Modification time
  56. c_filesize 8 bytes Size of data field
  57. c_maj 8 bytes Major part of file device number
  58. c_min 8 bytes Minor part of file device number
  59. c_rmaj 8 bytes Major part of device node reference
  60. c_rmin 8 bytes Minor part of device node reference
  61. c_namesize 8 bytes Length of filename, including final \0
  62. c_chksum 8 bytes Checksum of data field if c_magic is 070702;
  63. otherwise zero
  64. ============= ================== ==============================================
  65. The c_mode field matches the contents of st_mode returned by stat(2)
  66. on Linux, and encodes the file type and file permissions.
  67. c_mtime is ignored unless CONFIG_INITRAMFS_PRESERVE_MTIME=y is set.
  68. The c_filesize should be zero for any file which is not a regular file
  69. or symlink.
  70. c_namesize may account for more than one trailing '\0', as long as the
  71. value doesn't exceed PATH_MAX. This can be useful for ensuring that a
  72. subsequent file data segment is aligned, e.g. to a filesystem block
  73. boundary.
  74. The c_chksum field contains a simple 32-bit unsigned sum of all the
  75. bytes in the data field. cpio(1) refers to this as "crc", which is
  76. clearly incorrect (a cyclic redundancy check is a different and
  77. significantly stronger integrity check), however, this is the
  78. algorithm used.
  79. If the filename is "TRAILER!!!" this is actually an end-of-archive
  80. marker; the c_filesize for an end-of-archive marker must be zero.
  81. Handling of hard links
  82. ======================
  83. When a nondirectory with c_nlink > 1 is seen, the (c_maj,c_min,c_ino)
  84. tuple is looked up in a tuple buffer. If not found, it is entered in
  85. the tuple buffer and the entry is created as usual; if found, a hard
  86. link rather than a second copy of the file is created. It is not
  87. necessary (but permitted) to include a second copy of the file
  88. contents; if the file contents is not included, the c_filesize field
  89. should be set to zero to indicate no data section follows. If data is
  90. present, the previous instance of the file is overwritten; this allows
  91. the data-carrying instance of a file to occur anywhere in the sequence
  92. (GNU cpio is reported to attach the data to the last instance of a
  93. file only.)
  94. c_filesize must not be zero for a symlink.
  95. When a "TRAILER!!!" end-of-archive marker is seen, the tuple buffer is
  96. reset. This permits archives which are generated independently to be
  97. concatenated.
  98. To combine file data from different sources (without having to
  99. regenerate the (c_maj,c_min,c_ino) fields), therefore, either one of
  100. the following techniques can be used:
  101. a) Separate the different file data sources with a "TRAILER!!!"
  102. end-of-archive marker, or
  103. b) Make sure c_nlink == 1 for all nondirectory entries.