1. Overview

SquashFS is a type of filesystem that is compressed and read-only. One of the big advantages of SquashFS is its support of various compression methods such as gzip, xz, lzo, lz4, and zstd.

In this tutorial, we’ll learn how to mount a SquashFS filesystem for reading. Then, we’ll learn how to utilize an overlay for reading and writing to a SquashFS filesystem.

2. Why and How to Use SquashFS

SquashFS can be used for many different things but is typically utilized for live booting, archiving, or for use in devices with limited storage. Moreover, SquashFS has many benefits:

  • native compression
  • optimized performance
  • built-in data de-duplication
  • automatic backups

The downside of SquashFS, however, is that it’s read-only and thus requires special considerations when modifying.

To create a SquashFS filesystem, we’ll need to install squashfs-tools:

$ sudo apt install squashfs-tools

We use the apt command to install squashfs-tools, but may also install it via its sources.

3. Mounting SquashFS Filesystem

To mount a SquashFS filesystem, we can use the mount command:

$ sudo mount --type="squashfs" --options="loop" --source="/mnt/extdrive/mysquash.sqsh" --target="/tmp/mysquash"

Mounting a SquashFS filesystem is very similar to mounting any other filesystem. We begin by using the mount command with –type to specify the type as squashfs.

Then, we use the –options flag to set the option loop, indicating we want to use a loopback device. This is necessary since we’re mounting a file, not an actual device. In fact, we employ this method because it’s one of the more common ways that an end user might leverage SquashFS.

Finally, we indicate the location of the squash file we want to mount using –source and the mount point with –target.

Now, we should be able to access the mounted filesystem by switching to the /tmp/mysquash mount point. Since squash is read-only, we’ll not be able to modify the mounted filesystem contents, even to change permissions.

Once we’re done with the mounted filesystem we unmount it:

$ sudo umount --type="squashfs" /tmp/mymount

Alternatively, we could pass the location of a SquashFS filesystem, i.e., /mnt/extdrive/mysquash.sqsh.

4. Understanding OverlayFS

OverlayFS is a special type of filesystem that can combine multiple mount points together into a single structure. The two points that are being merged have specific names:

  • lower directory (read-only)
  • upper directory (typically read-writable)

The resulting mount point is a modifiable directory containing the data of the two directories. Importantly, the upper and lower directories remain with their original data and don’t change. This means that data written to the target directory is lost when the overlay is unmounted.

A great use for an overlay is in conjunction with a SquashFS mount. Setting the lower (read-only) directory of an overlay to a SquashFS mount enables us to read and write to the data in SquashFS. However, since the overlay doesn’t modify the upper or lower directory, we’ll need to recreate and overwrite the SquashFS file if we want to preserve changes made in the overlay.

5. Mounting SquashFS for Reading and Writing

After getting to know both, we can use OverlayFS to read and write to a SquashFS filesystem.

5.1. Preparing Directories

To begin, we mount the SquashFS filesystem via the method we used earlier:

$ sudo mount --type="squashfs" --options="loop" --source="/mnt/extdrive/mysquash.sqsh" --target="/tmp/mysquash"

Next, we create a few directories using the mkdir command:

$ mkdir /tmp/UPPER /tmp/WORK /tmp/TARGET

Let’s break these down:

  • /tmp/UPPER: upper directory that we’ll be merging with the SquashFS mount
  • /tmp/WORK: work directory, which facilitates changes between filesystems
  • /tmp/TARGET: target directory, mount point of the overlay

Having this setup, let’s continue with OverlayFS.

5.2. Mounting Overlay

Now, we can create our overlay using the mount command:

$ sudo mount --type="overlay" \
           --options="lowerdir=/tmp/mysquash,upperdir=/tmp/UPPER,workdir=/tmp/WORK" \
           --source="overlay" --target="/tmp/TARGET"

We set the –options starting with lowerdir which we set to the location of the SquashFS mount point. Likewise, we set upperdir to /tmp/UPPER and workdir to /tmp/WORK. Finally, we set –source=”overlay” indicating we’re using OverlayFS and –target directory to /tmp/TARGET.

After we create the overlay, the resulting directory /tmp/TARGET contains the contents of the SquashFS filesystem, which we’ll now be able to read and write to.

5.3. Syncing Changes to SquashFS Filesystem

Changes made in the /tmp/TARGET directory are lost when the overlay is unmounted. Because of this, we may need to sync the changes made to the SquashFS filesystem:

$ mksquashfs /tmp/TARGET /mnt/extdrive/mysquash.sqsh -noappend

In this example, we run the mksquashfs command with the first argument being the target directory of the overlay and the second argument being the location of the SquashFS filesystem.

We use the -noappend flag to ensure that mksquash overwrites instead of its default behavior of appending to preexisting SquashFS filesystems.

The SquashFS file should now reflect the changes we made in /tmp/TARGET.

5.4. Unmounting Overlay

Now, we just unmount both the overlay and SquashFS:

$ sudo umount --type="overlay" /tmp/TARGET
$ sudo umount --type="squashfs" /tmp/mysquash

In this example, we unmount the overlay and SquashFS mounts, specifying the –type option as overlay and squashfs respectively.

6. Conclusion

In this article, we learned about the SquashFS filesystem and its usage.

Afterward, we mounted a SquashFS filesystem for reading. Next, we explored overlays and how to use one to read and write to a SquashFS filesystem. Finally, we saw how to apply our knowledge in practice.

Comments are closed on this article!