We all know that file stores our information in many types of formats. But do you know that  we can use it as a storage device too. Surprised???? Let’s go through the crazy process :D

We are going to create a empty file in Linux, format it and then mount it as if we are mounting a partition.  This process is long , So to understand it easily I am dividing it into 4 steps.

Step 1 : Create an empty file of desired size (200MB in this case) using the command

dd if=/dev/zero of=/filename bs=1M count=200

Explanation : dd command allows us to copy a file with specified number of bytes. Here if means input file, of means output file, bs is block size in bytes. You can use suffixes like K (for Kilobytes), M (for Megabytes), and G (for Gigabytes) . Note: If you are using Mac OS, use K, M, G in lower case. Here, I have used bs to be equal to 1MB. Count parameter controls how many number of blocks are copy, So 200 value is used  to create a 200 MB file. Now you might be wondering what is /dev/zero ?? /dev/zero is a special device file that writes zeros. So, our newly created file contains nothing but zeros. You can view, the created file using hexdump command.

"hexdump" Command

Here starting address is 0000000 and each line contains 16 bytes, * in the second line means file only contains zeros from starting address to the end of file (c800190 in hex). This file isn’t of much use to us , So in next step we will create a filesystem so that we can use this file for storage purpose.

Step 2 : Create a filesystem on the file using mfks utility. Type  mkfs -t file_sytem_type /filename. Example: mkfs -t ext2 /filename. As in the image below also , you will get a warning press y and hit enter.

mkfs Command Output

You can also format the file as swap space using mkswap /filename .

Step 3 : Mount the filesystem on the file or enable it as swap space in case you formatted the file as swap.  You can see the images below for better understanding .

Mounting A File

In case of swap :

Mounting A Swapfile

Step 4 : Mount the filesystems at boot time by adding entries. If you want  your filesystem  to get automatically mounted at boot time, add the following entries in /etc/fstab

/filename /mount_point filesystem_type loop,defaults 0 0 

Where filename is the name of the file created, mount_point is the directory where you would like to use the filesystem, file system type means type of formatted filesystem.For Example : /filename /media/extra ext2 loop,defaults 0 0 

After, modifying /etc/fstab, you don’t have to reboot to test changes, you can type mount -a to test the changes, it will give you the details of errors if any during when OS tries to mount the filesystem. For swap space use the following.

/filename none swap defaults 0 0

Note:- While I was testing this on CentOS, mount -a didn’t work for swap, even though I could see adding 204792k swap on /filename. Priority:-1 extents:63 across:478252k in the output of  dmesg | tail, still no swap space was shown by free or swapon -s commands. But, after reboot swap got activated properly.

In answer to adrian comment :

How to extend size of the file:-

Lets say you create a file name /newfile of size 200MB and format it, after a while you want to extend the file size. To do that do the following

Step 1. Lets say you want to increase the file size by 100MB

dd   oflag=append  conv=notrunc  if=/dev/zero  of=/newfile   bs=1MB count=100

Now, the file is 100MB larger.

Step 2. Run file system check on the file

fsck -f /newfile

Step 3. Now if you formatted the file system in ext2,3 or 4, you can resize it by using

resize2fs  /newfile.

Thanks to geoff, Jean Beliveau for the feedback.

Pages : Follow ankurtwi on Twitter