Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add files to VFAT image without mounting

Tags:

I want to create a new VFAT image and add a few files to it.

# Create file of 1MB size: dd if=/dev/zero of=my-image.fat count=1 bs=1M # Format file as VFAT: mkfs.vfat ./my-image.fat 

Now I want to add the files ./abc, ./def and ./ghi to the image.

How do I do that without mount -o loop or fusermount? I only want to write to a new, empty, pristine VFAT image. I don't need deleting appending or any "complicated" operations.

I tried 7z -a because 7zip can read VFAT images, but it does not know how to write to it.

like image 363
kay Avatar asked Mar 13 '14 16:03

kay


People also ask

What is the difference between FAT and VFAT?

Unlike FAT, which restricts file names to having no more than eight characters, VFAT expanded that range to accommodate up to 255 characters. VFAT is also supported by other operating systems and is installed as a driver extension for all of them.

How do I create a VFAT partition?

Write the new partition info onto the disk with the w command, and exit fdisk. Make a vfat filesystem on the new partition by typing mkfs -t vfat /dev/sdc1 (assuming that /dev/sdc1 is the name of the new partition that you just created). It only takes a minute or two. Power down the disk or unplug it from the USB.


1 Answers

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 ::subdir mtype -i ~/imags/fat_file.img ::subdir/hello.txt 
like image 87
Anthony Avatar answered Oct 08 '22 07:10

Anthony