Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active October 24, 2022 18:02
Show Gist options
  • Save JayBrown/cf2712c5d0ff1718af794fdedfdd5c0d to your computer and use it in GitHub Desktop.
Save JayBrown/cf2712c5d0ff1718af794fdedfdd5c0d to your computer and use it in GitHub Desktop.
macOS: check if a disk image is APFS-formatted
#!/bin/bash
# ckimg v0.5
for filepath in "$@"
do
shortpath="${filepath/#$HOME/~}"
filename=$(basename "$filepath")
echo "Accessing: $shortpath"
extension="${filename##*.}"
! [[ $extension =~ ^(dmg|DMG|sparsebundle|SPARSEBUNDLE|sparseimage|SPARSEIMAGE|cdr|CDR|iso|ISO|img|IMG)$ ]] \
&& { echo "Wrong file type: continuing..." >&2 ; continue ; }
fsysr=$(hdiutil imageinfo "$filepath" 2>/dev/null)
fsys=$(echo "$fsysr" \
| sed -n '/partition-filesystems:/,/[A-Z]/p' \
| grep -v -e "partition-")
sysn=$(echo "$fsys" | wc -l | xargs)
if [[ $sysn -eq 0 ]] ; then
echo "Filesystem: n/a"
elif [[ $sysn -eq 1 ]] ; then
fsys=$(echo "$fsys" \
| sed -e "s/://g" | xargs)
echo "Filesystem: $fsys"
else
fsysx=$(echo "$fsys" | sort | sed -e "s/://g" \
| awk 1 ORS=' & ' \
| sed "s/ & $//" | xargs)
echo "Filesystems: $fsysx"
fi
format=$(echo "$fsysr" | awk -F": " '/^Format: /{print $2}')
if ! [[ $format ]] ; then
format="n/a"
compat="n/a"
fi
echo "Format: $format"
descr=$(echo "$fsysr" | awk -F": " '/^Format Description: /{print $2}')
echo "Description: $descr"
cryptinfo=$(hdiutil isencrypted "$filepath" 2>/dev/null)
if [[ $cryptinfo == "encrypted: NO" ]] ; then
crypt="none"
else
crypt=$(echo "$fsysr" | awk -F": " '/Encryption: /{print $2}')
! [[ $crypt ]] && crypt="n/a"
fi
echo "Encryption: $crypt"
if [[ $extension =~ ^(dmg|DMG)$ ]] ; then
fsys=$(echo "$fsys" | grep -v "EFI")
if [[ $(echo "$fsys" | grep "APFS") ]] ; then
compat="macOS 10.14+"
else
if [[ $format == "ULFO" ]] ; then
compat="OS X 10.11+"
elif [[ $format == "UDBZ" ]] ; then
if [[ $crypt == "AES-256" ]] ; then
compat="Mac OS X 10.5+"
else
compat="Mac OS X 10.4+"
fi
else
if [[ $crypt == "AES-256" ]] ; then
compat="Mac OS X 10.5+"
elif [[ $crypt == "AES-128" ]] ; then
compat="Mac OS X 10.3+"
else
compat="Mac OS X 10.2+"
fi
fi
fi
elif [[ $extension =~ ^(sparsebundle|SPARSEBUNDLE)$ ]] ; then
compat="Mac OS X 10.5+"
elif [[ $extension =~ ^(sparseimage|SPARSEIMAGE)$ ]] ; then
if [[ $crypt == "AES-256" ]] ; then
compat="Mac OS X 10.5+"
else
compat="Mac OS X 10.3+"
fi
elif [[ $extension =~ ^(cdr|CDR|iso|ISO)$ ]] ; then
compat="Mac OS X 10.0+"
elif [[ $extension =~ ^(img|IMG)$ ]] ; then
compat="OS 9"
fi
echo "Compatibility: $compat"
echo -n "Verifying " >&2 && while : ; do echo -n ▇ >&2 ; sleep 1 ; done &
trap "kill $!" EXIT
hdiverify=$(hdiutil verify "$filepath" 2>&1)
kill $! && trap " " EXIT
wait $! 2>/dev/null
if [[ $(echo "$hdiverify" | grep "has no checksum\.$") ]] ; then
echo -e "\nVerification: no checksum"
else
hdiresult=$(echo "$hdiverify" | grep "^hdiutil: verify: checksum" | rev | awk '{print $1}' | rev)
if [[ $hdiresult == "INVALID" ]] ; then
hdihash=$(echo "$hdiverify" | grep "^calculated" | xargs)
else
hdihash=$(echo "$hdiverify" | grep "^verified" | awk '{print substr($0, index($0,$2))}')
fi
echo -e "\nVerification: $hdiresult ($hdihash)"
fi
echo "********"
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment