b0701d01d7137ca11565b8eebba020679f9e8a0b
[invirt/packages/xvm-iscsi-config.git] / xvm-iscsi-connect
1 #!/bin/bash
2
3 set -e
4
5 good_nodes=""
6
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)
10
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)
16
17     echo "Connecting to $portal_ip via $ifaces"
18
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)
21
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
27     # extraneous ones.
28     for iface in $ifaces; do
29         good_nodes="$good_nodes "$(echo /etc/iscsi/nodes/*/$portal_ip,3260,1/$iface)
30     done
31
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
35     done
36 done
37
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'))
40 # and delete them.
41 echo "Removing "$(echo $bad_nodes | wc -w)" extraneous discovered targets"
42 for node in $bad_nodes; do
43     echo "Removing $node"
44
45     # Remove the node entry
46     rm $node;
47
48     # Remove the cached results of the discovery so iscsiadm doesn't
49     # get confused
50     find /etc/iscsi/send_targets -lname "${node%/*}" -delete
51 done