Tutorial: Loop File Systems

What is a loop filesystem?

Gnu / Linux (and the vast majority of unix systems) has a mechanism that allows mounting a file (previously formatted) as if it were a hard disk, usually without a partition table. This crappy diagram (made by me) explains how this mechanism works.

Flowchart that explains (roughly) how loops files work in GNU / linux.

** Does not fully represent actual operation.

Uses of loop files

1- Mount.iso files

The .iso files, the standard par excellence on CDs and DVDs, is nothing more than a loop file whose file system can be ISO 9960 (more common on CDs) or UDF (most common on DVDs). To mount it, we will use the mount command.

mkdir iso # create the directory chmod -R 666 iso # grant all users read permission mount image.iso iso / # mount the iso image (requires root)

The limitation you have is that, for obvious reasons, both file systems are read-only.

2- read-only squashfs

Suppose we have a folder of images and / or videos that is organized according to the year in which it was taken. Obviously, it is quite unlikely that we want to modify the data contained in folders from previous years. That's where Squashfs comes in. Squashfs is a read-only compressed filesystem, so once the image is made, we will not be able to modify the data within.

Thus, not only we will save huge amounts of space, but there will also be the curious effect that the reading of the files will be faster, by having to read smaller files, since nowadays decompressing a file is a minimum effort for most CPUs.

Leaving the preambles, to create the squashfs image we must use the mksquashfs command, whose syntax is simple:

mksquashfs directorio 1 [directorio 2 directorio 3...] imagen.sqsfs -comp [algoritmo de compresión] -b [tamaño del bloque ]

And here is a practical example:

mksquashfs fotos-2009 fotos-2010 fotos-2011 fotos-2012 fotos-2013 fotos_2009-2013.sqsfs -comp xz -bs 1M

Well, the chosen algorithm is xz since it is the one that higher compression ratio offers (of those available), while the chosen block size is the maximum. what allows for better compression results (by default they are 64KiB).

Now we should be able to mount it with a simple;

mount fotos_2009-2013.sqsfs fotos_2009-2013/

After checking that everything is correct, we can delete the original files or leave the file as a backup.

In case we want it to be mounted when the OS starts, we must add a line of this style to the / etc / fstab:

/dir/loop.sqsh /dir/mountdir squashfs ro,defaults 0 0

Entering incorrect data in the / etc / fstab file will cause the system to stop booting. To make sure everything works correctly, we can use the command mount -a

Now, what happens when we want to add or delete files, or simply extract these snapshots? Well, we have the command the command unsquashfs.

unsquashfs [opciones] snapshot.sqfs [Directorios o archivos que extraer]

By default it will use "squashfs-root" as the extraction directory. can be changed with -d option

Apart from this example, squashfs is also used in:
  • livecd's
  • Embedded Systems
  • Servers
  • In general, any read-only system

3- «Emergency swap»

Imagine that for whatever reason (hibernation, massive compilations ...) you need an extra swap. This is where the loop file comes into play, the first step is to make an empty file of a certain size, with a certain block size, we do this with the command dd:

dd if=/dev/zero of=loop bs=1M count=512

dd in the wrong hands is worse than a monkey with a shotgun. Think before pressing enter

In this case, the block is one MiB and the file size is 512MiB. It does not have to be this way.

We create and activate the swap with this command

mkswap loop && swapon loop

Bear in mind that the performance of a loop file is always somewhat lower than that of a real physical one.

4-do what you want

These are just some of the most characteristic examples, feel free to experiment with chroots, encryption systems, nested loops… Your possibilities are limitless. But, be careful with the commands you run, think carefully before pressing enter.

Links of Interest