SquashFS is an open source, read only, extremely compressible filesystem. Like other filesystems SquashFS is capable of de-duplicating the data passed to it, which helps it compress data further. Although not fully necessary to operate correctly, SquashFS is typically used with some kind of union filesystem when used for Live media (LiveUSBs and LiveCDs).
Activate the following kernel options must be activated for SquashFS support:
File systems ---> [*] Miscellaneous filesystems ---> [*] SquashFS 4.0 - Squashed file system support
Optional SquashFS support
File systems ---> [*] Miscellaneous filesystems ---> File decompression options (Decompress file data into an intermediate buffer) ---> Decompressor parallelisation options (Single threaded compression) ---> [*] Squashfs XATTR support [*] Include support for ZLIB compressed file systems [*] Include support for LZ4 compressed file systems [*] Include support for LZO compressed file systems [*] Include support for XZ compressed file systems [*] Use 4K device block size? [*] Additional option for memory-constrained systems (3) Number of fragments cached
Like most filesystems in Linux, the SquashFS filesystem tools come in a separate package. This package is called sys-fs/squashfs-tools. Set the desired support for the package by adjusting USE flags accordingly.
USE flags for sys-fs/squashfs-tools Tool for creating compressed filesystem type squashfs
||Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces||global|
||Enable support for LZ4 compression using app-arch/lz4||local|
||Support for LZMA (de)compression algorithm||global|
||Enable support for lzo compression||global|
||!!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically||global|
||Add support for extended attributes (filesystem-stored metadata)||global|
||Enable support for XZ ("LZMA2") compression using app-arch/xz-utils||local|
||Enable support for ZSTD compression using app-arch/zstd||local|
After setting flags as desired, update the system so the changes take effect:
emerge --ask --changed-use --deep sys-fs/squashfs-tools
Creation of SquashFS filesystems is performed with the mksquashfs command. To create a SquashFS filesystem of the home directory of a user named Larry, the following command could be used:
mksquashfs /home/larry /home/larry/home.squashfs
Depending on what kernel features were selected for SquashFS support, the output of the mksquashfs command will look something like the following:
Parallel mksquashfs: Using 4 processors Creating 4.0 filesystem on /home/larry/home.squashfs, block size 131072. [=========================================================================|] 4/4 100% Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 131072 compressed data, compressed metadata, compressed fragments, compressed xattrs duplicates are removed Filesystem size 0.82 Kbytes (0.00 Mbytes) 58.79% of uncompressed filesystem size (1.39 Kbytes) Inode table size 104 bytes (0.10 Kbytes) 46.02% of uncompressed inode table size (226 bytes) Directory table size 96 bytes (0.09 Kbytes) 77.42% of uncompressed directory table size (124 bytes) Number of duplicate files found 1 Number of inodes 7 Number of files 5 Number of fragments 1 Number of symbolic links 0 Number of device nodes 0 Number of fifo nodes 0 Number of socket nodes 0 Number of directories 2 Number of ids (unique uids + gids) 1 Number of uids 1 larry (1001) Number of gids 1 larry (1001)
Notice the command provides an excellent summary of what went into the newly created home.squashfs file. Information such as filesystem size, inode table, directory table, number of duplicate files, UIDs, and GUIs are easily readable. This information can be very helpful when attempting to gather specific information from the filesystem.
To mount a SquashFS, use the mount command's
-o loop option with escalated privileges:
mount -o loop ~/home.squashfs ~/tmp
If the mount command fails, see the related entry in the troubleshooting section below.
Now all the files that are included in home.squashfs are available under the ~/tmp mount point. List the files in the directory using the
-la options to see all the files:
ls -la ~/tmp
total 3 drwxr-xr-x 3 larry larry 125 Mar 31 13:51 . drwxr-xr-x 1 larry larry 130 Mar 31 13:52 .. -rw------- 1 larry larry 10 Mar 31 13:49 .bash_history -rw-r--r-- 1 larry larry 127 Mar 24 13:19 .bash_logout -rw-r--r-- 1 larry larry 193 Mar 24 13:19 .bash_profile -rw-r--r-- 1 larry larry 551 Mar 24 13:19 .bashrc -rw-r--r-- 1 larry larry 0 Mar 31 13:51 home.squashfs drwx------ 2 larry larry 3 Oct 23 06:52 .ssh
To unmount the filesystem, use the umount command with escalated privileges:
sudo umount ~/tmp
SquashFS files can be extracted using unsquashfs. Supposing the ~/tmp directory and the ~/home.squashfs file have been previously created in the steps above:
unsquashfs -d tmp/ -f home.squashfs
- If a filesystem target is not specified (~/tmp is the target in the example above) unsquashfs will create a folder called squashfs-root in the current directory and extract the files there.
- If a directory exists previous to the unsquashfs command being run, then the
-foptions must be used in order to force SquashFS extraction to the existing directory 
- When creating extracting to a new directory, the
-foption is not needed.
unsquashfs can be used to extract a specific file in the SquashFS. Again, presuming the ~/home.squashfs file has been previously created in the steps above, the .bashrc file can be extracted to the ~/tmp directory:
unsquashfs -d ~/tmp -f ~/home.squashfs -e .bashrc
Parallel unsquashfs: Using 4 processors 1 inodes (1 blocks) to write [===========================|] 1/1 100% created 1 files created 1 directories created 0 symlinks created 0 devices created 0 fifos
View the extract file using the ls command:
ls -la ~/tmp
total 4 drwxr-xr-x 1 larry larry 14 Mar 31 13:51 . drwxr-xr-x 1 larry larry 156 Mar 31 14:32 .. -rw-r--r-- 1 larry larry 551 Mar 24 13:19 .bashrc
mount: only root can use "--options" option
This error is should be self-explanatory. Login as the root user or use sudo to run mount the filesystem with escalated privileges.
- UnionFS - The original union filesystem.
- Aufs - Another union filesystem (based on UnionFS).
- OverlayFS - An overlay filesystem that exists in kernels 3.18 and higher.
- A SquashFS instructional video on YouTube.
- Forums thread TIP: Compressing portage using squashfs: initscript method
- Article/Script using SquashFS to store and update portage tree
- Portage tree using squashfs and overlayfs
- GitHub - nrdvana/squash-portage: Script to generate squashfs files of the Gentoo portage tree