Clones a VM while setting the CPU Shares while cloning

exclonewithcpushare.pl

Copyright © 2008 VMware, Inc. All rights reserved.

#!/usr/bin/perl -w
#
# Copyright 2008 VMware, Inc.  All rights reserved.
#
#######################################################################################
# DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS 
# OF ANY KIND, WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY 
# DISCLAIMS ANY IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY 
# QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. 
#######################################################################################

use strict;
use warnings;
use Getopt::Long;
use VMware::VIRuntime;
use VMware::VILib;

#######################################################################################
#
# exclonewithcpushare.pl
#	Example script to clone a vm with the customzied cpu shares
#	Parameters:
#		-- tempvm		Name of template virtual machine
#		-- name			Name of virtual machine to be created
#		-- source		Base virtual machine for the template
#		-- ram			RAM assigned to new virtual machine
#		-- reservation	Reservation assigned to new virtual machine
#		-- cpus			Numbers of cpu assigned to virtual machine

# This script works with VMware VirtualCenter 2.0 or later.

# This script works with VMware ESX Server 3.0 or later.
#######################################################################################

my %opts = (
   'tempvm' => {
      type      => "=s",
      help       => "Source Template",
   	  variable => "tempvm",
      required   => 1},
   'name' => {
      type      => "=s",
      help       => "New Virtual Machine Name",
  	  variable => "name",
	  default    => "Template_CloneVM"},
   'source' => {
      type      => "=s",
      help       => "Source Master.  Defines VM from which pools, datastore, etc are captured",
  	  variable => "source",
	  required   => 1},
	'ram' => {
      type      => "=i",
      help       => "Memory assigned to new virtual machine",
  	  variable => "ram",
      required   => 1},
	'reservation' => {
      type      => "=i",
      help       => "Reservation assigned to new virtual machine",
  	  variable => "reservation",
      required   => 1},
	'cpus' => {
      type      => "=i",
      help       => "Number of cpus assigned to new virtual machine",
  	  variable => "cpus",
      required   => 1},
);

# validate options, and connect to the server
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();

my $name = Opts::get_option ('name');
my $ram = Opts::get_option ('ram');
my $reservation = Opts::get_option ('reservation');
my $cpus = Opts::get_option ('cpus');

#######################################################################################
#	Find the template virtual machine
#######################################################################################
my $template = Opts::get_option ('tempvm');
my $template_view = Vim::find_entity_view (view_type => 'VirtualMachine', filter =>{ 'name'=> $template});
die "VM $template not found.\n" unless ($template_view);


#######################################################################################
#	Find the base virtual machine for template
#######################################################################################
my $source = Opts::get_option ('source');
my $vm_view = Vim::find_entity_view (view_type => 'VirtualMachine', filter =>{ 'name'=> $source});
die "VM not found.\n" unless ($vm_view);

#######################################################################################
#	Setting the location of new virtual machine (Assigned to base vm resourcepool)
#   Create VirtualMachineRelocateSpec
#######################################################################################
my $relocate_spec = VirtualMachineRelocateSpec->new (pool => $vm_view->resourcePool);

my $sl = SharesLevel->new('custom');
my $sh = SharesInfo->new(level => $sl, shares => 1);

#setting customized cpu shares
my $mem_res = ResourceAllocationInfo->new(reservation => $reservation,
                                                          limit => -1,
                                                          shares => $sh);
my $cpu = ResourceAllocationInfo->new(limit => '-1',
                                               shares => $sh);

my $config_spec = VirtualMachineConfigSpec->new(
                                      cpuAllocation => $cpu,
                                      memoryAllocation => $mem_res);

#Create VirtualMachineCloneSpec
my $clone_spec = VirtualMachineCloneSpec->new(powerOn => 0,
                                              config => $config_spec,
                                              template => 0,
                                              location => $relocate_spec);

print "Cloning virtual machine from template.\n";

print "Setting memory reservation to $reservation.\n";

# Creation of clone vm
$template_view->CloneVM_Task(folder => $template_view->parent, name => $name, spec => $clone_spec);

print "Clone operation is complete.\n";

# logout
Util::disconnect();

The sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.

You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.

VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.