Monday 17 March 2008

Create Linux Filesystem From An Ordinary File

Under Linux, you can create a regular file, format it as an ext2, ext3, or reiser filesystem, and then mount it just like a physical drive. It's then possible to read and write files to this newly-mounted device. You can also copy the complete filesystem, since it is just a file, to another computer.

First, you want to create a 20MB file or any size you want by executing the following command:
      $ dd if=/dev/zero of=disk-image count=40960
     40960+0 records in
     40960+0 records out
Next, to format this as an ext3 filesystem, you just execute the following command:
      $ /sbin/mkfs -t ext3 -q disk-image
     mke2fs 1.32 (09-Nov-2002)
     disk-image is not a block special device.
     Proceed anyway? (y,n) y
You are asked whether to proceed because this is a file, and not a block device. That is OK.

Next, you need to create a directory that will serve as a mount point for the loopback device.
      $ mkdir fs
You must do the next command as root, or with an account that has superuser privileges.
      # mount -o loop=/dev/loop0 disk-image fs
You can now create new files, write to them, read them, and do everything you normally would do on a disk drive. To make normal user to use this filesystem you need to give valid permission to the directory holding this filesystem.

No comments:

Post a Comment

Next previous home