Wait for tgtd socket to be available

tgtd returns execution control and backgrounds itself almost immediately
and before it has made it's listening socket available. This can cause a
race condition as the tgtd socket is not available when tgtadm is run,
resulting in an error:

failed to send request hdr to tgt daemon

Add a function to check if the socket is available before moving on to
calling tgtadm, and a wait_for helper function we can use.  We'll
check for the socket every 0.5 seconds, for up to 5 seconds.

I'm seeing this issue on almost every deploy using a ramdisk built from
Fedora 20. I'm not sure if something has changed in tgtd, but this
behavior is documented since Fedora 18 at least. In the systemd script
for tgtd, there is actually "sleep 5" to work around the problem.

See Also: https://bugzilla.redhat.com/show_bug.cgi?id=848942

Change-Id: Iffa9fc63393309ca653d592dff17316ecbea3e09
This commit is contained in:
James Slagle 2014-01-14 12:02:39 -05:00
parent de7deb7ce9
commit 8d910d10f8
1 changed files with 25 additions and 0 deletions

View File

@ -167,6 +167,30 @@ function find_disk() {
return 0
}
function check_tgtd_socket() {
echo -n "waiting for tgtd socket..."
if [ -e /var/run/tgtd.ipc_abstract_namespace.0 ]; then
echo "found"
return 0
else
echo "not found"
return 1
fi
}
wait_for(){
LOOPS=$1
SLEEPTIME=$2
shift ; shift
i=0
while [ $i -lt $LOOPS ] ; do
i=$((i + 1))
eval "$@" && return 0 || true
sleep $SLEEPTIME
done
return 1
}
function start_iscsi_target() {
local iqn=$1
local dev=$2
@ -176,6 +200,7 @@ function start_iscsi_target() {
mkdir -p /var/run
tgtd
wait_for 10 0.5 check_tgtd_socket
tgtadm --lld iscsi --mode target --op new --tid 1 --targetname "$iqn"
tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store "$dev"