Pale blue cloud  

   

   

Computer stuff

Automatically unmounting NFS file systems when calling suspend

Details

So a couple of months ago I bought a ReadyNAS  Duo for my online personal backups and to host the Logitech Squeezecenter software that I use for streaming audio in my home. It serves its purpose very well and I'm very happy with it (although the Squeezecenter software itself is a little sluggish, but then, what do you expect from a 186 BogoMips Sparc CPU).

My computer is a laptop (running openSUSE), and when I make backups or access my music collection on the NAS, I mount its file systems (wirelessly) over NFS. Here are the fstab entries for that (the NAS is called 'betelgeuse', by the way):

betelgeuse:/c/media     /export/media   nfs     auto,user 0 0
betelgeuse:/c/backup    /export/backup  nfs     auto,user 0 0
betelgeuse:/c/home      /export/home    nfs     auto,user 0 0

The only problem I had with this setup is when I suspended my laptop with NFS file systems mounted and then later powered it up again at a moment when the NAS was not available. In that case the system would be confused and whenever I tried to access a file system, lock up completely. Only a reboot would get me out of that situation.

Apparently this is a known issue of NFS. And the solution is to unmount the relevant file systems before suspending the laptop.

Of course, you don't want to do this manually every time because it's tedious, prone to be forgotten, requires root or sudo, etcetera. Moreover, any task that is stupid and repetitive, or falls in one of the categories mentioned, can, and should, be automated by scripting in my opinion.

I did some research into the matter, and stumbled on a package called pm-utils. The pm-utils wiki has this to say: "pm-utils is the new suspend and powerstate setting framework. It is designed to replace such scripts as those provided by the powersave package.

It is usually used by HAL to execute the various hacks needed to work around bugs in drivers and subsystems that are not yet aware of suspend. It is easily extensible by putting custom hooks into a directory, which can either be done by the system administrator or those hooks can be part of a package, especially if this package needs special attention during a system suspend or power state transition."

Basically, pm-utils allows you to create your own custom hooks (in the form of shell scripts) in the system's suspend scripts. This allows you to perform certain actions automatically right before the system suspends. Of course, by nature of interfering with the systems configuration, these scripts run as root. The openSUSE site has some more background information and examples, so I won't go deeper into that.

Messing around a bit I arrived at the following code:

#!/bin/bash
#
# Unmount NFS file systems before suspending, to
# prevent hangup when the system resume and NFS
# is not there.
#
#
case "$1" in
hibernate|suspend)
{
for fs in $(/bin/mount -t nfs | /usr/bin/awk {'print $3'}) ; do
echo "unmounting $fs";
/bin/umount $fs;
done
} 2>/dev/null
;;
# do nothing when resuming
thaw|resume)
{
echo "Nothing to do with NFS"; } 2>/dev/null
;;
*)
;;
esac
exit $?

I named the script 06nfs and copied it to /etc/pm/sleep.d (don't forget to make it executable; chmod 755).

Now, every time the laptop is placed into suspend mode, pm-suspend runs and executes the custom hook. Any mounted NFS file systems will be umounted, and not cause any problems when the laptop comes out of suspend and the NFS file system isn't there anymore.

   
© Palebluedot