[minipost] Create a loopback hard-drive partition inside a file in linux
This 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
root@gserver1:/# losetup /dev/loop0 loop: can't get info on device /dev/loop0: No such device or address
Step 2. Then create the file that is 5GB large (full of zeros)
root@gserver1:/# dd if=/dev/zero of=/HDloop0 bs=1024 count=5242880
Step 3. Then, mal the /dev/loop0 to the new file with losetup
root@gserver1:/# losetup /dev/loop0 /HDloop0
Lets have a look if it was successfull
root@gserver1:/# losetup /dev/loop0 /dev/loop0: [fe01]:10855 (/HDloop0)
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
root@gserver1:/# mkfs.ext2 /dev/loop0 mke2fs 1.42.5 (29-Jul-2012) Discarding device blocks: done Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
Step 5. And last by not least, we can mount it somewhere
root@gserver1:/# mkdir /mnt/test root@gserver1:/# mount -t ext2 /dev/loop0 /mnt/test
And that is it, to check you have a new filesystem, you can for example check the “df -h” as below
root@gserver1:/mnt# df -h Filesystem Size Used Avail Use% Mounted on rootfs 9.9G 2.2G 7.2G 24% / /dev/vdb 20G 18G 1.4G 93% /mnt /dev/loop0 5.0G 10M 4.7G 1% /mnt/test
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.