You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.2 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) Voltaire Ltd. 2006. ALL RIGHTS RESERVED.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. # Author: Dan Bar Dov <danb@voltaire.com>
  20. # iscsi_discovery:
  21. # * does a send-targets discovery to the given IP
  22. # * set the transport type to the preferred transport (or tcp is -t flag is not used)
  23. # * tries to login
  24. # * if succeeds,
  25. # o logout,
  26. # o mark record autmatic (unless -m flag is used)
  27. # * else
  28. # o reset transport type to TCP
  29. # o try to login
  30. # o if succeeded
  31. # + logout
  32. # + mark record automatic (unless -m flag is used)
  33. #
  34. usage()
  35. {
  36. echo "Usage: $0 <IP> [-p <port>] [-d] [-t <tcp|iser> [-f]] [-m] [-l]"
  37. echo "Options:"
  38. echo "-p set the port number (default is 3260)."
  39. echo "-d print debugging information"
  40. echo "-t set transport (default is tcp)."
  41. echo "-f force specific transport -disable the fallback to tcp (default is fallback enabled)."
  42. echo " force the transport specified by the argument of the -t flag."
  43. echo "-m manual startup - will set manual startup (default is automatic startup)."
  44. echo "-l login to the new discovered nodes (default is false)."
  45. }
  46. dbg()
  47. {
  48. $debug && echo $@
  49. }
  50. initialize()
  51. {
  52. trap "exit" 2
  53. debug=false
  54. force="0"
  55. log_out="1"
  56. startup_manual="0"
  57. #set default transport to tcp
  58. transport=tcp
  59. #set default port to 3260
  60. port=3260;
  61. }
  62. parse_cmdline()
  63. {
  64. if [ $# -lt 1 ]; then
  65. usage
  66. exit 1
  67. fi
  68. # check if the IP address is valid
  69. ip=`echo $1 | awk -F'.' '$1 != "" && $1 <=255 && $2 != "" && $2 <= 255 && $3 != "" && $3 <= 255 && $4 != "" && $4 <= 255 {print $0}'`
  70. if [ -z "$ip" ]; then
  71. echo "$1 is not a vaild IP address!"
  72. exit 1
  73. fi
  74. shift
  75. while getopts "dfmlt:p:" options; do
  76. case $options in
  77. d ) debug=true;;
  78. f ) force="1";;
  79. t ) transport=$OPTARG;;
  80. p ) port=$OPTARG;;
  81. m ) startup_manual="1";;
  82. l ) log_out=0;;
  83. \? ) usage
  84. exit 1;;
  85. * ) usage
  86. exit 1;;
  87. esac
  88. done
  89. }
  90. discover()
  91. {
  92. # If open-iscsi is already logged in to the portal, exit
  93. if [ $(iscsiadm -m session | grep -c ${ip}:${port}) -ne 0 ]; then
  94. echo "Please logout from all targets on ${ip}:${port} before trying to run discovery on that portal"
  95. exit 2
  96. fi
  97. connected=0
  98. discovered=0
  99. dbg "starting discovery to $ip"
  100. disc="$(iscsiadm -m discovery --type sendtargets --portal ${ip}:${port})"
  101. echo "${disc}" | while read portal target
  102. do
  103. portal=${portal%,*}
  104. select_transport
  105. done
  106. discovered=$(echo "${disc}" | wc -l)
  107. if [ ${discovered} = 0 ]; then
  108. echo "failed to discover targets at ${ip}"
  109. exit 2
  110. else
  111. echo "discovered ${discovered} targets at ${ip}"
  112. fi
  113. }
  114. try_login()
  115. {
  116. if [ "$startup_manual" != "1" ]; then
  117. iscsiadm -m node --targetname ${target} --portal ${portal} --op update -n node.conn[0].startup -v automatic
  118. fi
  119. iscsiadm -m node --targetname ${target} --portal ${portal} --login >/dev/null 2>&1
  120. ret=$?
  121. if [ ${ret} = 0 ]; then
  122. echo "Set target ${target} to automatic login over ${transport} to portal ${portal}"
  123. ((connected++))
  124. if [ "$log_out" = "1" ]; then
  125. iscsiadm -m node --targetname ${target} --portal ${portal} --logout
  126. fi
  127. else
  128. echo "Cannot login over ${transport} to portal ${portal}"
  129. iscsiadm -m node --targetname ${target} --portal ${portal} --op update -n node.conn[0].startup -v manual
  130. fi
  131. return ${ret}
  132. }
  133. set_transport()
  134. {
  135. transport=$1
  136. case "$transport" in
  137. iser)
  138. # iSER does not use digest
  139. iscsiadm -m node --targetname ${target} --portal ${portal} \
  140. --op update -n node.conn[0].iscsi.HeaderDigest -v None
  141. iscsiadm -m node --targetname ${target} --portal ${portal} \
  142. --op update -n node.conn[0].iscsi.DataDigest -v None
  143. ;;
  144. cxgb3i)
  145. # cxgb3i supports <= 16K packet (BHS + AHS + pdu payload + digests)
  146. iscsiadm -m node --targetname ${target} --portal ${portal} \
  147. --op update -n node.conn[0].iscsi.MaxRecvDataSegmentLength \
  148. -v 8192
  149. ;;
  150. esac
  151. transport_name=`iscsiadm -m node -p ${portal} -T ${target} |awk '/transport_name/ {print $1}'`
  152. iscsiadm -m node --targetname ${target} --portal ${portal} \
  153. --op update -n ${transport_name} -v ${transport}
  154. }
  155. select_transport()
  156. {
  157. set_transport $transport
  158. dbg "Testing $transport-login to target ${target} portal ${portal}"
  159. try_login;
  160. if [ $? != 0 -a "$force" = "0" ]; then
  161. set_transport tcp
  162. dbg "starting to test tcp-login to target ${target} portal ${portal}"
  163. try_login;
  164. fi
  165. }
  166. check_iscsid()
  167. {
  168. #check if iscsid is running
  169. pidof iscsid &>/dev/null
  170. ret=$?
  171. if [ $ret -ne 0 ]; then
  172. echo "iscsid is not running"
  173. echo "Exiting..."
  174. exit 1
  175. fi
  176. }
  177. check_iscsid
  178. initialize
  179. parse_cmdline "$@"
  180. discover