2

The context

I have two servers: a NAS and a client server. They are both small machines (Raspberry, OLinuXino) and are plugged to the same power strip (which has a power button).

The client server mounts the NFS volumes from NAS at startup.

The problem

When I switch on the power strip, both servers startup at the same time. But at the time the client tries to mount NFS volumes, they are not yet available from the NAS.

So I have to mount them manually and I want to automate this.

I use fstab to mount the volumes on a Debian Wheezy distribution.

nas.local:/media/myshare  /media/myshare nfs defaults 0 0

How I would solve it

So I thought I could add a script on the NAS that calls a ssh command to tell the client "mount your NFS volumes, they are ready now !".

Something like ssh -i idFile login@clientServer 'sudo mount -a' from the NAS side.

The question

What would be the best place where to put that kind of script ?

A NFS hook ? Something like a callback ?

Extra question

Or is there another and better way to do this ?

lauhub
  • 962
  • How are you mounting the NFS from the client? A fstab entry or are you using an automounter? What distros are you running? – garethTheRed Jun 26 '14 at 09:27
  • Sorry, I forget this basical information. I updated the question – lauhub Jun 26 '14 at 09:48
  • Are you using the soft option in your /etc/fstab on the client? If so, try removing it (although it may be wise to add the intr option if you do). – garethTheRed Jun 26 '14 at 10:04
  • No, no soft option used. And intr seems to be deprecated. (I added a sample of my fstab entry to the question) – lauhub Jun 26 '14 at 11:50

1 Answers1

2

I would try to protect against failures for several reason. A cron job that checks if the volume is mounted and then mounts it if it is not would accomplish this.

  1. Use I would create the script using the answer from Check if directory mounted with bash [closed]

  2. For the mounting in the bash script you are creating use Giles answer to modify your sudoers file to allow the cron script to execute as sudo How to run a specific program as root without a password prompt?. This may be an optional step depending on your setup.

  3. Set up your cron job. You will want two. The first would be to regularly check for the mount and mount it if not. That way if either fails independently it will come back up online. The second should employ a cron after bootup. This answer will help you Crontab job start +1 min after @reboot

From the server side: You could attach a script to the NFS service itself. You could edit your service script to execute after a short sleep after completion. The bash script could follow the form:

sleep 60 && my_mount_script.sh

Using systemd you could wait systemd to wait for command to complete before restart/shutdown or killing other processes for the nfs.service to start. The replacements would include removing the Before section and then changing the after service name to nfs.service

DarkSheep
  • 1,886
  • 3
  • 15
  • 18
  • Thank you for your answer. Actually, I had thought to use this method to achieve my purpose. But I wanted to know if I could raise an event from the server side to make client (at the moment it caught the event) check and mount the volume. And if it was a good solution too (or not) – lauhub Jun 26 '14 at 09:55
  • OK, your edit gives me a lead. Now, I don't know which file I should edit and where to put it in my Debian system. /etc/systemd/user or /etc/systemd/system ? Anywhere else ? Do I have to edit other configuration files ? – lauhub Jun 26 '14 at 12:15
  • You should match your configuration. Use the locate (from the mlocate package) command to find the nfs service (nfsd.service) and put it in there. If you working without systemd it will be located in /etc/init.d – DarkSheep Jun 26 '14 at 14:57
  • There is no nfsd.service on my system. And I don't feel I could edit safely the nfs-kernel-server startup script. – lauhub Jun 26 '14 at 15:23
  • 1
    If you don't have any dependencies on the NFS (or you have ones that you're OK if they fail) I would just put that in the rc.local file. – Didi Kohen Jun 26 '14 at 17:07