I did not want to do a code dump, but here you go
- package VManagement;
- # Paul Givens
- # Evaluator Group
- # 6/11/2013
- # A package for manipulaing virtual machines through vSphere
- # Public functions
- # addDisks
- use strict;
- use warnings;
- # I do not know why this lib is needed, delete it for a fun time
- use lib "/usr/lib/vmware-vcli/apps";
- use Exporter;
- use VMware::VIRuntime;
- use AppUtil::VMUtil;
- # Making a module
- our@ISA =qw( Exporter );
- our@EXPORT_OK=qw( addDisks );
- our@EXPORT =qw( addDisks );
- # Declarations
- sub addDisks;
- sub atomicAddDisks;
- # addDisks
- # a function to add disks to a virtual machine
- sub addDisks{
- # Getting arguments
- (my$viServer,my$login,my$password,my$verbose)=@_;
- # Setting the global variables
- $ENV{VI_SERVER} ="$viServer";
- $ENV{VI_USERNAME}="$login";
- $ENV{VI_PASSWORD}="$password";
- # Connecting
- Opts::parse();
- Opts::validate();
- Util::connect();
- # TEST start
- my@diskSizes=(1,2,1);
- my@datastores=("zebi1n","zebi1n","zebi1n");
- # TEST stop
- if($verbose){print"Connected to server\n";}
- atomicAddDisks("paul-vm1",\@diskSizes,\@datastores,$verbose);
- # Disconnecting
- Util::disconnect();
- }
- sub atomicAddDisks{
- # Getting arguments
- (my$vmName,my$diskSizesRef,my$datastoresRef,my$verbose)=@_;
- # Dereferencing
- my@diskSizes =@$diskSizesRef;
- my@datastores=@$datastoresRef;
- # Kb to Gb conversion factor
- my$scsiNum =0;
- my@devSpecs=();
- my$vmView = Vim::find_entity_view(view_type =>'VirtualMachine',
- filter =>{'name'=>$vmName});
- my$vmDevices=$vmView->config->hardware->device;
- my$diskFilename;
- my$diskFilenameIncrement=0;
- my$diskFilenameCur;
- my$diskNum=0;
- foreachmy$vmDevice(@$vmDevices){
- # Adjusting the diskNum for disks already present
- if(ref($vmDevice)eq'VirtualDisk'){
- $diskNum++;
- }
- }
- # Search virtual SCSI controller
- my$controller;
- my$numController=0;
- foreachmy$vmDevice(@$vmDevices){
- if(ref($vmDevice)=~/VirtualBusLogicController|VirtualLsiLogicController|VirtualLsiLogicSASController|ParaVirtualSCSIController/){
- Util::trace(1,"SCSI controller found : $&\n");
- $numController++;
- $controller=$vmDevice;
- }
- }
- my$controllerKey=$controller->key;
- # Set new unit number (7 cannot be used, and limit is 15)
- my$unitNum;
- my$vdiskNumber=$#{$controller->device}+1;
- if($vdiskNumber<7){
- $unitNum=$vdiskNumber;
- }
- elsif($vdiskNumber==15){
- die"ERR: one SCSI controller cannot have more than 15 virtual disks\n";
- }
- else{
- $unitNum=$vdiskNumber+1;
- }
- for(my$i=0;$i<@datastores;$i++){
- # Conversion to GB
- my$disksize= $diskSizes[$i]*1048576;
- my$datastore=$datastores[$i];
- # Increase the disk number to represent the machine to be added
- $diskNum++;
- $diskFilename="[$datastore] paul-vm1/paul-vm1_"."$scsiNum"."_".$unitNum.".vmdk";
- # Build virtual spec data object
- my$diskMode= VMUtils::get_diskmode(
- nopersist =>"",
- independent =>""
- );
- if($verbose){
- print"SPECS\n";
- print"\tVM Name:\t$vmName\n";
- print"\tDisk Name:\t$diskFilename\n";
- print"\tController Key:\t$controllerKey\n";
- print"\tUnit Number\t$unitNum\n";
- print"\tDisk Size\t$disksize\n";
- }
- my$devSpec= VMUtils::get_vdisk_spec(
- vm =>$vmView,
- backingtype =>'regular',
- diskMode =>$diskMode,
- fileName =>$diskFilename,
- controllerKey =>$controllerKey,
- unitNumber =>$unitNum,
- size =>$disksize);
- ###########################################
- # Run ReconfigVM method (in VMUtils package)
- # with previously defined specs
- ###########################################
- push(@devSpecs,$devSpec);
- #increase the unit number for each device
- $unitNum++;
- }
- my$vmSpec= VirtualMachineConfigSpec->new(deviceChange =>\@devSpecs);
- $vmView->ReconfigVM(spec =>$vmSpec);
- #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[0]]);
- #$vmView->ReconfigVM(spec => $vmSpec);
- #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[1]]);
- #$vmView->ReconfigVM(spec => $vmSpec);
- #my $vmSpec = VirtualMachineConfigSpec->new(deviceChange => [$devSpecs[2]]);
- #$vmView->ReconfigVM(spec => $vmSpec);
- }