The SquashFS tools exposed
Using mksquashfs
mksquashfs is the tool for creating new squashed file systems, and for appending new data to existing squashed file systems. The general command-line format for mksquashfs is: bash# mksquashfs source1 source2 ... destination [options]
|
- source1, source2, etc.: files and directories to be added to the resulting file system, given with relative and/or absolute paths
- destination: a regular file (filesystem image file), or a block device (such as /dev/fd0 or /dev/hda3) where you want to have your squashed file system
- When the new files are added to the new file system or appended to an existing one, mksquashfs will automatically rename files with duplicate names: if two or more files named text will appear in the same resulting directory, the second file will be renamed to text_1, third one to text_2 and so on.
- Duplicate files will be removed, so there will be only one physical instance (By the SquashFS 2.x, you can disable the detection/removal of the duplicates with the -no-duplicates option).
- If destination has a pre-existing SquashFS file system on it, by default, the new source items will be appended to the existing root directory. Examine the options table below to force mksquashfs to overwrite the whole destination and/or change the way new source items are added.
- If a single source file or directory is given, it becomes the root in a newly created file system. If two or more source files and/or directories are given, they will all become sub-items in the root of the new file system.
- The resulting filesystem will be padded to a multiple of 4 Kb: this is required for filesystems to be used on block devices. If you are very sure you don't ned this, use the -nopad option to disable this operation.
Command-line options
All possible options for mksquashfs are shown in the table below.Table 1. Command-line options of the mksquashfs tool
| Option | Description |
|---|---|
| -2.0 | force mksquashfs version 2.1 to create a version 2.0 filesystem |
| -all-root or -root-owned | make all files in the target file system owned by root (UID=0, GID=0) |
| -always-use-fragments | divide all files greater than block size into fragments (by the version 2.x). It will result in greater compression ratios |
| -b [block size] | use [block size] filesystem block size (32 Kbytes default for 2.x, 128 kbytes for 3.x) - this can be either 4096, 8192, 16384, 32768, 65536 or 131072 |
| -be or -le | force a big or little endian file system, respectively |
| -check-data | enable additional file system checks |
| -e [file1] ( [file2] ... ) | specify which files and/or directories to omit from the new file system that is to be created |
| -ef [file] | specify a file which contains the list of files/directories to exclude |
| -force-gid [GID] | set all group IDs in target file system to [GID] (can be specified as a name or a number) |
| -force-uid [UID] | set all user IDs in target file system to [UID] (can be specified as a name or a number) |
| -info | print files, their original size and compression ratio, as they are added to the file system |
| -keep-as-directory | if the source is a single directory, force this directory to be a subdirectory of the root in the created file system |
| -noappend | if the destination file/device already contains a squashed file system, overwrite it, rather than append the new data to an existing file system |
| -no-duplicates | do not detect/remove duplicate file names |
| -noD or -noDataCompression | do not compress the data |
| -noF or -noFragmentCompression | do not compress the fragments (avaliable by 2.x) |
| -no-fragments | do not generate fragment blocks (avaliable by 2.x, this will produce almost the same filesystem as 1.x did) |
| -noI or -noInodeCompression | do not compress the inode table |
| -nopad | do not pad the resulting file system to a multiple of 4 KBytes |
| -root-becomes [name] | can be used while appending to a pre-existing squashed file system: it will make a new root, and [name] directory will contain all pre-existing files/directories |
| -version | print the version, copyright and license message |
| -recover [name] | recover filesystem data using recovery file [name] (3.3) |
| -no-recovery | don't create a recovery file (3.3). |
| -no-exports | don't make avaliable file system to export via NFS (3.x) |
| -no-sparse | don't check for sparse files (3.x) |
| -processors [number] | set the number of CPU to create file system. By default it will be used all avaliable processors (3.x) |
| --read-queue [size] | set input queue to [size] Mb. (Default is 64 Mb)(3.x) |
| -write-queue [size] | set output queue to [size] Mb (3.x) |
| -sort [sort_file] | sort files relating to priorities in [sort_file] (3.x) |
| -wildcards | enable the extended shell wildcards to exclude directories/files (to be used with -e) |
| -regex | enable to use POSIX regular expressions (3.3) |
Using unsquashfs
unsquashfs is the tool for extracting data from squashed file systems. The general command-line format for unsquashfs is:unsquashfs [options] target [files/directories to extract]
|
- target is the squashed file system to extract.
- By not specifying any destination path, unsquashfs extracts the compressed file system in the ./squashfs-root directory.
- The tool does not extract a squashed file system on already exsisting directory unless the -f option is specified.
- You can specify on the command line, a multiple number of files/directories to extract and the items to be extracted can be also be given in a file with -e [file] option.
Table 2. Command-line options of the unsquashfs tool
| Option | Description |
|---|---|
| -v[ersion] | print the version, licence and copyright message |
| -i[nfo] | print the files as they are extracted from the file system |
| -l[ist] | list the squashed file system without extracting files |
| -li | list files with theyr attributes as they are unsquashed (3.3) |
| -ll | list the squashed file system files with attributes without any extraction (3.3) |
| -d[estination] path | specify a destination path for unsquashed items |
| -f[orce] | if files exist overwrite them |
| -s[tat] | display file system's superblock informations (it can discover the file system version and the options used to compress it) (3.3) |
| -e[f] [extract file] | list of directories or files to extract (entries given one per line) (3.3) |
| -r[egex] | treat extract names as POSIX regular expressions (3.3) |
Creating and using squashed file systems
Basic steps
In order to create a squashed file system out of a single directory (say, /some/dir), and output it to a regular file (thus, producing a file system image), you need to say only one magic phrase: bash# mksquashfs /some/dir dir.sqsh
|
bash# mkdir /mnt/dir bash# mount dir.sqsh /mnt/dir -t squashfs -o loop
|
bash# ls /mnt/dir
|
bash# mksquashfs /some/dir /dev/fd0
|
bash# mount /dev/fd0 /mnt/floppy -t squashfs
|
bash# ls /mnt/floppy
|
Squashing file systems
Operations described here correspond to most cases where a read-only compressed file system can be used, whether you want it to be on a block device or in a file. This could be anything from large FTP/HTTP-served archives that don't change often, to having a squashed /usr partition and anything alike with these.Example 1
Let's suppose you have a /var/arch directory with lots of files and that you want to turn it into a squashed file system and keep it on your root partition as a file (it will be a file system image that you will mount via a loopback device). The operations needed to perform are as follows.Squash the directory, then mount it via loopback to test it:
bash# mksquashfs /var/arch /var/arch.sqsh bash# mkdir /mnt/tmp
bash# mount /var/arch.sqsh /mnt/tmp -t squashfs -o loop
bash# ls /mnt/tmp
|
/var/arch.sqsh /var/arch squashfs ro,defaults 0 0
|
bash# umount /mnt/tmp bash# mount /var/arch
|
bash# ls /var/arch
|
Example 2
Say you have two hard disk partitions, /dev/hda6 (which is empty) and /dev/hda7 (which is bigger than /dev/hda6, mounted at /var/arch, contains some data and is full). Now, say you want to squash the /dev/hda7 file system and move it to/dev/hda6, then use /dev/hda7 for some other purposes. We will suppose you have the following line in /etc/fstab (reiserfs is just an example file system used on /dev/hda7): /dev/hda7 /var/arch reiserfs defaults 0 0
|
bash# mksquashfs /var/arch /var/arch.sqsh
bash# mkdir /mnt/tmp
bash# mount /var/arch.sqsh /mnt/tmp -t squashfs -o loop
bash# ls /mnt/tmp
|
bash# umount /dev/hda7 bash# dd if=/var/arch.sqsh of=/dev/hda6
|
/dev/hda6 /var/arch squashfs ro,defaults 0 0
|
bash# mount /var/arch bash# ls /var/arch
|
bash# rm /var/arch.sqsh
|
Creating tiny/embedded systems
By saying "tiny/embedded", I mean Linux systems that are being built for booting from floppy disks, IDE/USB flash disks, iso9660 CD-ROMs, small-sized hard drives and the like. Whether you want to have your whole root file system on a single media (a single partition, a single floppy), or have a modular system (several floppies or disk partitions), the procedure is almost identical. Creating such Linux systems themselves is out of scope of this HOWTO - there are dedicated HOWTOs and guides for this (like the Bootdisk HOWTO and Linux From Scratch - visit www.tldp.org to retrieve these documents).Squashed file systems on floppy/flash/hard disks
In order to use SquashFS for creating Linux systems on small disks, you just have to follow the usual steps for creating a minimal system, performing the following operations at respective points:- When developing a kernel for your system, make sure you enable SquashFS support so it can mount squashed file systems
- Use mksquashfs for creating read-only initial ram disks and/or root and/or other file systems
- Don't forget to set file system types to squashfs in /etc/fstab and/or the startup scripts of your system for mounting squashed file systems
bash# cd /home/user
bash# mksquashfs floppylinux root.sqsh -e usr
bash# mksquashfs floppylinux/usr usr.sqsh
|
Note 2: don't forget to specify squashfs in your root disk's /etc/fstab or startup scripts when mounting the /usr file system.
Insert a root disk in your 3.5" floppy drive (I assume you have a lilo or grub on it, and, thus, a file system exists on this floppy, and the root file system will reside under the /boot directory of this file system):
bash# mount /mnt/floppy
bash# cp root.sqsh /mnt/floppy/boot
|
bash# dd if=usr.sqsh of=/dev/fd0
|
Squashed file systems on CD-ROMs
With SquashFS, you can compress large file systems that will be used in live CDs (just as an example). For this purpose SquashFS is also used with UnionFS.- Enable SquashFS in the linux kernel of the target system
- Create a squashed root file system
- Modify the /etc/fstab or startup scripts of the target system to mount the squashd file system when you need it
Making it writeble
As mentioned, another interesting use for SquashFS is with Unionfs filesystem, which provides copy-on-write semantics for the read-only file systems, enahancing the possibilities. (For unionfs you can look at http://www.filesystems.org/project-unionfs.html)Just to make an example, you may want to make your /home/user squashed, to compress and backup your files without losing the possibility to apply changes or writing new files.
Create the ro.fs squashed file system and the rw.fs dir.
bash# mksquashfs /home/user1 ro.fs bash# mkdir /home/rw.fs
|
bash# mount -t squashfs ro.fs /mnt -o loop
|
bash# cd /home bash# mount -t unionfs -o dirs=rw.fs=rw:/mnt=ro unionfs user1
|
bash# cd /home/user1 bash# touch file1 bash# ls
|
bash# cd ..
bash# umount /home/user1
bash# umount /mnt
bash# ls /home/user1
bash# ls /home/rw.fs
|
When you want to add the new created files to the stable and compressed squashed file system, you have to add them to the exsisting one.
bash# mksquashfs /home/rw.fs /home/ro.fs
|
Make squashfs and unionfs modules loaded at boot time.
bash# echo squashfs >> /etc/modules bash# echo unionfs >> /etc/modules
|
chown user1 /home/rw.fs
|
... /home/ro.fs /mnt squashfs loop 0 0 unionfs /home/user1 unionfs dirs=/home/rw.fs=rw:/mnt=ro 0 0
|