How do I create a file and mount it as a filesystem?

Solution 1:

Your procedure is correct, but when mounting a file image as a filesystem you have to add the -o loop option to the mount command:

mount -t ext3 -o loop file /media/fuse

Also, the -t ext3 option is not strictly required, because mount can automatically determine the filesystem type.

Solution 2:

I tried to apply the steps and comments from the previous answer. It still took some work to figure it out, so I've added another answer for people after me.

The following procedure creates a local file named file and mounts it on a local directory named mounted_file.

  1. Create a fixed size file with e.g.
    dd if=/dev/zero of=file bs=1000 count=100
    
    which creates a file of 100 times 1000 bytes (100 kB) filled with zeroes.
  2. Format it with the desired file system, create a directory, mount it, and get permission to use it (owner is root):
    mkfs.ext3 file
    mkdir mounted_file/
    sudo mount -o loop file mounted_file/
    sudo chmod -R 777 mounted_file/
    
    The -o loop parameter is optional nowadays.
  3. To clean up afterwards:
    sudo umount mounted_file/
    rmdir mounted_file/
    rm file
    

Use mkfs.ext3 -n file to see the details of the file system that will created. If desired e.g. the block size (-b block-size) and number of inodes (-N number-of-inodes) can be changed.

Note that we can also run out of inodes (total number of files and directories) instead of diskspace, which is usually not clearly communicated.