AUTOTOOLS: Add a Makefile.am for the iscsi-* utilities

This commit is contained in:
Ronnie Sahlberg
2014-03-22 11:35:58 -07:00
committed by Ronnie Sahlberg
parent 65d3597c1b
commit c07779cfa7
7 changed files with 10 additions and 10 deletions

8
utils/Makefile.am Normal file
View File

@@ -0,0 +1,8 @@
AM_CPPFLAGS=-I../include "-D_U_=__attribute__((unused))" \
"-D_R_(A,B)=__attribute__((format(printf,A,B)))"
AM_CFLAGS=$(WARN_CFLAGS)
LDADD = ../lib/libiscsi.la
bin_PROGRAMS = iscsi-inq iscsi-ls iscsi-readcapacity16 \
iscsi-swp

350
utils/iscsi-inq.c Normal file
View File

@@ -0,0 +1,350 @@
/*
Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
#ifndef discard_const
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#endif
const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-inq";
void inquiry_block_limits(struct scsi_inquiry_block_limits *inq)
{
printf("wsnz:%d\n", inq->wsnz);
printf("maximum compare and write length:%d\n", inq->max_cmp);
printf("optimal transfer length granularity:%d\n", inq->opt_gran);
printf("maximum transfer length:%d\n", inq->max_xfer_len);
printf("optimal transfer length:%d\n",inq->opt_xfer_len);
printf("maximum prefetch xdread xdwrite transfer length:%d\n", inq->max_prefetch);
printf("maximum unmap lba count:%d\n", inq->max_unmap);
printf("maximum unmap block descriptor count:%d\n", inq->max_unmap_bdc);
printf("optimal unmap granularity:%d\n", inq->opt_unmap_gran);
printf("ugavalid:%d\n", inq->ugavalid);
printf("unmap granularity alignment:%d\n", inq->unmap_gran_align);
printf("maximum write same length:%d\n", (int)inq->max_ws_len);
}
void inquiry_logical_block_provisioning(struct scsi_inquiry_logical_block_provisioning *inq)
{
printf("Threshold Exponent:%d\n", inq->threshold_exponent);
printf("lbpu:%d\n", inq->lbpu);
printf("lbpws:%d\n", inq->lbpws);
printf("lbpws10:%d\n", inq->lbpws10);
printf("lbprz:%d\n", inq->lbprz);
printf("anc_sup:%d\n", inq->anc_sup);
printf("dp:%d\n", inq->dp);
printf("provisioning type:%d\n", inq->provisioning_type);
}
void inquiry_block_device_characteristics(struct scsi_inquiry_block_device_characteristics *inq)
{
printf("Medium Rotation Rate:%dRPM\n", inq->medium_rotation_rate);
}
void inquiry_device_identification(struct scsi_inquiry_device_identification *inq)
{
struct scsi_inquiry_device_designator *dev;
int i;
printf("Peripheral Qualifier:%s\n",
scsi_devqualifier_to_str(inq->qualifier));
printf("Peripheral Device Type:%s\n",
scsi_devtype_to_str(inq->device_type));
printf("Page Code:(0x%02x) %s\n",
inq->pagecode, scsi_inquiry_pagecode_to_str(inq->pagecode));
for (i=0, dev = inq->designators; dev; i++, dev = dev->next) {
printf("DEVICE DESIGNATOR #%d\n", i);
if (dev->piv != 0) {
printf("Device Protocol Identifier:(%d) %s\n", dev->protocol_identifier, scsi_protocol_identifier_to_str(dev->protocol_identifier));
}
printf("Code Set:(%d) %s\n", dev->code_set, scsi_codeset_to_str(dev->code_set));
printf("PIV:%d\n", dev->piv);
printf("Association:(%d) %s\n", dev->association, scsi_association_to_str(dev->association));
printf("Designator Type:(%d) %s\n", dev->designator_type, scsi_designator_type_to_str(dev->designator_type));
printf("Designator:[%s]\n", dev->designator);
}
}
void inquiry_unit_serial_number(struct scsi_inquiry_unit_serial_number *inq)
{
printf("Unit Serial Number:[%s]\n", inq->usn);
}
void inquiry_supported_pages(struct scsi_inquiry_supported_pages *inq)
{
int i;
for (i = 0; i < inq->num_pages; i++) {
printf("Page:0x%02x %s\n", inq->pages[i], scsi_inquiry_pagecode_to_str(inq->pages[i]));
}
}
void inquiry_standard(struct scsi_inquiry_standard *inq)
{
int i;
printf("Peripheral Qualifier:%s\n",
scsi_devqualifier_to_str(inq->qualifier));
printf("Peripheral Device Type:%s\n",
scsi_devtype_to_str(inq->device_type));
printf("Removable:%d\n", inq->rmb);
printf("Version:%d %s\n", inq->version, scsi_version_to_str(inq->version));
printf("NormACA:%d\n", inq->normaca);
printf("HiSup:%d\n", inq->hisup);
printf("ReponseDataFormat:%d\n", inq->response_data_format);
printf("SCCS:%d\n", inq->sccs);
printf("ACC:%d\n", inq->acc);
printf("TPGS:%d\n", inq->tpgs);
printf("3PC:%d\n", inq->threepc);
printf("Protect:%d\n", inq->protect);
printf("EncServ:%d\n", inq->encserv);
printf("MultiP:%d\n", inq->multip);
printf("SYNC:%d\n", inq->sync);
printf("CmdQue:%d\n", inq->cmdque);
printf("Vendor:%s\n", inq->vendor_identification);
printf("Product:%s\n", inq->product_identification);
printf("Revision:%s\n", inq->product_revision_level);
for (i = 0; i < 8; i++) {
if (inq->version_descriptor[i] == 0) {
continue;
}
printf("Version Descriptor:%04x %s\n",
inq->version_descriptor[i],
scsi_version_descriptor_to_str(
inq->version_descriptor[i]));
}
}
void do_inquiry(struct iscsi_context *iscsi, int lun, int evpd, int pc)
{
struct scsi_task *task;
int full_size;
void *inq;
/* See how big this inquiry data is */
task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "Inquiry command failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
full_size = scsi_datain_getfullsize(task);
if (full_size > task->datain.size) {
scsi_free_scsi_task(task);
/* we need more data for the full list */
if ((task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size)) == NULL) {
fprintf(stderr, "Inquiry command failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
inq = scsi_datain_unmarshall(task);
if (inq == NULL) {
fprintf(stderr, "failed to unmarshall inquiry datain blob\n");
exit(10);
}
if (evpd == 0) {
inquiry_standard(inq);
} else {
switch (pc) {
case SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES:
inquiry_supported_pages(inq);
break;
case SCSI_INQUIRY_PAGECODE_UNIT_SERIAL_NUMBER:
inquiry_unit_serial_number(inq);
break;
case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION:
inquiry_device_identification(inq);
break;
case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
inquiry_block_limits(inq);
break;
case SCSI_INQUIRY_PAGECODE_BLOCK_DEVICE_CHARACTERISTICS:
inquiry_block_device_characteristics(inq);
break;
case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
inquiry_logical_block_provisioning(inq);
break;
default:
fprintf(stderr, "Usupported pagecode:0x%02x\n", pc);
}
}
scsi_free_scsi_task(task);
}
void print_usage(void)
{
fprintf(stderr, "Usage: iscsi-inq [-?] [-?|--help] [--usage] [-i|--initiator-name=iqn-name]\n"
"\t\t[-e|--evpd=integer] [-c|--pagecode=integer] <iscsi-url>\n");
}
void print_help(void)
{
fprintf(stderr, "Usage: iscsi-inq [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -e, --evpd=integer evpd\n");
fprintf(stderr, " -c, --pagecode=integer page code\n");
fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n");
fprintf(stderr, " --usage Display brief usage message\n");
fprintf(stderr, "\n");
fprintf(stderr, "iSCSI URL format : %s\n", ISCSI_URL_SYNTAX);
fprintf(stderr, "\n");
fprintf(stderr, "<host> is either of:\n");
fprintf(stderr, " \"hostname\" iscsi.example\n");
fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n");
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
}
int main(int argc, char *argv[])
{
struct iscsi_context *iscsi;
const char *url = NULL;
struct iscsi_url *iscsi_url = NULL;
int evpd = 0, pagecode = 0;
int show_help = 0, show_usage = 0, debug = 0;
int c;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"usage", no_argument, NULL, 'u'},
{"debug", no_argument, NULL, 'd'},
{"initiator_name", required_argument, NULL, 'i'},
{"evpd", required_argument, NULL, 'e'},
{"pagecode", required_argument, NULL, 'c'},
{0, 0, 0, 0}
};
int option_index;
while ((c = getopt_long(argc, argv, "h?udi:e:c:", long_options,
&option_index)) != -1) {
switch (c) {
case 'h':
case '?':
show_help = 1;
break;
case 'u':
show_usage = 1;
break;
case 'd':
debug = 1;
break;
case 'i':
initiator = optarg;
break;
case 'e':
evpd = atoi(optarg);
break;
case 'c':
pagecode = atoi(optarg);
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
}
}
if (show_help != 0) {
print_help();
exit(0);
}
if (show_usage != 0) {
print_usage();
exit(0);
}
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
fprintf(stderr, "Failed to create context\n");
exit(10);
}
if (debug > 0) {
iscsi_set_log_level(iscsi, debug);
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
}
if (argv[optind] != NULL) {
url = strdup(argv[optind]);
}
if (url == NULL) {
fprintf(stderr, "You must specify the URL\n");
print_usage();
exit(10);
}
iscsi_url = iscsi_parse_full_url(iscsi, url);
if (url) {
free(discard_const(url));
}
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
exit(10);
}
iscsi_set_targetname(iscsi, iscsi_url->target);
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_url->user[0]) {
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
exit(10);
}
}
if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
fprintf(stderr, "Login Failed. %s\n", iscsi_get_error(iscsi));
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
exit(10);
}
do_inquiry(iscsi, iscsi_url->lun, evpd, pagecode);
iscsi_destroy_url(iscsi_url);
iscsi_logout_sync(iscsi);
iscsi_destroy_context(iscsi);
return 0;
}

