Home / Alt manpages / mkfs.fat(8)

  • mkfs.fat(8)
  • Admin command
  • linux

Create a FAT filesystem safely with mkfs.fat

You will finish with a FAT filesystem created in a disposable image, or on a deliberately selected device, and a check that confirms its basic structure. The examples use mkfs.fat from dosfstools 4.2, installed here as package version 4.2-1.1build1. The same command is also available under the names mkdosfs, mkfs.msdos and mkfs.vfat.

Allow about fifteen minutes for an image. A real device needs extra time to identify the target and decide whether its contents can be destroyed. You need a shell and dosfstools. Creating a filesystem on a block device normally needs elevated privileges; creating an image in a directory you own does not.

1. Check the installed command

Start with read-only checks. They do not change a disk or image:

$ command -v mkfs.fat
/usr/sbin/mkfs.fat
$ mkfs.fat --help
Usage: mkfs.fat [OPTIONS] TARGET [BLOCKS]
$ mkfs.fat -V
mkfs.fat 4.2 (2021-01-31)
$ dpkg-query -W -f='${Package} ${Version}\n' dosfstools
dosfstools 4.2-1.1build1

The installed command reports its version with -V. Do not use --version here: this 4.2 build treats that as an unrecognised option. The manpage describes the positional target as either a device or an image file.

Checkpoint

Stop if the command path or package version is not the one you expected. A different distribution release can have different defaults and diagnostics.

2. Create a disposable image first

An image lets you practise without touching a disk. The -C option creates the target file, and the final argument is a count of 1024-byte blocks, so 16384 means 16 MiB. This is not a sector count:

$ IMAGE=/tmp/mkfs-fat-example.img
$ rm -f "$IMAGE"
$ mkfs.fat -C -n EXAMPLE "$IMAGE" 16384
mkfs.fat 4.2 (2021-01-31)
$ stat -c 'size=%s bytes' "$IMAGE"
size=16777216 bytes

The rm line is safe only because IMAGE names the disposable path in this example. Review that variable before running it. The image is sparse: its apparent size is correct, but only filesystem metadata and other allocated areas need occupy storage immediately.

With -C, the block count is required because the new file has no existing size for mkfs.fat to use. Without -C, omitting the block count makes mkfs.fat use the available size of the target. If you specify a block count, remember that it is in KiB.

3. Verify the image without mounting it

Use fsck.fat in no-modify mode to inspect the new filesystem. The -n option checks without asking to repair anything, and -v gives useful detail:

$ fsck.fat -nv "$IMAGE"
fsck.fat 4.2 (2021-01-31)
Checking we can access the last sector of the filesystem
Boot sector contents:
System ID "mkfs.fat"
Media byte 0xf8 (hard disk)
       512 bytes per logical sector
      2048 bytes per cluster
         4 reserved sectors
         2 FATs, 16 bit entries
Checking for unused clusters.

Exact counts vary with the filesystem layout. Check for a successful exit status and confirm that the reported sector, cluster and FAT details match the options you chose:

$ printf 'fsck status: %s\n' "$?"
fsck status: 0

Do not mount an image just to prove that mkfs.fat wrote a filesystem. If you later mount it, use a controlled mount point, unmount it afterwards, and do not expose an untrusted filesystem to a service or desktop session without considering its contents.

4. Format a real partition only after identifying it

Formatting overwrites the target's filesystem metadata and usually makes existing files inaccessible. It is destructive and may interrupt a service or remove the only copy of data. Before using an actual device, identify it with a read-only command and confirm its mount state:

$ lsblk -o NAME,PATH,SIZE,FSTYPE,LABEL,MOUNTPOINTS
$ findmnt --source /dev/sdX1

Replace /dev/sdX1 with the exact partition you have selected. Never copy that placeholder unchanged. If findmnt prints a mount, unmount it during a planned maintenance window before formatting:

$ sudo umount /dev/sdX1
$ sudo mkfs.fat -F 32 -n BACKUP /dev/sdX1
$ sudo fsck.fat -nv /dev/sdX1

sudo is needed for the device operations when your account lacks write access. It does not make an ambiguous target safe. If you formatted the wrong device, stop writing to it and use a recovery process; there is no mkfs.fat undo command. Restore from a known-good backup rather than trying to recreate the old directory entries on the live device.

The examples use a partition. Formatting a whole disk can destroy its partition table and is normally the wrong operation for removable storage that is meant to contain one partition. By default mkfs.fat refuses devices that have partitions or virtual mappings. The -I option disables that safety check and should be reserved for a clearly understood superfloppy or other special layout.

5. Choose the FAT variant deliberately

When -F is absent, mkfs.fat chooses FAT12, FAT16 or FAT32 to fit the filesystem size. That is usually the least surprising choice. Use -F 12, -F 16 or -F 32 only when the reader requires a particular variant and you have checked that the target size supports it.

The default is two file allocation tables. The volume label supplied with -n can contain up to 11 characters and is encoded with DOS codepage 850 unless --codepage changes it. An empty label, whitespace, or NO NAME has the same effect as leaving -n out.

For ordinary removable media, leave alignment, sector size and cluster size at their defaults unless a documented consumer requires otherwise. If you set -s, the sectors-per-cluster value must be a power of two from 1 to 128. If you set -S, the logical sector size must be a power of two of at least 512 bytes. Values above 4096 bytes are not conforming to the FAT specification and may not work everywhere.

6. Keep special options in their lane

Several options affect compatibility rather than the basic act of creating a filesystem:

  • --variant=atari creates the legacy Atari form instead of the standard Microsoft FAT form. Use it only for an Atari-compatible consumer.
  • --offset=SECTOR writes the filesystem inside a disk image at a specified sector. It is useful for a partitioned image, but the offset must match the image layout.
  • --mbr=auto or an explicit yes/no value controls a fake MBR entry for particular whole-disk Windows cases. It is not a replacement for partitioning.
  • -c checks for bad blocks before creation, which can take a long time and is not a substitute for a storage health assessment.
  • --invariant makes normally time-based or random filesystem data repeatable. The manpage identifies it as a testing aid, not a general production setting.

mkfs.fat cannot create a bootable filesystem. If you need boot support, treat boot code, partition layout and the operating system installation as separate tasks. Do not infer bootability from a successful mkfs.fat run.

Done means

  • You checked the installed mkfs.fat and dosfstools versions.
  • You practised with -C and understood that its block count is in KiB.
  • fsck.fat -nv completed successfully without modifying the image.
  • Any real device was identified, unmounted and reviewed before the destructive command.
  • You left -I and specialised geometry or layout options unused unless a documented compatibility requirement justified them.
  • You have a recovery path, because mkfs.fat has no undo operation.