From 8d910d10f890937a08ebeaf1c7f19f9fbd0a191a Mon Sep 17 00:00:00 2001 From: James Slagle Date: Tue, 14 Jan 2014 12:02:39 -0500 Subject: [PATCH] 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 --- .../ramdisk/extra-data.d/scripts/d/init-func | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/elements/ramdisk/extra-data.d/scripts/d/init-func b/elements/ramdisk/extra-data.d/scripts/d/init-func index 8a8dcabc..533695d7 100755 --- a/elements/ramdisk/extra-data.d/scripts/d/init-func +++ b/elements/ramdisk/extra-data.d/scripts/d/init-func @@ -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"