Linux – mkfsdos: define the size of FAT16|32 file system on USB pendrive in Linux

fatlinux

Question: How does one define the size of a FAT16 / FAT 32 file system with mkdosfs in Linux

I want to create a, say 2 GB FAT16|32 filesystem on a partition in the size of 8 GB or more?

This is how far I got:

  • created a partition with fdisk
  • mkdosfs -F 16 /dev/sdb1 creates a FAT16 file system over the WHOLE partition – as long as this isn't larger tha 4 GB, I know
  • mkdosfs -F 32 /dev/sdb1 creates a FAT32 filesystem over the WHOLE partition. I know that this is the default and that I wouldn't neet to specify -F 32 but for the sake of completeness and style…
  • according to man mkdosfs the size of the file system is to be defined as the last argument
  • HOW?! So far all of my attempts to define the size returned error messages. All of them.

Guessing that I just ran into a massice misunderstanding and being frustrated about not being able to solve such a simple question all by myself I really wonder where the heck did I miss something in defining the size?!

Best Answer

This worked for me:

# truncate -s 8G foobar
# losetup -f --show foobar
/dev/loop0
# mkdosfs -F 32 /dev/loop0 $((2*1024*1024))
mkfs.fat 3.0.25 (2014-01-17)
Warning: block count mismatch: found 8388608 but assuming 2097152.
Loop device does not match a floppy size, using default hd params
# mount /dev/loop0 /mnt/tmp
# df -h /mnt/tmp
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      2.0G  4.0K  2.0G   1% /mnt/tmp

An alternative approach, in case the mkfs does not have an option to limit the size, is to create a loop device with limit:

# losetup -f --show --sizelimit $((2*1024*1024*1024)) /dev/loop0
/dev/loop1
# mkdosfs -F 32 /dev/loop1
mkfs.fat 3.0.25 (2014-01-17)
Loop device does not match a floppy size, using default hd params
# mount /dev/loop0 /mnt/tmp
# df -h /mnt/tmp
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      2.0G  4.0K  2.0G   1% /mnt/tmp

Of course, if it's a partition, you could also shrink the partition, then enlarge it again.

Related Question