Smart iSCSI Mounter v0.98

- Implemented quiet flag.
- Reworked help display.
- Re-wrote args handling to be less rigid.
This commit is contained in:
2020-05-23 15:09:20 +01:00
parent 3fae36c374
commit 5dc670cfcf
+60 -40
View File
@@ -11,37 +11,52 @@ MONITOR_PROC_CNT=7
_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)
_ISCSI_CON=$(iscsiadm -m session) > /dev/null
}
show_usage (){
echo "[Usage]"
echo "Please use either '-m (--mount)' to mount or '-u (--umount)' to unmount this volume."
show_help (){
echo "Smart iSCSI Mounter - Help"
echo ""
echo "Additional Options:"
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 (){
echo "INFO: Process '${MONITOR_PROC}' has ${_MONITOR_PROC_CNT} instances running."
if [ $(ls $MOUNT_DIR/. | wc -l) -ge 1 ]
if [ ${_FLAG_QUIET} == 0 ]
then
echo ""
echo "Directory is mounted at:" $MOUNT_DIR
else
echo ""
echo "Directory is un-mounted from:" $MOUNT_DIR
fi
echo "[Showing status of mount-point]"
echo "INFO: Process '${MONITOR_PROC}' has ${_MONITOR_PROC_CNT} instances running."
read_iscsi_con
echo ""
echo "There is/are currently '$(${_ISCSI_CON} 2>&1 | wc -l)' iSCSI connection(s) open:"
echo "${_ISCSI_CON}"
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 (){
@@ -50,28 +65,37 @@ check_proc_status (){
echo "WARNING: process '${MONITOR_PROC}' is still running, unable to unmount volume safely."
exit 2;
else
echo "INFO: Process ${MONITOR_PROC} is not running."
if [ ${_FLAG_QUIET} == 0 ]
then
echo "INFO: Process ${MONITOR_PROC} is not running."
fi
fi
}
mount_func (){
echo "[Attempting to mount iSCSI volume]"
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
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
report_status
mount /dev/disk/by-uuid/$DISK_UUID $MOUNT_DIR -o noauto,noperm > /dev/null 2>&1
}
umount_func (){
echo "[Attempting to un-mount iSCSI volume]"
if [ ${_FLAG_QUIET} == 0 ]
then
echo "[Attempting to un-mount iSCSI volume]"
fi
if [[ $_FLAG_FORCE -eq 0 ]]; then
check_proc_status
@@ -79,15 +103,15 @@ umount_func (){
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
umount -l $MOUNT_DIR > /dev/null 2>&1
sleep 3
iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --logout
report_status
iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --logout > /dev/null 2>&1
}
check_admin (){
@@ -101,6 +125,11 @@ check_admin (){
# 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
@@ -109,40 +138,31 @@ for arg in "$@"; do
check_admin
read_iscsi_con
mount_func
report_status
shift
;;
-u|--umount)
check_admin
read_iscsi_con
umount_func
report_status
shift
;;
-s|--status)
echo "[Showing status of mount-point]"
check_admin
read_iscsi_con
report_status
shift
;;
-r|--release)
echo "Smart iSCSI Mounter v0.97.1"
echo "Smart iSCSI Mounter v0.98"
shift
;;
-h|--help)
show_usage
shift
;;
-q|--quiet)
echo "Quiet"
show_help
shift
;;
esac
done
# Show usage if no parameters have been passed
if [[ $# -eq 0 ]]
then
show_usage
fi
exit 0;