use Time::Local;
use Parallel::ForkManager;
package main;
if ( @ARGV != 1 ) {
$! = 1;
die "USAGE: <testcasename.pl> profilepath \n"; #->profile is nothing but a list form of VM ipaddress or FQDN
}
$profile = shift(@ARGV);
print $profile;
$ostype="$^O\n";
print $ostype;
open (TR, "<$profile") or die "Test list is not found please create one or run the configuration";
@listuuid=<TR>;
close (TR);
$listofvm=@listuuid;
$file = "log_partition.txt";
if (! open (LOG, ">>$file"))
{
print "logprint: could not write to log file: $file!\n";
}
print LOG " Starting disk paritioning";
close LOG;
print "\n There are total of $listofvm VM it will start performing the task";
$max_forks = new Parallel::ForkManager(5);
foreach $testuuid (@listuuid){
$max_forks->start and next; #->multithreading you can run n number of machine
chomp $testuuid;
$disks="fdisk -l | grep 'Disk /dev/sd' ";
$disk_count="fdisk -l | grep 'Disk /dev/sd' | wc -l ";
&printlog("Running $disk_count");
&printlog("Running $disks");
$total_disk= &plink($testuuid,$disks);
$nummber_disk= &plink($testuuid,$disk_count);
&printlog ("Total number disk for the machine $testuuid $nummber_disk");
print $nummber_disk;
$disk_to_operate=$nummber_disk-1;
$no_of_disk=$disk_to_operate;
print "Total number of disk to be partition is $disk_to_operate";
&printlog ("Total number of disk to be partition is $disk_to_operate");
print "Running the parition";
for ($i=1;$i<=$disk_to_operate;$i++)
{
$mkdir ="mkdir \/disk-$i";
print "\n $mkdir";
&plink($testuuid,$mkdir);
}
$dd='b';
$j=1;
$pt=1;
while ($disk_to_operate!=0)
{
print "\n /dev/sd$dd \n";
$disk_part=" echo -e 'o\\nn\\np\\n1\\n\\n\\nw' \| fdisk \/dev\/sd$dd ";
print "$disk_part";
&plink($testuuid,$disk_part);
print"\n Running the filesystem parition ";
$part="mkfs\.xfs -f \/dev\/sd$dd$pt";
print "\n $part";
&plink($testuuid,$part);
sleep 2;
$mount= "mount \/dev\/sd$dd$pt \/disk-$j ";
print "$mount";
&plink($testuuid,$part);
&plink($testuuid,$mount);
$dd++;
$disk_to_operate--;
++$j;
}
for ($k=1;$k<=$no_of_disk;$k++)
{
for ($f=0;$f<=1;$f++)
{
$datagen= " dd if=\/dev\/urandom of=\/disk-$k\/random-file-1GB-$f.txt count=1024 bs=1M & ";
print $datagen;
&plink ($testuuid,$datagen);
#system(" dd if=/dev/urandom of=/disk-5/random-file-40G.txt count=1000 bs=4M & ");
}
}
$max_forks->finish;
} $max_forks->wait_all_children;
sub plink ($)
{
my $server=shift;
my $run_command=shift;
print $run_command;
my $user = "root";
my $password = "psswd";
my $plink="plink";
print $run_command;
print "\n Verify the platform Win or Linux";
if ($ostype eq 'MSWin32')
{
print "\n The platform is windows";
$ssh_command = qx(echo n | $plink -ssh -v -pw $password $user\@$server "$run_command");
print $ssh_command;
return $ssh_command;
}if ($ostype eq 'linux')
{
$ssh_command1=" sshpass -p $password ssh -o StrictHostKeychecking=no -t root\@$server '$run_command'";
system ($ssh_command1);
}
} #end of plinkexec
sub printlog ($)
{
my $string = shift(@_);
my $file = "log_partition.txt";
#print $file;
print $string;
$date = scalar(localtime);
$date =~ s/\s+/_/g;
$date =~ s/::/_/;
$date=~s/:/_/g;
if (! open (LOG, ">>$file"))
{
print "logprint: could not write to log file: $file!\n";
print "$string";
return 1;
}
print LOG " $string";
close LOG;
print "$string";
return 0;
}