Smaller SquashFS images by sorting file fragments
While working on the Slax build, I started looking at what happens to a small file on its way into a SquashFS image. Its contents are often compressed alongside other files. That means the file next to it can make a difference even when the compressor and all its settings stay the same.
The Slax build uses fragment-sort to generate a file order based on fragment contents. Using that order with the standard mksquashfs made the core system module, 01-core.sb, about 3% smaller. A 3–5% reduction is worth looking for in a tree with plenty of related files. The actual result depends on what is in the image and how much of it fits into fragments. The output is still an ordinary SquashFS filesystem. The native on-disk format is unchanged, so any Linux kernel with support for SquashFS and the image’s compression algorithm can read it.
What goes into a SquashFS fragment?
SquashFS works well for live systems because Linux can mount the compressed, read-only image and decompress blocks as it needs them. Slax uses SquashFS for its system modules. In my build, a data block holds up to 1 MiB of uncompressed data, the largest block size SquashFS supports.
What happens to files bigger than 1 MiB, or much smaller? By default, mksquashfs packs small files whole into shared fragment blocks. A 12 KiB file, for instance, can share a block with other small files. A larger file fills one or more data blocks. Any bytes left over form its tail. For example, take a file that is 1 MiB plus 256 KiB. Its first MiB fills a data block, leaving a 256 KiB tail. Tail-end packing is off by default. With the built-in -tailends option, that tail can go into a fragment block too, where it could share space with our 12 KiB file. Upstream mksquashfs guide
Now suppose the 12 KiB file and the tail contain some of the same text or code. In a shared fragment block, the compressor can encode the common bytes once and refer back to them. In separate blocks, compression starts fresh each time, so the same bytes have to be encoded again. By default, mksquashfs processes files in directory order. That tends to keep files from the same directory close together. Files in different parts of the tree can end up in separate fragment blocks even when their contents are similar.
To give similar fragments a better chance of sharing a block, we can change the order in which mksquashfs processes their files. Standard mksquashfs already accepts a sort list through -sort. You can write that list by hand. The fragment-sort tool builds the list automatically by comparing the contents of small files and eligible tails.
Find the neighbours that matter
So what makes two fragments good neighbours? First, they have to fit. With 1 MiB blocks, a 700 KiB file and a 350 KiB tail cannot fit together. Two large binaries can still be good candidates if their tails fit and share content. Libraries built from related code, similarly structured translations and generated text are good places to look for shared bytes. Already-compressed files usually offer fewer matches.
Checking the contents of every possible pair would take too long, so fragment-sort starts by looking for promising pairs. It slides an eight-byte window over each fragment and hashes the bytes at every position. It keeps a fingerprint only when the lowest nine bits of the hash are zero. With a well-distributed hash, that selects about one window in 512. The rule depends only on the eight bytes, so a selected window produces the same fingerprint even if it starts at a different offset in another file. An index tells it which fragments share a fingerprint. Fingerprints found in just one fragment or across too many fragments are dropped. For each fragment, the tool picks a few of the closest matches that can fit alongside it in one block.
The next question is whether putting those fragments together would save space. To estimate that, fragment-sort can try a quick compression test. With the optional --zstd-score flag, it uses one fragment’s bytes as a dictionary or prefix when compressing the other with Zstandard, then compares the size with compressing that fragment on its own. This score only informs the sort order. For the final image, Slax uses the XZ compressor and x86 BCJ filter already available in mksquashfs.
A list of good pairs still needs to become one order for the whole tree. To keep strong matches together without wasting block space, fragment-sort combines them into groups within the size limit and fits smaller groups into space left by larger ones. It writes that order as relative paths and numeric priorities for -sort. Stock mksquashfs applies the priorities, checks for duplicate files and decides where the fragment blocks actually end.
Try it on your own image
To measure the effect of the order, build two images from the same tree with identical options. The second build adds the sort list to the first build’s settings. Compile fragment-sort with the repository instructions. Run the example from a directory outside the source tree so the sort list and output images stay out of the image you are building.
The sorter needs to see the same fragment candidates as the builder. Give both tools the same block size with -b 1M and enable tails with -tailends. To let eligible tails and small files share a packing pool, both builds use the built-in mksquashfs action fragment(mixed)@true. Use -noappend so each build starts fresh. Keep the compression settings the same in both runs. This example uses XZ with the x86 BCJ filter from the Slax build. Upstream fragment actions
src=/mnt/source
./fragment-sort "$src" -b 1M -tailends \
--zstd-score --candidates 16 -o source.sort
mksquashfs "$src" baseline.sqsh -noappend \
-comp xz -Xbcj x86 -b 1M -tailends \
-action 'fragment(mixed)@true'
mksquashfs "$src" sorted.sqsh -noappend \
-comp xz -Xbcj x86 -b 1M -tailends \
-action 'fragment(mixed)@true' -sort source.sort
stat -c '%n %s bytes' baseline.sqsh sorted.sqshThe size difference between these two images is the effect of the sort list under those settings. To measure what the shared fragment pool contributes on its own, build a third image without the action or the sort list and compare it with the unsorted baseline.
By packing similar fragments together, Slax and other distributions that use SquashFS can make their images a little smaller while staying fully compatible with stock Linux kernels that support SquashFS and the chosen compression algorithm. Get fragment-sort on GitHub.
