vdiiomark
Enthusiast
Enthusiast

I did not want to do a code dump, but here you go

  1. package VManagement;
  2. # Paul Givens
  3. # Evaluator Group
  4. # 6/11/2013
  5. # A package for manipulaing virtual machines through vSphere
  6. # Public functions
  7. # addDisks
  8. use strict;
  9. use warnings;
  10. # I do not know why this lib is needed, delete it for a fun time
  11. use lib "/usr/lib/vmware-vcli/apps";
  12. use Exporter;
  13. use VMware::VIRuntime;
  14. use AppUtil::VMUtil;
  15. # Making a module
  16. our @ISA       = qw( Exporter );
  17. our @EXPORT_OK = qw( addDisks );
  18. our @EXPORT    = qw( addDisks );
  19. # Declarations
  20. sub addDisks;
  21. sub atomicAddDisks;
  22. # addDisks
  23. # a function to add disks to a virtual machine
  24. sub addDisks{
  25.   # Getting arguments
  26.   (my $viServer, my $login, my $password, my $verbose) = @_;
  27.   # Setting the global variables
  28.   $ENV{VI_SERVER}   ="$viServer";
  29.   $ENV{VI_USERNAME} ="$login";
  30.   $ENV{VI_PASSWORD} ="$password";
  31.   # Connecting
  32.   Opts::parse();
  33.   Opts::validate();
  34.   Util::connect();
  35.   # TEST start
  36.   my @diskSizes = (1,2,1);
  37.   my @datastores = ("zebi1n", "zebi1n", "zebi1n");
  38.   # TEST stop
  39.   if ($verbose) {print "Connected to server\n";}
  40.   atomicAddDisks("paul-vm1", \@diskSizes, \@datastores, $verbose); 
  41.   # Disconnecting
  42.   Util::disconnect();
  43. }
  44. sub atomicAddDisks{
  45.   # Getting arguments
  46.   (my $vmName, my $diskSizesRef, my $datastoresRef, my $verbose) = @_;
  47.   # Dereferencing
  48.   my @diskSizes  = @$diskSizesRef;
  49.   my @datastores = @$datastoresRef;
  50.   # Kb to Gb conversion factor
  51.   my $scsiNum  = 0;
  52.   my @devSpecs = ();
  53.   my $vmView   = Vim::find_entity_view(view_type => 'VirtualMachine',
  54.          filter => { 'name' => $vmName } );
  55.   my $vmDevices = $vmView->config->hardware->device;
  56.   my $diskFilename;
  57.   my $diskFilenameIncrement = 0;
  58.   my $diskFilenameCur;
  59.   my $diskNum = 0;
  60.   foreach my $vmDevice (@$vmDevices) {
  61.          # Adjusting the diskNum for disks already present 
  62.          if (ref($vmDevice) eq 'VirtualDisk') {
  63.                 $diskNum++;
  64.                 }
  65.   }
  66.   # Search virtual SCSI controller
  67.   my $controller;
  68.   my $numController = 0;
  69.   foreach my $vmDevice (@$vmDevices) {
  70.          if (ref($vmDevice) =~/VirtualBusLogicController|VirtualLsiLogicController|VirtualLsiLogicSASController|ParaVirtualSCSIController/) {
  71.                 Util::trace(1, "SCSI controller found : $&\n");
  72.                 $numController++;
  73.                 $controller = $vmDevice;
  74.          }
  75.   }
  76.   my $controllerKey = $controller->key;
  77.   # Set new unit number (7 cannot be used, and limit is 15)
  78.   my $unitNum;
  79.   my $vdiskNumber = $#{$controller->device} + 1;
  80.   if ($vdiskNumber < 7) {
  81.          $unitNum = $vdiskNumber;
  82.   }
  83.   elsif ($vdiskNumber == 15) {
  84.          die "ERR: one SCSI controller cannot have more than 15 virtual disks\n";
  85.   }
  86.   else {
  87.          $unitNum = $vdiskNumber + 1;
  88.   }
  89.   for(my $i = 0; $i < @datastores; $i++) {
  90.          # Conversion to GB
  91.          my $disksize =  $diskSizes[$i] * 1048576;
  92.          my $datastore = $datastores[$i];
  93.          # Increase the disk number to represent the machine to be added
  94.          $diskNum++;
  95.         
  96.          $diskFilename = "[$datastore] paul-vm1/paul-vm1_" . "$scsiNum" . "_" . $unitNum . ".vmdk";
  97.          # Build virtual spec data object
  98.          my $diskMode = VMUtils::get_diskmode(
  99.                 nopersist   => "",
  100.                 independent => ""
  101.          );
  102.          if ($verbose){
  103.                 print "SPECS\n";
  104.                 print "\tVM Name:\t$vmName\n";
  105.                 print "\tDisk Name:\t$diskFilename\n";
  106.                 print "\tController Key:\t$controllerKey\n";
  107.                 print "\tUnit Number\t$unitNum\n";
  108.                 print "\tDisk Size\t$disksize\n";
  109.          }
  110.          my $devSpec = VMUtils::get_vdisk_spec(
  111.                 vm            => $vmView,
  112.                 backingtype   => 'regular',
  113.                 diskMode      => $diskMode,
  114.                 fileName      => $diskFilename,
  115.                 controllerKey => $controllerKey,
  116.                 unitNumber    => $unitNum,
  117.                 size          => $disksize);
  118.          ###########################################
  119.          # Run ReconfigVM method (in VMUtils package)
  120.          # with previously defined specs
  121.          ###########################################
  122.          push(@devSpecs, $devSpec);
  123.          #increase the unit number for each device
  124.          $unitNum++;
  125.   }
  126.   my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => \@devSpecs);
  127.   $vmView->ReconfigVM(spec => $vmSpec);
  128.   #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[0]]);
  129.   #$vmView->ReconfigVM(spec => $vmSpec);
  130.   #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[1]]);
  131.   #$vmView->ReconfigVM(spec => $vmSpec);
  132.   #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[2]]);
  133.   #$vmView->ReconfigVM(spec => $vmSpec);
  134. }
Reply
0 Kudos