43
local pid="$1" portal="$2"
44
local tries=10 naplen=1 try=1
45
local out="" pidstgt="" name="tgtd"
47
# did we succesfully attach to the correct port?
48
out=$(netstat --program --all --numeric 2>/dev/null) ||
49
fail "failed to netstat --program --all --numeric"
50
pidstgt=$(echo "$out" |
51
awk '$4 == portal { print $7; exit(0); }' "portal=$portal")
52
if [ "$pidstgt" = "$pid/$name" ]; then
53
error "expected pid $pid is listening on $portal on try $try."
55
elif [ -n "$pidstgt" ]; then
56
# something else listening.
57
error "pid/process '$pidstgt' was listening on $portal." \
58
"expected '$pid/$name'."
61
# nothing listening. retry.
64
[ $try -le $tries ] || break
68
error "nothing listening on $portal after $tries tries"
42
72
if [ "$1" = "--help" -o "$1" = "-h" ]; then
77
107
# Racily try for a port. Annoyingly, tgtd doesn't fail when it's
78
108
# unable to use the requested portal, it just happily uses the default
80
111
while [ $tries -le 100 ]; do
81
112
# pick a port > 1024, and I think tgt needs one < 2^15 (32768)
83
[ "$port" -lt 32768 ] || continue
113
port=$((($RANDOM%(32767-1024))+1024+1))
85
115
portal="$ipv4addr:$port"
116
tried_ports="${tried_ports:+${tried_ports} }${port}"
86
117
error "going for $portal"
87
118
TGT_IPC_SOCKET="$socket" \
88
119
tgtd --foreground --iscsi "portal=$portal" >"$log" 2>&1 &
92
# did we succesfully attach to the correct port?
93
out=$(netstat --program --all --numeric 2>/dev/null) ||
94
fail "failed to netstat --program --all --numeric"
95
pidstgt=$(echo "$out" |
96
awk '$4 == portal { print $7; exit(0); }' "portal=$portal")
98
if [ -n "$pidstgt" ]; then
99
if [ "$pidstgt" != "$pid/tgtd" ]; then
100
error "'$pidstgt' was listening on $portal, not $pid"
102
error "grabbed $port in $pid on try $tries"
104
echo "export TGT_PID=$pid"
105
echo "export TGT_IPC_SOCKET=$socket"
106
echo "export CURTIN_VMTEST_ISCSI_PORTAL=$portal"
108
echo "$pid" > "$pidfile"
109
error "To list targets on this tgt: "
110
error " TGT_IPC_SOCKET=$socket tgtadm --lld=iscsi --mode=target --op=show"
111
error "For a client view of visible disks:"
112
error " iscsiadm --mode=discovery --type=sendtargets --portal=$portal"
113
error " iscsiadm --mode=node --portal=$portal --op=show"
118
error "nothing listening on $portal pid=$pid"
123
if wait_for_tgtd $pid $portal; then
124
error "grabbed $port in $pid on try $tries"
127
export TGT_IPC_SOCKET=$socket
128
export TGT_PORTAL=$portal
129
export CURTIN_VMTEST_ISCSI_PORTAL=\${TGT_PORTAL}
130
echo "To list targets on this tgt: " 1>&2
131
echo " TGT_IPC_SOCKET=\${TGT_IPC_SOCKET} tgtadm --lld=iscsi --mode=target --op=show" 1>&2
132
echo "For a client view of visible disks:" 1>&2
133
echo " iscsiadm --mode=discovery --type=sendtargets --portal=\${TGT_PORTAL}" 1>&2
134
echo " iscsiadm --mode=node --portal=\${TGT_PORTAL} --op=show" 1>&2
136
# unset to avoid cleanup
120
140
kill -9 $pid >/dev/null 2>&1
123
143
tries=$(($tries+1))
145
error "gave up. tried $tries times."
146
error "ports tried: ${tried_ports}"