#!/bin/sh
# This script is for formatting SD cards of at least 4GB
# It formats them as FAT32 with 32K cluster size
#
# Argument 1: Device inside /dev to be formatted, eg.: disk99
# Argument 2: Volume name for the partition, eg: UNTITLED
if [ $# -ne 2 ]
then
echo Usage: sdformat32 diskname volumename
exit 1
fi
disk=/dev/"$1"
partition=/dev/r"$1"s1
volume=$2
echo This will DESTROY $disk. Are you sure? "(y/n)"
read yn
if [ $yn == 'y' ]
then
echo Partitioning $disk with volume name $volume ...
diskutil partitionDisk $disk 1 MBRFormat MS-DOS $volume 3G
if [ $? -eq 0 ]
then
echo Unmounting $disk ...
diskutil unmountDisk $disk
if [ $? -eq 0 ]
then
echo Fixing and optimizing partition ...
newfs_msdos -v $volume -S 512 -c 64 -F 32 -m 0xf8 -k 0x6 -o 63 -i 0x1 $partition
if [ $? -eq 0 ]
then
echo Mounting $disk in case you want to write to it ...
echo Do not forget to eject the drive before removing it!
diskutil mountDisk $disk
if [ $? -eq 0 ]
then
echo Done
else
echo The disk was formatted but could not be mounted!
exit 6
fi
else
echo Could not reformat the partition!
exit 5
fi
else
echo Could not unmount volume!
exit 4
fi
else
echo Could not partition disk!
exit 3
fi
else
echo Aborting. Nothing done
exit 2
fi
exit 0