Boot script
The best thing I can come up with, is a script that will run at boot, and will check whether each Apple_HFS TYPE of volume (disk*s*) is mounted. When the volume isn't mounted, try to repair and mount the volume.
Script commands explained
List local HFS volumes
The diskutil list
command is used to retrieve all local volumes that contain an Apple HFS file system. For example:
$ diskutil list | grep ": Apple_HFS" | awk '{ print $NF }'
disk0s2
disk1s1
disk2s1
Is volume mounted?
The df -lnh
command is used to check whether that volume is mounted. When the volume is not mounted, the command returns no (empty) output. And when the volume is mounted, the output is like:
$ df -lnh | grep /dev/disk1s1
/dev/disk1s1 3.6Ti 13Mi 3.6Ti 1% /Volumes/L
Step by step installation
Create bash script (version #2)
$ sudo nano /Library/Scripts/BootRepairMount.sh
Paste this text into the editor and save it.
#!/bin/bash
TRIES=0
MAXTRIES=60
until diskutil list > /dev/null;do
TRIES=$(($TRIES+1))
if [ $TRIES -gt $MAXTRIES ]; then
exit $?
fi
sleep 1
done
TRIES=0
MAXTRIES=3
for OUTPUT in $(diskutil list | grep ': Apple_HFS' | awk '{ print $NF }')
do
if [[ -z $(df -lnh | grep /dev/$OUTPUT) ]]; then
echo "$OUTPUT is not mounted, repair and mount"
until diskutil repairVolume $OUTPUT; do
TRIES=$(($TRIES+1))
if [ $TRIES -gt $MAXTRIES ]; then
break
else
sleep 1
fi
done
TRIES=0
until diskutil mount $OUTPUT; do
TRIES=$(($TRIES+1))
if [ $TRIES -gt $MAXTRIES ]; then
break
else
sleep 1
fi
done
TRIES=0
fi
done
Set the permissions of the script for root access only
$ sudo chown -R root:admin /Library/Scripts/BootRepairMount.sh
Make the script readable and executable for all users
$ sudo chmod a=rx /Library/Scripts/BootRepairMount.sh
Make the script admin writeable and executable
$ sudo chmod u=rwx /Library/Scripts/BootRepairMount.sh
Test run the script
$ /Library/Scripts/BootRepairMount.sh
Create a .plist file for launchd to run on boot
$ sudo nano /Library/LaunchDaemons/nl.probackup.bootrepairmount.plist
And paste this text into the editor window.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nl.probackup.bootrepairmount</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Scripts/BootRepairMount.sh</string>
</array>
<key>UserName</key>
<string>root</string>
<key>UserGroup</key>
<string>wheel</string>
<key>RunAtLoad</key>
<true></true>
<key>Debug</key>
<true></true>
</dict>
</plist>
Change launchd plist permissions
$ sudo chown -R root:wheel /Library/LaunchDaemons/nl.probackup.bootrepairmount.plist
Load the plist into launchd
$ sudo launchctl load /Library/LaunchDaemons/nl.probackup.bootrepairmount.plist
Confirm that the plist is loaded
$ sudo launchctl list | grep .bootrepairmount
If the plist name appears, like:
851 - nl.probackup.bootrepairmount
it’s installed.
Stop running script on boot
To remove the .plist, in other words stop the script from being run at boot, type the following:
$ sudo launchctl unload -w /Library/LaunchDaemons/nl.probackup.bootrepairmount.plist
All in one line - copy and paste - installation
file=/Library/Scripts/BootRepairMount.sh;{ echo \#\!/bin/bash; echo TRIES=0; echo MAXTRIES=60; echo until\ diskutil\ list\ \>\ /dev/null\;do; echo $'\t'TRIES=\$\(\(\$TRIES+1\)\); echo $'\t'if\ \[\ \$TRIES\ -gt\ \$MAXTRIES\ \]\;\ then; echo $'\t'$'\t'exit\ \$\?; echo $'\t'fi; echo $'\t'sleep\ 1; echo done; echo TRIES=0; echo MAXTRIES=3; echo for\ OUTPUT\ in\ \$\(diskutil\ list\ \|\ grep\ \':\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Apple_HFS\'\ \|\ awk\ \'\{\ print\ \$NF\ \}\'\); echo do; echo $'\t'if\ \[\[\ -z\ \$\(df\ -lnh\ \|\ grep\ /dev/\$OUTPUT\)\ \]\]\;\ then; echo $'\t'$'\t'echo\ \"\$OUTPUT\ is\ not\ mounted,\ repair\ and\ mount\"; echo $'\t'$'\t'until\ diskutil\ repairVolume\ \$OUTPUT\;\ do; echo $'\t'$'\t'$'\t'TRIES=\$\(\(\$TRIES+1\)\); echo $'\t'$'\t'$'\t'if\ \[\ \$TRIES\ -gt\ \$MAXTRIES\ \]\;\ then; echo $'\t'$'\t'$'\t'$'\t'break; echo $'\t'$'\t'$'\t'else; echo $'\t'$'\t'$'\t'$'\t'sleep\ 1; echo $'\t'$'\t'$'\t'fi; echo $'\t'$'\t'done; echo $'\t'$'\t'TRIES=0; echo $'\t'$'\t'until\ diskutil\ mount\ \$OUTPUT\;\ do; echo $'\t'$'\t'$'\t'TRIES=\$\(\(\$TRIES+1\)\); echo $'\t'$'\t'$'\t'if\ \[\ \$TRIES\ -gt\ \$MAXTRIES\ \]\;\ then; echo $'\t'$'\t'$'\t'$'\t'break; echo $'\t'$'\t'$'\t'else; echo $'\t'$'\t'$'\t'$'\t'sleep\ 1; echo $'\t'$'\t'$'\t'fi; echo $'\t'$'\t'done; echo $'\t'$'\t'TRIES=0; echo $'\t'fi; echo done; } > ~/out_file;cat ~/out_file | sudo tee $file;rm ~/out_file;sudo chown -R root:admin $file;ls -l $file;sudo chmod a=rx $file;sudo chmod u=rwx /$file;file=/Library/LaunchDaemons/nl.probackup.bootrepairmount.plist;sudo launchctl unload -w $file &>/dev/null;{ echo \<\?xml\ version=\"1.0\"\ encoding=\"UTF-8\"\?\>; echo \<\!DOCTYPE\ plist\ PUBLIC\ \"-//Apple//DTD\ PLIST\ 1.0//EN\"\ \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"\>; echo \<plist\ version=\"1.0\"\>; echo \<dict\>; echo \<key\>Label\</key\>; echo \<string\>nl.probackup.bootrepairmount\</string\>; echo \<key\>ProgramArguments\</key\>; echo \<array\>; echo \<string\>/Library/Scripts/BootRepairMount.sh\</string\>; echo \</array\>; echo \<key\>UserName\</key\>; echo \<string\>root\</string\>; echo \<key\>UserGroup\</key\>; echo \<string\>wheel\</string\>; echo \<key\>RunAtLoad\</key\>; echo \<true\>\</true\>; echo \<key\>Debug\</key\>; echo \<true\>\</true\>; echo \</dict\>; echo \</plist\>; } > ~/out_file;cat ~/out_file | sudo tee /$file;rm ~/out_file;sudo chown -R root:wheel $file;sudo launchctl load -w -F $file;sudo launchctl list | grep .bootrepairmount
Best Answer
To mount a hard drive on a custom mountpoint:
From my answer to How to mount disk by UUID or LABEL in OS X El Capitan.
I've tested this by "mounting" a USB stick on an external hard drive. As an example,
As you can see,
/dev/disk2s1
, orUSB-32
is the name of the USB stick I am going to mount on the hard drive,/dev/disk1s2
, orHDD-1T
. To do so, make use of the-mountPoint
option. But first, make sure there is a landing directory to mount onto:And now, to mount:
The mounted disk will appear in the following locations:
/Volumes
, and/Volumes/HDD-1T/mountpoint
asUSB-32
andmountpoint
respectively.Be sure to unmount (
diskutil umount
) yourUSB-32
equivalent before remounting on yourHDD-1T
equivalent.