Creating a highly compressed SquashFS from a folder

Problem:

You have a large compressible read-only folder that eats up a lot of disk space. You want to compress it using SquashFS.

Solution:

Simply run the following command (replace the placeholders by the appropriate files/directories):

mksquashfs [source folder] [SquashFS target file] -b 1048576 -comp xz -Xdict-size 100%

This command compresses the entire source folder into the SquashFS target file. See below for information about the compression options.

Make sure that the SquashFS target file doesn’t exist before using this command. If it already exists, mksquashfs tries to update it, but this might yield undesirable results (I didn’t check that, it probably only produces a lot of error messages but a valid output file)

You can mount the resulting file using this command (assuming the target folder exists and is empty):

mount [SquashFS file] [folder you want to mount it in]

If this command fails, you might need to specify some options explicitly:

mount -o loop -t squashfs [SquashFS file] [folder you want to mount it in]

If you want to auto-mount the SquashFS, you can also add an /etc/fstab entry like this:

[SquashFS file] [folder you want to mount it in] squashfs auto,defaults 0 0

XZ vs GZip Compression

mksquashfs compresses using gzip (deflate) as default, but if the compression time doesn’t matter as much as final size, you should use the xz (LZMA2) option of more recent SquashFS version.

The command above activates xz (-comp xz) using the highest possible compression options and the highest possible blocksize (1 MiB instead of the default 128 kiB). Therefore the process of creating the SquashFS file is slower than if using default options, but the resulting file is much smaller and might be (depending on disk IO time etc) a bit faster. LZMA2 is a highly asymmetric compression algorithm, so decompression is much faster and uses less memory than compression