[minipost] Create a loopback hard-drive partition inside a file in linux

TuxTIP_mergedThis article is really just a quick documentation for something that I do almost each year, and each year I must google-search how I did the last time. So from now on I will have it in my own notes …. here!

Mu current problem was that I have VPS system from a small provider hpcloud.com 🙂 , but the base image is divided to 10 GB of system partition and another 20 GB of data partition. And as luck would have it, I needed 25 GB for data. One of the options was to buy more storage, but I have seen that my minimalistic debian hardly used more than 1,7 GB from the 10 GB system space and I wanted to make use of the remaining space.

Additionally, resizing the partition was not an option as this was the provider mandatory separation, so I decided to use a loopback file emulating a hard-drive.

What this means is that I will create a 5 GB file in the system partition, and mount it as a directory in the data partition, creating a directory structure illusion of 25GB system (more advanced users can try using RAID1 and combine the 5GB and 20GB filesystems, but this is out of scope now).

Step 1. First, lets find a free loop<index> device by checking the losetup /dev/loop<index> until we get no response

Step 2. Then create the file that is 5GB large (full of zeros)

Step 3. Then, mal the /dev/loop0 to the new file with losetup

Lets have a look if it was successfull

Step 4. Great, now lets create a filesystem there, I still prefer ext2 because this is not used for critical data, so we will save some IO calls by not using journal

Step 5. And last by not least, we can mount it somewhere

And that is it, to check you have a new filesystem, you can for example check the “df -h” as below

Now I have additional 5 GB inside the main /mnt with 20GB. With RAID tools (mdadm) you should also be able to put together the two and create one 25 GB partition, but that is outiside of today scope.

If you enjoyed this blog, please share.

About Peter Havrila

Author’s Profile