How to mount NFS shares during system boot

From Wiki-UX.info
Jump to: navigation, search

Abstract

The following procedure detailes how set a "nfsmount" script on the system rc boot sequence to mount NFS shares. Alternative, you can modify the /etc/fstab to declare the entries but this is not recommended because may block the system boot due to network problems or NFS server outgages.

1. Copy the following initialization script under /sbin/init.d/nfsmount. Set the required permissions.

#!/sbin/sh
#
# @(#)B.11i
#
# NOTE:    This script is not configurable!  Any changes made to this
#          script will be overwritten when you upgrade to the next
#          release of HP-UX.
#
# WARNING: Changing this script in any way may lead to a system that
#          is unbootable.  Do not modify this script.

#
# Automatically mount NFS remote shares during system boot.
#

# Allowed exit values:
#       0 = success; causes "OK" to show up in checklist.
#       1 = failure; causes "FAIL" to show up in checklist.
#       2 = skip; causes "N/A" to show up in the checklist.
#           Use this value if execution of this script is overridden
#           by the use of a control variable, or if this script is not
#           appropriate to execute for some other reason.
#       3 = reboot; causes the system to be rebooted after execution.
#       4 = background; causes "BG" to show up in the checklist.
#           Use this value if this script starts a process in background mode.

# Input and output:
#       stdin is redirected from /dev/null
#
#       stdout and stderr are redirected to the /etc/rc.log file
#       during checklist mode, or to the console in raw mode.

PATH=/usr/sbin:/usr/bin:/sbin
export PATH

# NOTE: If your script executes in run state 0 or state 1, then /usr might
#       not be available.  Do not attempt to access commands or files in
#       /usr unless your script executes in run state 2 or greater.  Other
#       file systems typically not mounted until run state 2 include /var
#       and /opt.

rval=0

# Check the exit value of a command run by this script.  If non-zero, the
# exit code is echoed to the log file and the return value of this script
# is set to indicate failure.

set_return() {
        x=$?
        if [ $x -ne 0 ]; then
                echo "EXIT CODE: $x"
                rval=1  # script FAILed
        fi
}

case $1 in
'start_msg')
        echo "Mounting the NFS remote file systems"
        ;;

'stop_msg')
        echo "Unmounting the NFS remote file systems"
        ;;

'start')

        # source the system configuration variables
        if [ -f /etc/rc.config.d/nfsmount ] ; then
                . /etc/rc.config.d/nfsmount
        else
                echo "ERROR: /etc/rc.config.d/nfsmount defaults file MISSING"
        fi

        # Check to see if this script is allowed to run...
        if [ "$NFS_MOUNT" != 1 ]; then
                rval=2
        else

        # Execute the commands to mount the NFS shares
	# Example:
	# mount -F nfs [ -o nfs options ] <nfs_server_hostname_or_ip_address>:<nfs_share> <nfs_mount_point>

        fi
        ;;

'stop')
        # source the system configuration variables
        if [ -f /etc/rc.config.d/nfsmount ] ; then
                . /etc/rc.config.d/nfsmount
        else
                echo "ERROR: /etc/rc.config/nfsmount defaults file MISSING"
        fi

        # Check to see if this script is allowed to run...
        if [ "$NFS_MOUNT" != 1 ]; then
                rval=2
        else

        # Execute the commands to unmount the NFS shares
        # Use "nfsmount -a" if your current NFS installation support this flag
	# else use "umount <mount point>
	# nfsmount -a

        fi
        ;;

*)
        echo "usage: $0 {start|stop|start_msg|stop_msg}"
        rval=1
        ;;
esac

exit $rval
# chown bin:bin /sbin/init.d/nfsmount
# chmod 0555 /sbin/init.d/nfsmount

2. Create a "system configuration variable" file that containts the startup control variable. Set the permissions.

# echo "NFS_MOUNT=1" > /etc/rc.config.d/nfsmount
# chown bin:bin /etc/rc.config.d/nfsmount
# chmod 0444 /etc/rc.config.d/nfsmount

3. Modify the script "start" case option to add your remote NFS shares.

.
.
.
'start')

        # source the system configuration variables
        if [ -f /etc/rc.config.d/nfsmount ] ; then
                . /etc/rc.config.d/nfsmount
        else
                echo "ERROR: /etc/rc.config.d/nfsmount defaults file MISSING"
        fi

        # Check to see if this script is allowed to run...
        if [ "$CONTROL_VARIABLE" != 1 ]; then
                rval=2
        else

        # Execute the commands to mount the NFS shares
	# Example:
	# mount -F nfs [ -o nfs options ] <nfs_server_hostname_or_ip_address>:<nfs_share>  <nfs_mount_point>
	mount -F nfs 192.168.0.100:/remotenfs /localnfs

        fi
        ;;
.
.
.

4. Modify the script "stop" case option to add your remote NFS shares umount.

.
.
.
'stop')
        # source the system configuration variables
        if [ -f /etc/rc.config.d/nfsmount ] ; then
                . /etc/rc.config.d/nfsmount
        else
                echo "ERROR: /etc/rc.config/nfsmount defaults file MISSING"
        fi

        # Check to see if this script is allowed to run...
        if [ "$NFS_MOUNT" != 1 ]; then
                rval=2
        else

        # Execute the umount command to unmount the NFS shares
        # umount <nfs_mount>

	umount /localnfs

        fi
        ;;
.
.
.

5. Soft link the the nfsmount script to a proper startup sequence, normally under /sbin/init.d/rc3.d. Use the reverse numbering scheme to link the stop version. Remember to use the [ 1000 - Start value ] formula to choose the killing script secuence number.

For example, to mount the NFS during the init 3 transition just after starting the NFS server, use the following method:

# ls /sbin/rc3.d/*
/sbin/rc3.d/S100nfs.server     /sbin/rc3.d/S823hpws_tomcat    /sbin/rc3.d/S870hpsmhd
/sbin/rc3.d/S200tps.rc         /sbin/rc3.d/S823hpws_webmin    /sbin/rc3.d/S990dtlogin.rc
/sbin/rc3.d/S800cmcluster      /sbin/rc3.d/S823hpws_webproxy  /sbin/rc3.d/S99prngd.rc
/sbin/rc3.d/S823hpws_apache    /sbin/rc3.d/S823hpws_xmltools

The script will be run between "S100nfs.server" and "S200tps.rc" initialization script, for example, using the S150 sequence.

# ln -s /sbin/init.d/nfsmount /sbin/rc3.d/S150nfsmount

The killing script when transition to init 2 will be K850 (1000 - 150).

# ln -s sbin/init.d/nfsmount /sbin/rc2.d/K850nfsmount

That way, the overall file system layout for the script will be:

# find /sbin -name "*nfsmount" -exec ll {} \;
-r-xr-xr-x   1 bin        bin           4426 Aug 26 12:59 /sbin/init.d/nfsmount
lrwxrwxrwx   1 root       sys             21 Aug 26 14:22 /sbin/rc2.d/K850nfsmount -> sbin/init.d/nfsmount
lrwxrwxrwx   1 root       sys             22 Aug 26 14:22 /sbin/rc3.d/S150nfsmount -> /sbin/init.d/nfsmount

Authors