# delVolume.pl
#
# by Vijayababu Dandi
# Version 1.0
# Feb 27, 2008
##############################################################################################
# Perl must be installed and running on the Linux server where this utility will be used.
# NSS must be installed to get the VFS support.
# (The SDK for this API set can be downloaded from http://developer.novell.com/wiki/index.php/# Virtual_File_Services_for_NetWare.)
##############################################################################################
#This tool can be used to delete:
# 1.Specified Volume
# 2.Volumes created on particular pool
# 3.All Volumes on OES2 Linux server.
#
##############################################################################################
sub lowerToUpper($)
{
my $string = $_[0];
$string =~ tr/[a-z]/[A-Z]/;
return $string;
}
sub SetDataStream($*)
{
my $fh = $_[0];
my $datastream = $_[1];
my $command;
my $result;
my $error;
$command = "";
seek $fh, 0, 0;
if (!syswrite($fh, $command, length($command)))
{
$result .= "Unable to send datastream command to NDS management. ";
seek $fh, 0, 0;
if (sysread($fh,$error, 1000))
{
$result .= $error;
}
$result .= "\n";
}
return $result;
}
sub WriteCommand($*)
{
my $fh = $_[0];
my $result;
my $reply;
my $error;
my $command = $_[1];
seek $fh, 0, 0;
if (!syswrite($fh, $command, length($command)))
{
$result .= "Unable to send command to virtual file. ";
seek $fh, 0, 0;
if (sysread($fh, $error, 1000))
{
$result .= $error;
}
$result .= "\n";
}
else
{
seek $fh, 0, 0;
my $readLen = 999;
while ($readLen)
{
$readLen = sysread($fh, $reply, 1000);
$result .= $reply;
}
}
return $result;
}
sub ListVolumes($*)
{
my $command = $_[0];
my $showdetails = $_[1];
my $result;
my @volume = ();
my $ret;
my $nssResult;
$command = "$command";
if (!($ret = SetDataStream(FILE, "command")))
{
$result .= $ret;
$nssResult = WriteCommand(FILE,"$command");
}
@volume = ParseVolume($nssResult);
return (@volume);
}
sub ListPools
{
my $result;
my $ret;
if (!($ret = SetDataStream(FILE, "command")))
{
$result .= $ret;
my $nssResult = WriteCommand(FILE,
"".
"");
$result .= ParseListPools($nssResult);
}
return $result;
}
sub ListAllVolumes
{
my $command = " ";
my @myresult;
my @xml;
if (!($ret = SetDataStream(FILE, "command")))
{
$myresult .= $ret;
my $nssResult = WriteCommand(FILE,$command);
@myresult = ParseVolumeAll($nssResult);
}
return @myresult;
}
sub ParseListPools($)
{
my $xml = $_[0];
my $result;
my @xml;
my $ret;
my $poolName;
my $description;
my $reporter;
my $type;
@xml = $xml =~ /(.*?)<\/poolSimpleInfo>/gs;
$ret ='';
foreach $result (@xml)
{
if ($result =~ //s)
{
if ($result =~ /(.*?)<\/description>/gs)
{
$description = $1;
}
if ($result =~ /(.*?)<\/reporter>/gs)
{
$reporter = $1;
}
if ($1 != 0)
{
return "-----------------------------------------------------------\n".
"Error No : $1\n".
"Description : $description\n".
"Reporter : $reporter\n";
}
}
$poolName = "";
$poolState = "";
if ($result =~ /(.*?)<\/poolName>/s)
{
$poolName = $1;
}
$ret .= "$poolName\n";
}
return $ret;
}
sub ParseVolume($)
{
my $xml = $_[0];
my $result;
my @xml;
my $ret;
my $volName = "";
my @volarray;
@xml = $xml =~ /(.*?)<\/getPoolInfo>/gs;
@xml = $xml =~ /(.*)<\/volumeInfo>/s;
@xml = split (/<\/volumeName>/,$xml);
foreach $result (@xml)
{
if ($result =~ //s)
{
if ($1 != 0)
{
return "Error $1 getting Volume Info\n";
}
}
if ($result =~ /(.*)/gs)
{
$volName = $1;
push(@volarray,$volName);
}
}
return @volarray;
}
sub ParseResult($$)
{
my $xml = $_[0];
my $name = $_[1];
my $result = "";
my @xml;
if (@xml = $xml =~ /(.*)<\/removeVolume>/s)
{
foreach $result (@xml)
{
if ($result =~ /(.*?)<\/description>/gs)
{
$description = $1;
}
if ($result =~ /(.*?)<\/reporter>/gs)
{
$reporter = $1;
}
if ($result =~ //s)
{
if ($1 != 0)
{
return " Error No :$1 \n".
" Description :$description \n".
" Reporter :$reporter \n";
}
else
{
return "-------------------------------------\n".
"volume $name successfully deleted\n".
"-------------------------------------\n";
}
}
}
}
else
{
return "Error parsing results of NSS operation\n";
}
}
sub ParseVolumeAll($)
{
my $xml = $_[0];
my $result;
my @xml;
my $ret;
my $volName = "";
my @volume = ();
@xml = $xml =~ /(.*?)<\/listVolumes>/gs;
@xml = split (/<\/volumeName>/,$xml);
foreach $result (@xml)
{
if ($result =~ //s)
{
if ($1 != 0)
{
return "Error $1 getting Volume Info\n";
}
}
if ($result =~ /(.*)/gs)
{
$volName = $1;
push(@volume,$volName);
}
}
return @volume;
}
sub RemoveVolume($)
{
my $result;
my $ret;
my $vol = $_[0];
my $remove = "";
print "Deleting Volume : $vol\n";
if (!($ret = SetDataStream(FILE, "command")))
{
my $nssResult = WriteCommand(FILE,
"".
"$vol".
$remove.
"");
$result .= ParseResult($nssResult, $vol);
print "$result\n";
}
}
sub deleteAllVolumes
{
my @volume = ();
my $result;
my $poolTag;
my $poolName;
print "Pools Existing:\n";
print ListPools;
print "Enter the Pool Name: ";
chomp($poolName = );
$poolTag = "$poolName";
my $command= "$poolTag";
my $option = "";
@volume = ListVolumes($command,$option);
if ( $#volume+1 == 0 )
{
print "--------------------------------------------------\n".
"$poolName does not contain volume\n".
"--------------------------------------------------\n";
}
foreach my $vol (@volume)
{
RemoveVolume($vol);
}
}
my $command = $_[1];
open(FILE,"+>", "/_admin/Manage_NSS/manage.cmd")
or die "Error opening NSS management file ($!) on server";
MAIN:
while(1)
{
do
{
print "1. To Delete Specified Volume\n".
"2. To Delete all Volumes\n".
"3. To Delete volumes created on specified pool\n".
"4. Exit\n".
"Select action: ";
$action = ;
}while ($action != 0 && ($action < 1 || $action > 4));
if ($action == 1)
{
my @vol=ListAllVolumes();
if ($#vol == -1 )
{
print "No Volumes on the machine\n".
"-----------------------------\n";
}
print "Existing Volumes\n".
"---------------------------\n";
foreach my $v (@vol)
{
print "$v\n";
}
print "Enter Volume Name:";
chomp(my $vol = );
$vol = lowerToUpper($vol);
RemoveVolume($vol);
}
elsif ($action == 2)
{
my @vol=ListAllVolumes();
foreach my $v (@vol)
{
RemoveVolume($v);
}
}
elsif ($action == 3)
{
deleteAllVolumes();
}
elsif ($action == 4)
{
exit(0);
}
else
{
last MAIN;
}
}
close(FILE);