452
utils/iscsi-ls.c Normal file
View File

@@ -0,0 +1,452 @@
/*
Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
#ifndef discard_const
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#endif
int showluns;
int useurls;
const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-ls";
struct client_state {
int finished;
int status;
int lun;
int type;
const char *username;
const char *password;
};
void event_loop(struct iscsi_context *iscsi, struct client_state *state)
{
struct pollfd pfd;
while (state->finished == 0) {
pfd.fd = iscsi_get_fd(iscsi);
pfd.events = iscsi_which_events(iscsi);
if (poll(&pfd, 1, -1) < 0) {
fprintf(stderr, "Poll failed");
exit(10);
}
if (iscsi_service(iscsi, pfd.revents) < 0) {
fprintf(stderr, "iscsi_service failed with : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
}
void show_lun(struct iscsi_context *iscsi, int lun)
{
struct scsi_task *task;
struct scsi_inquiry_standard *inq;
int type, no_media;
long long size = 0;
int size_pf = 0;
static const char sf[] = {' ', 'k', 'M', 'G', 'T' };
/* check we can talk to the lun */
tur_try_again:
if ((task = iscsi_testunitready_sync(iscsi, lun)) == NULL) {
fprintf(stderr, "testunitready failed\n");
exit(10);
}
if (task->status == SCSI_STATUS_CHECK_CONDITION) {
if (task->sense.key == SCSI_SENSE_UNIT_ATTENTION && task->sense.ascq == SCSI_SENSE_ASCQ_BUS_RESET) {
scsi_free_scsi_task(task);
goto tur_try_again;
}
}
no_media = 0;
if (task->status == SCSI_STATUS_CHECK_CONDITION
&& task->sense.key == SCSI_SENSE_NOT_READY
&& task->sense.ascq == SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT) {
/* not an error, just a cdrom without a disk most likely */
no_media = 1;
} else if (task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "TESTUNITREADY failed with %s\n", iscsi_get_error(iscsi));
exit(10);
}
scsi_free_scsi_task(task);
/* check what type of lun we have */
task = iscsi_inquiry_sync(iscsi, lun, 0, 0, 64);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "failed to send inquiry command : %s\n", iscsi_get_error(iscsi));
exit(10);
}
inq = scsi_datain_unmarshall(task);
if (inq == NULL) {
fprintf(stderr, "failed to unmarshall inquiry datain blob\n");
exit(10);
}
type = inq->device_type;
scsi_free_scsi_task(task);
if (type == SCSI_INQUIRY_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS) {
struct scsi_readcapacity10 *rc10;
task = iscsi_readcapacity10_sync(iscsi, lun, 0, 0);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr, "failed to send readcapacity command\n");
exit(10);
}
rc10 = scsi_datain_unmarshall(task);
if (rc10 == NULL) {
fprintf(stderr, "failed to unmarshall readcapacity10 data\n");
exit(10);
}
size = rc10->block_size;
size *= rc10->lba;
for (size_pf=0; size_pf<4 && size > 1024; size_pf++) {
size /= 1024;
}
scsi_free_scsi_task(task);
}
printf("Lun:%-4d Type:%s", lun, scsi_devtype_to_str(type));
if (type == SCSI_INQUIRY_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS) {
printf(" (Size:%lld%c)", size, sf[size_pf]);
}
if (no_media) {
printf(" (No media loaded)");
}
printf("\n");
}
void list_luns(struct client_state *clnt, const char *target, const char *portal)
{
struct iscsi_context *iscsi;
struct scsi_task *task;
struct scsi_reportluns_list *list;
int full_report_size;
int i;
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
printf("Failed to create context\n");
exit(10);
}
if (clnt->username != NULL) {
if (iscsi_set_initiator_username_pwd(iscsi, clnt->username, clnt->password) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
exit(10);
}
}
if (iscsi_set_targetname(iscsi, target)) {
fprintf(stderr, "Failed to set target name\n");
exit(10);
}
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_connect_sync(iscsi, portal) != 0) {
printf("iscsi_connect failed. %s\n", iscsi_get_error(iscsi));
exit(10);
}
if (iscsi_login_sync(iscsi) != 0) {
fprintf(stderr, "login failed :%s\n", iscsi_get_error(iscsi));
exit(10);
}
/* get initial reportluns data, all targets can report 16 bytes but some
* fail if we ask for too much.
*/
if ((task = iscsi_reportluns_sync(iscsi, 0, 16)) == NULL) {
fprintf(stderr, "reportluns failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
full_report_size = scsi_datain_getfullsize(task);
if (full_report_size > task->datain.size) {
scsi_free_scsi_task(task);
/* we need more data for the full list */
if ((task = iscsi_reportluns_sync(iscsi, 0, full_report_size)) == NULL) {
fprintf(stderr, "reportluns failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
list = scsi_datain_unmarshall(task);
if (list == NULL) {
fprintf(stderr, "failed to unmarshall reportluns datain blob\n");
exit(10);
}
for (i=0; i < (int)list->num; i++) {
show_lun(iscsi, list->luns[i]);
}
scsi_free_scsi_task(task);
iscsi_destroy_context(iscsi);
}
void discoverylogout_cb(struct iscsi_context *iscsi, int status, void *command_data _U_, void *private_data)
{
struct client_state *state = (struct client_state *)private_data;
if (status != 0) {
fprintf(stderr, "Failed to logout from target. : %s\n", iscsi_get_error(iscsi));
exit(10);
}
if (iscsi_disconnect(iscsi) != 0) {
fprintf(stderr, "Failed to disconnect old socket\n");
exit(10);
}
state->finished = 1;
}
void discovery_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
{
struct iscsi_discovery_address *addr;
if (status != 0) {
fprintf(stderr, "Failed to do discovery on target. : %s\n", iscsi_get_error(iscsi));
exit(10);
}
for(addr=command_data; addr; addr=addr->next) {
struct iscsi_target_portal *portal = addr->portals;
while (portal != NULL) {
if (useurls == 1 && showluns == 0) {
printf("iscsi://%s/%s/0\n", portal->portal, addr->target_name);
} else {
printf("Target:%s Portal:%s\n", addr->target_name, portal->portal);
}
if (showluns != 0) {
list_luns(private_data, addr->target_name, portal->portal);
}
portal = portal->next;
}
}
if (iscsi_logout_async(iscsi, discoverylogout_cb, private_data) != 0) {
fprintf(stderr, "iscsi_logout_async failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
void discoverylogin_cb(struct iscsi_context *iscsi, int status, void *command_data _U_, void *private_data)
{
if (status != 0) {
fprintf(stderr, "Login failed. %s\n", iscsi_get_error(iscsi));
exit(10);
}
if (iscsi_discovery_async(iscsi, discovery_cb, private_data) != 0) {
fprintf(stderr, "failed to send discovery command : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
void discoveryconnect_cb(struct iscsi_context *iscsi, int status, void *command_data _U_, void *private_data)
{
if (status != 0) {
fprintf(stderr, "discoveryconnect_cb: connection failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
if (iscsi_login_async(iscsi, discoverylogin_cb, private_data) != 0) {
fprintf(stderr, "iscsi_login_async failed : %s\n", iscsi_get_error(iscsi));
exit(10);
}
}
void print_usage(void)
{
fprintf(stderr, "Usage: iscsi-ls [-?|--help] [--usage] [-i|--initiator-name=iqn-name]\n"
"\t\t[-s|--show-luns] <iscsi-portal-url>\n");
}
void print_help(void)
{
fprintf(stderr, "Usage: iscsi-ls [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -s, --show-luns Show the luns for each target\n");
fprintf(stderr, " --url Output targets in URL format\n");
fprintf(stderr, " (does not work with -s)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n");
fprintf(stderr, " --usage Display brief usage message\n");
fprintf(stderr, "\n");
fprintf(stderr, "iSCSI Portal URL format : %s\n", ISCSI_PORTAL_URL_SYNTAX);
fprintf(stderr, "\n");
fprintf(stderr, "<host> is either of:\n");
fprintf(stderr, " \"hostname\" iscsi.example\n");
fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n");
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
}
int main(int argc, char *argv[])
{
struct iscsi_context *iscsi;
struct iscsi_url *iscsi_url = NULL;
struct client_state state;
const char *url = NULL;
int c;
static int show_help = 0, show_usage = 0, debug = 0;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"usage", no_argument, NULL, 'u'},
{"debug", no_argument, NULL, 'd'},
{"show-luns", no_argument, NULL, 's'},
{"url", no_argument, NULL, 'U'},
{"initiator_name", required_argument, NULL, 'i'},
{0, 0, 0, 0}
};
int option_index;
while ((c = getopt_long(argc, argv, "h?uUdi:s", long_options,
&option_index)) != -1) {
switch (c) {
case 'h':
case '?':
show_help = 1;
break;
case 'u':
show_usage = 1;
break;
case 'U':
useurls = 1;
break;
case 'd':
debug = 1;
break;
case 'i':
initiator = optarg;
break;
case 's':
showluns = 1;
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
}
}
if (show_help != 0) {
print_help();
exit(0);
}
if (show_usage != 0) {
print_usage();
exit(0);
}
if (optind != argc -1) {
print_usage();
exit(0);
}
memset(&state, 0, sizeof(state));
if (argv[optind] != NULL) {
url = strdup(argv[optind]);
}
if (url == NULL) {
fprintf(stderr, "You must specify iscsi target portal.\n");
print_usage();
exit(10);
}
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
printf("Failed to create context\n");
exit(10);
}
if (debug > 0) {
iscsi_set_log_level(iscsi, debug);
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
}
iscsi_url = iscsi_parse_portal_url(iscsi, url);
if (url) {
free(discard_const(url));
}
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
exit(10);
}
iscsi_set_session_type(iscsi, ISCSI_SESSION_DISCOVERY);
if (iscsi_url->user[0]) {
state.username = iscsi_url->user;
state.password = iscsi_url->passwd;
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
exit(10);
}
}
if (iscsi_connect_async(iscsi, iscsi_url->portal, discoveryconnect_cb, &state) != 0) {
fprintf(stderr, "iscsi_connect failed. %s\n", iscsi_get_error(iscsi));
exit(10);
}
event_loop(iscsi, &state);
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
return 0;
}

View File

@@ -0,0 +1,205 @@
/*
Copyright (C) 2012 by Peter Lieven <pl@kamp.de>
Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <poll.h>
#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
#ifndef discard_const
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#endif
const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-readcapacity16";
void print_usage(void)
{
fprintf(stderr, "Usage: iscsi-readcapacity16 [-?] [-?|--help] [--usage] [-i|--initiator-name=iqn-name] [-s] <iscsi-url>\n");
}
void print_help(void)
{
fprintf(stderr, "Usage: iscsi_readcapacity16 [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -s, --size print target size only\n");
fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n");
fprintf(stderr, " --usage Display brief usage message\n");
fprintf(stderr, "\n");
fprintf(stderr, "iSCSI URL format : %s\n", ISCSI_URL_SYNTAX);
fprintf(stderr, "\n");
fprintf(stderr, "<host> is either of:\n");
fprintf(stderr, " \"hostname\" iscsi.example\n");
fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n");
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
}
int main(int argc, char *argv[])
{
struct iscsi_context *iscsi;
const char *url = NULL;
struct iscsi_url *iscsi_url = NULL;
int show_help = 0, show_usage = 0, debug = 0, size_only=0;
int c;
struct scsi_task *task;
struct scsi_readcapacity16 *rc16;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"usage", no_argument, NULL, 'u'},
{"debug", no_argument, NULL, 'd'},
{"size", no_argument, NULL, 's'},
{"initiator_name", required_argument, NULL, 'i'},
{"evpd", required_argument, NULL, 'e'},
{"pagecode", required_argument, NULL, 'c'},
{0, 0, 0, 0}
};
int option_index;
while ((c = getopt_long(argc, argv, "h?udi:s", long_options,
&option_index)) != -1) {
switch (c) {
case 'h':
case '?':
show_help = 1;
break;
case 'u':
show_usage = 1;
break;
case 's':
size_only = 1;
break;
case 'd':
debug = 1;
break;
case 'i':
initiator = optarg;
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
}
}
if (show_help != 0) {
print_help();
exit(0);
}
if (show_usage != 0) {
print_usage();
exit(0);
}
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
fprintf(stderr, "Failed to create context\n");
exit(10);
}
if (debug > 0) {
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
iscsi_set_log_level(iscsi, debug);
}
if (argv[optind] != NULL) {
url = strdup(argv[optind]);
}
if (url == NULL) {
fprintf(stderr, "You must specify the URL\n");
print_usage();
exit(10);
}
iscsi_url = iscsi_parse_full_url(iscsi, url);
if (url) {
free(discard_const(url));
}
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
exit(10);
}
iscsi_set_targetname(iscsi, iscsi_url->target);
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_url->user[0]) {
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
exit(10);
}
}
if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
fprintf(stderr, "Login Failed. %s\n", iscsi_get_error(iscsi));
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
exit(10);
}
task = iscsi_readcapacity16_sync(iscsi, iscsi_url->lun);
if (task == NULL || task->status != SCSI_STATUS_GOOD) {
fprintf(stderr,"failed to send readcapacity command\n");
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
exit(10);
}
rc16 = scsi_datain_unmarshall(task);
if (rc16 == NULL) {
fprintf(stderr,"failed to unmarshall readcapacity16 data\n");
scsi_free_scsi_task(task);
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
exit(10);
}
if (!size_only) {
printf("RETURNED LOGICAL BLOCK ADDRESS:%" PRIu64 "\n", rc16->returned_lba);
printf("LOGICAL BLOCK LENGTH IN BYTES:%u\n", rc16->block_length);
printf("P_TYPE:%d PROT_EN:%d\n", rc16->p_type, rc16->prot_en);
printf("P_I_EXPONENT:%d LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT:%d\n", rc16->p_i_exp, rc16->lbppbe);
printf("LBPME:%d LBPRZ:%d\n", rc16->lbpme, rc16->lbprz);
printf("LOWEST ALIGNED LOGICAL BLOCK ADDRESS:%d\n", rc16->lalba);
printf("Total size:%" PRIu64 "\n", rc16->block_length * (rc16->returned_lba + 1));
}
else
{
printf("%" PRIu64 "\n", rc16->block_length * (rc16->returned_lba + 1));
}
scsi_free_scsi_task(task);
iscsi_destroy_url(iscsi_url);
iscsi_logout_sync(iscsi);
iscsi_destroy_context(iscsi);
return 0;
}

253
utils/iscsi-swp.c Normal file
View File

@@ -0,0 +1,253 @@
/*
Copyright (C) 2013 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
#ifndef discard_const
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#endif
const char *initiator = "iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-swp";
void print_usage(void)
{
fprintf(stderr, "Usage: iscsi-swp [-?] [-?|--help] [--usage] [-i|--initiator-name=iqn-name]\n"
"\t\t[-s|--swp {on|off}] <iscsi-url>\n");
}
void print_help(void)
{
fprintf(stderr, "Usage: iscsi-swp [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -s, --swp={on|off} Turn software write protect on/off\n");
fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n");
fprintf(stderr, " --usage Display brief usage message\n");
fprintf(stderr, "\n");
fprintf(stderr, "iSCSI URL format : %s\n", ISCSI_URL_SYNTAX);
fprintf(stderr, "\n");
fprintf(stderr, "<host> is either of:\n");
fprintf(stderr, " \"hostname\" iscsi.example\n");
fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n");
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
}
int main(int argc, char *argv[])
{
struct iscsi_context *iscsi;
const char *url = NULL;
struct iscsi_url *iscsi_url = NULL;
int show_help = 0, show_usage = 0, debug = 0;
int c;
int ret = 0;
int swp = 0;
struct scsi_task *sense_task = NULL;
struct scsi_task *select_task = NULL;
struct scsi_mode_sense *ms;
struct scsi_mode_page *mp;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"usage", no_argument, NULL, 'u'},
{"debug", no_argument, NULL, 'd'},
{"initiator_name", required_argument, NULL, 'i'},
{"swp", required_argument, NULL, 's'},
{0, 0, 0, 0}
};
int option_index;
while ((c = getopt_long(argc, argv, "h?udi:s:", long_options,
&option_index)) != -1) {
switch (c) {
case 'h':
case '?':
show_help = 1;
break;
case 'u':
show_usage = 1;
break;
case 'd':
debug = 1;
break;
case 'i':
initiator = optarg;
break;
case 's':
if (!strcmp(optarg, "on") || !strcmp(optarg, "ON")) {
swp = 1;
}
if (!strcmp(optarg, "off") || !strcmp(optarg, "OFF")) {
swp = 2;
}
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
}
}
if (show_help != 0) {
print_help();
exit(0);
}
if (show_usage != 0) {
print_usage();
exit(0);
}
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
fprintf(stderr, "Failed to create context\n");
exit(10);
}
if (debug > 0) {
iscsi_set_log_level(iscsi, debug);
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
}
if (argv[optind] != NULL) {
url = strdup(argv[optind]);
}
if (url == NULL) {
fprintf(stderr, "You must specify the URL\n");
print_usage();
ret = 10;
goto finished;
}
iscsi_url = iscsi_parse_full_url(iscsi, url);
free(discard_const(url));
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
iscsi_set_targetname(iscsi, iscsi_url->target);
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_url->user != NULL) {
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
ret = 10;
goto finished;
}
}
if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
fprintf(stderr, "Login Failed. %s\n", iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
sense_task = iscsi_modesense10_sync(iscsi, iscsi_url->lun,
0, 1, SCSI_MODESENSE_PC_CURRENT,
SCSI_MODEPAGE_CONTROL,
0, 255);
if (sense_task == NULL) {
printf("Failed to send MODE_SENSE10 command: %s\n",
iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
if (sense_task->status != SCSI_STATUS_GOOD) {
printf("MODE_SENSE10 failed: %s\n",
iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
ms = scsi_datain_unmarshall(sense_task);
if (ms == NULL) {
printf("failed to unmarshall mode sense datain blob\n");
ret = 10;
goto finished;
}
mp = scsi_modesense_get_page(ms, SCSI_MODEPAGE_CONTROL, 0);
if (mp == NULL) {
printf("failed to read control mode page\n");
ret = 10;
goto finished;
}
printf("SWP:%d\n", mp->control.swp);
switch (swp) {
case 1:
mp->control.swp = 1;
break;
case 2:
mp->control.swp = 0;
break;
default:
goto finished;
}
printf("Turning SWP %s\n", (swp == 1) ? "ON" : "OFF");
select_task = iscsi_modeselect10_sync(iscsi, iscsi_url->lun,
1, 0, mp);
if (select_task == NULL) {
printf("Failed to send MODE_SELECT10 command: %s\n",
iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
if (select_task->status != SCSI_STATUS_GOOD) {
printf("MODE_SELECT10 failed: %s\n",
iscsi_get_error(iscsi));
ret = 10;
goto finished;
}
finished:
if (sense_task != NULL) {
scsi_free_scsi_task(sense_task);
}
if (select_task != NULL) {
scsi_free_scsi_task(select_task);
}
if (iscsi_url != NULL) {
iscsi_destroy_url(iscsi_url);
}
iscsi_logout_sync(iscsi);
iscsi_destroy_context(iscsi);
return ret;
}