Skip to content

Instantly share code, notes, and snippets.

@zcshiner
Created January 1, 2024 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zcshiner/7216ef77910cfe4e420274fbfbe4f431 to your computer and use it in GitHub Desktop.
Save zcshiner/7216ef77910cfe4e420274fbfbe4f431 to your computer and use it in GitHub Desktop.
Mount APFS volumes as read only
#!/bin/bash
# Usage
# mountAPFSreadOnly device
# Example
# mountAPFSreadOnly /dev/disk8s1
# inspiration via: https://forums.macrumors.com/threads/how-to-mount-an-external-drive-apfs-as-read-only.2241919/post-28578009
#require sudo
if [ "$(whoami)" != "root" ] ; then
echo "Run this script with sudo" >&2
exit 1
fi
# stdout is hidden on some commands to make the output make more sense
diskutil unmount $1 > /dev/null
echo "Volume passphrase needed"
diskutil apfs unlockVolume $1 -nomount > /dev/null
diskutil mount readOnly $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment