SquashFS

On this page 7

SquashFS is a compressed, read-only filesystem for Linux that stores an entire directory tree, including permissions, ownership, symbolic links and extended attributes, inside a single image file or block device. The kernel decompresses data on demand when files are read, so a large system can live in a small image without ever being unpacked to disk.

In the Linux filesystem landscape, SquashFS fills a niche that writable filesystems such as ext4, XFS and Btrfs do not: immutable, space-efficient distribution of complete trees. It underpins live installation media, embedded router firmware, Snap packages, AppImage bundles and many container and initramfs designs, usually combined with OverlayFS or tmpfs when a writable layer is needed.

How SquashFS stores and compresses data

SquashFS builds its image once, with the user-space tool mksquashfs, and the kernel only ever reads it. Because the layout never changes after creation, the format can pack metadata tightly and compress file data, inodes and directory tables separately, which a writable filesystem cannot do without heavy fragmentation.

File contents are split into data blocks of 4 KiB to 1 MiB, with 128 KiB as the default. Each block is compressed independently, so the kernel can decompress only the block that holds the requested byte range. Small files and file tails are gathered into shared fragment blocks, which avoids wasting a whole block on a 300-byte configuration file. Identical files are detected during creation and stored once through duplicate detection.

The image consists of a small number of regions in a fixed order. The following table lists them and what each one contributes.

RegionContentPurpose
SuperblockMagic number, block size, compressor, countsIdentifies the image and its parameters
Compression optionsCompressor-specific settingsTells the kernel how to decompress
Data blocks and fragmentsCompressed file contentsHolds the actual file data
Inode tableCompressed inode recordsStores mode, owner, size and timestamps
Directory tableCompressed directory entriesMaps names to inodes
Fragment, export and ID tablesLookup indexesLocate fragments, support NFS export, map UIDs and GIDs
Extended attribute tableCompressed xattrsKeeps SELinux labels and file capabilities
Main regions of a SquashFS image

Reads go through the normal page cache. Since kernel 5.x, SquashFS can also decompress directly into page cache pages and use multiple decompressor threads per CPU, which reduces the latency penalty that early versions showed on random access.

Compression algorithms and their trade-offs

The compressor is chosen when the image is built and recorded in the superblock; the kernel must have the matching decompressor enabled. The choice is a trade-off between image size, build time and read speed, and the right answer depends on whether storage or CPU is the scarcer resource.

CompressorRatioDecompression speedTypical use
gzipGoodModerateDefault, maximum compatibility
xzBestSlowLive media and firmware where size matters most
zstdVery goodFastModern distributions and Snap packages
lz4ModestVery fastRead-heavy workloads on slow CPUs
lzoModestFastOlder embedded systems
lzmaBestSlowLegacy images, superseded by xz
Compressors supported by SquashFS

zstd at levels 15 to 19 has become a common default because it approaches xz ratios while decompressing several times faster. xz with the BCJ filter for x86 or ARM shrinks executable code further, which is why firmware builders still favor it. Larger block sizes improve ratio but force more data to be decompressed for each small random read.

SquashFS is best understood against the filesystems it is usually combined with or mistaken for. It is not a general-purpose disk filesystem, and it is not a replacement for archive formats when files must be extracted and modified.

TechnologyWritableCompressionMain role
SquashFSNoAlways, whole imageImmutable compressed trees
EROFSNoOptional, fixed-size outputRead-only system partitions with low latency
OverlayFSUpper layer onlyNone of its ownAdds a writable layer over a read-only lower layer
tmpfsYesNoneRAM-backed scratch space
ext4YesNoneGeneral-purpose disk storage
BtrfsYesOptional, per extentWritable storage with transparent compression
tar.xz archiveNoWhole streamTransfer; must be extracted before use
SquashFS and neighboring filesystem technologies

The key difference from a tar archive is random access: a SquashFS image is mounted and individual files are read in place, whereas a compressed tarball must be read sequentially. Compared with EROFS, SquashFS usually achieves better compression ratios, while EROFS was designed for lower read latency on phones and is increasingly preferred for Android system partitions.

Creating and mounting SquashFS images

The workflow has two steps: build the image from a directory, then mount it. mksquashfs rootfs/ root.sqfs -comp zstd -b 1M creates an image with zstd and 1 MiB blocks, and mount -t squashfs -o loop root.sqfs /mnt attaches it through a loop device. The companion tool unsquashfs lists or extracts contents without mounting.

  • Use `-all-root` or -force-uid when building images from an unprivileged build tree so ownership is correct.
  • Use `-xattrs` (default in recent versions) to keep SELinux labels and file capabilities.
  • Use -e or an exclude file to keep caches and build artifacts out of the image.
  • Use `squashfuse` to mount images without root privileges through FUSE, as AppImage does.
  • Add an fstab entry with the loop option when an image must mount at boot.

The choice of options depends on the workload. The decision table below summarizes common situations.

SituationRecommended settingsReason
Live ISO for downloadxz or zstd -Xcompression-level 19, 1 MiB blocksSmallest download size
Router firmware on small flashxz with BCJ filter, 256 KiB blocksFits limited flash, code compresses well
Application bundle launched oftenzstd or lz4, 128 KiB blocksFast startup and random reads
Container base layerszstd, default block sizeBalance of size and access speed
Choosing SquashFS build options

Limitations and common mistakes

The defining limitation is that SquashFS images cannot be modified in place. Any change, even a single byte, requires rebuilding the image or adding a writable layer. Designs that need updates therefore use OverlayFS on top of SquashFS, A/B image slots, or delta updates that replace the whole image atomically.

  • Random-read performance on slow CPUs can suffer with xz and large blocks, causing slow application startup.
  • Building on a host with the wrong UID mapping produces images where files belong to the build user instead of root.
  • Forgetting -xattrs strips file capabilities, so tools such as ping may fail inside the image.
  • Mounting an image whose compressor is not enabled in the kernel fails with an unhelpful error.
  • Treating SquashFS as tamper-proof is wrong: integrity requires dm-verity or a signed image, not compression alone.

The short version

SquashFS is a way to shrink a whole folder of files into one compact package that a Linux computer can open and use directly, without unpacking it first. Think of a vacuum-sealed bag of clothes that you could still reach into and pull out a single shirt.

The catch is that the package is sealed for reading only. To change anything, you build a new package or place a writable layer on top. That makes it ideal for things that should stay the same, such as an installer on a USB stick or the software inside a home router.

Where it makes a difference

A team that maintains firmware for a line of home routers has 16 MB of flash storage per device, but the root filesystem with its web interface, network daemons and libraries takes about 38 MB uncompressed. They build the root tree with mksquashfs using xz with the ARM BCJ filter and 256 KiB blocks, which brings the image down to roughly 11 MB.

At boot, the kernel mounts the SquashFS image as the read-only root, and a small writable JFFS2 or UBIFS partition is layered on top with OverlayFS to hold user settings. Firmware updates replace the whole image, and a factory reset simply wipes the overlay. The outcome is a device that fits its flash budget, recovers cleanly from configuration mistakes and cannot have its core system files accidentally altered.