I observe that the 'SECTORS_PER_FAT' is 2 by default. I want to make it 9. How can I do this?
It seems this number is automatically calculated. Since the File Allocation Table has one entry per cluster and the number of clusters is finite and known in advance, there is no point of allocating more sectors to FAT.
In your case 2 is not "by default", it's determined mostly by the filesystem size (the size of Disk.img
) and the cluster size.
Take the minimal possible cluster size (i.e. minimal logical sector size (-S512
) and minimal number of sectors per cluster (-s1
)):
# 'touch' is not needed, 'truncate' is enough
truncate Disk.img -s 1M
mkfs.vfat -F12 -S512 -s1 Disk.img
# the below line extracts the value we're after
<Disk.img od -j22 -N2 -tu2 | awk 'NR==1{print $2}'
This gives you the maximal number of clusters for the given file size, so the maximal number of entries in FAT, so the maximal FAT size, so the maximal number of sectors needed per FAT. Still it's only 6.
I think in theory you can formally allocate more sectors and the filesystem will be valid. The point is any additional sector in FAT would never be used because all clusters are already covered by entries in these 6 sectors. In practice mkfs.vfat
doesn't waste sectors like this.
Rough calculation how 1 MiB and -S512 -s1
give 6 sectors per FAT:
- We neglect structures other than clusters as if all the space contributed to clusters. With
-S512 -s1
a cluster takes 0.5 KiB. 1 MiB holds exactly 2 Ki of such clusters. Note in this calculation the number of clusters is inflated with respect to the strict value.
- In FAT12 one entry takes 12 bits (2 entries per 3 bytes), so 1.5 B. We need one entry per cluster. 2 Ki of entries take 3 KiB. So FAT should take 3 KiB and this number is also inflated.
- A single sector is 0.5 KiB. You need 6 sectors to hold 3 KiB. Since 3 KiB was somewhat inflated, you need at most 6 sectors.
The tool calculates this number more strictly.
The number of FATs (-f
) hardly matters. It's not that clusters are divided among separate FATs. Additional FATs are just copies and each still serves all clusters. More FATs take more space, this slightly reduces the number of clusters. For some "threshold sizes" of the filesystem this phenomenon affects the number of sectors per FAT. However for 1 MiB and -S512 -s1
the needed number of sectors per FAT is 6 regardless of -f
(note: in my tests mkfs.vfat
refused to create more than 4 FATs).
After we maximized the number of clusters for the given filsystem size, the easiest way to increase the number of sectors per FAT is to make the filesystem bigger. We got 6 for 1 MiB, so probably we will get 9 for 1.5 MiB. Indeed:
truncate Disk.img -s 1536k
mkfs.vfat -F12 -S512 -s1 Disk.img
<Disk.img od -j22 -N2 -tu2 | awk 'NR==1{print $2}'
Note 1536k
is not the minimal size to get 9. In my tests 1389k
gave me 9 and 1388k
gave me 8. Now one can see how -f
affects the number. For 1389k
-f2
(the default value) gives 9 and -f3
gives 8. For 1388k
-f1
gives 9 and -f2
gives 8.
My testbed:
- Debian GNU/Linux 9
mkfs.vfat
as symlink to mkfs.fat
mkfs.fat
version: 4.1 (2017-01-24)
References:
- The FAT filesystem by Andries Brouwer
(It's from 2002 but I don't think FAT12 has significantly changed since)
- Design of the FAT file system on Wikipedia