|
Mac GeekeryGet your geek on. |
Mounting NFS volumes via StartupItemsHere's a nifty hack to automatically mount NFS filesystems at startup via Startup Items. (Yes, I know you can do automounts in NetInfo, but this way you can more easily recover the system if your NFS server or network goes south for the winter.) First, we create our configuration file. In my case, I am mounting NFS volumes from my Linux server (nigiri.unxgeek.us), and I have 5 NFS exports that I and/or my wife (yes, contrary to popular geek myth, these do exist) use on a regular basis: Anime, Music, Storage, Backups, and Home. Create a configuration file in /etc named nfsvolumes (/etc/nfsvolumes). Mine looks like this: NFSVOLUMES="nigiri.unxgeek.us:/export/storage \ nigiri.unxgeek.us:/export/music \ nigiri.unxgeek.us:/export/home \ nigiri.unxgeek.us:/export/anime \ nigiri.unxgeek.us:/export/backups" NFSMOUNT=-YES- We set two variables, NFSVOLUMES and NFSMOUNT in /etc/nfsvolumes.
Next, we need to create a directory to hold the NFSvolumes script and its StartupParameters.plist. In an attempt to keep /System as clean as possible, I place mine in /Library/StartupItems. [Ed. Note: And also because /System/ is for the core system only. Anything a user creates should go into the Library in their home or the one at the root of the drive.] # mkdir -p /Library/StartupItems/NFSvolumes And in /Library/StartupItems/NFSvolumes/StartupParameters.plist, you need to have the following: {
Description = "Remote NFS Volumes";
Provides = ("NFSvolumes");
Requires = ("DirectoryServices","NFS");
Uses = ("Disks", "Network Time");
OrderPreference = "Last";
}
Since the StartupParameters.plist is described in full on macdevcenter.com, I suggest you read their article than me clutter this one up. Last, but not least, we need the script for NFSvolumes. It should be named the same as its parent directory. #!/bin/sh
# NFSvolumes
#
# start up script for Mac OS X
#
# SystemStartup calls this script with "start" as root.
#
# source /etc/rc.common
. /etc/rc.common
# if /etc/nfsvolumes exists, source it. Otherwise,
# disable ourselves this run.
if [ -e /etc/nfsvolumes ]; then
. /etc/nfsvolumes
else
# our configuration is missing
# make sure we don't run
NFSMOUNT=-NO-
fi
# we specify the PATH, but still use absolute paths
# to invoke binaries.
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/libexec
# function to start the "service"
StartService ()
{
if [ "${NFSMOUNT:=-NO-}" = "-YES-" ]; then
# display console message during startup sequence
ConsoleMessage "Mounting Remote NFS Volumes"
# this script expects the entries in NFSVOLUMES to be in
# a particular format, if they aren't in the format this
# script needs, BAD THINGS CAN/MAY/WILL HAPPEN (tm).
# expected format:
# hostname.domain.tld:/full/path/to/exported/filesystem
for rvol in $NFSVOLUMES
do
# get the local directory name for the mount point
ldir=$(/bin/echo $rvol|/usr/bin/sed 's/://')
# if the local directory name is not a directory, or
# if it does not exist, try to forcibly remove it and
# then create it.
if [ ! -d /NFS/$ldir ]; then
/bin/rm -f /NFS/$ldir
/bin/mkdir -p /NFS/$ldir
fi
# mount the nfs volumes
/sbin/mount_nfs $rvol /NFS/$ldir
done
fi
}
# function to stop the "service"
StopService ()
{
ConsoleMessage "Unmounting Remote NFS Volumes"
for rvol in $NFSVOLUMES
do
ldir=$(/bin/echo $rvol|/usr/bin/sed 's/://')
/sbin/umount /NFS/$ldir
done
}
# unmount and remount
RestartService ()
{
if [ "${NFSMOUNT:=-NO-}" = "-YES-" ]; then
ConsoleMessage "Remounting Remote NFS Volumes"
for rvol in $NFSVOLUMES
do
ldir=$(/bin/echo $rvol|/usr/bin/sed 's/://')
/sbin/umount /NFS/$ldir
/sbin/mount_nfs $rvol /NFS/$ldir
done
fi
}
RunService "$1"
Don't forget to make NFSvolumes executable. # chmod 755 /Library/StartupItems/NFSvolumes/NFSvolumes Now, to test run the script you can invoke it in Terminal (as root): # /Library/StartupItems/NFSvolumes/NFSvolumes start Optionally, to manually stop the "service" you can use the following: # /Library/StartupItems/NFSvolumes/NFSvolumes stop You can also use the command the system uses rather than typing in the whole path: # SystemStarter start NFSvolumes Now, after restarting, you should automatically have the NFS volumes mounted with icons on the desktop for quick reference. If you manually mount or unmount the NFS exports by script, you may want to relaunch the Finder to refresh the icons on your desktop. As discussed in the CLI Disk Management article, you can execute disktool -r to refresh the disks in the Finder Oh, one final word: if you're like me and using a non-Mac OS X based operating system for your NFS server, you'll want to enable asynchronous writes in the server's NFS configuration file (normally, /etc/exports on other UNIX boxes). About unixgeek |
|
||||
We use another way, which, while different, works very well for us in my lab, and is much simpler:
1. Create a mount map file in /etc (I call mine “mymounts”)
2. In /etc/mymounts enter the mount properties:
(name opts host:/export_path) for example:
music rw,net,tcp nigiri.unxgeek.us:/export/music
3. Modify the NFS startup item (/System/Library/StartupItems/NFS/NFS) to consult /etc/mymounts (you must sudo or be root, and be sure to make a backup first, just in case):
after this line: ln -s /automount/Servers /Network/Servers
enter: automount -m /nfs_mounts /etc/mymounts
4.Reboot, or, to test, run the automount command that you just entered in the NFS Startup Item.
This method will mount any export listed in /etc/mymounts with the name specified in the first field of that file in a directory specified by the automount command (in this case /nfs_mounts). It will not place the mount on the desktop. But it is a quick and very efficient way to dynamically mount nfs servers. And if the server goes down or is unavailable at boot time, the client won’t miss a beat (unless, of course, there is a file in use on the server at the time, in which case you’ll get a pretty descriptive and useful error message in the Finder asking you what you want to do about the disconnected server.)
Anyway, I thought that seeing this article so soon after dealing with nfs mount issues in my lab, and only on my second visit to macgeekery, was kismet, so I thought I should post something. Hopefully someone finds it useful or enlightening or something.
BTW, great site!
Thanks a lot for posting this hack. It worked great for me, but only after I modified the NFSvolumes script very slightly. I was getting “Operation not permitted”. So I googled some and found that because most Linux systems require the use of a reserved port for NFS mounting, I had to change this line:
/sbin/mount_nfs $rvol /NFS/$ldir
to this:
/sbin/mount_nfs -P $rvol /NFS/$ldir
Thanks again
Thanks lamb. you can also directly call mount_nfs and mount the nfs share by executing:
sudo /sbin/mount_nfs -P nfsserver:/shared/ /Users/username/mnt
Is there any way to mount this as a regular user?
I don’t want to use “sudo” because is asking for a password, and this is a step I want to avoid.
Any ideas?