Path: blob/main/sys/contrib/xz-embedded/linux/Documentation/xz.txt
48375 views
1XZ data compression in Linux2============================34Introduction56XZ is a general purpose data compression format with high compression7ratio and relatively fast decompression. The primary compression8algorithm (filter) is LZMA2. Additional filters can be used to improve9compression ratio even further. E.g. Branch/Call/Jump (BCJ) filters10improve compression ratio of executable data.1112The XZ decompressor in Linux is called XZ Embedded. It supports13the LZMA2 filter and optionally also BCJ filters. CRC32 is supported14for integrity checking. The home page of XZ Embedded is at15<https://tukaani.org/xz/embedded.html>, where you can find the16latest version and also information about using the code outside17the Linux kernel.1819For userspace, XZ Utils provide a zlib-like compression library20and a gzip-like command line tool. XZ Utils can be downloaded from21<https://tukaani.org/xz/>.2223XZ related components in the kernel2425The xz_dec module provides XZ decompressor with single-call (buffer26to buffer) and multi-call (stateful) APIs. The usage of the xz_dec27module is documented in include/linux/xz.h.2829The xz_dec_test module is for testing xz_dec. xz_dec_test is not30useful unless you are hacking the XZ decompressor. xz_dec_test31allocates a char device major dynamically to which one can write32.xz files from userspace. The decompressed output is thrown away.33Keep an eye on dmesg to see diagnostics printed by xz_dec_test.34See the xz_dec_test source code for the details.3536For decompressing the kernel image, initramfs, and initrd, there37is a wrapper function in lib/decompress_unxz.c. Its API is the38same as in other decompress_*.c files, which is defined in39include/linux/decompress/generic.h.4041scripts/xz_wrap.sh is a wrapper for the xz command line tool found42from XZ Utils. The wrapper sets compression options to values suitable43for compressing the kernel image.4445For kernel makefiles, two commands are provided for use with46$(call if_needed). The kernel image should be compressed with47$(call if_needed,xzkern) which will use a BCJ filter and a big LZMA248dictionary. It will also append a four-byte trailer containing the49uncompressed size of the file, which is needed by the boot code.50Other things should be compressed with $(call if_needed,xzmisc)51which will use no BCJ filter and 1 MiB LZMA2 dictionary.5253Notes on compression options5455Since the XZ Embedded supports only streams with no integrity check or56CRC32, make sure that you don't use some other integrity check type57when encoding files that are supposed to be decoded by the kernel. With58liblzma, you need to use either LZMA_CHECK_NONE or LZMA_CHECK_CRC3259when encoding. With the xz command line tool, use --check=none or60--check=crc32.6162Using CRC32 is strongly recommended unless there is some other layer63which will verify the integrity of the uncompressed data anyway.64Double checking the integrity would probably be waste of CPU cycles.65Note that the headers will always have a CRC32 which will be validated66by the decoder; you can only change the integrity check type (or67disable it) for the actual uncompressed data.6869In userspace, LZMA2 is typically used with dictionary sizes of several70megabytes. The decoder needs to have the dictionary in RAM, thus big71dictionaries cannot be used for files that are intended to be decoded72by the kernel. 1 MiB is probably the maximum reasonable dictionary73size for in-kernel use (maybe more is OK for initramfs). The presets74in XZ Utils may not be optimal when creating files for the kernel,75so don't hesitate to use custom settings. Example:7677xz --check=crc32 --lzma2=dict=512KiB inputfile7879An exception to above dictionary size limitation is when the decoder80is used in single-call mode. Decompressing the kernel itself is an81example of this situation. In single-call mode, the memory usage82doesn't depend on the dictionary size, and it is perfectly fine to83use a big dictionary: for maximum compression, the dictionary should84be at least as big as the uncompressed data itself.8586Future plans8788Creating a limited XZ encoder may be considered if people think it is89useful. LZMA2 is slower to compress than e.g. Deflate or LZO even at90the fastest settings, so it isn't clear if LZMA2 encoder is wanted91into the kernel.9293Support for limited random-access reading is planned for the94decompression code. I don't know if it could have any use in the95kernel, but I know that it would be useful in some embedded projects96outside the Linux kernel.9798Conformance to the .xz file format specification99100There are a couple of corner cases where things have been simplified101at expense of detecting errors as early as possible. These should not102matter in practice all, since they don't cause security issues. But103it is good to know this if testing the code e.g. with the test files104from XZ Utils.105106Reporting bugs107108Before reporting a bug, please check that it's not fixed already109at upstream. See <https://tukaani.org/xz/embedded.html> to get the110latest code.111112Report bugs to <[email protected]> or visit #tukaani on113Freenode and talk to Larhzu. I don't actively read LKML or other114kernel-related mailing lists, so if there's something I should know,115you should email to me personally or use IRC.116117Don't bother Igor Pavlov with questions about the XZ implementation118in the kernel or about XZ Utils. While these two implementations119include essential code that is directly based on Igor Pavlov's code,120these implementations aren't maintained nor supported by him.121122123124