Skip to content

Instantly share code, notes, and snippets.

@akrabat
Created August 5, 2020 08:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akrabat/361241b4f102d0a93d502edd8b97c55d to your computer and use it in GitHub Desktop.
Save akrabat/361241b4f102d0a93d502edd8b97c55d to your computer and use it in GitHub Desktop.
Add a volume to /etc/fstab to prevent auto-mounting. See http://akrabat.com/prevent-an-external-drive-from-auto-mounting-on-macos/
#!/usr/bin/env bash
# Usage: ./no_automount My\ Disk
NAME=$1
if [ -z "$NAME" ] ; then
echo "Usage: no_automount {Disk Name}"
exit 1
fi
FSTAB=/etc/fstab
# Add an volume as not auto-mounted to the /etc/fstab file
# by it's identifier. Also pass in the volume name to add a
# comment on that line so that we can identify it later.
#
# example usage:
# add_identifier disk4s1 "Swiftsure Clone"
# add_identifier disk4s5 "Swiftsure Clone - Data"
function add_identifier {
ID=$1
VOLUME_NAME=$2
if [ -z "$VOLUME_NAME" ] ; then
echo "add_identifier() takes two parameters: ID and VOLUME_NAME"
exit 2
fi
# get UUID and TYPE from `diskutil info $ID`
UUID=`diskutil info "$ID" | grep "Volume UUID" | awk '{print $NF}'`
TYPE=`diskutil info "$ID" | grep "Type (Bundle)" | awk '{print $NF}'`
# Remove this UUID from fstab file
sudo sed -i '' "/$UUID/d" $FSTAB
# Add this UUID to fstab file
echo "UUID=$UUID none $TYPE rw,noauto # $VOLUME_NAME" | sudo tee -a $FSTAB > /dev/null
echo "Added $UUID ($VOLUME_NAME) to $FSTAB"
}
# Add all volumes that start with $NAME to the /etc/fstab such
# that they do not automout.
# Get list of identifiers and volume names from `diskutil info`
LIST=`diskutil list | grep "$NAME"`
# Iterate over $LIST
echo "$LIST" | while read LINE
do
# Example of $LINE:
# 1: APFS Volume Swiftsure Clone - Data 592.1 GB disk4s1
# Extract disk identifier which is the last field on the line
ID=`echo $LINE | awk '{print $NF}'`
# Extract volume name in the middle of $LINE by:
# 1. remove all characters before $NAME using regex capture assign to $PARTIAL
[[ ${LINE} =~ ($NAME.*) ]] && PARTIAL=${BASH_REMATCH[1]}
# 2. Cut out the last 3 fields (size, units & ID) from $PARTIAL
# by reversing, cutting and un-reversing
VOLUME_NAME=`echo $PARTIAL | rev | cut -d' ' -f 4- | rev`
add_identifier $ID "$VOLUME_NAME"
done
# All done!
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment