0

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?

2
  • 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.
    – sawdust
    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).
    – sawdust
    Feb 29 at 4:03

1 Answer 1

1

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)
    return total_clusters * cluster_size
print(calculate_fat32_used_space([536870912, 536870912])) # For two 500MB files

All sizes in bytes (easy to convert to whatever). Just get an array of filesizes as parameter and that's it. It should be trivial to adapt this to work with other unit sizes. I'd also account for some overhead in terms of space for the FAT32 fs itself. Maybe 5 MB?

1
  • It turned out to be around 2 Mb at 1 Gb partition size, 5 Mb at 2 Gb and 7 Mb at 3 Gb.
    – Nick L
    Mar 1 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .