Add files to VFAT image without mounting Add files to VFAT image without mounting linux linux

Add files to VFAT image without mounting


I want to do the exact same thing as part of an image build for an embedded system. It's really annoying that the entire build, which takes ~3hrs, could be completely unattended except for the final steps which required a password in order to mount a VFAT image. Fortunately, I found a set of tools which solve the problem.

You want mcopy provided by GNU mtools.

Mtools is a collection of utilities to access MS-DOS disks from GNU and Unix without mounting them.

It also supports disk images such as VFAT image files.

As an example, the following command will copy the file hello.txt from your current directory into the subdirectory subdir of the VFAT file system in ~/images/fat_file.img:

mcopy -i ~/images/fat_file.img hello.txt ::subdir/hello.txt

There are more useful inclusions in mtools, such as mdir and mtype which are great for inspecting your image file without having to mount it.

mdir -i ~/images/fat_file.img ::mdir -i ~/images/fat_file.img ::subdirmtype -i ~/imags/fat_file.img ::subdir/hello.txt


What you want is basically impossible. You can't just "stuff" some file data onto the end of a disk image and have those files magically "appear" within the image. Feel free to stuff in the data, but there's more to a filesystem than just the data. You have to EXACTLY replicate the metadata operations that the file system handles for you, e.g. updating the FAT tables.

In other words, you'd have to build the ENTIRE FAT filesystem handling code in your own code. Which is utterly ludicrous. Just mount the image, use normal file operations on that mounted file system, then dismount it again. Boom, done.