sub clonewithds(){ # Get the datastore from the name passed in as a paramenter. # Make sure it meets the minimum size requirement (4G) ## TODO: Find an available datastore(s) based on the size of the ## Source VM(s). my $destDS = "TargetDatastore" # Get the host as passed in as a parameter my $host_name = "targetHost"; # Find_entity_view returns exactly one objects (fails on error). my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => {'name' => $host_name}); # Retrieve parent resource pool my $comp_res_view = Vim::get_view(mo_ref => $host_view->parent); # Get a MoRef to the Datastroe based on name. # disksize may be optional, just hard-coding to 4G for now. my %ds_info = HostUtils::get_datastore(host_view => $host_view, datastore => $destDS, disksize => 4194304); if ($ds_info{mor} eq 0) { if ($ds_info{name} eq 'datastore_error') { $logger->error("Datastore $destDS not available"); return undef; } if ($ds_info{name} eq 'disksize_error') { $logger->error("The free space available is less then the specified disk size or the host is not accessible"); return undef; } } # Building the "relocate" spec which specifies destination datastore, # host, resource pool, etc. my $relocate_spec = VirtualMachineRelocateSpec->new(datastore => $ds_info{mor}, host => $host_view, pool => $comp_res_view->resourcePool); my $clone_spec; my $config_spec; my $customization_spec; # A clone spec tells if this should be powered on, is it a template, # and the 'relocate' spec. $clone_spec = VirtualMachineCloneSpec->new( powerOn => 0, template => 0, location => $relocate_spec, ); # Call the "clone" function. Catch errors and report. eval { $vm->CloneVM(folder => $destFolder, name => $fullClone, spec => $clone_spec); }; if ($@) { handleError("clone_vm",$@,ref($@),ref($@->detail)); } }