Monthly archives: August 2014

Create loopback filesystems on Linux

Posted on August 4, 2014 Comments

Filesystem containers are immensely useful for easy storage of data, or to use as a filesystem where you can quickly delete/recreate it. You don’t have to transfer individual files to back up or move data, which can be a real pain. When I was running a proxy server, I used this method to maintain logs and cache. These two things would take ages to back up and create an offline copy for, since file transfers tend to slow down with thousands of files. A container was the best solution here.

To create a filesystem, start with this command, which will fill a file with zeros up to the specified amount using the dd command.

dd if=/dev/zero of=filesystem.img bs=1M count=40000

The above command will create a ~40GB file.

dd is not verbose, so you will not see any feedback that the file is being created. But if you check the size, filesystem.img it should be growing. Depending on your system, it will take time to allocate all of the space. Once it is complete dd will end, and you can begin to format:

mke4fs filesystem.img

You can use any filesystem you wish, in the case of cache/logging I’d recommend a filesystem without a journal as it will be faster. Run through the process and format the filesystem like you normally would. Once it’s done, you should be able to mount.

mkdir /home/containerfs
sudo mount -o loop filesystem.img /home/containerfs

If everything worked, you should now have a filesystem mounted to /home/containerfs.