Creating a FAT file system and save it into a file in GNU/linux?

I tell you my problem: I want to create a FAT file system and save it into a file so I can mount it in linux using something like:

sudo mount -t msdos <file> <dest_folder>

Maybe I'm wrong and this cannot be done.

Anyway, the problem is this: I'm trying to create the file containing a FAT file system, and I'm running this command:

sudo mkfs.vfat  -F 32 -r 112 -S 512 -v -C "test.fat" 100

That, accordingly to the mkfs man page, will create a FAT32 file system with 112 rootdir entries, logical sector size of 512 bytes, 100 blocks in total, and save it into "test.fat".

But it fails, and the bash tells me:

mkfs.vfat: unable to create test.fat

What is going on? I think I am misunderstanding how mkfs works and how to use it. It is possible to write a filesystem into a file?


Solution 1:

You have a file of 0 bytes in size. You can only create a filesystem on a file that has a specified size.

Here is how to do it properly:

  1. dd if=/dev/zero of=fat.fs bs=1024 count=SIZE how big do you want the filesystem; specify it as SIZE * 1024.
  2. mkfs.vfat fat.fs formats the file as the filesystem FAT.
  3. mount -o loop fat.fs /mnt mounts fat.fs to /mnt.