Filesystems are usually created on a partition. When all your hard drive is already partitioned, creating a new partition can become a pain and creating a new filesystem within another filesystem file can save you a lot of hassles.
This tutorial will show the few steps required to have a new filesystem layout set within another filesystem.
One common use case is with the swap file. Let imagine for instance that you have a machine hard drive partitions already set up with no extra space left.
You decided to give your computer a treat and add some extra RAM. As you have more RAM, you should have more swap space allocated.
If you have your filesystem sitting on a LVM, this will be as easy as expanding your swap partition, but unfortunately, at the moment, some linux distro do not come with LVM set up out of the box.
Nothing is lost, as long as you have some free space on another partition, you can use this space and convert it to another filetype (swap in this case).
1. Creating the “partition”
In order to create a new “partition”, we need to creating a blank file of the size of the partition. Here the file is going to be created in the /data directory, change this value according to your system.
Let’s create a file of 512M filled up with zeros:
# dd if=/dev/zero of=/data/test_filesystem bs=1M count=512 512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 30.9203 seconds, 17.4 MB/s
That’s it, we have created our new “box” to host another filesystem.
2. Formating the partition
Now, we need to format this newly created partition. Depending on the filesystem you want to use, there will be different commands and parameters.
2.1. Swap file
To create a new swap file, use this command:
# mkswap /data/test_filesystem
2.2. Ext2/3 file
Ext2 and Ext3 command do not need any specific arguments, but you will be warned that the file is not a block device and be prompted to weather or not you still want to proceed. Then choose “y”
# mkfs.ext2 /data/test_filesystem mke2fs 1.40.2 (12-Jul-2007) /data/test_filesystem is not a block special device. Proceed anyway? (y,n)
Will create an ext2 layout, while:
# mkfs.ext3 /data/test_filesystem
will create an ext3 layout.
2.3. Reiserfs file
When formatting as a reiserfs partition, you will need to force the process with the “-f” flag like:
# mkfs.reiserfs -f /data/test_filesystem
To confirm that your file is a valid filesystem; you can use the command file
$ file /data/test_filesystem /data/test_filesystem: Linux rev 1.0 ext2 filesystem data
3. Mounting the partition
3.1. Swap file
Like a standard swap partition, you need to use swapon to mount your swap file:
# swapon /data/test_filesystem
3.2. Ext2/3 and Reiserfs
To be able to mount a file as a partition, you will need to use -o loop option to use a loop device like you would do if you were mounting an ISO file.
# mkdir /tmp/test_filesystem # mount -o loop /data/test_filesystem /tmp/test_filesystem # mount | grep test /data/test_filesystem on /tmp/test_filesystem type ext2 (rw,loop=/dev/loop0)