VMware - Sample Code - Virtual Machine Reconfiguration Webinar Sample Code

VMware Virtual Machine Reconfiguration Webinar Sample Code

exaddnetwork.pl

Copyright © 2008 VMware, Inc. All rights reserved.

#!/usr/bin/perl -w

#######################################################################################
# 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;

#######################################################################################
#
# exaddnetwork.pl
#	Example script to add a network card virtual machine
#	Parameters:
#		-- server 		server name (either VC or ESX dns or ip address)
#		-- username 	credentials
#		-- password 		
#		-- vm  		name of virtual machine to query
#		-- name 	      name of the virtual disk to create
#		-- size 		size of virtual disk to create in MB (default is 10)
#           --persistent            (if selected, specify that virtual disk should be persistent)
#           --independent           (if selected, specify that virtual disk is independent of the vm)
#######################################################################################


#
#	Retrieve standard params (--server , --username , --password 
#	--vm selects the name of the virtual machine to create the virtual disk
#	--name selects the name of the virtual disk to create
#	--size selects the size of the virtual disk in MB (default is 1)
#	--persistent (if selected, specify that virtual disk should be persistent)
#	--independent (if selected, specify that virtual disk is independent of the vm)
#
my %opts = (
   vm  => {
      type     => "=s",
      variable => "vmName",
      help     => "Virtual Machine Name",
      required => 1},
   network  => {
      type     => "=s",
      variable => "networkName",
      help     => "Name of the network to add",
      default  => "VM Network"});

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

#######################################################################################
#
#	Find the Virtual Machine
#######################################################################################
my $vm_name = Opts::get_option ('vm');
my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => "^$vm_name\$"});
Fail ("Virtual Machine $vm_name was not found.\n") unless ($vm_view);

#######################################################################################
#	Create the specification for ReconfigVM
#		... Backing Info defines the network backing
#		... VirtualDeviceConfigSpec specifies the operation (add), and the device
#######################################################################################

# Create disk VirtualDeviceConfigSpec
my $network = Opts::get_option ('network');
my $backing_info = VirtualEthernetCardNetworkBackingInfo->new(deviceName => $network);
my $networkSpec = VirtualPCNet32->new(key => -1,                   
                                      backing => $backing_info);			
my $devspec = VirtualDeviceConfigSpec->new(
                                           operation => VirtualDeviceConfigSpecOperation->new('add'),
                                           device => $networkSpec);
                                           
my $vmspec = VirtualMachineConfigSpec->new( deviceChange => [$devspec], 
                                            changeVersion => $vm_view->config->changeVersion );
print "Adding Network Card $network on $vm_name\n";
eval {
    $vm_view->ReconfigVM( spec => $vmspec );
    };
if ($@) {
    print "Reconfiguration failed.\n $@";
    }
else {
    print "Virtual Network card added.\n";
    }

# logout
Util::disconnect();


sub Fail {
    my ($msg) = @_;
    Util::disconnect();
    die ($msg);
    exit ();
}

sub FindDevice {
    my ($vm, $name) = @_;
    my $devices = $vm->config->hardware->device;
    foreach my $device (@$devices) {
        return $device if ($device->deviceInfo->label eq $name);
        }
    return undef;
}

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.