#! /bin/bash # (c) Copyright 2010 Christoph Jahn # http://www.christoph-jahn.com # NAME # mount_noauto.sh - Mount file systems that are not # automatically mounted # # # SYNOPSIS # # mount_noauto.sh [-u] # # OPTIONS -u Perform umount instead of mount # # # DESCRIPTION # There may be situations when you have network mounts that # should not be done automatically on system startup. # E.g. you are working on a laptop and are not always # connected to the relevant server(s). In this case you # probably want to set the respective entries in /etc/fstab # to noauto. But now you have to manually call mount for # each entry, once you are connected to the server. # # This script helps you here by going over all the entries # from /etc/fstab and call mount where appropriate. # # Only those lines will be processed where the file system # type is matching one of the entries in the FS_SUPPORTED_LIST # variable. Also, if the mount point is already used, the # entry will be skipped. # # There is currently no check as to whether the server is # reachable. So you may run into time-outs and mount errors # for entries, where there is currently no connection to # the server. # # CAUTION: So far this has only been used on Fedora 12 # with CIFS mounts # # PLANNED CHANGES # - NEW: Ping server before trying to mount # List of file system types that should be considered, others will skipped FS_SUPPORTED_LIST="smb cifs nfs nfs4" # Read from this file CFG_FILE=/etc/fstab PRG=`basename $0` # Initialize mount/umount flag; by default we try to mount UMOUNT=0 while getopts 'u' OPTION do case $OPTION in u) UMOUNT=1 ;; ?) echo "Wrong option" echo $"Usage: $PRG mount network drives" echo $" $PRG -u umount network drives" exit 1 ;; esac done shift $(($OPTIND - 1)) # Do we do a mount or umount? Set internal params accordingly if [ $UMOUNT = 0 ]; then MOUNT_CMD=mount MOUNT_GREP_RC=1 SKIP_MSG="already mounted" else MOUNT_CMD=umount MOUNT_GREP_RC=0 SKIP_MSG="not mounted" fi # Read config file line by line while read line; do # Skip empty and comment lines if [ -n "$line" ]; then # Non-empty line, check for comment NO_COMMENT=`echo $line | grep -e "^#.*" > /dev/null;echo $?` else # Empty line, always skip NO_COMMENT=0 fi if [ $NO_COMMENT -eq 1 ]; then # Determine file system type FS=`echo $line | awk '{ print $3 }'` # Supported FS type? FS_SUPPORTED=`echo $FS_SUPPORTED_LIST | grep -v $FS > /dev/null; echo $?` # Only process supported file systems if [ $FS_SUPPORTED -eq 1 ]; then # Determine network path and local mount point SERVER_SHARE=`echo $line | awk '{ print $1 }'` MOUNTPOINT=`echo $line | awk '{ print $2 }'` # Check if mountpoint already in use. This is safer than # checking for the "normal" server/share because it is # possible that something else has been mounted there by mistake. SHARE_MOUNTED_RC=`mount -t $FS | grep $MOUNTPOINT > /dev/null; echo $?` # Perform actual mount/umount operation if [ $SHARE_MOUNTED_RC -eq $MOUNT_GREP_RC ]; then echo "$MOUNT_CMD : $MOUNTPOINT" $MOUNT_CMD $MOUNTPOINT else echo "Skipped $MOUNT_CMD of $MOUNTPOINT : $SKIP_MSG" fi fi fi done < $CFG_FILE