Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: HOWTO: Automatically mount NFS shares without autofs

  1. #1
    Join Date
    Jan 2008
    Beans
    15

    Lightbulb HOWTO: Automatically mount NFS shares without autofs

    This howto has been moved to the Wiki

    The thread is kept open in order to have a place for discussing the Wiki page.
    Last edited by mörgæs; September 22nd, 2012 at 09:33 PM. Reason: Moved to Wiki

  2. #2
    Join Date
    Nov 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Exclamation Re: HOWTO: Automatically mount NFS shares without autofs

    Hi

    I thank you for posting this, it would be helpfull if it worked for me but it doesnt, maybe you can help?

    In addition of what I post below the /media/whateverdirectory exists.

    My nfsmount.sh this exists and is executable in /usr/local/bin
    Code:
    #!/bin/bash
    
    # The hostname or IP-address of the fileserver:
    FILESERVER="192.168.1.7"
    # Check every X seconds (60 is a good default):
    INTERVAL=60
    # The shares that need to be mounted:
    MOUNTS=( "/media/TV_SERIES" "/media/MOVIES" "/media/MUSIC" "/media/SOURCES" )
    
    while true; do
        ping -c 1 "$FILESERVER" &>/dev/null
        if [ $? -eq 0 ]; then
            # Fileserver is up.
            logger "Fileserver is up."
            for MOUNT in ${MOUNTS[@]}; do
                mount | grep -E "^${FILESERVER}:${MOUNT} on .*$" &>/dev/null
                if [ $? -ne 0 ]; then
                    # NFS mount not mounted, attempt mount
                    logger "NFS shares not mounted; attempting to mount ${MOUNT}:"
                    mount -t nfs ${FILESERVER}:${MOUNT} ${MOUNT} ${MOUNT} ${MOUNT}
                fi
            done
        else
            # Fileserver is down.
            logger "Fileserver is down."
            for MOUNT in ${MOUNTS[@]}; do
                mount | grep -E "^${FILESERVER}:${MOUNT} on .*$" &>/dev/null
                if [ $? -eq 0 ]; then
                    # NFS mount is still mounted; attempt umount
                    logger "Cannot reach ${FILESERVER}, unmounting NFS share ${MOUNT}."
                    umount -f ${MOUNT}
                fi
            done
        fi
        sleep $INTERVAL
    done
    My nfsmount.conf exists and is 644 owned by root:root in /etc/init
    Code:
    # nfsmount
    # mount NFS shares on fileserver, if present
    # Created by JeroenHoek - http://bit.ly/c4Pwv6
    # Modded by X3, Team iQuik 2010
    
    description    "Mount NFS-shares"
    
    start on (filesystem)
    respawn
    
    exec /usr/local/bin/nfsmount
    My nfsmount upstart created it exists in /etc/init.d
    Code:
    #!/bin/sh -e
    ### BEGIN INIT INFO
    # Provides:          nfsmount
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start nfsmount daemon at boot time
    # Description:       Enable nfsmount service provided by daemon.
    ### END INIT INFO
    # upstart-job
    #
    # Symlink target for initscripts that have been converted to Upstart.
    
    set -e
    
    INITSCRIPT="$(nfsmount "$0")"
    JOB="${INITSCRIPT%.sh}"
    
    if [ "$JOB" = "upstart-job" ]; then
        if [ -z "$1" ]; then
            echo "Usage: upstart-job JOB COMMAND" 1>&2
        exit 1
        fi
    
        JOB="$1"
        INITSCRIPT="$1"
        shift
    else
        if [ -z "$1" ]; then
            echo "Usage: $0 COMMAND" 1>&2
        exit 1
        fi
    fi
    
    COMMAND="$1"
    shift
    
    
    if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
        ECHO=echo
    else
        ECHO=:
    fi
    
    $ECHO "Rather than invoking init scripts through /etc/init.d, use the service(8)"
    $ECHO "utility, e.g. service $INITSCRIPT $COMMAND"
    
    case $COMMAND in
    status)
        $ECHO
        $ECHO "Since the script you are attempting to invoke has been converted to an"
        $ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
        $COMMAND "$JOB"
        ;;
    start|stop|restart)
        $ECHO
        $ECHO "Since the script you are attempting to invoke has been converted to an"
        $ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
        PID=$(status "$JOB" 2>/dev/null | awk '/[0-9]$/ { print $NF }')
        if [ -z "$PID" ] && [ "$COMMAND" = "stop" ]; then
            exit 0
        elif [ -n "$PID" ] && [ "$COMMAND" = "start" ]; then
            exit 0
        elif [ -z "$PID" ] && [ "$COMMAND" = "restart" ]; then
            start "$JOB"
            exit 0
        fi
        $COMMAND "$JOB"
        ;;
    reload|force-reload)
        $ECHO
        $ECHO "Since the script you are attempting to invoke has been converted to an"
        $ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
        reload "$JOB"
        ;;
    *)
        $ECHO
        $ECHO "The script you are attempting to invoke has been converted to an Upstart" 1>&2
        $ECHO "job, but $COMMAND is not supported for Upstart jobs." 1>&2
        exit 1
    esac
    this never works tried
    Code:
    root@iMedia:/usr/local/bin# nfsmount
    No command 'nfsmount' found, did you mean:
     Command 'ntfsmount' from package 'ntfsprogs' (main)
    nfsmount: command not found
    root@iMedia:/usr/local/bin#
    Code:
    root@iMedia:/usr/local/bin# ./nfsmount.sh
    Usage: mount -V                 : print version
           mount -h                 : print this help
           mount                    : list mounted filesystems
           mount -l                 : idem, including volume labels
    So far the informational part. Next the mounting.
    The command is `mount [-t fstype] something somewhere'.
    Details found in /etc/fstab may be omitted.
           mount -a [-t|-O] ...     : mount all stuff from /etc/fstab
           mount device             : mount device at the known place
           mount directory          : mount known device here
           mount -t type dev dir    : ordinary mount command
    Note that one does not really mount a device, one mounts
    a filesystem (of the given type) found on the device.
    One can also mount an already visible directory tree elsewhere:
           mount --bind olddir newdir
    or move a subtree:
           mount --move olddir newdir
    One can change the type of mount containing the directory dir:
           mount --make-shared dir
           mount --make-slave dir
           mount --make-private dir
           mount --make-unbindable dir
    One can change the type of all the mounts in a mount subtree
    containing the directory dir:
           mount --make-rshared dir
           mount --make-rslave dir
           mount --make-rprivate dir
           mount --make-runbindable dir
    A device can be given by name, say /dev/hda1 or /dev/cdrom,
    or by label, using  -L label  or by uuid, using  -U uuid .
    Other options: [-nfFrsvw] [-o options] [-p passwdfd].
    For many more details, say  man 8 mount .
    Usage: mount -V                 : print version
           mount -h                 : print this help
           mount                    : list mounted filesystems
           mount -l                 : idem, including volume labels
    So far the informational part. Next the mounting.
    The command is `mount [-t fstype] something somewhere'.
    Details found in /etc/fstab may be omitted.
           mount -a [-t|-O] ...     : mount all stuff from /etc/fstab
           mount device             : mount device at the known place
           mount directory          : mount known device here
           mount -t type dev dir    : ordinary mount command
    Note that one does not really mount a device, one mounts
    a filesystem (of the given type) found on the device.
    One can also mount an already visible directory tree elsewhere:
           mount --bind olddir newdir
    or move a subtree:
           mount --move olddir newdir
    One can change the type of mount containing the directory dir:
           mount --make-shared dir
           mount --make-slave dir
           mount --make-private dir
           mount --make-unbindable dir
    One can change the type of all the mounts in a mount subtree
    containing the directory dir:
           mount --make-rshared dir
           mount --make-rslave dir
           mount --make-rprivate dir
           mount --make-runbindable dir
    A device can be given by name, say /dev/hda1 or /dev/cdrom,
    or by label, using  -L label  or by uuid, using  -U uuid .
    Other options: [-nfFrsvw] [-o options] [-p passwdfd].
    For many more details, say  man 8 mount .
    Usage: mount -V                 : print version
           mount -h                 : print this help
           mount                    : list mounted filesystems
           mount -l                 : idem, including volume labels
    So far the informational part. Next the mounting.
    The command is `mount [-t fstype] something somewhere'.
    Details found in /etc/fstab may be omitted.
           mount -a [-t|-O] ...     : mount all stuff from /etc/fstab
           mount device             : mount device at the known place
           mount directory          : mount known device here
           mount -t type dev dir    : ordinary mount command
    Note that one does not really mount a device, one mounts
    a filesystem (of the given type) found on the device.
    One can also mount an already visible directory tree elsewhere:
           mount --bind olddir newdir
    or move a subtree:
           mount --move olddir newdir
    One can change the type of mount containing the directory dir:
           mount --make-shared dir
           mount --make-slave dir
           mount --make-private dir
           mount --make-unbindable dir
    One can change the type of all the mounts in a mount subtree
    containing the directory dir:
           mount --make-rshared dir
           mount --make-rslave dir
           mount --make-rprivate dir
           mount --make-runbindable dir
    A device can be given by name, say /dev/hda1 or /dev/cdrom,
    or by label, using  -L label  or by uuid, using  -U uuid .
    Other options: [-nfFrsvw] [-o options] [-p passwdfd].
    For many more details, say  man 8 mount .
    Usage: mount -V                 : print version
           mount -h                 : print this help
           mount                    : list mounted filesystems
           mount -l                 : idem, including volume labels
    So far the informational part. Next the mounting.
    The command is `mount [-t fstype] something somewhere'.
    Details found in /etc/fstab may be omitted.
           mount -a [-t|-O] ...     : mount all stuff from /etc/fstab
           mount device             : mount device at the known place
           mount directory          : mount known device here
           mount -t type dev dir    : ordinary mount command
    Note that one does not really mount a device, one mounts
    a filesystem (of the given type) found on the device.
    One can also mount an already visible directory tree elsewhere:
           mount --bind olddir newdir
    or move a subtree:
           mount --move olddir newdir
    One can change the type of mount containing the directory dir:
           mount --make-shared dir
           mount --make-slave dir
           mount --make-private dir
           mount --make-unbindable dir
    One can change the type of all the mounts in a mount subtree
    containing the directory dir:
           mount --make-rshared dir
           mount --make-rslave dir
           mount --make-rprivate dir
           mount --make-runbindable dir
    A device can be given by name, say /dev/hda1 or /dev/cdrom,
    or by label, using  -L label  or by uuid, using  -U uuid .
    Other options: [-nfFrsvw] [-o options] [-p passwdfd].
    For many more details, say  man 8 mount .
    Code:
    root@iMedia:/usr/local/bin# nfsmount start
    No command 'nfsmount' found, did you mean:
     Command 'ntfsmount' from package 'ntfsprogs' (main)
    nfsmount: command not found
    Code:
    root@iMedia:/usr/local/bin# /etc/init.d/nfsmount start
    /etc/init.d/nfsmount: 17: nfsmount: not found
    I've ran out of ideas, what have I done wrong?
    I cant see the mistake or error.
    Last edited by X3lectric; July 28th, 2010 at 03:40 PM. Reason: adding extra info, if anyone cares.

  3. #3
    Join Date
    Jan 2008
    Beans
    15

    Re: HOWTO: Automatically mount NFS shares without autofs

    X3lectric:
    Can you mount your NFS shares from the command-line? This should be explained in the SettingUpNFSHowTo.

    Also, you call the script as nfsmount, but the actual script appears to be named nfsmount.sh. Is the name correct? You should see the name of your script appear in the console if you type nfs and hit tab twice.

  4. #4
    Join Date
    May 2007
    Beans
    3

    Re: HOWTO: Automatically mount NFS shares without autofs

    Thanks!!!! It's working. Thanks!!! May I translate this how-to in polish with your credits of course?

  5. #5
    Join Date
    Jan 2008
    Beans
    15

    Re: HOWTO: Automatically mount NFS shares without autofs

    yp2:
    By all means. Just linking to this thread is more than sufficient really. As far as I am concerned, consider this mini-HOWTO in the public domain. I'm glad it can be of use to others.

    I'm still using it for my home network; it works quite well.

  6. #6
    Join Date
    May 2007
    Beans
    3

    Re: HOWTO: Automatically mount NFS shares without autofs

    Thaks.

  7. #7
    Join Date
    Dec 2006
    Beans
    11

    Re: HOWTO: Automatically mount NFS shares without autofs

    This is a great little script. Just needed a bit of tweaking to make it work with my system/setup. ${Fileserver}:${Mount} language only works if the shares are located in the same place on both systems,I found it easy to modify the .sh file as follows to suit my need.

    #!/bin/bash

    # The hostname or IP-address of the fileserver:
    FILESERVER="192.168.0.10"
    # Check every X seconds (60 is a good default):
    INTERVAL=360
    # The shares that need to be mounted (server side)
    SHAREA="/home/jatatar/storage"
    SHAREB="/home/jatatar/Downloads"
    SHAREC="/home/jatatar/Documents"
    # Place where shares will be on local system (if different from server location)
    MOUNTA=${SHAREA}
    MOUNTB="/media/maverick-documents"
    MOUNTC="/media/maverick-downloads"

    while true; do
    ping -c 1 "$FILESERVER" &>/dev/null
    if [ $? -eq 0 ]; then
    # Fileserver is up.
    mount -t nfs ${FILESERVER}:${SHAREA} ${MOUNTA}
    mount -t nfs ${FILESERVER}:${SHAREB} ${MOUNTB}
    mount -t nfs ${FILESERVER}:${SHAREC} ${MOUNTC}
    else
    # Fileserver is down.
    logger "Fileserver is down."
    umount ${MOUNTA}
    umount ${MOUNTB}
    umount ${MOUNTC}
    fi
    sleep $INTERVAL
    done


    ###
    I also eliminated the extra checking to see if the folders were already mounted...due to the interval re-running the script, if the server was unavailable, folders are unmounted. Seemed logically simpler this way.

    Also could add more fileservers in a second do loop to expand, could check mulitple folders for you. This is a good alternative to sudo mount -a as you can already see either in Places, or desktop if your server is available.


    Only question is how much resource does this script use?

  8. #8
    Join Date
    Dec 2006
    Beans
    11

    Re: HOWTO: Automatically mount NFS shares without autofs

    Been experimenting with this script because I wanted to accomplish two things...
    1) Automount another share located on a seperate fileserver using the same script.

    2) Only attempt mounts when internet connection is available

    For #1, I changed my nfsmounts file as follows:
    #!/bin/bash

    # The hostname or IP-address of the fileserver:
    FILESERVERA="192.168.0.12"
    FILESERVERB="192.168.2.4"

    # Check every X seconds (60 is a good default):
    INTERVAL=60

    # FILESERVERA -- The shares that need to be mounted (server side)
    SHAREA="/home/jatatar/Music"
    SHAREB="/home/jatatar/Downloads"
    SHAREC="/home/jatatar/Documents"
    SHARED="/home/jatatar/Videos"

    # FILESERVERB -- The shares that need to be mounted (server side)
    SHAREE="/mnt/HD_A2"

    # - Place where shares will be on local system (if different from server location)
    MOUNTA="/media/mvaerick-music"
    MOUNTB="/media/maverick-documents"
    MOUNTC="/media/maverick-downloads"
    MOUNTD="/media/maverick-video"
    MOUNTE="/home/jatatar/dlink"

    while true; do
    ping -c 1 "$FILESERVERA" &>/dev/null
    if [ $? -eq 0 ]; then
    # FileserverA is up.
    mount -t nfs ${FILESERVERA}:${SHAREA} ${MOUNTA}
    mount -t nfs ${FILESERVERA}:${SHAREB} ${MOUNTB}
    mount -t nfs ${FILESERVERA}:${SHAREC} ${MOUNTC}
    mount -t nfs ${FILESERVERA}:${SHARED} ${MOUNTD}
    else
    # FileserverA is down.
    logger "FileserverA is down."
    umount ${MOUNTA}
    umount ${MOUNTB}
    umount ${MOUNTC}
    umount ${MOUNTD}
    fi

    ping -c 1 "$FILESERVERB" &>/dev/null
    if [ $? -eq 0 ]; then
    # FileserverB is up.
    mount -t nfs ${FILESERVERB}:${SHAREE} ${MOUNTE}
    else
    # FileserverB is down.
    logger "FileserverB is down."
    umount ${MOUNTE}
    fi

    sleep $INTERVAL
    done

    Changed verbage to add 2 fileservers and then added ping check and mounting of shares in a second if else statement. This could be expanded infinitely as needed. My server has two separate NICS and thus has two separate ips.

    For #2, linked nfsmounts-serv to the network/if-up.d directory

    using command:

    sudo ln -s /usr/local/bin/nfsmounts-serv /etc/network/if-up.d

    files in /etc/network/if-up.d directory are executed when nm-applet has a valid network connection this way on bootup program starts on network connection and loops every 60 seconds.

    You could easily save or move your script to that directory, but I prefer the symlink.

  9. #9
    Join Date
    Jun 2007
    Location
    USA
    Beans
    524
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: HOWTO: Automatically mount NFS shares without autofs

    I noticed some autofs issues when I did that 200 line kernel hack a while back to optimize my Lucid desktop. This script came to the rescue, but it will not work with a typical nfsv4 setup. Followon posters pointed me in the right direction -

    Typicsl /etc/fstab =


    Code:
    #nfs drive
    192.168.11.5:/export/ /media/exportA  nfs4  proto=tcp,port=2049,rsize=8192,wsize=8192,timeo=60,retrans=6,rw,noatime,intr  0  0
    This sets up a typical nfs setup with the server export(s). Because of this, the script would not work as written and required heavy modification. Here is the correct way -

    Code:
    #!/bin/bash
    
    # The hostname or IP-address of the fileserver:
    FILESERVER="192.168.11.5"
    # Check every X seconds (60 is a good default):
    INTERVAL=360
    # The shares that need to be mounted:
    SHAREA="/exportA/"
    SHAREB="/exportB/"
    SHAREC="/exportC/"
    MOUNTA="/media/exportA"
    MOUNTB="/media/exportB"
    MOUNTC="/media/exportC"
    
    while true; do
    	ping -c 1 "$FILESERVER" &>/dev/null
    	if [ $? -eq 0 ]; then
    		# Fileserver is up.
    		logger "Fileserver is up."
    		mount ${FILESERVER}:${SHAREA} ${MOUNTA}
    		mount ${FILESERVER}:${SHAREB} ${MOUNTB}
    		mount ${FILESERVER}:${SHAREC} ${MOUNTC}
    		else
    		# Fileserver is down.
    		logger "Fileserver is down."
    		umount ${MOUNTA}
    		umount ${MOUNTB}
    		umount ${MOUNTC}
    	fi
    	sleep $INTERVAL
    done

    Thats an example of 3 shares mounted via /etc/fstab entries. You still have to setup your NFS shares the right way, autofs or no.

  10. #10
    MountainX's Avatar
    MountainX is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Jan 2008
    Location
    A place with no mountains
    Beans
    1,609
    Distro
    Kubuntu

    Re: HOWTO: Automatically mount NFS shares without autofs

    I'm looking for a solution that will allow me to suspend to RAM. I have all my data on a mounted NFS share. This prevents my computer from being able to suspend.

    Could this script be a solution for me?

    I suppose another script to auto-unmount upon suspending will be needed too. Maybe something like the script here would be a starting point.

    Anyone want to help me with this?
    Desktop: KX Studio (Kubuntu 12.04)
    Laptop & Netbook: Kubuntu 12.04
    Tablet: Samsung Galaxy Tab 10.1
    Phone: Nexus 4 Cyanogenmod

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •