How to calculate the minimum size for a FAT partition to fit given files?

Is there a way to calculate how big a partition needs to be to fit a file (or several files of a given size)?

Let's say I want to make a FAT32 partition on a usb drive and store 1 Gb file (or two 500 Mb files, or anything else) there, and I want the partition to be as small as possible.

What would be the algorithm to calculate the size of this partition, besides trial and error?

• Depends on the cluster size and partition size. Besides two allocation tables (which depend on the cluster & partition sizes), there are fixed-sized areas consisting of a boot sector & its reserved area, plus the root directory. You can run experiments go derive a formula. Format a few partitions of varying sizes, and figure out the max sized file you can create. Feb 29 at 3:54
• IIRC if you can obtain the start LBA of the first file, then you have the salient information you need. Everything before that LBA is filesystem overhead, and everything after that LBA is for files (unless the root directory has to expand). Feb 29 at 4:03

It depends on the cluster size (usually 4 KB, but you can pick other values when formatting) and on how many files this 1 GB is stored. Any file will use at least one cluster and files larger than a cluster will use an integer number of cluster, occupying some uneeded space at it's end with very rare exceptions. You just need to round up (ceil) to cluster size for each file.

``````from math import ceil

def calculate_fat32_used_space(file_sizes, cluster_size=4096):
clusters_used = [ceil(size / cluster_size) for size in file_sizes]
total_clusters = sum(clusters_used)