Commit c019a5ac authored by Mike Hibler's avatar Mike Hibler

Argh! FreeNAS changed the API again.

This time between 9.3-release and 9.3-stable. Setting up an iSCSI target
has changed. Previously everything was done with the target resource.
Now you have to use both targetgroup and target...
parent efc23cc2
......@@ -206,17 +206,30 @@ sub extents()
}
#
# Print uninterpreted iSCSI target info.
# Print semi-interpreted iSCSI target info.
#
# XXX At some point between 9.3 stable releases, they changed most of the
# target info to be associated with a target group instead. We just recouple
# them here.
#
sub targets()
{
my $airef = freenasTargetList(0);
foreach my $ai (keys %{$airef}) {
foreach my $key ("id", "name", "alias", "serial", "portalgroup", "authgroup", "authtype", "initiatorgroup") {
my $val = $airef->{$ai}->{$key};
my $tref = freenasTargetList(0);
my $tgref = freenasTargetGroupList(1);
foreach my $t (keys %{$tref}) {
foreach my $key ("id", "name", "alias") {
my $val = $tref->{$t}->{$key};
print "$key=$val "
if (defined($val));
}
if (exists($tgref->{$t})) {
my $tg = $tgref->{$t};
foreach my $key ("portalgroup", "initiatorgroup", "authgroup", "authtype", "initialdigest") {
my $val = $tg->{$key};
print "$key=$val "
if (defined($val));
}
}
print "\n";
}
......@@ -230,7 +243,7 @@ sub assocs()
{
my $aref = freenasAssocList();
foreach my $a (keys %{$aref}) {
foreach my $key ("id", "target", "target_name", "extent", "extent_name") {
foreach my $key ("id", "target", "target_name", "target_group", "extent", "extent_name") {
my $val = $aref->{$a}->{$key};
print "$key=$val "
if (defined($val));
......
......@@ -46,7 +46,7 @@ use Exporter;
qw(
freenasPoolList freenasVolumeList freenasSliceList
freenasAuthInitList freenasExtentList freenasTargetList
freenasAssocList
freenasTargetGroupList freenasAssocList
freenasVolumeCreate freenasVolumeDestroy freenasFSCreate
freenasVolumeSnapshot freenasVolumeClone
freenasVolumeDesnapshot freenasVolumeDeclone
......@@ -60,6 +60,7 @@ use Exporter;
$FREENAS_API_RESOURCE_IFACE $FREENAS_API_RESOURCE_IST_EXTENT
$FREENAS_API_RESOURCE_IST_AUTHI $FREENAS_API_RESOURCE_IST_TARGET
$FREENAS_API_RESOURCE_IST_TGTGROUP
$FREENAS_API_RESOURCE_IST_ASSOC $FREENAS_API_RESOURCE_VLAN
$FREENAS_API_RESOURCE_VOLUME $FREENAS_API_RESOURCE_SNAPSHOT
);
......@@ -88,6 +89,7 @@ our $FREENAS_API_RESOURCE_IFACE = "network/interface";
our $FREENAS_API_RESOURCE_IST_EXTENT = "services/iscsi/extent";
our $FREENAS_API_RESOURCE_IST_AUTHI = "services/iscsi/authorizedinitiator";
our $FREENAS_API_RESOURCE_IST_TARGET = "services/iscsi/target";
our $FREENAS_API_RESOURCE_IST_TGTGROUP= "services/iscsi/targetgroup";
our $FREENAS_API_RESOURCE_IST_ASSOC = "services/iscsi/targettoextent";
our $FREENAS_API_RESOURCE_VLAN = "network/vlan";
our $FREENAS_API_RESOURCE_VOLUME = "storage/volume";
......@@ -1097,6 +1099,40 @@ sub freenasTargetList($) {
return $thash;
}
#
# Return list of target groups
#
sub freenasTargetGroupList($) {
my ($byname) = @_;
my $thash = {};
my $tinfo = freenasRequest($FREENAS_API_RESOURCE_IST_TGTGROUP,
"GET", { "limit" => 0 });
foreach my $t (@$tinfo) {
# XXX shorten the names
foreach my $key (keys %{$t}) {
# XXX groups do nt have names, name them by target id
if ($key eq "iscsi_target") {
$t->{'name'} = $t->{$key};
delete $t->{$key};
}
elsif ($key =~ /^iscsi_target_(.*)/) {
if (!exists($t->{$1})) {
$t->{$1} = $t->{$key};
delete $t->{$key};
}
}
}
if ($byname) {
$thash->{$t->{'name'}} = $t;
} else {
$thash->{$t->{'id'}} = $t;
}
}
return $thash;
}
#
# Return list of associations.
# If getnames is non-zero, we resolve the extent/target indicies into names.
......@@ -1118,6 +1154,7 @@ sub freenasAssocList() {
#
my $einfo = freenasExtentList(0);
my $tinfo = freenasTargetList(0);
my $tginfo = freenasTargetGroupList(1);
foreach my $a (@$ainfo) {
# XXX shorten the names
......@@ -1139,6 +1176,11 @@ sub freenasAssocList() {
}
}
}
# XXX associate with the target group too
if (exists($a->{'target'}) && exists($tginfo->{$a->{'target'}})) {
$a->{'target_group'} = $tginfo->{$a->{'target'}}->{'id'};
}
$ahash->{$a->{'id'}} = $a;
}
......
......@@ -832,14 +832,13 @@ sub exportSlice($$$$) {
# This is not just an API thing, you cannot do it through the
# GUI either!
#
my $serial = genSerial();
# XXX ugh2, this used to all be done with iscsi/target, but now
# they have broken it into two pieces: create the target, create
# a target group.
#
$res = freenasRequest($FREENAS_API_RESOURCE_IST_TARGET,
"POST", undef,
{"iscsi_target_name" => $iqn,
"iscsi_target_portalgroup" => $ISCSI_GLOBAL_PORTAL,
"iscsi_target_initiatorgroup" => $tag,
"iscsi_target_authtype" => "None",
"iscsi_target_serial" => $serial});
{"iscsi_target_name" => $iqn});
if (!$res) {
warn("*** ERROR: blockstore_exportSlice: $volname: ".
"Failed to create iSCSI target: $@");
......@@ -852,6 +851,19 @@ sub exportSlice($$$$) {
}
my $tindex = $res->{'id'};
$res = freenasRequest($FREENAS_API_RESOURCE_IST_TGTGROUP,
"POST", undef,
{"iscsi_target" => $tindex,
"iscsi_target_initiatorgroup" => $tag,
"iscsi_target_portalgroup" => $ISCSI_GLOBAL_PORTAL,
"iscsi_target_authgroup" => undef,
"iscsi_target_authtype" => "None"});
if (!$res) {
warn("*** ERROR: blockstore_exportSlice: $volname: ".
"Failed to create iSCSI targetgroup: $@");
return -1;
}
# Bind iSCSI target to slice (extent)
$res = freenasRequest($FREENAS_API_RESOURCE_IST_ASSOC,
"POST", undef,
......@@ -1302,7 +1314,7 @@ sub unexportSlice($$$$) {
# If there are no networks left in the authorized initiators list, then
# we are completely done with this export - tear it down!
if (!@pruned_networks) {
my ($associd,$targetid,$extentid,$msg);
my ($associd,$targetid,$extentid,$tgroupid,$msg);
my $assocs = freenasAssocList();
foreach my $aid (keys %$assocs) {
if (exists($assocs->{$aid}->{'target_name'}) &&
......@@ -1312,6 +1324,7 @@ sub unexportSlice($$$$) {
$associd = $aid;
$targetid = $assocs->{$aid}->{'target'};
$extentid = $assocs->{$aid}->{'extent'};
$tgroupid = $assocs->{$aid}->{'target_group'};
last;
}
}
......@@ -1325,6 +1338,14 @@ sub unexportSlice($$$$) {
"Failed to disassociate iSCSI target with extent:\n$msg");
}
# Remove target group.
if (!$tgroupid ||
!freenasRequest("$FREENAS_API_RESOURCE_IST_TGTGROUP/$tgroupid",
"DELETE", undef, undef, undef, \$msg)) {
warn("*** WARNING: blockstore_unexportSlice: $volname: ".
"Failed to remove iSCSI target group:\n$msg");
}
# Remove iSCSI target.
if (!$targetid ||
!freenasRequest("$FREENAS_API_RESOURCE_IST_TARGET/$targetid",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment