#! /bin/sh do_start() { # setting NFS server's IP IP="10.0.2.5" # you need to have a backup of /etc/fstab if [ -e "/etc/fstab.save" ] then # test if server is alive if ! ping -c 1 -w 5 $IP 2>&1 >/dev/null then # if it is not alive echo "tarantino is down, mount -a impossible." else # if NFS server is alive then trying to mount directories echo "Mounting tarantino's directories via NFS" # getting shared directories SHARED=`showmount -e $IP|grep "^/"|awk -F" " '{print $1;}'` # for each shared directory for DIR in $SHARED do # test if a similar directory is present on local host if test -d $DIR then # do nothing if it exists a="" else # else we create it echo " $DIR does not exist, then creating it" mkdir $DIR fi echo " Adding $IP:$DIR in /etc/fstab" # adding an entry in /etc/fstab echo "$IP:$DIR $DIR nfs defaults 0 0" >> /etc/fstab # saving the mounted path into a log file echo "$DIR" >> /var/log/mounted.log done # finally mount all mount -a echo "Done" fi else echo "File /etc/fstab.save does not exist, mount -a impossible" fi } do_stop() { echo "Stopping mounts on tarantino" # for each mounted directory we try to unmount them for i in `cat /var/log/mounted.log` do echo " Unmounting $i" umount $i done echo " Setting normal fstab" # setting original fstab cp /etc/fstab.save /etc/fstab # removing log file rm /var/log/mounted.log echo "Done" } case $1 in start) do_start ;; stop) do_stop ;; restart) do_stop sleep 10 do_start ;; *) echo "Usage: service mount-nds {start|stop|restart}" exit 1 ;; esac exit 0