Add -w flag to iscsictl(8) utility, to make it wait for successfull

session establishment.  Scripting is kind of hard without it.

Reviewed by:	mav@
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D3872
This commit is contained in:
trasz 2015-10-17 13:06:52 +00:00
parent 2bdfbb5b0f
commit 16294b3699
2 changed files with 89 additions and 7 deletions

View File

@ -27,7 +27,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd September 12, 2014
.Dd October 17, 2015
.Dt ISCSICTL 8
.Os
.Sh NAME
@ -36,7 +36,9 @@
.Sh SYNOPSIS
.Nm
.Fl A
.Fl p Ar portal Fl t Ar target Op Fl u Ar user Fl s Ar secret
.Fl p Ar portal Fl t Ar target
.Op Fl u Ar user Fl s Ar secret
.Op Fl w Ar timeout
.Nm
.Fl A
.Fl d Ar discovery-host Op Fl u Ar user Fl s Ar secret
@ -70,6 +72,7 @@
.Nm
.Fl L
.Op Fl v
.Op Fl w Ar timeout
.Sh DESCRIPTION
The
.Nm
@ -113,6 +116,10 @@ Target name.
CHAP login.
.It Fl v
Verbose mode.
.It Fl w
Instead of returning immediately, wait up to
.Ar timeout
seconds until all configured sessions are successfully established.
.El
.Pp
Certain parameters are necessary when adding a session.
@ -132,9 +139,11 @@ via configuration file.
.Pp
Since connecting to the target is performed in background, non-zero
exit status does not mean that the session was successfully established.
Use
Use either
.Nm Fl L
to check the connection status.
to check the connection status, or the
.Fl w
flag to wait for session establishment.
.Pp
Note that in order for the iSCSI initiator to be able to connect to a target,
the

View File

@ -592,12 +592,70 @@ kernel_list(int iscsi_fd, const struct target *targ __unused,
return (0);
}
static int
kernel_wait(int iscsi_fd, int timeout)
{
struct iscsi_session_state *states = NULL;
const struct iscsi_session_state *state;
const struct iscsi_session_conf *conf;
struct iscsi_session_list isl;
unsigned int i, nentries = 1;
bool all_connected;
int error;
for (;;) {
for (;;) {
states = realloc(states,
nentries * sizeof(struct iscsi_session_state));
if (states == NULL)
xo_err(1, "realloc");
memset(&isl, 0, sizeof(isl));
isl.isl_nentries = nentries;
isl.isl_pstates = states;
error = ioctl(iscsi_fd, ISCSISLIST, &isl);
if (error != 0 && errno == EMSGSIZE) {
nentries *= 4;
continue;
}
break;
}
if (error != 0) {
xo_warn("ISCSISLIST");
return (error);
}
all_connected = true;
for (i = 0; i < isl.isl_nentries; i++) {
state = &states[i];
conf = &state->iss_conf;
if (!state->iss_connected) {
all_connected = false;
break;
}
}
if (all_connected)
return (0);
sleep(1);
if (timeout > 0) {
timeout--;
if (timeout == 0)
return (1);
}
}
}
static void
usage(void)
{
fprintf(stderr, "usage: iscsictl -A -p portal -t target "
"[-u user -s secret]\n");
"[-u user -s secret] [-w timeout]\n");
fprintf(stderr, " iscsictl -A -d discovery-host "
"[-u user -s secret]\n");
fprintf(stderr, " iscsictl -A -a [-c path]\n");
@ -609,7 +667,7 @@ usage(void)
fprintf(stderr, " iscsictl -R [-p portal] [-t target]\n");
fprintf(stderr, " iscsictl -R -a\n");
fprintf(stderr, " iscsictl -R -n nickname [-c path]\n");
fprintf(stderr, " iscsictl -L [-v]\n");
fprintf(stderr, " iscsictl -L [-v] [-w timeout]\n");
exit(1);
}
@ -631,6 +689,7 @@ main(int argc, char **argv)
const char *conf_path = DEFAULT_CONFIG_PATH;
char *nickname = NULL, *discovery_host = NULL, *portal = NULL,
*target = NULL, *user = NULL, *secret = NULL;
int timeout = -1;
long long session_id = -1;
char *end;
int ch, error, iscsi_fd, retval, saved_errno;
@ -641,7 +700,7 @@ main(int argc, char **argv)
argc = xo_parse_args(argc, argv);
xo_open_container("iscsictl");
while ((ch = getopt(argc, argv, "AMRLac:d:i:n:p:t:u:s:v")) != -1) {
while ((ch = getopt(argc, argv, "AMRLac:d:i:n:p:t:u:s:vw:")) != -1) {
switch (ch) {
case 'A':
Aflag = 1;
@ -692,6 +751,13 @@ main(int argc, char **argv)
case 'v':
vflag = 1;
break;
case 'w':
timeout = strtol(optarg, &end, 10);
if ((size_t)(end - optarg) != strlen(optarg))
xo_errx(1, "trailing characters after timeout");
if (timeout < 0)
xo_errx(1, "timeout cannot be negative");
break;
case '?':
default:
usage();
@ -782,6 +848,8 @@ main(int argc, char **argv)
if (vflag != 0)
xo_errx(1, "-v cannot be used with -M");
if (timeout != -1)
xo_errx(1, "-w cannot be used with -M");
} else if (Rflag != 0) {
if (user != NULL)
@ -811,6 +879,8 @@ main(int argc, char **argv)
xo_errx(1, "-i cannot be used with -R");
if (vflag != 0)
xo_errx(1, "-v cannot be used with -R");
if (timeout != -1)
xo_errx(1, "-w cannot be used with -R");
} else {
assert(Lflag != 0);
@ -896,6 +966,9 @@ main(int argc, char **argv)
failed += kernel_list(iscsi_fd, targ, vflag);
}
if (timeout != -1)
failed += kernel_wait(iscsi_fd, timeout);
error = close(iscsi_fd);
if (error != 0)
xo_err(1, "close");