7 for i in $(invirt-getconf --ls iscsi.targets); do
8 # Extract the portal to talk to
9 portal_ip=$(invirt-getconf iscsi.targets.$i.ip)
11 # Extract the interfaces we should talk to this portal
12 # on. Unfortunately, we have to ask about all the interfaces
13 # simultaneously, because iscsiadm deletes all existing mentions
14 # of a portal when you run discovery again.
15 ifaces=$(for j in $(invirt-getconf --ls iscsi.targets.$i.ifaces); do invirt-getconf iscsi.targets.$i.ifaces.$j; done)
17 echo "Connecting to $portal_ip via $ifaces"
19 # Use SendTargets to discover the available targets on the given portal.
20 iscsiadm -m discovery -t st -p $portal_ip:3260 $(for iface in $ifaces; do echo -I $iface; done)
22 # Because of protocol limitations (see
23 # http://www.pdl.cmu.edu/mailinglists/ips/mail/msg05174.html), the
24 # discovery attempt may have returned additional targets that are
25 # unreachable via this interface/portal. Make a list of discovered
26 # target, portal, interface combinations so we can later remove
28 for iface in $ifaces; do
29 good_nodes="$good_nodes "$(echo /etc/iscsi/nodes/*/$portal_ip,3260,1/$iface)
32 # If the discovery attempt did not log into the target, explicitly do so now.
33 for iface in $ifaces; do
34 iscsiadm -m node -p $portal_ip:3260 -I $iface -l
38 # Find all the nodes we now know about that we weren't supposed to
39 bad_nodes=$(find /etc/iscsi/nodes -type f | grep -Fxvf <(echo "$good_nodes" | sed 's/ /\n/g'))
41 echo "Removing "$(echo $bad_nodes | wc -w)" extraneous discovered targets"
42 for node in $bad_nodes; do
45 # Remove the node entry
48 # Remove the cached results of the discovery so iscsiadm doesn't
50 find /etc/iscsi/send_targets -lname "${node%/*}" -name "*,${node##*/}" -print -delete