#!/usr/local/bin/perl
use strict;
use XML::Simple;
use Getopt::Long;
use Text::ParseWords;
#use Log::Any '$log';
#use Log::Any::Adapter ('File', '/var/log/comstar.log');
my $config = "/etc/ctl.conf";
my $target="iqn.2014-06.org.freebsd:target0";
#$log->info("Start with arguments: @ARGV");
open (P,"/usr/sbin/ctladm devlist -x |")||die "Cannot execute: $!";
my $output = join "", <P>;
close(P);
my $luns = XMLin($output, ForceArray => 1, KeyAttr => { lun=>'id'} );
my $command = shift @ARGV;
if ( $command eq "list-lu" ){
my $key;
print " GUID DATA SIZE SOURCE\n";
print "-------------------------------- ------------------- ----------------\n";
foreach $key(keys %{$luns->{lun}})
{
my @lun = quotewords(",", 0, $luns->{lun}->{$key}->{ctld_name}[0]);
if ($lun[0] eq $target)
{
$luns->{lun}->{$key}->{file}[0] =~ s/dev\/zvol/dev\/zvol\/rdsk/;
print sprintf "%32u %20u %s\n", $key, $luns->{lun}->{$key}->{blocksize}[0]*$luns->{lun}->{$key}->{size}[0], $luns->{lun}->{$key}->{file}[0];
}
}
}
elsif ( $command eq "list-view" )
{
my $id = shift @ARGV;
$id = shift @ARGV;
my @lun = quotewords(",", 0, $luns->{lun}->{$id}->{ctld_name}[0]);
print "View Entry: 0\n Host group : All\n Target group : All\n";
print " LUN : " . $lun[2] . "\n";
}
elsif ( $command eq "delete-lu" )
{
my $id = shift @ARGV;
my $find=0;
open(IN_CONF,$config);
my @file_content = <IN_CONF>;
close(IN_CONF);
my @lun = quotewords(",", 0, $luns->{lun}->{$id}->{ctld_name}[0]);
open(OUT_CONF,">$config");
foreach (@file_content)
{
if ($_ eq "#PROXMOX\n")
{
$find = 1;
}
if ($_ ne "lun " . $lun[2] . " {path " . "$luns->{lun}->{$id}->{file}[0]" . "}\n" )#|| !$find )
{
print OUT_CONF;
}
}
close(OUT_CONF);
my $command = "/usr/sbin/ctladm remove -b block -l " . $id;
system('service ctld forcereload');
}
elsif ( $command eq "create-lu" || $command eq "import-lu" )
{
my $filepath = shift @ARGV;
if ( $command eq "create-lu" )
{
$filepath = shift @ARGV;
$filepath = shift @ARGV;
$filepath = shift @ARGV;
$filepath = shift @ARGV;
}
$filepath =~ s/dev\/zvol\/rdsk/dev\/zvol/;
my $key;
my $lun_id=0;
my $find;
my @lun;
while(1)
{
$lun_id = $lun_id + 1;
$find=0;
foreach $key(keys %{$luns->{lun}})
{
@lun = quotewords(",", 0, $luns->{lun}->{$key}->{ctld_name}[0]);
if ( $lun_id == $lun[2] && $lun[0] eq $target )
{
$find=1;
last;
}
}
if ( !$find ) {last;}
};
open(IN_CONF,$config);
my @file_content = <IN_CONF>;
close(IN_CONF);
open(OUT_CONF,">$config");
foreach (@file_content)
{
if ($_ eq "#PROXMOX\n")
{
print OUT_CONF;
print OUT_CONF "lun " . $lun_id . " {path " . $filepath . "}\n";
} else
{
print OUT_CONF;
}
}
close(OUT_CONF);
my $command = "/usr/sbin/ctladm create -b block -o file=" . $filepath . " -o cfiscsi_lun=". $lun_id . " -o cfiscsi_target=". $target;
# system($command);
system('service ctld forcereload');
}
elsif ( $command eq "modify-lu" )
{
my $size = shift @ARGV;
$size = shift @ARGV;
my $id = shift @ARGV;
my $command = "/usr/sbin/ctladm modify -b block -l " . $id . " -s ". $size;
system($command);
}