Back to home page

OSCL-LXR

 
 

    


0001 ============================
0002 XZ data compression in Linux
0003 ============================
0004 
0005 Introduction
0006 ============
0007 
0008 XZ is a general purpose data compression format with high compression
0009 ratio and relatively fast decompression. The primary compression
0010 algorithm (filter) is LZMA2. Additional filters can be used to improve
0011 compression ratio even further. E.g. Branch/Call/Jump (BCJ) filters
0012 improve compression ratio of executable data.
0013 
0014 The XZ decompressor in Linux is called XZ Embedded. It supports
0015 the LZMA2 filter and optionally also BCJ filters. CRC32 is supported
0016 for integrity checking. The home page of XZ Embedded is at
0017 <https://tukaani.org/xz/embedded.html>, where you can find the
0018 latest version and also information about using the code outside
0019 the Linux kernel.
0020 
0021 For userspace, XZ Utils provide a zlib-like compression library
0022 and a gzip-like command line tool. XZ Utils can be downloaded from
0023 <https://tukaani.org/xz/>.
0024 
0025 XZ related components in the kernel
0026 ===================================
0027 
0028 The xz_dec module provides XZ decompressor with single-call (buffer
0029 to buffer) and multi-call (stateful) APIs. The usage of the xz_dec
0030 module is documented in include/linux/xz.h.
0031 
0032 The xz_dec_test module is for testing xz_dec. xz_dec_test is not
0033 useful unless you are hacking the XZ decompressor. xz_dec_test
0034 allocates a char device major dynamically to which one can write
0035 .xz files from userspace. The decompressed output is thrown away.
0036 Keep an eye on dmesg to see diagnostics printed by xz_dec_test.
0037 See the xz_dec_test source code for the details.
0038 
0039 For decompressing the kernel image, initramfs, and initrd, there
0040 is a wrapper function in lib/decompress_unxz.c. Its API is the
0041 same as in other decompress_*.c files, which is defined in
0042 include/linux/decompress/generic.h.
0043 
0044 scripts/xz_wrap.sh is a wrapper for the xz command line tool found
0045 from XZ Utils. The wrapper sets compression options to values suitable
0046 for compressing the kernel image.
0047 
0048 For kernel makefiles, two commands are provided for use with
0049 $(call if_needed). The kernel image should be compressed with
0050 $(call if_needed,xzkern) which will use a BCJ filter and a big LZMA2
0051 dictionary. It will also append a four-byte trailer containing the
0052 uncompressed size of the file, which is needed by the boot code.
0053 Other things should be compressed with $(call if_needed,xzmisc)
0054 which will use no BCJ filter and 1 MiB LZMA2 dictionary.
0055 
0056 Notes on compression options
0057 ============================
0058 
0059 Since the XZ Embedded supports only streams with no integrity check or
0060 CRC32, make sure that you don't use some other integrity check type
0061 when encoding files that are supposed to be decoded by the kernel. With
0062 liblzma, you need to use either LZMA_CHECK_NONE or LZMA_CHECK_CRC32
0063 when encoding. With the xz command line tool, use --check=none or
0064 --check=crc32.
0065 
0066 Using CRC32 is strongly recommended unless there is some other layer
0067 which will verify the integrity of the uncompressed data anyway.
0068 Double checking the integrity would probably be waste of CPU cycles.
0069 Note that the headers will always have a CRC32 which will be validated
0070 by the decoder; you can only change the integrity check type (or
0071 disable it) for the actual uncompressed data.
0072 
0073 In userspace, LZMA2 is typically used with dictionary sizes of several
0074 megabytes. The decoder needs to have the dictionary in RAM, thus big
0075 dictionaries cannot be used for files that are intended to be decoded
0076 by the kernel. 1 MiB is probably the maximum reasonable dictionary
0077 size for in-kernel use (maybe more is OK for initramfs). The presets
0078 in XZ Utils may not be optimal when creating files for the kernel,
0079 so don't hesitate to use custom settings. Example::
0080 
0081         xz --check=crc32 --lzma2=dict=512KiB inputfile
0082 
0083 An exception to above dictionary size limitation is when the decoder
0084 is used in single-call mode. Decompressing the kernel itself is an
0085 example of this situation. In single-call mode, the memory usage
0086 doesn't depend on the dictionary size, and it is perfectly fine to
0087 use a big dictionary: for maximum compression, the dictionary should
0088 be at least as big as the uncompressed data itself.
0089 
0090 Future plans
0091 ============
0092 
0093 Creating a limited XZ encoder may be considered if people think it is
0094 useful. LZMA2 is slower to compress than e.g. Deflate or LZO even at
0095 the fastest settings, so it isn't clear if LZMA2 encoder is wanted
0096 into the kernel.
0097 
0098 Support for limited random-access reading is planned for the
0099 decompression code. I don't know if it could have any use in the
0100 kernel, but I know that it would be useful in some embedded projects
0101 outside the Linux kernel.
0102 
0103 Conformance to the .xz file format specification
0104 ================================================
0105 
0106 There are a couple of corner cases where things have been simplified
0107 at expense of detecting errors as early as possible. These should not
0108 matter in practice all, since they don't cause security issues. But
0109 it is good to know this if testing the code e.g. with the test files
0110 from XZ Utils.
0111 
0112 Reporting bugs
0113 ==============
0114 
0115 Before reporting a bug, please check that it's not fixed already
0116 at upstream. See <https://tukaani.org/xz/embedded.html> to get the
0117 latest code.
0118 
0119 Report bugs to <lasse.collin@tukaani.org> or visit #tukaani on
0120 Freenode and talk to Larhzu. I don't actively read LKML or other
0121 kernel-related mailing lists, so if there's something I should know,
0122 you should email to me personally or use IRC.
0123 
0124 Don't bother Igor Pavlov with questions about the XZ implementation
0125 in the kernel or about XZ Utils. While these two implementations
0126 include essential code that is directly based on Igor Pavlov's code,
0127 these implementations aren't maintained nor supported by him.