Skip to content

Commit

Permalink
Add script to prevent disk automount
Browse files Browse the repository at this point in the history
  • Loading branch information
michelegera committed Dec 10, 2023
1 parent ef7376d commit d5cd214
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/os/create_symbolic_links.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ create_symlinks() {
"shell/irbrc"
"shell/rgignore"
"shell/screenrc"
"shell/scripts"
"shell/ssh"

)
Expand Down
62 changes: 62 additions & 0 deletions src/shell/scripts/no_automount
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/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.
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

0 comments on commit d5cd214

Please sign in to comment.