#!/bin/bash set -e good_nodes="" for i in $(invirt-getconf --ls iscsi.targets); do # Extract the portal to talk to portal_ip=$(invirt-getconf iscsi.targets.$i.ip) # Extract the interfaces we should talk to this portal # on. Unfortunately, we have to ask about all the interfaces # simultaneously, because iscsiadm deletes all existing mentions # of a portal when you run discovery again. ifaces=$(for j in $(invirt-getconf --ls iscsi.targets.$i.ifaces); do invirt-getconf iscsi.targets.$i.ifaces.$j; done) echo "Connecting to $portal_ip via $ifaces" # Use SendTargets to discover the available targets on the given portal. iscsiadm -m discovery -t st -p $portal_ip:3260 $(for iface in $ifaces; do echo -I $iface; done) # Because of protocol limitations (see # http://www.pdl.cmu.edu/mailinglists/ips/mail/msg05174.html), the # discovery attempt may have returned additional targets that are # unreachable via this interface/portal. Make a list of discovered # target, portal, interface combinations so we can later remove # extraneous ones. for iface in $ifaces; do good_nodes="$good_nodes "$(echo /etc/iscsi/nodes/*/$portal_ip,3260,1/$iface) done # If the discovery attempt did not log into the target, explicitly do so now. for iface in $ifaces; do iscsiadm -m node -p $portal_ip:3260 -I $iface -l done done # Find all the nodes we now know about that we weren't supposed to bad_nodes=$(find /etc/iscsi/nodes -type f | grep -Fxvf <(echo "$good_nodes" | sed 's/ /\n/g')) # and delete them. echo "Removing "$(echo $bad_nodes | wc -w)" extraneous discovered targets" for node in $bad_nodes; do echo "Removing $node" # Remove the node entry rm $node; # Remove the cached results of the discovery so iscsiadm doesn't # get confused find /etc/iscsi/send_targets -lname "${node%/*}" -delete done