How to repack initrd.img?
Solution 1:
I figured it out how to make exactly the same initrd.img
archive.
Bodhi.zazen answer will probably work because this is commonly known solution:
find . | cpio --quiet --dereference -o -H newc | lzma -7 > ../cusotm.initrd.lz
but the question was different. This answer would be good if in cpio archive there is one gzipped file system but in this situation there is also Intel firmware in specific folder structure which I want to keep.
To keep the same folder hierarchy three steps are needed:
Make CPIO file system archive with simple -o option without newc format in created before eg. base folder:
find . | cpio -o | gzip -9 > ../base/file_system.gz
Make proper archive with newc format containing kernel/x86/microcode/GenuineIntel.bin:
find kernel/ | cpio -o -H newc > new_initrd.img
Add gzipped filesystem archive to the proper new_initrd.img:
find base/ | cpio -o >> new_initrd.img
Solution 2:
I recently ran into this same question and my web searching led me to this thread, so in case it helps others following in those footsteps, here's a 2018 answer to an old question...
It seems in "recent" kernels the initrd.img file can contain an uncompressed cpio archive (i.e. containing microcode updates) prepended to the (compressed) cpio archive containing the normal initramfs directory tree.
This is discussed briefly in the Debian Wiki page:
https://wiki.debian.org/initramfs#How_to_inspect_initramfs
, but more precise code for parsing through this sort of initrd.img file can be found in the splitinitramfs()
function within the unmkinitramfs
command found in the initramfs-tools-core
package (e.g.
https://git.launchpad.net/ubuntu/+source/initramfs-tools/tree/unmkinitramfs
).
I have not tried to rebuild this sort of initrd.img file myself, but based on that Wiki page it seems that to edit the initramfs boot scripts, one would not want to unpack the GenuineIntel archive at all. Instead, you could just preserve that cpio archive as-is somewhere separately, then unpack the second (compressed) archive, modify the directory tree, and rebuild the compressed cpio archive, then concatenate the saved microcode archive with the newly-generated one.
(The code that originally generated this "prepended" archive is found in /usr/share/initramfs-tools/hooks/intel_microcode
.)
Solution 3:
You repackage with
cd your_working_directory_with_modifications
find . | cpio --quiet --dereference -o -H newc | lzma -7 > ../cusotm.initrd.lz
The second command renames the initrd, you specifiy the initrd to use when booting in grub.
I suggest you test (boot)the custom initrd before moving or renaming it.
Additional information from the discussion in the comments:
First I do not think you are understanding the role of cpio / tar. both cpio and tar take a number of files and/or directories and make them into one file or archive.
Second I do not think you understand the role of compression, compression simply makes the resulting archive smaller. You can use any tool you wish for compression.
See
https://wiki.ubuntu.com/CustomizeLiveInitrd
https://wiki.gentoo.org/wiki/Initramfs/Guide
Third, the linux kernel uses cipo rather then tar.
See
https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt
See the "Why cpio rather than tar?" section
Why cpio rather than tar?
This decision was made back in December, 2001. The discussion started here:
http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1538.html
And spawned a second thread (specifically on tar vs cpio), starting here:
http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1587.html
The quick and dirty summary version (which is no substitute for reading the above threads) is:
1) cpio is a standard. It's decades old (from the AT&T days), and already widely used on Linux (inside RPM, Red Hat's device driver disks). Here's a Linux Journal article about it from 1996:
http://www.linuxjournal.com/article/1213
It's not as popular as tar because the traditional cpio command line tools require _truly_hideous_ command line arguments. But that says nothing either way about the archive format, and there are alternative tools, such as:
http://freecode.com/projects/afio
2) The cpio archive format chosen by the kernel is simpler and cleaner (and thus easier to create and parse) than any of the (literally dozens of) various tar archive formats. The complete initramfs archive format is explained in buffer-format.txt, created in usr/gen_init_cpio.c, and extracted in init/initramfs.c. All three together come to less than 26k total of human-readable text.
3) The GNU project standardizing on tar is approximately as relevant as Windows standardizing on zip. Linux is not part of either, and is free to make its own technical decisions.
4) Since this is a kernel internal format, it could easily have been
something brand new. The kernel provides its own tools to create and extract this format anyway. Using an existing standard was preferable, but not essential.5) Al Viro made the decision (quote: "tar is ugly as hell and not going to be supported on the kernel side"):
http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1540.html
explained his reasoning:
http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1550.html http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1638.html
and, most importantly, designed and implemented the initramfs code.