Repairing old FAT flash drives

I have done it many times before, and most of the time I am reinventing the wheel over and over again. Why not just document it here and now?

Problem

Due to the need to interact with some electronics such as speakers and file transfer to printing places, I have to maintain a number of flash drives formatted as FAT drives. Despite its rather popular usage with significantly wide support, FAT is not exactly the most robust filesystem in the wild. Therefore it is not uncommon to find filesystem errors when we have to use FAT.

Since I have been using Linux for many years (from at least 2011), today all of my computers are using Linux only. Therefore I have the need to be able to diagnose and deal with damaged FAT drives. Now how do we do that in Linux?

Check FAT filesystem

There are several options, but I mainly use this askubuntu answer. Meanwhile another answer suggests this filesystem troubleshooting article. The tool in question is available in Arch Linux repository as dosfstools.

To install it from Arch Linux repository, we can run the following command:

sudo pacman -S dosfstools

After installing the package, we will have access to the tool fsck.vfat. For example if we want to check the filesystem inside /dev/sdb1 (change to the appropriate device name), we can simply run the following:

sudo fsck.vfat -tawl /dev/sdb1

The options I use are:

  • -t: mark unreadable clusters as bad
  • -a: automatically fix errors
  • -w: write to disk immediately
  • -l: list processed filenames

Check for fake flash drives

Sometimes the problem is not because of a broken filesystem, but due to a fake flash drive. Essentially a fake flash drive claims that they have more storage space than they actually have. When interacting with such drive, it would at first appear fine if you just store files smaller than its actual storage capacity. Once you went beyond the actual storage capacity (because the device claims it still has more free spaces), it will start overwriting existing files.

For that we will also need a tool called f3 - Fight Flash Fraud. On Arch Linux, you can obtain the tool from the following AUR package: f3

Then to check whether the announced disk size is true or not (in this example we use /dev/sdb), we can use:

f3probe -t /dev/sdb

Note that here we did not specify the partition number (eg. /dev/sdb1 or /dev/sdb2), but rather, the block device itself (/dev/sdb).

Reformat the flash drive

Finally, to reformat the partition with FAT:

sudo mkfs.vfat -n MYFLASHDRIVE /dev/sdb1

The -n MYFLASHDRIVE option allows us to label the newly formatted partition as MYFLASHDRIVE.

If we want to specify the FAT type:

sudo mkfs.vfat -F 32 -n MYFLASHDRIVE /dev/sdb1

Conclusion

With this article, now you can:

  1. Check and repair damages of your FAT drives using fsck.vfat.
  2. Check if the device you have is announcing their true storage size or not using f3probe.
  3. Format the device with FAT filesystem using mkfs.vfat.