Skip to content

Instantly share code, notes, and snippets.

@jonathan-beard
Last active August 19, 2022 16:55
Show Gist options
  • Save jonathan-beard/e7f46ad3e34746bf4d7b3b15dcb5588d to your computer and use it in GitHub Desktop.
Save jonathan-beard/e7f46ad3e34746bf4d7b3b15dcb5588d to your computer and use it in GitHub Desktop.
os_x_nfs

First, you'll need to decide where to setup the mount point. The most convenient place is within /System/Volumes/Data/Volumes/ given everything will just work if the directory is there. You don't need to create this directory yet since we'll make a script that'll automatically create it on boot. This path, let's say /System/Volumes/Data/Volumes/OurNFS is the mount point we use for the local ned of our NFS, we'll call this [path to local mountpoint] within the rest of the text.

Another thing you'll need is the mount point on the NFS server you're planning to mount locally. This can be obtained using the showmount -e <server> command from the OS X terminal. The path given by showmount for our target server will be referred to as [nfs share path] within the text below.

You'll need this for the next step as well. The output of this command, the You'll need to make a file in /etc/ named auto_nfs as root. Your auto_nfs should look something like this:

[path to local mountpoint] -fstype=nfs,nolockd,noresvport,hard,bg,intr,rw,tcp nfs://<servername>:/[nfs share path]

Add this to auto_master to have it include our new auto_nfs file:

/-          auto_nfs    -nobrowse,nosuid

In /Library/LaunchDaemons make a file called com.automt.startup.plist, Technically you can call it whatever you want, just make sure it's in this directory, It's where OS X's launch controller picks up and manages daemons from. When you make the file, root should be owner, you do not need to make it executable, it's really just a launch manifest for the run script within it.

Here's the contents of that file (com.automt.startup.plist).

<?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>com.automt.startup.plist</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/usr/local/bin/run-nfs.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Because OS X will delete your mount point periodically, we have to have this launch daemon check on boot to ensure the it's still there and has the correct permissions.

Here's the contents of the run-nfs.sh script that is located in my /usr/local/bin directory.

#!/usr/bin/env bash

SHAREDIR=[path to local mountpoint]


if [[ -e $SHAREDIR ]]
then
    echo "Exists, likely mounted"
    exit
else
    mkdir $SHAREDIR
    chmod 775 $SHAREDIR
    automount -cv
    echo "Done"
    exit
fi

Root should own this one, and should be executable by root. Once all this is done, reboot and you should have your new NFS drive up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment