10

If /proc/config.gz is unavailable, how do I know what decompression algorithms the running kernel is capable of using on a compressed cpio initramfs?

Is the gzip algorithm always available, even when CONFIG_DECOMPRESS_GZIP is not y when building the kernel?

3 Answers 3

4

Compression algorithms are declared in lib/decompress.c. Gzip is defined in lib/decompress_inflate.c and doesn't get any special status; it'll only be there if CONFIG_DECOMPRESS_GZIP is y when the kernel is compiled.

The list of available compression algorithms is the compressed_formats structure. Since it's defined as static, it isn't available in other source files. The only function that uses it is thus the decompress_method function in lib/decompress.c itself. So the only way to get a kernel to use the table of supported algorithms is to attempt to decompress something that starts with the appropriate two-byte magic sequence, and see if that function returns the algorithm name.

You can tell which decompression functions are supported by searching the list of kernel symbols (/proc/kallsyms). The functions aren't identified as such, so you'll need to bake in the list of symbol names.

</proc/kallsyms cut -d " " -f 3 |
grep -xF -e gunzip -e bzip2 -e unlzma -e unxz -e unlzo -e unlz4
1

Awk for the win:

samveen@x230:~ $ awk '/^name/{n=$3}/^type/{if($3=="compression"){print n}}' </proc/crypto 
lz4
lzo-rle
lzo
deflate
compress_null

Explaination:

Print the kernel algorithms from /proc/crypto flagged as type compression.

Note: /proc is always available in every linux kernel based environment, be it via a initrd or containers (like docker), so this method should work for all recent kernels

Caveat Emptor:

  • As pointed out in the comments, this does seem to miss xz, even for my env.
  • As per this thread in the kernel mailing list, this test is only available kernel 2.6 onwards.
  • Lastly, as pointed out in the comments, if the kernel build doesn't include algorithms under crypto, you get exactly what's included there. For example, v2.6 implemented just deflate, while v3.18.140 had deflate and lz4.
3
  • Nice! I love awk, but this appears to miss a lot of methods that the /proc/kallsyms method from Gilles shows. (lza, lzma, xz). Also, on old (2.6 series) kernels, popular when asked, /proc/crypto had no compression algorithms, so I'm not sure this is an answer to the exact question asked, about what compressions were usable with initramfs on the current kernel.
    – MattBianco
    Aug 9, 2023 at 8:35
  • /proc is not always available in containers like docker: If these are getting stripped down to the minimum for security reasons, it may have been omitted deliberately.
    – Juergen
    Nov 24, 2023 at 16:36
  • @Juergen Anything in a docker container will also never interact with the compression algorithms in the kernel, so there is never any need to check for kernel compression algorithms inside a docker container.
    – Samveen
    Nov 24, 2023 at 17:39
0

You can get to know your compression type by going into the directory sys/power/tuxonice/compression there u will a file named algorithm go into it it will have the decompression type in it for me it was written lzo.

1
  • /sys/power/tuxonice does not exist on a typical Linux.
    – Juergen
    Nov 24, 2023 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .