Using A File As A Storage Device

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 😀

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

Really interesting post, didn’t even know this was possible. Using ubuntu for three years and it still amazes me all you can learn.

I’d like to know how would you modify these steps to create an ext4 with encription and password to decript it. When you install some distros you have the option to encrypt your home file system, so it should be posible to do the same with this kind of files, am I wrong?

My idea would be to put this file into dropbox, and mount it somewhere else (outside dropbox) so I can sync the content of that “folder” trough the cloud without worrying about privacy.

That will be the solution I’ve been serching to create an encrypted folder inside my dropbox where to put sensitive stuff. Sorry if the explanation isn’t really clear, my english is not very good.

Thank u for feedback animaletdesequia.
here is how to create an ext4 encrypted file
1. losetup /dev/loop0 /newfile where new file is the file we created earlier.
2. cryptsetup -c aes create newhome /dev/loop0
it will ask you for passphrase.
now create ext4 filesystem on the file
3. mkfs -t ext4 /dev/loop0
now you can mount it using
4. mount /dev/mapper/newhome /home/username
or you could use
1. losetup -e aes /dev/loop0 /newfile
2. mkfs -t ext4 /dev/loop0
3. mount -o loop,encryption=aes /newfile /mount_point
You can simple copy the encrypted file into the dropbox folder and mount it from there.
I plan to write an article on encryption soon…:)

There’s an alternative method using cryptmount, and I manage to make it work, but anyway i’ll try this one since if I’m not wrong crypsetup is from the gnu project, I supose this metod is more like using build-in tools.

“I mean in what situation would warrent the use of a file as a storage device for daily use?
Does it reduce disk fragmentation? Does it make consolidation of files for backup simpler?
For testing, I assume it makes sense to create file and dump different file systems for experimentation.”
As animaletdesequia pointed out in comment above, you can simply mount the encrypted file that is in the dropbox folder, as your home folder. So, that even on cloud your files are safe.
Also, Lets say you have four partitions, /dev/sda1, sda2, sda3, sda4. Now, sda1 is running out of space. But, sda4 has 10GB free space. So, since sda4 is at the end of the disk, and sda1 at the beginning, you will have to resize all of them just to get some extra spade in sda1. Instead, you could just create a file on sda4 and mount it inside a sub-folder, where sda1 is mounted.
About backup, yes it does make backup simpler, rather than having to archive, you can just copy one file. But there is also a disadvantage, if you create a 2GB file, and mount it. Only 1GB is used. But, while making backup you will have to copy the entire file. So, you will end up copying 1GB more. You could make a small file and extend it as you are using it. But, than can be a lot of work for some people.
About fragmentation, I guess it could reduce disk fragmentation.

mount -t loop /filename /mnt
can by very bad if your mount point are in the directory /mnt like in many old linux !
It safer to use make and use a new mount point:
mkdir /new_mount_point
mount -t loop /filename //new_mount_point

I can offer a tip. First, for dd, bs is bitsize in bytes, you wrote kilobytes.
Secondly, you can use letter suffixes such as K, M and G in addition to the number amounts of sizes. For example, the command would include “bs=1M count=200” for two-hundred megabytes.
A good write-up, flossstuff. You can do pretty much anything with linux.
Also, I think there is an oldish saying about linux: “Everything is a file.”

thanks for pointing that out geoff, that was a naive mistake. I have fixed the errors that you pointed and updated the post. “You can do pretty much anything with linux” – totally agree with you there. May the source be with you. 🙂

This is superb. It deserves follow on articles. Can this filesystem be encrypted as well? what about expanding storage? how about access in Windows using FAT (I work on Linux at home and Windows at work)?

Will consider covering the encryption, expanding storage part in some another article, about Windows, I havn’t heard of any tool that allows you to mount a formatted file under windows except for cd images. Please share if anyone finds anything. Thanks for the feedback.