Mount volume over a not-previously-existing folder
Solution 1:
Technically you can only mount a volume on an existing folder. The trick that nevertheless makes this possible for macOS in general is that a program makes sure to create the folder before performing the mount.
This kind of functionality is generally known (across operating systems) as an "auto mounter". And indeed macOS contains a program named automount
that automatically creates folders and mounts according to the map setup in /etc/auto_master. This is mainly used with certain types of network shares.
The main automounter that most users will interact with is named diskarbitrationd
. This program listens for events such as USB drives being connected, DVDs inserted, etc. and mounts any mountable file system on them.
diskarbitrationd
uses information in /etc/fstab
to determine any special options or mount point configuration that the user has set for a specific drive. Those mount points needs to exist in advance however, and they won't automatically be deleted after the file system is unmounted.
If you just want to chose a different folder name within /Volumes
, you can simply open Disk Utility and right-click on the volume to choose "Rename". The next time you insert the drive, it will be mounted in a folder with that name.
If you want to do something completely different than the manual behavior, you could disable diskarbitrationd
entirely, and create your own program for mounting drives automaticallly. Note however that it is a lot of work for something that sounds like it could be solved in a better wa.y
Finally a work around could be that you create a program (i.e. a small script) that is run whenever something is mounted. When your special drive is mounted, it could automatically unmount it, create the custom mount point and mount it there. You would need special handling to also delete the folder when the disk is unmounted again. If you can create small scripts, the trick to getting this working is to create a launchd daemon definition file for the script, and use the StartOnMount
key. This makes launchd run your script every time something is mounted.