Tag Archives: Linux file system from file

Creating a File System from normal Linux file.

In Linux, it is possible to create file system using normal disc file. To do so, take a disc file, format it as  ext2 or ext3 file system and mount it  just like a physical drive. It is now possible to operate the file system as other file systems. This article show you how to create file system using disc file.

This is a nice way to investigate different file systems without having to reformat a physical drive, which means you avoid the trouble of moving all your data. This method is very quick compared to preparing a physical device. You can then read and write files to the mounted device, but what is truly great about this technique is that you can explore different file systems such as reiserfs, ext3, or ext2 without having to purchase an additional physical drive. Since the same file can be mounted on more than one mount point.

Creating a filesystem by this allows you to set a hard limit on the usage of space, which will be equal to the file size. The contents cannot grow beyond the file, you can easily keep track of how much space is being used.

First of all, you have to create a file with desire size.

Here for my case i am taking file of 10 MB.

To create file dd command is used.Which will read input from /dev/zero driver file . /dev/null is a special file (in this case, a pseudo linux -device) that provides an endless stream of null characters.

$ dd if=/dev/zero of=myFs bs=1024 count=10240

10240+0 records in

10240+0 records out

10485700 bytes (10 MB) copies, 0.0507155 s, 207 MB/s

With above command you created a 10 MB file with null characters because, block size of dd is 1024. That makes the size: 1024*10240=10485760.

$ ls -l myFs

-rw-rw-r– 1 bhargav bhargav 10485760 Jun 30 17:45 myFs

As next step , it is essential to formate created file to ext2 filesystem.The below command converts file to ext2 type file system.

Command will ask your conformation to proceed because this is not a block device. That is OK. We will mount this as a loopback device so that this file will simulate as a block device.

$ mke2fs myFs

mke2fs myFs

This created filesystem can be mount through loopback devices.

The main purpose for using a loopback device is to fake out some piece of software so that you can “mount” a file as a disk and read the file system in it; the thing that the loopback device points to does not have to be a separate drive, but it can be a file. Once you mount your blank file, you can store individual files in it, and even copy it to a new volume, instantly filling that volume with the directory tree and all that it contains!

Now, you need to create a directory which will act as s a mount point for the loopback device.

$ mkdir /mnt/Fs

Next, you just want to find out what the next available loopback device number is. Normally, loopback devices start at zero (/dev/loop0) and work their way up (/dev/loop1, /dev/loop2, … /dev/loopn).To find that you need to look at /proc/mounts.

$ cat /proc/mounts

cat /proc/mounts

On my computer, I have no loop back devices mounted, so I’m OK to start with zero. You must do the next command as root, or with an account that has superuser privileges.

$ mount -o loop=/dev/loop0 myFs /mnt/Fs

That’s it. You just mounted the file as a device.

You can now create new files, write to them, read them, and do everything you normally would do on a disk drive

 

References

creating a file which contains an ex2fs filesystem. 2013. creating a file which contains an ex2fs filesystem. [ONLINE] Available at: http://uranus.chrysocome.net/linux/ex2fsfile.htm. [Accessed 20 November 2013].

Leave a comment

Filed under Linux File system