Mounting a file as a file system in Linux

This is called mounting a loopback device.

3 steps.
  1. Creating the file that can be mounted as a file system
  2. Assigning the file as a block device
  3. Mounting the block device to a path
Creating the file that can be mounted as a file system

Use the dd command to create an empty file of the size which you want your file system.
  $ dd if=/dev/zero of=my_fs bs=1024 count=30720
of – parameter specifies the name of the file
bs – size of the block (represents x no of KB. You can specify x no of MB using xM: i.e. “bs=1M”)
count – how many blocks to be present (thus the size of the file system = bs*count)
if – from which device the file content initially should be filled with.
But still the file that gets created is not a proper filesystem yet. you need to format it using some file system type. In order to do that first lets assign the file to a loop back device (the next step)
Assigning the file as a block device

In linux a file system has to be a block device in order for it to be managed as a file system. In this particular case we use a set of defined loop back devices for our purpose. eg: /dev/loop0 up to /dev/loop8
in order to check if a particular loop back device is already being used use the command,
  $ losetup /dev/loop0
If its already being used you will see it mentioned in the output of the above command. Once you find a free loop back device to attach/assign the file to it
  $ losetup /dev/loop0 my_fs
This command needs to be issued as a sudoer user.
Now lets format the loopback device we just attached. 
$ mkfs -t ext3 -m 1 -v /dev/loop0
Effectively we are creating a filesystem inside a file using the above command. I’m formatting it as ext3 file system. You may have to use sudo again.
Mounting the block device to a path

The hard work is done. Now to just mount.
  $ mount -t ext3 /dev/loop0 /mnt/fs_mount_location/
You may ommit the “-t ext3” parameter in this case since usually linux file systems are automatically detected at mount. You may have to sudo the above command. 
Thats it actually. Once you are done you should clear up after you ofcourse. to do that just 2 commands
  $ umount /mnt/fs_mount_location/
to unmount. you may need sudo again
  $ losetup -d /dev/loop0
to detach the file from the loop back device. Need sudo again.
Steps are curtasy of http://www.walkernews.net/2007/07/01/create-linux-loopback-file-system-on-disk-file/

One Response to “Mounting a file as a file system in Linux”

  1. Nagilum Says:

    If you use truncate you can save some time and space. So for your 30MB file you can simply do:
    truncate -s 30M my_fs
    The file will be a sparse file which I believe you can also create using dd and skip=xx , but truncate seems a bit more straight forward to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: