logout-all.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh
  2. #
  3. # This script logs out from all active iSCSI sessions, excluding those
  4. # listed in /run/open-iscsi/shutdown-keep-sessions. That file is
  5. # generated by umountiscsi.sh and determines which sessions should not
  6. # be terminated.
  7. #
  8. ISCSIADM=/sbin/iscsiadm
  9. PIDFILE=/run/iscsid.pid
  10. ISCSI_ROOT_KEEP_ALL_SESSIONS_AT_SHUTDOWN=0
  11. if [ -f /etc/default/open-iscsi ]; then
  12. . /etc/default/open-iscsi
  13. fi
  14. if [ -f /etc/iscsi/iscsi.initramfs ] && [ $ISCSI_ROOT_KEEP_ALL_SESSIONS_AT_SHUTDOWN -eq 1 ]; then
  15. # Don't logout from any sessions if root is on initramfs and the
  16. # administrator wanted it that way.
  17. exit 0
  18. fi
  19. if [ ! -s $PIDFILE ] || ! kill -0 `sed -n 1p $PIDFILE` >/dev/null 2>/dev/null ; then
  20. # Don't logout from iSCSI sessions if daemon isn't running
  21. echo "iSCSI initiator daemon not running, not logging out from targets." >&2
  22. exit 1
  23. fi
  24. EXCLUDED_SESSIONS=""
  25. if [ -f /run/open-iscsi/shutdown-keep-sessions ] ; then
  26. _EXCLUDED_SESSIONS=$(cat /run/open-iscsi/shutdown-keep-sessions)
  27. for s in ${_EXCLUDED_SESSIONS} ; do
  28. EXCLUDED_SESSIONS="${EXCLUDED_SESSIONS:+$EXCLUDED_SESSIONS }${s}"
  29. done
  30. fi
  31. # trivial case
  32. if [ -z "$EXCLUDED_SESSIONS" ] ; then
  33. $ISCSIADM -m node --logoutall=all
  34. exit $?
  35. fi
  36. in_set() {
  37. eval _set=\$$1
  38. case "${_set}" in
  39. ("$2"|*" $2"|"$2 "*|*" $2 "*) return 0 ;;
  40. (*) return 1 ;;
  41. esac
  42. }
  43. # go through all iSCSI sessions, but exclude those where we don't want
  44. # to logout from
  45. RC=0
  46. for host_dir in /sys/devices/platform/host* ; do
  47. [ -d "$host_dir"/iscsi_host* ] || continue
  48. for session_dir in "$host_dir"/session* ; do
  49. if in_set EXCLUDED_SESSIONS "$session_dir" ; then
  50. continue
  51. fi
  52. $ISCSIADM -m session -r "$session_dir" --logout
  53. rc=$?
  54. if [ $rc -ne 0 ] ; then
  55. RC=1
  56. fi
  57. done
  58. done
  59. exit $RC