Written by Christian Ahmer | 11/24/2023

SquashFS

SquashFS is a compressed read-only filesystem for Linux, providing excellent compression ratios and high performance when reading data from disk, even with the overhead of on-the-fly decompression. It was designed primarily to be used for system software deployment where read-only access is a standard requirement, such as in embedded systems or live operating system distributions.


The fundamental concept behind SquashFS is to allow a file system to be compressed as a whole, thereby conserving disk space while still enabling individual files to be accessed without the need to decompress the entire file system. SquashFS achieves this by dividing the file system into blocks, typically ranging from 4KB to 1MB, and compressing each block independently. This strategy permits random access to files, which is crucial for a usable system.

SquashFS supports a variety of compression algorithms, including Gzip, LZO, LZ4, Zstandard, and XZ. This flexibility allows developers and system integrators to make trade-offs between compression speed, decompression speed, and the level of compression. Depending on the specific requirements of the use case—such as the need for faster decompression or maximum space saving—one algorithm may be preferred over the others.

An advantage of SquashFS is its support for all the standard Linux file system features, such as hard links, symbolic links, device nodes, FIFOs and sockets, extended attributes, and sparse files. This compatibility makes it easy to convert an existing Linux file system into SquashFS without losing any functionality or having to modify the file system contents.

SquashFS also includes advanced features such as deduplication. It can identify duplicate blocks within the file system during the creation process and store only one copy of each. This not only saves space but also allows for more efficient reads since a single cache entry can serve multiple read requests.

For file systems that require both a read-only portion and a writable portion, SquashFS can be combined with a writable file system through a union mount. This approach is widely used in live CD/DVD environments where the base operating system is stored in a SquashFS image, and a temporary file system (like tmpfs) is used for runtime changes.

Creating a SquashFS image is a straightforward process. The `mksquashfs` tool is used to convert a directory tree into a SquashFS file system image, which can then be mounted using the `mount` command, just like any other file system. The `unsquashfs` tool can extract the entire contents of a SquashFS file system, or individual files, for inspection or modification.

The read-only nature of SquashFS provides several benefits in terms of security and stability. Since the file system cannot be written to, it is immune to corruption from system crashes or power failures during write operations. This also means that the file system is secure against malware that attempts to modify system files.

Due to its high compression, fast decompression, and read-only architecture, SquashFS is an excellent choice for distributing applications or operating systems where space is at a premium, such as in embedded devices or when shipping software on physical media. It's also a preferred choice for the root file system on live CDs and USB sticks, as it allows for a large amount of software to be packed into a small space, with the additional security benefits of being read-only.

In summary, SquashFS is a specialized file system that addresses the needs of read-only data distribution, combining space efficiency with performance. Its use of block-based compression, support for multiple compression algorithms, and compatibility with standard Linux file system features make it a powerful tool for a wide array of Linux-based applications.