Programmatically creating mount points in macOS 10.12

Apple have confirmed to me (https://openradar.appspot.com/radar?id=4948585099558912) that with macOS 10.12, permissions to create a folder inside /Volumes require root access.

Using the mount command requires that the mount point exists first, so you're going to have to sudo to create that folder for the mount point.

I develop an app which presents a list of network shares to a the user, and allows them to selectively mount them. I handle this by firing off a command process in the background which uses mkdir and mount to mount the network drive. Of course this now fails, as it doesn't have permission to create the mount point.

One option I am looking into is modifying sudoers to give all users permission to use mkdir.

Can anyone else think of a way to programmatically mount a network drive via Terminal without password prompts, or messing with sudo?


Solution 1:

As I answered at macOS Sierra: AppleScript mount volume keeps asking for login, I think the way to do it now is to create the mount point somewhere the user does have write access to, such as the user's home directory:

mkdir -p ~/mnt
mount_smbfs "//my_username:my_password@my_hostname/share" ~/mnt

Solution 2:

Here's good news around Sierra the mounting challenges: The original Unix automountd works again as it is supposed to work.

Do as root this: Add into your /etc/auto_master for example this line to a static mount map. I called it "Media", but you can do anything you like.

/etc/auto_master:
/Volumes/Media   auto_media

then make a file /etc/auto_media with the entries of your mount-points, these are two in example for picture and music. My Synology NAS fully supports apple files system, but you can do this on any file server and pick other file system. See man auto_master.

/etc/auto_master:
Bilder  afp://yournasuser:naspw@Media/Bilder
Musik   afp://yournasuser:naspw@Media/Musik

Also make the mountpoint folder manually mkdir /Volume/Media and reload all with automount -vc If you now click as a USER in Finder to the /Volumes/Media folder, Finder will automatically show the subfolders Bilder and Musik. If you click into one of those, automountd will automatically mount the volumes AS the USER that was requesting it and NOT as root. This is the key to the solution!!! Check out the mount status, it will show the following:

Type mount
...
map auto_media on /Volumes/Media (autofs, automounted, nobrowse)
//yournasuser@Media/Bilder on /Volumes/Media/Bilder (..automounted,.. mounted by yourMacUser)

Please note the "mounted by Username" at the end of the last line above.

I realized that the mkdir /Volumes/Media is persistent on one Mac, but all folders in /Volumes are wiped out on another Mac. In this case you have to execute after a the reboot a script that creates the folder and reloads the automounter. Something like this:

#!/bin/sh
mkdir /Volumes/Media
automount -vc

I hope this works for everybody.