Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 1 | ============================ |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 2 | XZ data compression in Linux |
| 3 | ============================ |
| 4 | |
| 5 | Introduction |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 6 | ============ |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 7 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 8 | XZ is a general purpose data compression format with high compression |
| 9 | ratio and relatively fast decompression. The primary compression |
| 10 | algorithm (filter) is LZMA2. Additional filters can be used to improve |
| 11 | compression ratio even further. E.g. Branch/Call/Jump (BCJ) filters |
| 12 | improve compression ratio of executable data. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 13 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 14 | The XZ decompressor in Linux is called XZ Embedded. It supports |
| 15 | the LZMA2 filter and optionally also BCJ filters. CRC32 is supported |
| 16 | for integrity checking. The home page of XZ Embedded is at |
| 17 | <http://tukaani.org/xz/embedded.html>, where you can find the |
| 18 | latest version and also information about using the code outside |
| 19 | the Linux kernel. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 20 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 21 | For userspace, XZ Utils provide a zlib-like compression library |
| 22 | and a gzip-like command line tool. XZ Utils can be downloaded from |
| 23 | <http://tukaani.org/xz/>. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 24 | |
| 25 | XZ related components in the kernel |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 26 | =================================== |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 27 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 28 | The xz_dec module provides XZ decompressor with single-call (buffer |
| 29 | to buffer) and multi-call (stateful) APIs. The usage of the xz_dec |
| 30 | module is documented in include/linux/xz.h. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 31 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 32 | The xz_dec_test module is for testing xz_dec. xz_dec_test is not |
| 33 | useful unless you are hacking the XZ decompressor. xz_dec_test |
| 34 | allocates a char device major dynamically to which one can write |
| 35 | .xz files from userspace. The decompressed output is thrown away. |
| 36 | Keep an eye on dmesg to see diagnostics printed by xz_dec_test. |
| 37 | See the xz_dec_test source code for the details. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 38 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 39 | For decompressing the kernel image, initramfs, and initrd, there |
| 40 | is a wrapper function in lib/decompress_unxz.c. Its API is the |
| 41 | same as in other decompress_*.c files, which is defined in |
| 42 | include/linux/decompress/generic.h. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 43 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 44 | scripts/xz_wrap.sh is a wrapper for the xz command line tool found |
| 45 | from XZ Utils. The wrapper sets compression options to values suitable |
| 46 | for compressing the kernel image. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 47 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 48 | For kernel makefiles, two commands are provided for use with |
| 49 | $(call if_needed). The kernel image should be compressed with |
| 50 | $(call if_needed,xzkern) which will use a BCJ filter and a big LZMA2 |
| 51 | dictionary. It will also append a four-byte trailer containing the |
| 52 | uncompressed size of the file, which is needed by the boot code. |
| 53 | Other things should be compressed with $(call if_needed,xzmisc) |
| 54 | which will use no BCJ filter and 1 MiB LZMA2 dictionary. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 55 | |
| 56 | Notes on compression options |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 57 | ============================ |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 58 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 59 | Since the XZ Embedded supports only streams with no integrity check or |
| 60 | CRC32, make sure that you don't use some other integrity check type |
| 61 | when encoding files that are supposed to be decoded by the kernel. With |
| 62 | liblzma, you need to use either LZMA_CHECK_NONE or LZMA_CHECK_CRC32 |
| 63 | when encoding. With the xz command line tool, use --check=none or |
| 64 | --check=crc32. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 65 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 66 | Using CRC32 is strongly recommended unless there is some other layer |
| 67 | which will verify the integrity of the uncompressed data anyway. |
| 68 | Double checking the integrity would probably be waste of CPU cycles. |
| 69 | Note that the headers will always have a CRC32 which will be validated |
| 70 | by the decoder; you can only change the integrity check type (or |
| 71 | disable it) for the actual uncompressed data. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 72 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 73 | In userspace, LZMA2 is typically used with dictionary sizes of several |
| 74 | megabytes. The decoder needs to have the dictionary in RAM, thus big |
| 75 | dictionaries cannot be used for files that are intended to be decoded |
| 76 | by the kernel. 1 MiB is probably the maximum reasonable dictionary |
| 77 | size for in-kernel use (maybe more is OK for initramfs). The presets |
| 78 | in XZ Utils may not be optimal when creating files for the kernel, |
| 79 | so don't hesitate to use custom settings. Example:: |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 80 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 81 | xz --check=crc32 --lzma2=dict=512KiB inputfile |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 82 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 83 | An exception to above dictionary size limitation is when the decoder |
| 84 | is used in single-call mode. Decompressing the kernel itself is an |
| 85 | example of this situation. In single-call mode, the memory usage |
| 86 | doesn't depend on the dictionary size, and it is perfectly fine to |
| 87 | use a big dictionary: for maximum compression, the dictionary should |
| 88 | be at least as big as the uncompressed data itself. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 89 | |
| 90 | Future plans |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 91 | ============ |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 92 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 93 | Creating a limited XZ encoder may be considered if people think it is |
| 94 | useful. LZMA2 is slower to compress than e.g. Deflate or LZO even at |
| 95 | the fastest settings, so it isn't clear if LZMA2 encoder is wanted |
| 96 | into the kernel. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 97 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 98 | Support for limited random-access reading is planned for the |
| 99 | decompression code. I don't know if it could have any use in the |
| 100 | kernel, but I know that it would be useful in some embedded projects |
| 101 | outside the Linux kernel. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 102 | |
| 103 | Conformance to the .xz file format specification |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 104 | ================================================ |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 105 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 106 | There are a couple of corner cases where things have been simplified |
| 107 | at expense of detecting errors as early as possible. These should not |
| 108 | matter in practice all, since they don't cause security issues. But |
| 109 | it is good to know this if testing the code e.g. with the test files |
| 110 | from XZ Utils. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 111 | |
| 112 | Reporting bugs |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 113 | ============== |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 114 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 115 | Before reporting a bug, please check that it's not fixed already |
| 116 | at upstream. See <http://tukaani.org/xz/embedded.html> to get the |
| 117 | latest code. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 118 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 119 | Report bugs to <lasse.collin@tukaani.org> or visit #tukaani on |
| 120 | Freenode and talk to Larhzu. I don't actively read LKML or other |
| 121 | kernel-related mailing lists, so if there's something I should know, |
| 122 | you should email to me personally or use IRC. |
Lasse Collin | 24fa040 | 2011-01-12 17:01:22 -0800 | [diff] [blame] | 123 | |
Mauro Carvalho Chehab | 29c8c4a | 2017-05-17 09:54:22 -0300 | [diff] [blame] | 124 | Don't bother Igor Pavlov with questions about the XZ implementation |
| 125 | in the kernel or about XZ Utils. While these two implementations |
| 126 | include essential code that is directly based on Igor Pavlov's code, |
| 127 | these implementations aren't maintained nor supported by him. |