Sample Header Ad - 728x90

Create FAT-formatted disk image that can fit 1G file

2 votes
2 answers
284 views
I'm struggling to create a FAT-formatted a disk image that can store a file of known size. In this case, a 1 GiB file. For example:
# Create a file that's 1 GiB in size.
dd if=/dev/zero iflag=count_bytes of=./large-file bs=1M count=1G
# Measure file size in KiB.
LARGE_FILE_SIZE_KIB="$(du --summarize --block-size=1024 large-file | cut --fields 1)"
# Create a FAT-formatted disk image.
mkfs.vfat -vv -C ./disk.img "${LARGE_FILE_SIZE_KIB}"
# Mount disk image using a loopback device.
mount -o loop ./disk.img /mnt
# Copy the large file to the disk image.
cp --archive ./large-file /mnt
The script fails with the following output:
++ dd if=/dev/zero iflag=count_bytes of=./large-file bs=1M count=1G
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 39.6962 s, 27.0 MB/s
+++ du --summarize --block-size=1024 large-file
+++ cut --fields 1
++ LARGE_FILE_SIZE_KIB=1048580
++ mkfs.vfat -vv -C ./disk.img 1048580
mkfs.fat 4.2 (2021-01-31)
Auto-selecting FAT32 for large filesystem
Boot jump code is eb 58
Using 32 reserved sectors
Trying with 8 sectors/cluster:
Trying FAT32: #clu=261627, fatlen=2048, maxclu=262144, limit=65525/268435446
Using sector 6 as backup boot sector (0 = none)
./disk.img has 64 heads and 63 sectors per track,
hidden sectors 0x0000;
logical sector size is 512,
using 0xf8 media descriptor, with 2097144 sectors;
drive number 0x80;
filesystem has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 2048 sectors, and provides 261627 clusters.
There are 32 reserved sectors.
Volume ID is f0de10c3, no volume label.
++ mount -o loop ./disk.img /mnt
++ cp --archive ./large-file /mnt
cp: error writing '/mnt/large-file': No space left on device
How do I create a FAT-formatted disk image that's large enough store a file of known size? Resources: - https://linux.die.net/man/1/dd - https://linux.die.net/man/8/mkfs.vfat - https://linux.die.net/man/8/mount - https://linux.die.net/man/1/cp - https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system#Size_limits #### EDIT 1 My assumption was that mkfs.vfat -C ./disk.img N would create an image that has N KiBs of usable space, but I guess that's not the case. #### EDIT 2 It seems like a dead end to try to calculate exactly how big the disk image needs to be to store a file of known size because of the complexities around FAT sector/cluster size limits. As suggested in the answers, I've settled for adding 20% extra space to the disk image to allow for FAT overhead.
Asked by jdeanwallace (123 rep)
Apr 26, 2024, 12:42 PM
Last activity: Apr 30, 2024, 01:35 PM