git clone git@gist.github.com:35b1c2781cd7a782cc521a75731b4040.git
ln -s $(pwd)/disable_automount.sh ~/.local/bin/no-mount
no-mount <DISK_NAME>
Last active
April 17, 2024 13:14
-
-
Save pythoninthegrass/35b1c2781cd7a782cc521a75731b4040 to your computer and use it in GitHub Desktop.
Disable external drives from automatically mounting on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# SOURCE: https://akrabat.com/prevent-an-external-drive-from-auto-mounting-on-macos/ | |
FSTAB=/etc/fstab | |
NAME=$1 | |
if [ -z "$NAME" ] ; then | |
echo "Usage: $(basename $0) <DISK_NAME>" | |
exit 1 | |
fi | |
# 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. | |
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 -r 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 | |
# 2. Cut out the last 3 fields (size, units & ID) from $PARTIAL | |
# by reversing, cutting and un-reversing | |
[[ ${LINE} =~ ($NAME.*) ]] && PARTIAL=${BASH_REMATCH[1]} | |
VOLUME_NAME=$(echo $PARTIAL | rev | cut -d' ' -f 4- | rev) | |
add_identifier $ID "$VOLUME_NAME" | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment