Tag Archive : squashfs

/ squashfs

Managing SquashFS File

December 9, 2015 | Uncategorized | No Comments

SquashFS is a compressed read-only file system. SquashFS compress files, inodes, and directories, and supports block sizes up to 1 MB for greater compression.

SquashFS is intended for general read-only file system use and in constrained block device / memory systems (e.g. embedded systems) where low overheaded is needed. Originally it use gzip compression, now it support LZMA, LZMA2 and LZO compression.

In this article, we will discuss about some notes about how we can manage SquashFS. The term manage in this arcitle including: creating, mounting, and modifying SquashFS file. In this article I use Slackware64 14.0 as host. However you can use any Linux distribution which has SquashFS tools installed.

You can always skip all basic and introduction, but I recommend you to read it.

Preparation

Check whether you have installed required tools. Test by invoking following binaries:

mksquashfs
unsquashfs

If you don’t have it yet, you can download or compile it from source. Just go to their official site.

For Slackware, go to Slackbuilds site for SquashFS.

I assume you have the required tools.

Overview

As stated before, SquashFS is a compressed read-only file system. You can imagine it as a file which are exactly a file system. It’s like database file for sqlite, or a disk image for various Virtual Machine apps but only serve a purpose as a filesystem. The read-only filesystem means it can only be read, no write operation allowed. Modification will require dissection of the file.

Other important things:

  1. Data, inodes, directories are compressed
  2. SquashFS stores full uid/gids (32 bits), and file creation time
  3. Files up to 2^64 bytes are supported; file systems can be up to 2^64 bytes
  4. Inode and directory data are highly compacted, and packed on byte boundaries; each compressed inode is on average 8 bytes in length (the exact length varies on file type, i.e. regular file, directory, symbolic link, and block/character device inodes have different sizes)
  5. SquashFS can use block sizes up to up to 64 Kb (2.x) and 1Mb (3.x). The default size is 128Kb (3.x), which achieves greater compression ratios than the normal 4K block size
  6. By the 2.x release it was introduced the concept of fragment blocks: an ability to join multiple files smaller than block size into a single block, achieving greater compression ratios
  7. File duplicates are detected and removed
  8. Both big and little endian architectures are supported; SquashFS can mount file systems created on different byte-order machines

SquashFS Capable Kernel

The on-disk format of SquashFS has stabilized enough that it has been merged into the 2.6.29 version of Linux kernel. I assume you use kernel with version greater than 2.6.29. If you are not, you should patch your kernel. But, I think today distribution has ship kernel greater than 2.6.29.

The Tools

You should check the command-line options for mksquashfs and unsquashfs for each capabilities.

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:

mksquashfs source1 source2 ... destination [options]

Where:

  1. source1, source2, etc are files and directories to be added to the resulting filesystem, given with relative and/or absolute paths.
  2. destination is a regular file or a block device (such as /dev/fd0 or /dev/sda2) where the squashed file system will be created.

Notes, for default mksquashfs behavior:

  • 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.

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]

Where the target is the squashed file system to extract.

Notes for unsquashfs behavior:

  • 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.

Managing SquashFS

This section will describe the use of mksquashfs and unsquashfs for managing SquashFS file.

Creating a SquashFS File

Creating Simple SquashFS

Creating a squashed file system out of a single directory (for ex: /some/dir), and output it to a regular file as image.sqsh.

mksquashfs /some/dir image.sqsh

Note that the output filename could be anything and any extension.

mksquashfs then will perform the squashing and print the resulting number of inodes and size of data written, as well as the average compression ratio. Now we have /some/dir directory image in image.sqsh file.

The content of image.sqsh would be any file or directory inside of /some/dir.

If you want to output the filesystem directly into a device, i.e. USB flash disk at /dev/sdb1:

mksquashfs /some/dir /dev/sdb1

Squashing File Systems

This section will describe how to create read-only compressed file system.

Mounting SquashFS File

To loop SquashFS file, you will use loopback device. The command is:

mount -o loop -t squashfs image.sqsh /mnt/dir

For image.sqsh as the SquashFS file and /mnt/dir as the mount point.

You should see that the SquashFS can only be accessed read-only.

To mount at boot time, you should at this entry to /etc/fstab (assume the image is image.sqsh and the mount point is /mnt/dir):

/some/dir/image.sqsh /mnt/dir squashfs loop 0 0

SquashFS and Write Operation using UnionFS

Unionfs provides copy-on-write semantics for the read-only file systems (such as SquashFS) which enhancing the possibilities. More detail on the UnionFS can be seen at http://www.filesystems.org/project-unionfs.html

Basically, UnionFS is a union of both writable filesystem and read-only filesystem, merged to a single mount point.

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:

mksquashfs /home/user1 ro.fs
mkdir /home/rw.fs

Mount the squashed ro.fs file system using the loopback device:

mount -t squashfs -o loop ro.fs /mnt

Mount the unionfs filesystem as union of /mnt/and /home/rw.fs. This will be merged under /home/user1 location:

cd /home
mount -t unionfs -o dirs=rw.fs=rw:/mnt=ro unionfs user1

The write operations under rw.fs will be done to /home/rw.fs.

To mount squashed directory tree at startup, you should load the squashfs and unionfs modules at boot time. Change the owner of the writable branch to match user1.

Here is the complete command list:

echo squashfs >> /etc/modules
echo unionfs >> /etc/modules
chown user1 /home/rw.fs

Add appropriate entry to /etc/fstab to mount the SquashFS and UnionFS at boot time.

/home/ro.fs /mnt/squashfs loop 0 0
unionfs /home/user1 unionfs dirs=/home/rw.fs=rw:/mnt=ro 0 0

Extract the File System

Also known as unsquashing. The unsquash operation will decompress the squashed file.

Suppose we have a file image.sqsh and want to unsquash it:

unsquashfs image.sqsh

A new directory will be created, as squashfs-root. The content of squashfs-root will be the content of file you or directory you use to create the squashed filesystem.

Social media & sharing icons powered by UltimatelySocial