Wednesday 20 November 2013

Linux: How to create a filesystem in a file

There are two steps. One should create a large empty file. Then that file should be partitioned and formatted.

To create the empty file on a Linux filesystem, use fallocate:


fallocate -l 10G path/to/file/image.img


This won't work in non-Linux file systems such as FAT32. For those you can used the dd command to create a file full of zeroes.

dd if=/dev/zero of=/path/to/file/image.img -bs=10M -count=20480


The above command will create a 200gb file. The calculation is 204800 / 10 = 20480. The block size should be the size of the storage device's buffer. The speed of creation of the file depends on the size of the block size.

After creating the file, the filesystem should be created. mke2fs will give a warning but you can proceed after entering 'y'.

mke2fs -t ext4 /path/to/file


To mount the filesystem of the file, create a directory in the /mnt directory, and then mount it using the mount command. The filesystem won't show up as a device in the file manager but will function as a separate filesystem when you navigate to the mount directory.

sudo mkdir /mnt/backup_vol
sudo mount -o loop /path/to/disk_image /mnt/backup_vol


The filesystem can be accessed from /mnt/backup_vol

To unmount the filesystem:

sudo umount /mnt/backup_vol


Reference: http://freecode.com/articles/virtual-filesystem-building-a-linux-filesystem-from-an-ordinary-file

No comments:

Post a Comment