LinuxCommandLibrary

mksquashfs

Create a compressed SquashFS filesystem image

TLDR

Create or append files and directories to a squashfs filesystem (compressed using gzip by default)

$ mksquashfs [path/to/file_or_directory1 path/to/file_or_directory2 ...] [filesystem.squashfs]
copy

Create or append files and directories to a squashfs filesystem, using a specific [comp]ression algorithm
$ mksquashfs [path/to/file_or_directory1 path/to/file_or_directory2 ...] [filesystem.squashfs] -comp [gzip|lzo|lz4|xz|zstd|lzma]
copy

Create or append files and directories to a squashfs filesystem, [e]xcluding some of them
$ mksquashfs [path/to/file_or_directory1 path/to/file_or_directory2 ...] [filesystem.squashfs] -e [file|directory1 file|directory2 ...]
copy

Create or append files and directories to a squashfs filesystem, [e]xcluding those ending with gzip
$ mksquashfs [path/to/file_or_directory1 path/to/file_or_directory2 ...] [filesystem.squashfs] -wildcards -e "[*.gz]"
copy

Create or append files and directories to a squashfs filesystem, [e]xcluding those matching a regex
$ mksquashfs [path/to/file_or_directory1 path/to/file_or_directory2 ...] [filesystem.squashfs] -regex -e "[regex]"
copy

SYNOPSIS

mksquashfs source1 [source2 ...] destination.squashfs [options]

PARAMETERS

-b SIZE
    Sets the data block size (e.g., 1M, 512K). Default is 1MB. Larger blocks often yield better compression but use more memory during decompression.

-comp ALGORITHM
    Specifies the compression algorithm to use. Common values include gzip, lzma, lzo, xz, zstd, lz4.

-X OPTION
    Passes specific options directly to the selected compression algorithm (e.g., -Xcompression-level 9 for Gzip/XZ).

-p N
    Uses N processors (cores) for parallel compression, significantly speeding up image creation on multi-core systems.

-e PATTERN
    Excludes files or directories matching the specified pattern from the Squashfs image.

-no-duplicates
    Disables checking for duplicate files. This can speed up creation but might result in a larger image if duplicates exist.

-root-becomes DIR
    Makes the specified directory DIR the new root of the Squashfs filesystem, effectively placing all content under it.

-info
    Prints a detailed list of all files and directories as they are added to the Squashfs archive.

-noD
    Disables compression of data blocks. Only metadata will be compressed.

-all-root
    Sets all file and directory ownership within the Squashfs image to root (UID 0, GID 0).

-noappend
    Prevents appending to an existing Squashfs file. If the destination file exists, mksquashfs will overwrite it.

-version
    Displays the mksquashfs utility version information and exits.

DESCRIPTION

mksquashfs is a powerful utility for creating Squashfs filesystems. Squashfs is a compressed, read-only filesystem designed for Linux, commonly used for distributing live CDs, embedded Linux systems, and firmware images due to its excellent compression ratios and efficient read performance.

It supports various compression algorithms (e.g., Gzip, LZMA, LZO, XZ, Zstandard, LZ4), allowing users to balance compression ratio with speed. mksquashfs can process multiple input sources (directories, files, or even other Squashfs images) and merge them into a single output Squashfs file, handling deduplication of identical files and metadata. It's optimized for both space saving and fast access to packaged content.

CAVEATS

Squashfs is a read-only filesystem; content within the created image cannot be modified directly after creation. Any changes require rebuilding the entire image. Choosing the optimal block size and compression algorithm involves a trade-off between image size, creation time, and decompression performance.

COMMON USE CASES

mksquashfs is extensively used for creating compressed archives for distribution, such as bootable live CD/USB images, firmware updates for embedded devices, and as a lightweight, read-only root filesystem for system installations or containers. Its efficiency also makes it suitable for archiving large collections of static data to save disk space.

PERFORMANCE TUNING

Key options for performance tuning are -b (block size) and -comp (compression algorithm) with its -X options. Larger block sizes often lead to better compression but require more RAM during decompression. Modern algorithms like zstd or lz4 offer excellent decompression speeds, while xz typically achieves the best compression ratio. Using -p N (parallel processing) can drastically reduce image creation time on multi-core machines.

HISTORY

Squashfs, and subsequently mksquashfs, was originally developed by Phillip Lougher, with its first public release around 2002. It gained significant traction due to its efficient compression and read-only nature, becoming a standard for Linux live distributions (e.g., Ubuntu Live CDs) and embedded systems. The Squashfs kernel module was integrated into the Linux mainline kernel from version 2.6.29, cementing its role as a widely adopted filesystem for static data.

SEE ALSO

unsquashfs(1), mount(8), squashfs(5), tar(1), gzip(1)

Copied to clipboard