Using bash script to clear out current iscsi connections

Started by Dieselboy, January 15, 2020, 11:00:15 PM

Previous topic - Next topic

Dieselboy

I am running failover tests with Red Hat Virtualisation. One of the caveats with RHV is that when you remove a disk which is an iscsi volume, RHV-M does not tell the host(s) to log out of the TCP connection for it. This can result in an outage, because if you then offline and delete the volume on the storage side it can cause RHV hosts to lock up because the hosts see the storage go down (because the active connection becomes broken). Unfortunately, it is not the case of RHV seeing that one volume go down, it locks up all storage operations and if it's not fixed almost immediately after occurring then it can bring down the whole host.

So with that, after executing failover I need to manually log out all of the iscsi connections on the host. However these failover hosts actually boot from iscsi themselves. Also there is a "Storage Domain" iscsi volume that relates to the failover environment that shouldnt be logged out as this will create a problem. This is really just for cleanup and to return the failover environment to the initial state. I was using notepad++ with find/replace. But while waiting for failover to complete, I decided to try and script it.

So I wrote a script. Basically, it lists all the active connections to iscsi, filters out ones that must not be logged out. Then turns the output into a series of log out commands and then runs them.

#!/bin/bash
# important lines to exclude from logout - such as the host boot disk and storage domain
IMPORTANTISCSI1=ucsc
IMPORTANTISCSI2=rhvm-he
IMPORTANTISCSI3=rhv
TEMPFILE=/tmp/current-sessions.txt


# save current sessions to a temp file
iscsiadm -m session > $TEMPFILE

# remove important lines from the output
sed -i 's/.*'"$IMPORTANTISCSI1"'.*//' $TEMPFILE
sed -i 's/.*'"$IMPORTANTISCSI2"'.*//' $TEMPFILE
sed -i 's/.*'"$IMPORTANTISCSI3"'.*//' $TEMPFILE

# replace IP part with iscsiadm part
sed -i 's/^tcp: \[.*\] \(\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\):3260,2460/iscsiadm --mode node --targetname/' $TEMPFILE

# replace non-flash part with -u for log out
sed -i 's/(non-flash)/-u/' $TEMPFILE

# execute logging out of all remaining connections from the file
chmod +x $TEMPFILE
sh -c "cd /tmp/; ./current-sessions.txt"

# remove temp file
rm -rf $TEMPFILE

# print something to finish off
echo "Logging out iscsi connections are done!"