LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-28-2013, 01:28 AM   #1
harshaabba
Member
 
Registered: Aug 2009
Posts: 73

Rep: Reputation: -14
Script to mount NFS Share


hi all,

I need to mount following share to the mentioned following mount point.I can do this using fstab using the following statement.

192.168.1.1:/NFS1 /mnt/NFS1 nfs defaults 0 0

But during the server boot if remote nfs mount point does not exist, server might hang.

I want to include this to start up script. Is there any predefined script you created which I can use.

My server platform is RHEL 5.4 32 bit version.



Thank You.
 
Old 10-28-2013, 03:32 AM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by harshaabba View Post
But during the server boot if remote nfs mount point does not exist, server might hang.

I want to include this to start up script. Is there any predefined script you created which I can use.
Predefined? No. (Well, I've never seen one.) Easy to write? Yes.

I also had problems with a system (not a Linux system but the concepts are the same) hanging because we had defined the NFS mount in /etc/fstab. If the NFS server was down for maintenance we'd hang on a reboot. So I used a script to mount (and umount) NFS filesystems at work some years ago. The script was written like a typical `init' script with a case/esac structure and `start'/`stop' sections). The script pinged the system hosting the remote filesystem. If the pings succeeded, I'd then use ``showmount -e remote-host'' and grep the output to see if the NFS service on that host was actually exporting the filesystem we wanted to mount. If both of those checks were successful, then we'd mount the NFS filesystem. I seem to recall that we'd hang if we were shutting down and the NFS server was down as well. The umount would sit there until what seemed like forever so the `stop' section pinged the NFS server before doing the umount. If the pings failed, we'd issue ``umount -f''.

I put the script in ``/usr/local/etc/init.d/mount_umount_nfs_fs'' and created a symbolic link to it in /etc/rc3.d called ``S95mount_nfs_filesystems'' and another in /etc/rc0.d (``K00umount_nfs_filesystems'').

The script we used was actually table-driven as we needed to access multiple NFS servers. A record in the table/file looked like:
Code:
nfssvr:exported-fs:mount-options:local_mount-point
The complication added by reading from the file is minor and only involves performing those checks and the eventual mount inside a loop that uses parameters read from the file:
Code:
NFSFSDAT=whereever-you-stash-the-nfs-export-mount-point-data-file
case $1 in
'start')
   cat ${NFSFSDAT} | while read REC
   do
      NFSSVR=`echo $REC | cut -d: -f1`
      EXPFS=`echo $REC | cut -d: -f2`
      MNTOPTS=`echo $REC | cut -d: -f3`
      MNTPT=`echo $REC | cut -d: -f4`

      # Test to see if the NFS filesystem is not mounted. If so, we proceed.
      # If it is we drop through to the bottom of the loop.
      if [ -z "`mount | grep nfs | grep ${NFSSVR}:${EXPFS}`" ]
      then
         # Ping $NFSSVR and test if alive
         ping -c 3 $NFSSVR -q > /dev/null
         if [ $? -eq 0 ]
         then
            if [ -n "`showmount -e $NFSSVR | grep $EXPFS`" ]
            then
               # NFS server is up so perform mount
               echo "Mounting ${NFSSVR}:${EXPFS}..."
               mount -t nfs -o ${MNTOPTS} ${NFSSVR}:${EXPFS} ${MNTPT}
            else
               echo "NFS server ${NFSSVR} is not exporting \"${EXPFS}\". Not mounted."
            fi
         else
            echo "NFS server not reachable. Not mounting ${NFSSVR}:${EXPFS}."
         fi
      else
         echo "${NFSSVR}:${EXPFS} is already mounted."
      fi
   done
   ;;
'stop')

   (same sort of stuff done here but for unmounting... and remember the different umount if the pings fail)

   ;;
esac
(Note: I don't have access to the systems where I was using this script so I wrote this from memory; I can't guarantee that everything's error-free.)
The first check looks to see if the filesystem is already mounted and skips the other checks and the mount if it is. Why? Well, if there had been a failure of one (or more) of the NFS servers while our system was booting and mounting, once the remote system was back up, we could merely run the script by hand to mount the missing filesystem(s). We got a simple reminder that the the mount had already been done but no ugly error messages from mount(8). (Similar logic was in there for the umounts: don't umount if it's not mounted.) After that the ping check is done and, if successful, the mount is done.

One thing I didn't take into account in the above script fragment is checking to see if the host trying to mount the NFS exported filesystem is able/authorized to do so. That would be an extra pipe from the `showmount -e' command into a grep for the local hostname:
Code:
showmount -e $NFSSVR | grep $EXPFS | grep $HOSTNAME
(I don't recall having to do that in my old script but probably should have. Mounts would obviously fail if the NFS was up and exporting the desired filesystem but you were not authorized to mount it. It'd probably be best to find that out with the test of showmount rather than trying to figure out from a mount failure which might not give you a very descriptive error message.)

Once I had this script running, I left the entries in /etc/fstab but commented out and included a comment line noting that NFS filesystems were in use and that the new script was handling their mounting. (In case another admin was poking around in the fstab and hadn't read the memo about the new script.)

Hope this points you in the right direction...
More questions? Post them here and I'll watch for 'em.

--
Rick
 
1 members found this post helpful.
Old 10-28-2013, 10:24 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by harshaabba View Post
hi all,

I need to mount following share to the mentioned following mount point.I can do this using fstab using the following statement.

192.168.1.1:/NFS1 /mnt/NFS1 nfs defaults 0 0

But during the server boot if remote nfs mount point does not exist, server might hang.

I want to include this to start up script. Is there any predefined script you created which I can use.

My server platform is RHEL 5.4 32 bit version.



Thank You.
instead of scripting use the tools at your hands. autofs. use that. autofs allows for mount points to be mounted on the fly, also if you add the bg option you will not have to worry about things hanging on either server or client.

https://access.redhat.com/site/docum...ig-autofs.html

https://access.redhat.com/site/docum...ng_autofs.html

https://www.centos.org/docs/5/html/D...ig-autofs.html

just a few fast links.
 
1 members found this post helpful.
Old 10-28-2013, 11:34 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Ah... autofs. That's what I get for posting in a caffeine-deprived state late at night. Maybe I'd have thought of that if we were using NFS on our home network.

(We didn't use automount/autofs for production systems. I can't remember why the Solaris admins were gun shy about it but on the Tru64 side we had problems with buggy NFS client that would hang up hard (requiring a reboot) if the remote side hiccuped. Once we had a workaround script we never switched back. Easier to continue using what was working than have to deal with the egg on your face if NFS burned us again.)
 
Old 10-28-2013, 12:58 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
autofs and automounter can have issues without the bg flag, you might want to also consider adding the timeout flags too.

Code:
$ cat /etc/auto.NFS 
TV_Shows	-rw,soft,intr,bg,rsize=8192,wsize=8192	server:/exports
thats my autofs config file on my laptop for my NFS server. you just need to pair it properly with your exports

Code:
cat /etc/exports 
#
#	/etc/exports

#	NFS4
/exports *(rw,insecure,subtree_check,crossmnt,fsid=0)

#	NFSv3
/exports/path/to/foo *(rw,insecure,no_subtree_check,fsid=3010)
/exports/second/path/to/foo *(rw,insecure,no_subtree_check,fsid=3020)
note, i have insecure in my exports due to running a few Macs in my house. OSx is unable to connect without the insecure flag on the NFS server.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NFS mount of smb mount of windows share: permission denied problem :( Bagatur Linux - Networking 4 07-07-2009 11:34 AM
How can I mount a NFS share that have a SMB share mounted? perezyanez Linux - Networking 2 09-26-2008 09:03 AM
nfs mount: can't write to the share ubyt3m3 Solaris / OpenSolaris 6 08-02-2008 08:01 PM
Can't mount nfs share from fstab but can mount it manually - help Mountain Linux - Networking 1 03-30-2008 08:34 PM
Cannot Mount NFS Share Ingla Linux - Server 3 08-02-2007 08:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration