Create a FAT file system image on Linux

How to create the image file

[UPDATE: 2018.12.11.]
I’m sorry for the late correction, the approval request of comments landed in the SPAM. Stefan Naumann and Wojciech Franczyk pointed out correctly:

“Hi. In the point 3 you are creating FAT filesystem on the disk image, but you should have it created only on the partition. This is corrupting the image. You can check it trying fdisk -l test.img after performing the point 3 – you will get no partitions.

To fix it we first need to map the partition to /dev:
sudo losetup –offset 1048576 -f test.img
offset value is the start sector of the partition [2048] multiplied by sector size [512] to get bytes.

And create FAT filesystem on the partition, not disk:
sudo mkfs.vfat /dev/loop0

I’m leaving the solution here because I had exactly this problem as I needed valid whole disk image (to boot it), not only the partition 🙂
Nice tutorial thought, thanks for that, It helped me. Cheers.”

  1. Create a file filled with zeros:
    $ dd if=/dev/zero of=test.img count=50 bs=1M

    This command makes a 50 MB image file. Change the “count” argument for different size.

  2. Create the partition (and partition table):
    $ fdisk test.img
    
    Command (m for help): o
    Building a new DOS disklabel with disk identifier 0x46ac6035.
    
    Command (m for help): n
    Partition type:
      p primary (0 primary, 0 extended, 4 free)
      e extended
    Select (default p): <Enter>
    Using default response p
    Partition number (1-4, default 1): <Enter>
    First sector (2048-99999, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-99999, default 99999): <Enter>
    Using default value 99999
    Partition 1 of type Linux and of size 47.8 MiB is set
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): c
    
    Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'
    
    Command (m for help): w
    The partition table has been altered!
    
    Syncing disks
  3. Create the FAT file system in the image
    $ mkfs.vfat test.img 
    mkfs.fat 3.0.22 (2013-07-19)

 How to mount the image and copy files

  1. Create a directory for mounting
    $ sudo mkdir /mnt/test
  2. Mount the image
    $ sudo mount test.img /mnt/test

    Now you can copy/delete files in /mnt/test directory which will be written into the image file.

  3. After file operations unmount the image
    $ sudo umount /mnt/test
  4. Delete the directory
    $ sudo rmdir /mnt/test

4 thoughts on “Create a FAT file system image on Linux

  1. Stefan Naumann

    Hello.

    As far as I see it mkfs.fat writes the FAT and partition information into the first sectors of the image, i.e. overwriting the perfectly fine MBR created before. That means: You probably don’t have an MBR anymore in that ISO (or IMG) after writing the FAT-filesystem.

    Reply
  2. Wojciech Franczyk

    Hi. In the point 3 you are creating FAT filesystem on the disk image, but you should have it created only on the partition. This is corrupting the image. You can check it trying fdisk -l test.img after performing the point 3 – you will get no partitions.

    To fix it we first need to map the partition to /dev:
    sudo losetup –offset 1048576 -f test.img
    offset value is the start sector of the partition [2048] multiplied by sector size [512] to get bytes.

    And create FAT filesystem on the partition, not disk:
    sudo mkfs.vfat /dev/loop0

    I’m leaving the solution here because I had exactly this problem as I needed valid whole disk image (to boot it), not only the partition 🙂
    Nice tutorial thought, thanks for that, It helped me. Cheers.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.