#!/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 _FLAG_QUIET=0 # The use of iscsiadm requires admin privilages read_iscsi_con (){ _ISCSI_CON=$(iscsiadm -m session) > /dev/null } show_help (){ echo "Smart iSCSI Mounter - Help" echo "" echo "[OVERRIDE] [FUNCTION]" echo "Example: smart-iscsi -q -m" echo "" echo "Overrides:" echo "'-q (--quiet)'" echo "'-f (--force)'" echo "" echo "Functions:" echo "'-m (--mount)'" echo "'-u (--umount)'" echo "'-s (--status)'" echo "'-r (--release)'" echo "'-h (--help)'" } report_status (){ if [ ${_FLAG_QUIET} == 0 ] then echo "[Showing status of mount-point]" 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}" fi } 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 if [ ${_FLAG_QUIET} == 0 ] then echo "INFO: Process ${MONITOR_PROC} is not running." fi fi } mount_func (){ if [ ${_FLAG_QUIET} == 0 ] then echo "[Attempting to mount iSCSI volume]" fi if [[ ${_ISCSI_CON} == *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]] then echo "" 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 > /dev/null 2>&1 sleep 3 mount /dev/disk/by-uuid/$DISK_UUID $MOUNT_DIR -o noauto,noperm > /dev/null 2>&1 } umount_func (){ if [ ${_FLAG_QUIET} == 0 ] then echo "[Attempting to un-mount iSCSI volume]" fi if [[ $_FLAG_FORCE -eq 0 ]]; then check_proc_status fi if [[ ${_ISCSI_CON} != *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]] then echo "" 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 > /dev/null 2>&1 sleep 3 iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --logout > /dev/null 2>&1 } check_admin (){ if [[ $EUID -ne 0 ]] then echo "This command requires root, please re-run with appropriate permissions." exit 1; fi } # Process input parameters for arg in "$@"; do case "$arg" in -q|--quiet) read_iscsi_con _FLAG_QUIET=1 shift ;; -f|--force) _FLAG_FORCE=1 shift ;; -m|--mount) check_admin read_iscsi_con mount_func report_status shift ;; -u|--umount) check_admin read_iscsi_con umount_func report_status shift ;; -s|--status) check_admin read_iscsi_con report_status shift ;; -r|--release) echo "Smart iSCSI Mounter v0.98" shift ;; -h|--help) show_help shift ;; esac done exit 0;