Script for mounting NFS

I wrote in a previous post on Sharing folder using NFS in Hardy. Now, I don’t know about you, but I got tried of going into command line and running the mount/umount command every time I wanted to connect to a NFS server.

Granted, I could add the appropriate entries into /etc/fstab and auto-mount the shares permanently. But I hate doing things permanently. I prefer to the NFS shares to work like SMB, mount when required, unmount when I am done.

So, a while back, I wrote a nifty script to do just that. I have been using the script since Gutsy and it still works in Hardy.

Prerequisites

For the script to work, you would need to install “zenity” and “libnotify-bin”. Zenity provides the dialogboxes required by the script, while libnotify-bin provides the pop-up balloon messages. Install them using Synaptic Package Manager, or run the following terminal command:

sudo apt-get install zenity libnotify-bin

The script

Download the script mount-nfs.sh. Make the script executable by running this terminal command:

chmod +x mount-nfs.sh

What the script does

Say you have a NFS server with IP address 192.168.1.2. You are sharing the following directories in the server:

/home/user/share/SUBDIRA
/home/user/share/SUBDIRB

When you run this script from a client PC, the shared directories will be mounted to:

$MOUNTPATH/SUBDIRA
$MOUNTPATH/SUBDIRB

respectively, in your client PC, where $MOUNTPATH is specified by you in the script (explained below).

Modifying the variables

Before you can use the script, you would need to change a few things to your requirements. These changes are located in the beginning of the script, delineated by the line of dashes.

  1. Set the icon. This can be any icon, used by the script when showing a pop-up balloon message, and while sitting in the notification area. E.g. I use the gnome-log icon:
    # Icon file
    	ICONFILE=$HOME/icons/gnome-log.png
  2. Set the location where the pop-up balloon message will appear. I wanted my message to appear on the top-right corner of a 1680×1080 screen, so I set the coordinates to (1600,20). If you delete these lines, the default location is bottom-right corner.
    # location of notification window
    	NWX=1600
    	NWY=20
  3. Set the NFS server path. The format is
    ipaddr:/path/

    The last character must be a slash “/”, else the script will not work. I have multiple shared subdirectories under “/home/user/share” (replace user with the actual login username), so the line look like this:

    # NFS server path (last character must be a slash)
    	NFSSERVERPATH=192.168.1.2:/home/user/share/
  4. On the client PC side, I mounted to subdirectories of /home/user, so I use the $HOME variable. If you specify another location outside your home directory, make sure the mount point has your user’s permission. Again, the last character must be a slash “/”, else the script will not work.
    # where to mount the volume (last character must be a slash)
    	MOUNTPATH=$HOME/
  5. Lastly, as mentioned, I have multiple shared subdirectories under “/home/user/share”. In this example, I have
    /home/user/share/SUBDIRA
    /home/user/share/SUBDIRB

    So, the mount list looks like this:

    # list of NFS
    	MOUNTLIST="SUBDIRA SUBDIRB"
  6. I use zenity list dialogbox for the selection of subdirectory to mount. If you have one subdirectory only, use:

    MOUNTLIST="SUBDIR"

    Then, comment out the next three lines below it to remove zenity list dialogbox, like this:

    # request NFS to mount
    #	MOUNTDIR=`zenity --height=200 --list --title="NFS" \
    #		--text="Select mount" --column=Location $MOUNTLIST`

    Test the script

    Run this terminal command:

    ./mount-nfs.sh

    If it works, you should get a pop-up balloon message to tell you the NFS share is mounted. At the same time, the script’s icon will appear in the notification area.

    If it does not work, check the message in the terminal for clue on what went wrong.

    Unmount

    To unmount, click the icon in the notification area. A zenity dialogbox will pop-up to confirm dismount. Click yes. After unmount is complete, you should see another pop-up balloon message to tell you the NFS share is dismounted.

    Conclusion

    That’s it. I put all my user scripts into subdirectory “/home/user/bin” to keep the home directory tidy. I also add a panel launcher for the script, so it is fully point and click to run the script.

Post a Comment

Your email is never published nor shared. Required fields are marked *