#!/bin/bash DISK_UUID="12E6B87AE6B86021" IQN="iqn.2000-01.com.synology:SR3.VMs" PORTAL_IP="192.168.0.3" MOUNT_DIR="/home/dunestorm/VMs" MONITOR_PROC="vmware" MONITOR_PROC_CNT=7 # Do not modify _MONITOR_PROC_CNT=$(pgrep -f $MONITOR_PROC | wc -l) _ISCSI_CON="" _FLAG_FORCE=0 # The use of iscsiadm requires admin privilages read_iscsi_con (){ _ISCSI_CON=$(iscsiadm -m session) } show_usage (){ echo "[Usage]" echo "Please use either '-m (--mount)' to mount or '-u (--umount)' to unmount this volume." echo "" echo "Additional Options:" echo "'-s (--status)'" echo "'-r (--release)'" exit 1; } report_status (){ echo "INFO: Process '${MONITOR_PROC}' has ${_MONITOR_PROC_CNT} instances running." if [ $(ls $MOUNT_DIR/. | wc -l) -ge 1 ] then echo "" echo "Directory is mounted at:" $MOUNT_DIR else echo "" echo "Directory is un-mounted from:" $MOUNT_DIR fi read_iscsi_con echo "" echo "There is/are currently '$(${_ISCSI_CON} 2>&1 | wc -l)' iSCSI connection(s) open:" echo "${_ISCSI_CON}" } check_proc_status (){ if [ ${_MONITOR_PROC_CNT} -gt $MONITOR_PROC_CNT ] then echo "WARNING: process '${MONITOR_PROC}' is still running, unable to unmount volume safely." exit 2; else echo "INFO: Process ${MONITOR_PROC} is not running." fi } mount_func (){ echo "[Attempting to mount iSCSI volume]" if [[ ${_ISCSI_CON} == *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]] then echo "WARNING: You're already connected to the '${IQN}' iSCSI target." echo "Use the -f (--force) flag to bypass this warning." exit 2; fi iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --login sleep 3 mount /dev/disk/by-uuid/$DISK_UUID $MOUNT_DIR -o noauto,noperm report_status } umount_func (){ echo "[Attempting to un-mount iSCSI volume]" if [[ $_FLAG_FORCE -eq 0 ]]; then check_proc_status fi if [[ ${_ISCSI_CON} != *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]] then echo "WARNING: You're not currently connected to the '${IQN}' iSCSI target." echo "Use the -f (--force) flag to bypass this warning." exit 2; fi umount -l $MOUNT_DIR sleep 3 iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --logout report_status } check_admin (){ if [[ $EUID -ne 0 ]] then echo "This command requires root, please re-run with appropriate permissions." exit 1; fi } # Process input parameters while (( "$#" )); do case "$2" in -f|--force) _FLAG_FORCE=1 ;; esac case "$1" in -m|--mount) check_admin read_iscsi_con mount_func ;; -u|--umount) check_admin read_iscsi_con umount_func ;; -s|--status) echo "[Showing status of mount-point]" check_admin read_iscsi_con report_status ;; -r|--release) echo "Smart iSCSI Mounter v0.97" ;; -*) show_usage ;; esac exit 0; done # Show usage if no parameters have been passed if [[ $# -eq 0 ]] then show_usage fi