29

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.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • 1
    What is the objection to mounting it? – Mark Setchell Mar 13 '14 at 16:47
  • 8
    @MarkSetchell `mount` needs sudo, `fusermount` needs fuse. Do I really need to elaborate more? – Kijewski Mar 13 '14 at 19:08
  • 1
    Check out [genimage](https://github.com/pengutronix/genimage). It generates filesystem/disk/flash images from recipes written in [libconfuse syntax](https://github.com/libconfuse/libconfuse). Here is [my recipe](https://github.com/Un1Gfn/beaglebone/blob/5455711cdcbebcad8a192293d7662a919461df55/cfg.sh) for a 2MiB FAT12 image and a disk image wrapping it. It runs mcopy behind the scene. You don't have to hand craft the scripts your self. (warning - build git `master` instead of buggy v14!) – Darren Ng Aug 04 '21 at 09:53

2 Answers2

60

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
Anthony
  • 12,177
  • 9
  • 69
  • 105
  • This is a great option for those using WSL which has a sub-par implementation of `mount`. – ubiquibacon Oct 03 '19 at 07:12
  • 2
    There is also fatcat utility, which can list files inside fat32.img. For example, `fatcat my_fat32.img -l /`. See manual here http://manpages.ubuntu.com/manpages/bionic/man1/fatcat.1.html – Ashark Jan 19 '21 at 14:23
-17

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I know what hardships are involved in filesystem operations. That's one of the reasons I don't want to use mount. I don't need a journal for example, no rights management, or any of the VFS abstraction. Editing the image offline should be a much easier. Still I don't find any tool for my question. – Kijewski Mar 13 '14 at 19:07
  • vfat has no journaling and no rights. The tool you want **IS** mounting the image. Anything else will require EXACTLy replicating what the file system engines are doing anyways, so why not use the one you've already got, instead of flailing around for something you're not going to find? – Marc B Mar 13 '14 at 19:08
  • 1
    I found fusermount to work in half of the cases at best. Mount requires sudo rights. So I dislike both options. (Might be that fuse got less terrible in the last years. I stood away from it if I could.) – Kijewski Mar 13 '14 at 19:11