Apple nfsd wrapper

From Stack Overflow
Jump to: navigation, search
#!/bin/sh
# Brian Enigma <brian@netninja.com>
# 7/7/2006
# Start/stop NFS file sharing on a normal (non-server) OS X macine.
# It probably only works in OS X 10.4 and later because earlier
# versions started/stopped the portmap service in a different way.

# These are the shares you want to export
SHARES="/private/var/htdocs/pxccc \
        /private/var/htdocs/perplexcitycardcatalog"

# This is the IP address to allow, leave empty to let everyone in
#CLIENTS="10.1.2.56"
CLIENTS=""

# NFS options.  In this case, all accesses look like they come from
# my account because we're not using NIS authentication and user IDs
OPTS="mapall=brian"

case "$1" in
"stop" )
    # Must be run as root
    if [ $EUID -ne 0 ]; then
        echo "Relaunching under sudo"
        sudo $0 $*
        exit $?
    fi
    echo "Stopping service, removing shares"
    # remove export
    nicl . -delete /exports
    killall -USR1 automount
    killall -TERM automount
    killall -TRAP nfsd-server nfsd-master mountd rpc.lockd rpc.statd nfsiod
    rm -f /var/run/NFS.StartupItem
	launchctl stop com.apple.portmap
    ;;
"start" )
    # Must be run as root
    if [ $EUID -ne 0 ]; then
        echo "Relaunching under sudo"
        sudo $0 $*
        exit $?
    fi
    nicl . -create /exports
    for SHARE in $SHARES ; do
        ESCAPED_SHARE=`echo $SHARE | sed 's|/|\\\\/|g'`
        echo "Adding share $SHARE (/exports/${ESCAPED_SHARE})"
        nicl . -create /exports/${ESCAPED_SHARE}
        if [ -z "$CLIENTS" ]; then
            nicl . -append /exports/${ESCAPED_SHARE} clients ""
        else
            nicl . -append /exports/${ESCAPED_SHARE} clients $CLIENTS
        fi
        if [ ! -z "$OPTS" ]; then
            nicl . -append /exports/${ESCAPED_SHARE} opts $OPTS
        fi
    done
    echo "Starting service"
    cd /
    launchctl start com.apple.portmap
    /System/Library/StartupItems/NFS/NFS start
    ;;
* )
    echo "Invalid operation: $0 {start | stop}"
    exit 1
    ;;
esac