Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: HOWTO: Automatically mount NFS shares without autofs

  1. #11
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: HOWTO: Automatically mount NFS shares without autofs

    Quote Originally Posted by MountainX View Post
    Could this script be a solution for me?
    ...
    Anyone want to help me with this?
    It might it might not be, but i have been having the same problem as you since moving to maverick from lucid.

    I just tried commenting out the nfs mount from /etc/mtab , then issued a forced unmount :

    Code:
    #!/bin/sh
    SERVER_NAME=edge.home
    SHARE_NAME=/share
    sed s/"$SERVER_NAME:"/"#$SERVER_NAME:"/g -i /etc/mtab
    umount $SHARE_NAME -f
    source : http://wikiri.upc.es/index.php/NFS_force_umount

    After running code to that effect, i was able to click suspend from the gnome-panel session applet and actually have the computer suspend.

    I suspect that according to the NFS docs we might need to have "hard,intr" in the options of the fstab.

    hard
    meaning the client will continue to attempt access to the server if the server is not there. and will refuse all requests to die.

    intr
    meaning that it will still attempt the behavior of hard, bu will instead honor any requests to die.


    it's late and i'm about to fall over asleep, but i thought i'd document this here for you to find. maybe it's useful.
    Fear is the mindkiller....
    The little death that obliterates...

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

    Re: HOWTO: Automatically mount NFS shares without autofs

    thanks airtonix. That's interesting. I have been using hard,intr and I played with soft,intr and every option I could think of.

    But I do like the idea of using a script to work around this problem. Here are some initial thoughts about it:

    Code:
    #!/bin/bash
    #
    # gnome-power-control
    # Author: AgenT
    # see http://ubuntuforums.org/showthread.php?p=8947629
    
    #SETUP
    #1) Save the above as: gnome-power-control
    #2) Change file permissions to execute (right click on file -> properties -> permissions)
    # or sudo chmod a+x /usr/bin/gnome-power-control
    #3) optional: move this script into your ~/bin/ folder for easy access!
    # or place it anywhere on the path. Check with $ echo $PATH
    #USAGE
    #To suspend: gnome-power-control suspend
    #To hibernate: gnome-power-control hibernate
    
    SERVER_NAME=edge.home
    
    # write all unwritten data (just in case)
    sync
    
    # comment out NFS mounts (mine are all on one server, fortunately)
    sed s/"$SERVER_NAME:"/"#$SERVER_NAME:"/g -i /etc/mtab
    
    #need to do this for all NFS mounts! How?
    umount -a -t NFS -f
    
    case $1 in
    suspend)
    echo Suspending
        dbus-send --print-reply \
            --system \
            --dest=org.freedesktop.DeviceKit.Power \
            /org/freedesktop/DeviceKit/Power \
            org.freedesktop.DeviceKit.Power.Suspend
    ;;
    hibernate)
    echo Hibernating
        dbus-send --print-reply \
            --system \
            --dest=org.freedesktop.DeviceKit.Power \
            /org/freedesktop/DeviceKit/Power \
            org.freedesktop.DeviceKit.Power.Hibernate
    ;;
    *)
    echo Not supported command: '"'$1'"'
    echo Usage: $0 '<suspend|hibernate>'
    exit 1
    ;;
    esac
    
    # restore mount descriptors
    sed s/"#$SERVER_NAME:"/"$SERVER_NAME:"/g -i /etc/mtab
    
    # mount
    sudo mount -a
    sources:
    http://www.linux.com/archive/feature/114220
    http://ubuntuforums.org/showthread.php?p=8947629
    Last edited by MountainX; March 26th, 2011 at 05:58 AM.
    Desktop: KX Studio (Kubuntu 12.04)
    Laptop & Netbook: Kubuntu 12.04
    Tablet: Samsung Galaxy Tab 10.1
    Phone: Nexus 4 Cyanogenmod

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

    Re: HOWTO: Automatically mount NFS shares without autofs

    There are a bunch of good suggestions here:
    http://ubuntuforums.org/showthread.php?p=8947629

    Combining those suspend scripts with airtonix's suggestions gave me a solution that appears to work. (I have only tested it once so far.)

    Code:
    #!/bin/bash
    #
    # gnome-power-control
    # Original Author: AgenT
    # see http://ubuntuforums.org/showthread.php?p=8947629
    
    #SETUP
    #1) Save the above as: suspend-unmount.sh
    #2) Change file permissions to execute (right click on file -> properties -> permissions)
    # or sudo chmod a+x /usr/bin/suspend-unmount.sh
    #3) optional: move this script into your ~/bin/ folder for easy access!
    # or place it anywhere on the path. Check with $ echo $PATH
    #USAGE
    #To suspend: suspend-unmount.sh
    
    SERVER_NAME=192.168
    
    # write all unwritten data (just in case)
    sync
    
    # comment out NFS mounts (mine are all on one server, fortunately)
    sed s/"$SERVER_NAME"/"#$SERVER_NAME"/g -i /etc/mtab
    
    #need to do this for all NFS mounts! This is experimental:
    umount -a -t NFS -f
    
    sudo pm-suspend
    
    # restore mount descriptors
    sed s/"#$SERVER_NAME"/"$SERVER_NAME"/g -i /etc/mtab
    
    # mount
    sudo mount -a
    When I ran it, it suspended beautifully and really quickly. Waking up was trouble-free too. But upon waking up, I saw this output:

    Code:
    ~/bin$ sudo ./suspend-unmount.sh 
    mount.nfs: /volume/one is busy or already mounted
    mount.nfs: /volume/two is busy or already mounted
    mount.nfs: /volume/three is busy or already mounted
    mount.nfs: /volume/four is busy or already mounted
    mount.nfs: /volume/five is busy or already mounted
    Not a big deal for me. Those are just messages, not errors. But I might experiment with variations on the mounting.

    EDIT: even though everything is working, the NSF mounts on SERVER_NAME are missing from /etc/mtab after resuming. But I can access the NFS shares just fine. Not exactly sure what's going on...
    Last edited by MountainX; March 26th, 2011 at 06:28 AM.
    Desktop: KX Studio (Kubuntu 12.04)
    Laptop & Netbook: Kubuntu 12.04
    Tablet: Samsung Galaxy Tab 10.1
    Phone: Nexus 4 Cyanogenmod

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

    Re: HOWTO: Automatically mount NFS shares without autofs

    I tested a few more times with some modifications. This code works for me:

    Code:
    #!/bin/bash
    sync
    umount -a -t NFS -f
    sudo pm-suspend
    sudo mount -a -t NFS
    Changing the umount command to this:
    Code:
    umount -a -t NFS -l
    causes suspending to fail. So the force unmount is required in my case. I wonder if it is really safe, even with the sync? The man page says:

    Force unmount even if busy. This can cause data loss.
    Last edited by MountainX; March 26th, 2011 at 06:42 AM.
    Desktop: KX Studio (Kubuntu 12.04)
    Laptop & Netbook: Kubuntu 12.04
    Tablet: Samsung Galaxy Tab 10.1
    Phone: Nexus 4 Cyanogenmod

  5. #15
    Join Date
    Nov 2011
    Beans
    3

    Re: HOWTO: Automatically mount NFS shares without autofs

    A truly excellent script! I use it for all my Linux clients at home that need to use the NFS shares on my NETGEAR ReadyNAS. Since the ReadyNAS uses its volume in the mount point (like /c/music) I decided to modify the script so its able to use separate mount points for server/client. Also, on my 11.10 it didnt seem to correctly detect if the mount was already up, so I used proc/mounts instead. Maybe my changes can help someone (as the original script helped me)

    Code:
    #!/bin/bash
    # The hostname or IP-address of the fileserver:
    FILESERVER="192.168.0.1"
    # Check every X seconds (60 is a good default):
    INTERVAL=60
    # Delimeter used for separating fileserver/client shares below:
    DELIMETER="|"
    # The shares that need to be mounted:
    MOUNTS=( "/c/music|/home/xxx/Homenet/Music" "/c/pictures|/home/xxx/Homenet/Pictures" "/c/homevideo|/home/xxx/Homenet/Homevideo" "/c/video|/home/xxx/Homenet/Video" )
    
    declare -a MOUNTP
    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
    			MOUNTP=(`echo ${MOUNT//$DELIMETER/ }`) # do the split
    			if grep -qs ${MOUNTP[1]} /proc/mounts; then
    				logger "${MOUNTP[1]} is mounted"
    			else
    				# NFS mount not mounted, attempt mount
    				logger "NFS shares not mounted; attempting to mount ${FILESERVER}:${MOUNTP[0]} to ${MOUNTP[1]}:"
    				mount -t nfs ${FILESERVER}:${MOUNTP[0]} ${MOUNTP[1]}
    			fi
    		done
    	else
    		# Fileserver is down.
    		logger "Fileserver is down."
    		for MOUNT in ${MOUNTS[@]}; do
    			MOUNTP=(`echo ${MOUNT//$DELIMETER/ }`) # do the split
    			if grep -qs ${MOUNTP[1]} /proc/mounts; then
    				# NFS mount is still mounted; attempt umount
    				logger "Cannot reach ${FILESERVER}, unmounting NFS share ${MOUNTP[1]}."
    				umount -f ${MOUNTP[1]}
    			fi
    		done
    	fi
    	sleep $INTERVAL
    done

  6. #16
    Join Date
    Apr 2008
    Beans
    2

    Re: HOWTO: Automatically mount NFS shares without autofs

    Hi,

    i just found this script which is pretty much useful in our data center environment. i tuned it to use "rpcinfo" instead of "ping". ping only guarantees that the server runs. rpcinfo guarantees that actually the nfs server daemon is listening for connections - which is pretty much more useful. furthermore i added some useful mountoptions and added it as a config option. i upped that script to my github account, so its freely useable by everyone. i hope this is okay for you? You`re mentioned everywhere as the original author of this cool script!

    Github: https://github.com/martinseener/yard...pts/autonfs.sh

    Thank you...
    Last edited by xlslx; July 23rd, 2012 at 05:04 PM. Reason: typos

  7. #17
    Join Date
    Jul 2007
    Beans
    37

    Re: HOWTO: Automatically mount NFS shares without autofs

    just wanted to mention another script which is able
    to process fstab-like files and automatically mount/umount
    if the corresponding host is up/down

    https://github.com/prometheus0/scrip...utomounterd.sh

    hth
    Last edited by thinking; September 2nd, 2012 at 03:20 PM.

  8. #18
    Join Date
    Jan 2008
    Beans
    15

    Re: HOWTO: Automatically mount NFS shares without autofs

    Long overdue update: I have merged the excellent modifications made by Tobcro and xlslx, and made some minor modifications to the main script.

    I've put it up on github:
    https://github.com/jdhoek/util/blob/master/autonfs.sh

    Too bad I can't edit the first post.

  9. #19
    Join Date
    Jan 2008
    Beans
    15

    Re: HOWTO: Automatically mount NFS shares without autofs

    Because older posts can no longer be edited, I have moved this howto to the Wiki: https://help.ubuntu.com/community/Au...outAutofsHowto

    I think I've got it right with the recent updates to the script. Let me know if something is wrong.

Page 2 of 2 FirstFirst 12

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
  •