1

I'm writing a program which is meant to be loaded and run exclusively from a FAT32 SD card. I need to create an SD card image for distribution as well as for testing the storage and retrieval of files through this program (also exclusively on the SD card).

I want to use a BASH script to automate the creation of this IMG file which contains the binaries of my program. What command line program can I use to achieve this and how do I use it?

1
  • 1
    Bash is a CLI shell. Why wouldn't you use that
    – Allan
    Commented Oct 24, 2023 at 3:55

2 Answers 2

1

The script below would create the disk image file myimage.img, then copy the contents of the Desktop folder MyFolder to a FAT32 formatted volume stored in this disk image.

#!/bin/bash
# Create the empty disk image file,
dd if=/dev/zero of=myimage.img bs=1m count=100
# Assign a /dev entry to the disk image file,
devEntry="$(hdiutil attach -nomount myimage.img)"
# Show ouput from hdiutil.
echo "$devEntry"
# Remove whitespace from variable devEntry.
devEntry="$(echo $devEntry)"
# Create a MBR partition table and mount the newly created FAT32 volume.
diskutil erasedisk fat32 MYSD mbr "$devEntry"
# Copy files from the folder to FAT32 volume.
cp -R ~/Desktop/MyFolder/ /Volumes/MYSD
# Remove all ._* files.
dot_clean /Volumes/MYSD
# Eject the disk image.
hdiutil detach "$devEntry"

A few comments are given below.

  • The size of the disk image is 100 MiB. You can change this by editing count=100.
  • The sector size of the SD card is assumed to be 512 bytes.
  • You can use the dd command to transfer the image to a SD card. The capacity of the SD card must be at least that of the disk image file size.
  • The use of dot_clean is optional.
  • If the SD card does not require a partition table, then you can replace the command diskutil erasedisk fat32 MYSD mbr "$devEntry" with the command given below.
    diskutil erasevolume fat32 MYSD "$devEntry"
    
2
  • This worked! It generated the IMG file I wanted. I also checked the contents of the file and it wasn't full of zeroes either. I then mounted it in an emulator and ran my program and it appears to work fine.
    – AcinonX
    Commented Oct 24, 2023 at 13:24
  • By the way: when I didn't specify mbr in diskutil, it defaulted to GPT (or at least, the text "UEFI" showed up in the hex editor when viewing the file.)
    – AcinonX
    Commented Oct 24, 2023 at 13:35
0

You could use the diskutil command line tool to achieve what you want. It allows mounting a file as a sort of loopback device, and it is able to perform FAT32 formatting.

2
  • diskutil resulted in this weird error (exit code 22 during copy process if I remember) and I couldn't find anything about it online. I prefer David Anderson's dd solution.
    – AcinonX
    Commented Oct 24, 2023 at 13:11
  • Yes, dd is a fine program - here you just use it to create a file of a specific size filled with zeroes. You can basically use any program to create a file of a size you want, it doesn't have to be filled with zeroes.. so you could even just record screencast with Quicktime and use that as a starting point ;-)
    – jksoegaard
    Commented Oct 24, 2023 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .