30

How do I create a file of size 10M, format it with ext3 filesystem and then mount it in /media/fuse?

I tried with

mkfs -t ext3 file

then to mount it

mount -t ext3 file /media/fuse

It didn't work because it said that file wasn't a block device. Can anybody help me?

7
  • 5
    You just have to add -o loop to the mount command.
    – enzotib
    Commented Dec 7, 2011 at 19:54
  • 1
    A great tutorial here: ibm.com/developerworks/linux/library/l-linux-filesystem
    – arrange
    Commented Dec 7, 2011 at 20:07
  • To loop-mount a ISO file, I do this mount -o loop file.iso /media/iso.
    – Anonymous
    Commented Dec 7, 2011 at 20:50
  • @enzotib Can you post that as an answer to be upvoted/accepted please? Commented Feb 28, 2012 at 18:17
  • @TomBrossman: done
    – enzotib
    Commented Feb 28, 2012 at 18:47

3 Answers 3

22

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.

4
  • 7
    he is right but forgot to add the way to create a file dd if=/dev/zero of=file bs=1024 count=10240 this is the way to create a 10Mb file... then you can format it with mkfs.ext3
    – maniat1k
    Commented Feb 28, 2012 at 19:20
  • 8
    Now mount does not need the -o loop option anymore, it can understand by itself we it need it.
    – enzotib
    Commented Oct 3, 2013 at 12:33
  • 1
    Just to elaborate on the top answer with further explanation of why this is (was) needed: mount operates on block devices. To the kernel, a file is not a block device. It can become a block device, however, by configuring a loop device to use it as backing. This is what you're doing with -o loop. In recent times loop device handling is a lot easier: since kernel 2.6.35 loop devices set up during mount are now destroyed automatically when unmounted, and the mount command can automatically set up a loop device with just -o loop instead of specifying a particular loop device. Commented Apr 12, 2022 at 4:57
  • 1
    And as other comments state, recently mount is smart enough to do the loop setup transparently without even -o loop Commented Apr 12, 2022 at 4:58
10

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.

2
  • i get an error Not enough space to build proposed filesystem while setting up superblock on mkfs.ext3 file, i've created the file with dd, adding more in size doesn't do anything
    – mekb
    Commented Jul 12, 2021 at 7:16
  • nevermind, the file didn't end in .fs
    – mekb
    Commented Jul 12, 2021 at 7:24
0

Personally, I prefer using truncate -s 10M filename (then mkfs) because truncate expands dynamically unlike fallocate or dd and can also be resized larger (or smaller) without damaging data with losetup and resize2fs. And of course it can be mounted as a filesystem.

See here for more detail: https://unix.stackexchange.com/a/698656/346155

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .