3

initramfs archives on Linux can consist of a series of concatenated, gzipped cpio files.

Given such an archive, how can one extract all the embedded archives, as opposed to only the first one?

The following is an example of a pattern which, while it appears to have potential to work, extracts only the first archive:

while gunzip -c | cpio -i; do :; done <input.cgz

I've also tried the skipcpio helper from dracut to move the file pointer past the first cpio image, but the following results in a corrupt stream (not at the correct point in the input) being sent to cpio:

# this isn't ideal -- presumably would need to rerun with an extra skipcpio in the pipeline
# ...until all files in the archive have been reached.
gunzip -c <input.cgz | skipcpio /dev/stdin | cpio -i
4

gunzip needs to be run only once (consuming all input), whereas cpio should be run once per embedded archive, like so:

gunzip -c <input.cgz | while cpio -i; do :; done
2
/usr/lib/dracut/skipcpio $your-initrd-img | zcat | cpio -id --no-absolute-file-names

or else

/usr/lib/dracut/skipcpio $your-img | gunzip -c | cpio -id

(in FreeBSD there is no --no-absolute-file-names option to cpio)

This small program skipcpio if part of dracut package. But you can download the code (skipcpio.c) and compile it even under FreeBSD.

You need this when extracting dracut created initrd images, at least under RedHat-powered distros, like Fedora. It places a file called "early_cpio" into the image, so extracting your initramfs in a normal way known before will not work.

  • Ahh! You'll note that I mention skipcpio in my question -- it's one of the things I tried before asking -- but I assumed that it expected a cpio stream as input (as the name would imply), not a gzip stream. That said, I think the answer I already have is a better fit for the question ("[H]ow can one extract all the embedded archives[...]?"), since it extracts all cpio archives, as opposed to only the second one in the stream. – Charles Duffy Apr 27 '16 at 13:52
  • Of course, of course. Mine only relates to dracut-created archives, I even suspect, to RedHat-derived distros... Well, dracut IS powered by RH, is it not?? I've just put it here for further usage by those who might need this "weird" kind of thing. Like myself, when I needed to add modules to an existing initramfs to make a system boot in the first place, so running dracut to create one was out of question... Neither was it possible to chroot into that system from a Live CD, because I don't know a ZFS-aware Live CD distribution, and my Linux installation is ZFS-based. – Kostya Berger Apr 28 '16 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.