Skip to content

Script that creates a FAT32 disk image from directory contents without root privileges

License

Notifications You must be signed in to change notification settings

Othernet-Project/dir2fat32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

dir2fat32

Create a FAT32 disk image from the contents of a directory, without mounting or root privileges, completely automatically.

Prerequisites

dir2fat32 requires the following packages to be installed on the host Linux system:

  • util-linux (fdisk)
  • dosfstoos (mkfs.vfat)
  • mtools (mmd, mcopy)

Quick usage guide

Before running the script, you need to prepare the source directory. The directory should not contain any symlinks or paths with characters that are illegal on FAT32. Note that file and directory permissions do not really matter.

Once the directory is prepared, invoke the dir2fat32 script:

$ ./dir2fat32.sh test.img 200 test
==============================================
Output file:      test.img
Partition size:   200 MiB
Image size:       208 MiB
Sector size:      512 B
Source dir:       test
==============================================
===> Creating container image
===> Creating FAT32 partition image
===> Copying files
  Creating hardware
  Copying bash_functions.sh
  Copying aliases_example.txt
  Copying colors.sh
  Copying alias_manager.sh
  Copying hardware/backlight.sh
  Copying README.rst
===> Copying partition into container
===> Removing partition image file
===> DONE

The above example creates an image file called test.img that is 208 MiB in size and contains the contents of the test directory.

The image has a DOS partition table with a single FAT32 partition. You can verify this with fdisk:

$ fdisk -l test.img
Disk test.img: 216 MiB, 226492416 bytes, 442368 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x23410f22

Device     Boot Start    End Sectors  Size Id Type
test.img1       16384 425983  409600  200M  b W95 FAT32

The disk image can be mounted for testing purposes using loopback devices.

$ sudo modprobe loop
$ losetup /dev/loop0 test.img
$ sudo partprobe test.img
$ mkdir -p mnt
$ sudo mount /dev/loop0p1 mnt
$ ls mnt
... contents of the test directory ...

The logical sector size can be set using the -S option. The valid values are the same as for the -S option for mkfs.fat command, and those are: 512, 1024, 2048, 4096, 8192, 16384, 32768. For flash-based media such as SD cards and USB sticks, this value should ideally match the page size for optimal performance.

Note that setting the logical sector size too high will render a technically incorrect partition image which has less clusters than a FAT32 partition should. This can lead to mounting issues due to the FAT type being determined solely based on the number of clusters. A warning is shown if the number of clusters is low:

===> Creating FAT32 partition image
WARNING: Not enough clusters for a 32 bit FAT!

Why is my image larger than the specified size?

The size of the generated image is always several MiB larger than the specified size. The specified size is the size of the partition and there is some overhead as well as 8 MiB offset.

License

This script is released under GNU GPLv3. See the COPYING file for more details or visit http://www.gnu.org/licenses/.

About

Script that creates a FAT32 disk image from directory contents without root privileges

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages