Working with squashfs filesystems (with examples)
- Linux
- November 5, 2023
1: 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
Motivation: This command is used to create or append files and directories to a squashfs filesystem. Squashfs is a compressed read-only filesystem that is commonly used in embedded systems or for creating Live CDs. By using this command, you can easily create a squashfs filesystem containing all the specified files and directories.
Explanation: The mksquashfs
command takes one or more file or directory paths as input, followed by the name of the squashfs filesystem to create or append to. By default, the compression algorithm used is gzip
, which provides a good balance between compression ratio and speed.
Example Output:
Creating filesystem.squashfs...
Adding path/to/file_or_directory1...
Adding path/to/file_or_directory2...
Created filesystem.squashfs successfully.
2: Create or append files and directories to a squashfs filesystem, using a specific compression algorithm
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -comp gzip|lzo|lz4|xz|zstd|lzma
Motivation: Depending on the requirements of your system, you may want to choose a different compression algorithm for your squashfs filesystem. This command allows you to specify the compression algorithm to use, providing you with flexibility and control over the compression ratio and speed.
Explanation: In addition to the file and directory paths and the name of the squashfs filesystem, this command also accepts the -comp
option, followed by the desired compression algorithm. The available compression algorithms are gzip
, lzo
, lz4
, xz
, zstd
, and lzma
.
Example Output:
Creating filesystem.squashfs with lz4 compression...
Adding path/to/file_or_directory1...
Adding path/to/file_or_directory2...
Created filesystem.squashfs successfully.
3: Create or append files and directories to a squashfs filesystem, excluding some of them
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -e file|directory1 file|directory2 ...
Motivation: There may be some files or directories that you want to exclude from the squashfs filesystem. This command allows you to specify the files or directories to exclude, giving you control over the content of the resulting filesystem.
Explanation: In addition to the file and directory paths and the name of the squashfs filesystem, this command accepts the -e
option, followed by the list of files or directories to exclude. You can specify multiple files or directories to exclude by separating them with a space.
Example Output:
Creating filesystem.squashfs...
Adding path/to/file_or_directory1...
Adding path/to/file_or_directory2...
Excluded file/directory1.
Excluded file/directory2.
Created filesystem.squashfs successfully.
4: Create or append files and directories to a squashfs filesystem, excluding those ending with .gz
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -wildcards -e "*.gz"
Motivation: Sometimes, you may want to exclude all files matching a specific pattern or wildcard. This command allows you to exclude files ending with the .gz
extension, giving you more control over the content of the squashfs filesystem.
Explanation: In addition to the file and directory paths and the name of the squashfs filesystem, this command accepts the -wildcards
option, which enables wildcard matching. The -e
option is then used to specify the wildcard pattern *.gz
, which matches all files ending with .gz
.
Example Output:
Creating filesystem.squashfs...
Adding path/to/file_or_directory1...
Adding path/to/file_or_directory2...
Excluded all files ending with .gz.
Created filesystem.squashfs successfully.
5: Create or append files and directories to a squashfs filesystem, excluding those matching a regular expression
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -regex -e "regular_expression"
Motivation: In some cases, you may need more advanced exclusion patterns. This command allows you to use regular expressions to exclude specific files or directories from the squashfs filesystem, giving you fine-grained control over the content.
Explanation: In addition to the file and directory paths and the name of the squashfs filesystem, this command accepts the -regex
option, which enables regular expression matching. The -e
option is then used to specify the regular expression pattern to match the files or directories to exclude.
Example Output:
Creating filesystem.squashfs...
Adding path/to/file_or_directory1...
Adding path/to/file_or_directory2...
Excluded all files/directories matching the regular expression.
Created filesystem.squashfs successfully.
By understanding and utilizing these different use cases of the mksquashfs
command, you can effectively create or append files and directories to squashfs filesystems, customize the compression algorithm, and exclude specific files or directories based on patterns or regular expressions.