VMware Virtual Machine Set Boot Order Sample Code


Copyright 2008 VMware, Inc. All rights reserved.

#!/usr/bin/perl -w
# Copyright 2006 VMware, Inc. All rights reserved. # # This script allows users to set the boot preference for a virtual machine. This operation requires # following parameters to be specified. # vmname - Virtual Machine name # bootWith - Boot preference options. Example: allow:cd,hd,net
use strict; use warnings; use VMware::VIRuntime; use VMware::VMUtil; use Data::Dumper; my %opts = ( vmname => { type => "=s", help => "The name of the virtual machine", required => 1, }, bootWith => { type => "=s", help => "The boot preference in the following format 'allow:commaSeparatedListOfAllowedClasses' or 'deny:commaSeparatedListOfDisallowedClasses' such as allow:cd,hd or deny:all", required => 1, }, ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my %filterhash = (); my $bootWith = Opts::get_option('bootWith'); my $vmname = Opts::get_option('vmname'); my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {name => $vmname}); if($vm_view) { my $vm_config_spec = VirtualMachineConfigSpec->new( name => $vmname, extraConfig => [OptionValue->new( key => 'bios.bootDeviceClasses', value => $bootWith ),] ); $vm_view->ReconfigVM( spec => $vm_config_spec ); }
=head1 NAME
VMBootOrder.pl - Set a boot preference for a virtual machine.
VMBootOrder.pl --vmname <virtual machine name> --bootWith <bootOrderPreference>
This VI Perl command-line utility provides an interface for setting the boot order preference for an existing virtual machine.
=head1 OPTIONS
=item B<vmname>
Base name of the virtual machine.
=item B<bootWith>
With ESX3.5 you can add 'bios.bootDeviceClasses' option to *.vmx. Its value has format 'allow:commaSeparatedListOfAllowedClasses' or 'deny:commaSeparatedListOfDisallowedClasses'. For example 'allow:cd' will allow you to boot from CD only, while 'deny:net' will allow you to boot from anything except network. Supported class names are cd (for cdrom), hd (for harddisk), net (for network), and fd (for floppy). Note that option sets only list of allowed device classes, not their order - so 'allow:net,cd' and 'allow:cd,net' are equivalent.
Set the boot order preference of the virtual machine so that it boots only from the CD: VMBootOrder.pl --vmname sampleVM --bootWith allow:cd
Set the boot order preference of the virtual machine so that it boots from anything except floppy drive: VMBootOrder.pl --vmname sampleVM --bootWith deny:fd
Set the boot order preference of the virtual machine so that it boots only from either the net or the hard drive:
VMBootOrder.pl --vmname sampleVM --bootWith allow:net,hd
This utility works with VMware VirtualCenter 2.5 or later.
This utility works with VMware ESX Server 3.5 or later.

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.