Tải bản đầy đủ (.pdf) (72 trang)

ADVANCED SERVER VIRTUALIZATION VMware and Microsoft Platforms in the Virtual Data center phần 9 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.1 MB, 72 trang )

589
Chapter 25
Scripting with Microsoft
Virtual Server, VMware
GSX Server and ESX Server
Where the consumer versions of Microsoft Virtual PC and VMware Worksta-
tion come with command line control, no such simplicity is available with the
virtualization server products. Virtualization server control is achieved through
the graphical user-interfaces or programming.  ere is a lot of sample source
code that either comes bundled with the products or is downloadable from the
vendor support site; however this does not mean that all critical information is
covered in detail. VMware provides their sample scripting examples installed
with their products and Microsoft has a support site dedicated to scripting Vir-
tual Server. Rather than reiterating what is bundled with the products, this chap-
ter is taking a real-world need like performing backups of virtual machines and
presents a walk-through on how to accomplish that task with each of the script-
ing application programming interfaces (APIs).
Getting Started with Application Programming Interfaces (APIs)
 e starting point in writing a script or “scripting” any application is to fi nd out
what language bindings are provided to drive the product’s automation facili-
ties. Sometimes it is a built-in scripting language, other times it is a static or dy-
namically loaded native library or a managed programming assembly or interop
library like for a Microsoft .NET service. In the case of Microsoft Virtual Server
and VMware GSX Server on Windows their scripting interfaces are driven by
a Component Object Model (COM) library that is registered with Windows
Marshall_AU3931_C025.indd 589Marshall_AU3931_C025.indd 589 4/13/2006 1:58:15 PM4/13/2006 1:58:15 PM
590  Advanced Server Virtualization
upon product installation. For VMware ESX Server its scripting interface is
driven by Perl version 5.6 integration modules. Since both vendors’ virtualiza-
tion product APIs on Windows are written in COM, they are easily accessible
through Visual Basic scripting, VBScript. For complex automation and complex


data structure manipulation it is necessary to use an integrated development
environment (IDE) and either the Visual Basic or C++ programming language,
however for simplicity and ease of use the examples used in this chapter will be
based on VBScript.
The VMware Application Programming Interface
VMware refers to their APIs as VmCOM and VmPerl named after the bindings
they are implemented with respectively.  e programmable objects in VmCOM
are registered under the ProgId VMComLib.*. For ESX Server or GSX Server
on Linux, the underlying API bindings are written in Perl version 5.6 modules.
VMware provides both VmCOM and VmPerl access on Windows and only
VmPerl on Linux since COM is not available. Figure 25.1 contains the high-
level interfaces used to access specifi c VMware functionality.
 e defi nitive reference guide for these interfaces is provided in a document
entitled VMware Scripting API Guide (Adobe PDF format).  e latest Script-
ing API guide is available on VMware’s Web site at />support/developer/scripting-API.
VMware VirtualCenter Infrastructure Software Development Kit (SDK)
In addition to the host-level scripting APIs, VMware also publishes a set of
high-level Web Services interfaces to manage an entire data center installation
of GSX Server and ESX Server called the VMware Virtual Center Infrastructure
SDK.  e Virtual Center Infrastructure SDK is not as easily scriptable and uses
a Common Information Model (CIM)-based object and data model for each
host, virtual machine, and guest it manages. It is possible to script the Virtual
Center Infrastructure SDK using any client that can interpret a Web Services
Defi nition Language (WSDL) specifi cation. WSDL automation is available
Interface Name Manages
VmConnectParams Connecting to virtual machines
Vm ServerCtl Operations for all virtual machines
VmCollection General collections of VMware objects
VmCtl Operations for a specifi c virtual mahine
VmQuestion Virtual server status and interactive management prompts

Figure 25.1 High-Level VMware Interfaces.
Marshall_AU3931_C025.indd 590Marshall_AU3931_C025.indd 590 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  591
through a WS-* compatible Perl library but is usually done from a Java2 or
.NET integrated development environment.  e Virtual Center Infrastructure
SDK is beyond the scope of this chapter but the off ering demands additional
investigation if systems management of virtual machines on a site-wide deploy-
ment scale is important.
The Microsoft Virtual Server 2005 Application Programming Interface
 e Microsoft Virtual Server API is called the Microsoft Virtual Server 2005
COM API and is registered in the COM registry under its ProgId “VirtualServ-
er.Application.”  e Virtual Server COM API is a rich set of interfaces that
handles host and guest OS device and power state management, monitoring,
and control. In fact the entire Web-based Virtual Server Administrative Console
is a natively built Web server CGI component that uses the COM API exclu-
sively to manage Virtual Server. Anything that is possible via the Web interface is
scriptable by programming.  e API contains a few options that are not exposed
through the Web interface and that makes it a bit more powerful than the user
interface such as creating virtual machines and virtual networks in arbitrary path
locations.
For reference, the complete set of interfaces in the Virtual Server API are
listed in Figure 25.2, Figure 25.3, and Figure 25.4.
Microsoft Virtual Server 2005 COM Interfaces
 e starting point to managing all of the Virtual Server 2005 interfaces is the
IVMVirtualServer interface. IVMVirtualServer has many methods that return
Interface Name Manages
IVMAccessRights User and Group Access Rights: accounts and
IVMAccessRightsCollection permissions for accessing Virtual Server
IVMAccountant CPU scheduling, disk and network i/o counters and
VM uptime

IVMDHCPVirtualNetworkServer DHCP parameters for virtual networks
IVMDisplay Dimensions, video mode and thumbnail of guest OS
displays.
IVMDVDDrive Collection of and specifi c CD/DVD device media
IVMDVDDriveCollection connected to a host drive or captured virtual media and
IVMDVDDriveEvents insertion/removal of media event notifi cation.
IVMFloppyDrive Floppy device media connected to a host drive or
IVMFloppyDriveCollection captured virtual media and insertion/removal of
IVMFloppyDriveEvents media event notifi cation.
Figure 25.2 Virtual Server General Security and Removable Media Interfaces.
Marshall_AU3931_C025.indd 591Marshall_AU3931_C025.indd 591 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
592  Advanced Server Virtualization
Figure 25.4 Virtual Server Host, Virtual Machine Events, and Network Interfaces.
IVMGuestOS Guest OS services: Heartbeat, time synchronization,
VM Additions and orderly OS shutdown.
IVMHardDisk Collection of and specifi c virtual Hard Disk fi les
IVMHardDiskConnection of IDE and SCSI disks, including Undo disks.
IVMHardDiskConnectionCollection
IVMHostlnfo Detailed CPU, memory, OS, networking, serial and
parallel ports and removable devices of the host system.
IVMKeyboard Simulation of typing keys in a Guest OS.
IVMMouse Guest OS mouse status and simulation of button
and mouse clicks.
IVMNetworkAdapter Collection of and characteristics of virtual network
IVMNetworkAdapterCollection adapter cards.
IVMParallelPort Collection of and characteristics of the virtual
IVMParallelPortCollection parallel port (LPT).
IVMRCAuthenticator Collection of and enumeration of supported
IVMRCAuthenticatorCollection Authentication methods over the VMRC remote
console interface.

IVMSCSIController Collection and parameters of the virtual SCSI
IVMSCSIControllerCollection controller cards including bus sharing.
IVMSecurity Applies fi ne-grain security controls over Virtual
Server objects.
IVMSerialPort Collection of and characteristics of the virtual
IVMSerialPortCollection serial ports.
IVMSupportDriver Collection of and enumeration of support drivers
IVMSupportDriverCollection installed on the host system.
IVMTask Collection of and enumeration of task status for long
IVMTaskCollection running operations like merging undo disks or starting
up a virtual machine.
Figure 25.3 Virtual Server Guest OS Interfaces.
IVMVirtualMachine Collection of and top-level managing objects and events
IVMVirtualMachineCollection for a Virtual Machine.
IVMVirtualMachineEvents
IVMVirtualNetwork Collection of and enumeration of physical and virtual
IVMVirtualNetworkCollection networks that virtual network adapters are connected to.
IVMVirtualServer Collection of and top-level managing objects and events
IVMVirtualServerEvents for Virtual Server 2005.
Marshall_AU3931_C025.indd 592Marshall_AU3931_C025.indd 592 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  593
concrete instances of all other interface types. Next to IVMVirtualServer, the
object accessed the most frequently is IVMVirtualMachine and in turn the
IVMGuestOS object is used to manage such operations as graceful guest OS
shutdowns.
Controlling a Virtual Server Through Scripting
 e preceding sections described the APIs and their access interfaces, this section
will apply the APIs in a useful exercise. Each of the APIs organizes the controls
over virtual servers into a set of interfaces and objects representing the virtual
server application, a virtual machine, and many of its attached virtual devices.

As virtual machines are created, each virtualization platform builds collections of
objects that compose a complete state of the host installation. In addition to the
basic objects, the control of the virtual machine breaks down further into a fam-
ily of related operations like power state management (e.g., turning the virtual
machine on, off , or suspending it) and virtual device management—changing
the state of the attached virtual hard disks, CD/DVD-ROM media and virtual
networking (e.g., connecting and disconnecting media or networking access). As
calls are made into API methods or to update the state of virtual server objects,
the calls are actually updating either the underlying virtual machine confi gura-
tion or the internal running states of the virtualization platform. In many cases
scripting API invocations are manipulating the same control methods that the
graphical user-interfaces of Microsoft Virtual Server and VMware are.
Because scripting is another aspect of controlling a virtualized
server, confl ict is avoided by controlling the server through the
graphical user-interface or through scripting, these are mutu-
ally exclusively.  is means that only one control method can
be used at a time, not both simultaneously. What happens if
the exclusivity rule is not followed? A change in one control method aff ects
the internal state of other controlling environments. In other words there
is no proper arbitration or brokering.
While the graphical user-interfaces are generally status refl ecting point-and-
click tools, scripts are usually not and are not expecting their basic assumptions
to be disrupted by changes by the virtualization platform GUIs. It is safest to
run a script on a virtual machine when the graphical user interface is not run-
ning. Where this is unavoidable, do not have virtualization management screens
active for the same machines accessed through scripting automation. Even if the
GUI is running an “observation mode” and not changing a virtual machine’s
confi guration, the GUI and script sometimes have to have locking access to the
Marshall_AU3931_C025.indd 593Marshall_AU3931_C025.indd 593 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
594  Advanced Server Virtualization

underlying object resources, which are not designed to be shared.  e locking
prevents either control method from obtaining the write-exclusivity required
to change makes.  ese types of locking errors are diffi cult to debug and diag-
nose or worse resolve themselves, which just leads to user frustration and testing
problems.
Programming References Are a Key to Success
 ere is a saying, “Sometime you don’t know, what you don’t know.”  is most
certainly applies to scripting.  ere are a lot of possible dead ends and road-
blocks than can be run into, this is why it is critical to have technical refer-
ences available. With this in mind, it is important to become familiar with the
programming references for the virtual server product(s) that are going to be
scripted against. For Microsoft Virtual Server the main source of reference is the
“Virtual Server Programming Guide” in the Virtual Server Start menu group
and available code examples in Microsoft’s Script Center Repository at http://
www.microsoft.com/technet/scriptcenter/scripts/default.mspx.  e Program-
ming Guide is the ultimate reference for every object, method, property, con-
stants, and enumerations.  e Script Center Repository is a collection chocked
full of sample code and best practices that provide just enough information to
cobble together a solution or to get familiar with subtle details on a particular
operation like shutting down a virtual machine.
For VMware GSX Server and ESX Server the choice is installing the Script-
ing API and downloading the latest documentation from VMware at http://
www.vmware.com/support/developer/scripting_download.html. If the API is
installed, the sample scripts are in \Program Files\VMware\VMware VmCOM
Scripting API\SampleScripts. As with most references it is unnecessary to read
them cover-to-cover. It is only necessary to index and search through the refer-
ences as needed.
Real-World Scripting: Backing up Virtual Machines
Now armed with all the information and references needed, the best way to learn
is by writing a script that is not only useful, but used on a regular basis to solve

a problem. One of the most common problems with running virtualization is
that the diffi culty of backing up the environment is multiplied by an order of
magnitude, because now instead of just backing up the host, the backup must
include all of the virtual machines.  ese virtual machines represent running
machines themselves, so to just backup the host is not enough to have the virtual
machines covered. Backing up running virtual machines is a challenge because
the virtual machine hard disk fi les are large and open or “in use.”  is is further
compounded by the fact that the content state of the virtual machine is chang-
ing while you are backing up. Assuming there is a maintenance window for each
Marshall_AU3931_C025.indd 594Marshall_AU3931_C025.indd 594 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  595
virtual machine, it is better to take that machine temporarily out of service, back
it up, and then fi nally start it again.  at sounds easy but virtual machines can
be in various power states of operation like turned off , suspended, or running.
Backups should not be disruptive, so the expectation is to backup a single virtual
machine at a time and leave it in the same state as it was before the backup took
place. If a virtual machine were on, it is expected to be able to safely shutdown
the guest operating system and back up the virtual machine fi les, then restart the
machine. Finally, to minimize downtime the scripting APIs have special access
to features of virtualization like undoable disks or redo logs that allow capturing
changes to a snapshot of the virtual hard disk while it is running. In other words,
virtual machine backups can minimize downtime if a backup creates a runtime
redo log or undoable drive that allows the base disk to be backed up with a
consistent disk state (meaning no writes are occurring to the base disk during
backup, because it is in a read only mode). After the backup, the virtualization
platforms can merge any changes made briefl y during the backup and continue.
 e advantage of this fl exibility is that downtime is minimized to that virtual
machine’s backup time. It does not always require a restart to enable a layer of
diff erencing disk or merge the diff erences once a backup is complete, assuming
the write changes during the backup are reasonably small (a few hundred MBs

at most).
Security and Microsoft Virtual Server
As part of Microsoft’s Trustworthy Computing Initiative, Microsoft performed a
comprehensive security audit of Virtual Server API and the access methods need-
ed to invoke it. To simplify the scripting code, if Distributed COM (DCOM)
allows remote scripting of Virtual Server it is easiest to set the authentication and
impersonation defaults in the dcomcnfg MMC snap-in to “Connect” and “Im-
personate” respectively. Without these changes, additional programmatic COM
security initialization using COM’s CoInitialize and CoInitializeSecurity with
principal identity (log-in) information is required to run these scripts.  ose
additional security modes are not covered in this chapter.
Backing Up Microsoft Virtual Server
 e backup strategy here is to access Virtual Server and get a list of registered
virtual machines, for each virtual machine, obtain its pre-backup power state,
shutdown the machine if is running, then defer the backup of the virtual ma-
chine fi les themselves to the preferred backup method and resume the operation
of the virtual machine in the same power state as before the backup.  e main
takeaway here is to not simply shutdown all virtual machines, back them up
Marshall_AU3931_C025.indd 595Marshall_AU3931_C025.indd 595 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
596  Advanced Server Virtualization
and power on without regard to their initial state.  is would be problematic if
you do not have the host capacity to run all registered machines simultaneously.
Below is the code to do this:
‘Enable error handling
On Error Resume Next
‘Instantiate a Virtual Server COM API object
Set objVS = CreateObject(“VirtualServer.Application”)
‘Get a collection of all virtual machines
Set colVMs = objVS.VirtualMachines
‘Iterate through the collection of virtual machines

For Each objVM in colVMS
‘objVM is the currently selected VM from the
collection
‘Get the current VM’s power state and save for
later
Set objPowerState = objVM.State
If (Not objPowerState = vmstate_Off Then
‘The VM is ON, request a shutdown
‘if VM Additions are installed
Set objGuestOS = objVM.GuestOS
If (Not objGuestOS is Nothing) Then
‘We have VM Additions, request a graceful
shutdown
Set ShutdownTask = objGuestOS.Shutdown()
‘Wait for the Guest to shutdown
ShutdownTask.WaitForCompletion()
Else
‘One choice is to powerdown the VM to
‘prevent virtual disks from changing
objVM.TurnOff()
‘Another possibility is to Save the VM
State
Set SaveTask = objVM.Save()
SaveTask.WaitForCompletion()
End If
End If
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
’’’
‘Backup the fi les using your preferred backup method ‘
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

’’’
‘Recall the original power state and restore it
If (Not objPowerState = vmstate_Off Then
Marshall_AU3931_C025.indd 596Marshall_AU3931_C025.indd 596 4/13/2006 1:58:17 PM4/13/2006 1:58:17 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  597
‘ The machine was running before the backup,
resume operations
If (objPowerState = vmstate_Running) Then
‘This will startup or unsave a virtualm
machine
Set StartupTask = objVM.Startup()
‘This is optional. Comment out to speed up
backups
StartupTask.WaitForCompletion()
End If
End If
Next
Backing Up VMware GSX Server
To backup GSX Server, fi rst the VMware inventory of virtual machines must be
accessed and a list of registered virtual machines must be gotten.  en for each
virtual machine, obtain its pre-backup power state and shutdown the machine
if it is running. Next, defer the backup of the virtual machine fi les themselves to
the preferred backup method and resume the operation of the virtual machine in
the same power state as before the backup.  e main takeaway here is to preserve
the initial state all virtual machines. If this was not done and all machines were
powered on simultaneously, then the host could run out of capacity when trying
to run all registered machines simultaneously.
‘Instantiate GSX Server vmCOM API objects
Set cp = CreateObject(“VmCOM.VmConnectParams”)
Set server = CreateObject(“VmCOM.VmServerCtl”)

‘Connect to GSX Server
server.Connect cp
‘Get a collection of all virtual machines
Set vmCollection = server.RegisteredVmNames
‘Iterate through the collection of virtual machines
For each vmName in vmCollection
‘Instantiate a vmCOM Control object
Set vm = CreateObject(“VmCOM.VmCtl”)
s = “path=” & vmName
On Error Resume Next ‘ Clear error object
‘Connect to this virtual machine by path
vm.Connect cp,vmName
If err.Number = vmErr_VMBUSY Then
‘Include warning
s = s & “ UNAVAILABLE (controlled by local
console)”
Marshall_AU3931_C025.indd 597Marshall_AU3931_C025.indd 597 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
598  Advanced Server Virtualization
ElseIf err.Number <> 0 Then
‘If not busy get error
s = s & “ ERROR CONNECTING desc=’” & err.
Description & “’”
Else
‘Reset error control, making errors fatal past
this point
On Error Goto 0
s = s & “ state=” & State2Str(vm) & “ os=” & _
vm.Confi g(“guestos”)
‘Check to see if a vmQuestion is pending against
‘the virtual machine

If vm.ExecutionState = vmExecutionState_Stuck
Then
‘Retrieve the question and answer choices
Set q = vm.PendingQuestion
Set choices = q.choices
s = s & “ question= ‘” & q.text & “’
choices=”
For each choice in choices
s = s & “[“ & choice & “] “
Next
‘Check for redo log questions
Set r = new RegExp
r.pattern = “undoable disk”
r.ignorecase = True
Set matches = r.Execute(q.text)
If matches.count > 0 Then
‘Check for OK or redo log answers in
choices
For i = 1 to choices.count
If choices(i) = “Append” Or _
choices(i) = “Keep” Or _
choices(i) = “OK” Then
WScript.Echo(s)
s = “ > Automatically “ _
& “selecting ‘” & q.choices(i)
vm.AnswerQuestion q,i
Exit For
End If
Next
End If

End If
Marshall_AU3931_C025.indd 598Marshall_AU3931_C025.indd 598 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  599
‘Get the current VM’s power state and save for later
Set objPowerState = vm.ExecutionState
If (objPowerState <> vmExecutionState_Off Then
‘The VM is ON, request a shutdown
Vm.Stop cp, vmName
End If
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
’’’
‘Backup the fi les using your preferred backup method ‘
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
’’’
‘Recall the original power state and
‘restore it
If (objPowerState <> vmExecutionState_Off) Then
‘ The machine was running before the backup,
‘ resume operations
If (objPowerState = vmExecutionState_On) Then
vm.Start cp, vmName
End If
End If
End If
Next
‘Utility function: Convert virtual machine state to a
printable string
Function State2Str(vm)
Select Case vm.ExecutionState
Case vmExecutionState_On

State2Str = “ON”
Case vmExecutionState_Off
State2Str = “OFF”
Case vmExecutionState_Suspended
State2Str = “SUSPENDED”
Case vmExecutionState_Stuck
State2Str = “STUCK”
Case Else
State2Str = “UNKNOWN”
End Select
End Function
Backing Up VMware ESX Server
 e backup strategy in this case is similar to VMware GSX Server, which is
to the VMware inventory of virtual machines must be accessed and a list of
Marshall_AU3931_C025.indd 599Marshall_AU3931_C025.indd 599 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
600  Advanced Server Virtualization
registered virtual machines must be gotten.  en for each virtual machine, ob-
tain its pre-backup power state and shutdown the machine if it is running. Next
defer the backup of the virtual machine fi les themselves to the preferred backup
method and resume the operation of the virtual machine in the same power state
as before the backup.  e point here is to preserve the initial state all virtual ma-
chines, just as it was for GSX Server. If this was not done and all machines were
powered on simultaneously, then the host could run out of capacity when trying
to run all registered machines simultaneously.
# Import vmPerl API Packages
use VMware::VmPerl;
use VMware::VmPerl::VM;
use VMware::VmPerl::Server;
use VMware::VmPerl::ConnectParams;
# Use Perl strict checking

use strict;
# Get Server authentication information from the com-
mand-line arguments
my ($server_name, $user, $passwd) = @ARGV;
# Change this to your Administration port if it is
different.
my $port = 902;
# Create a ConnectParams object using authentication
credentials
my $connect_params =
VMware::VmPerl::ConnectParams::new($server_name,
$port,$user,$passwd);
# Create a Server object
my $server = VMware::VmPerl::Server::new();
# Establish a persistent connection with server
if (!$server->connect($connect_params)) {
my ($error_number, $error_string) = $server->get_
last_error();
die “Could not connect to server: Error $error_number:
$error_string\n”;
}
# Get a list of all virtual machine confi gurations
registered
# with the server.
my @list = $server->registered_vm_names();
# Check the list of virtual machines for errors
if(!defi ned($list[0])) {
Marshall_AU3931_C025.indd 600Marshall_AU3931_C025.indd 600 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  601
my ($error_number, $error_string) = $server->get_

last_error();
die “Could not get list of VMs: Error $error_
number: $error_string\n”;
}
# Declare a local confi guration for a virtual machine
instance
my $confi g;
# Iterate through the collection of virtual machines
foreach $confi g (@list) {
# Declare a VM placeholder object
my $vm = VMware::VmPerl::VM::new();
# Connect to the VM, using the ConnectParams object.
if (!$vm->connect($connect_params, $confi g)) {
# Couldn’t connect, report the error message
my ($error_number, $error_string) = $server-
>get_last_error();
print STDERR “Could not connect to VM $confi g:
Error “ .
“ $error_number: $error_string\n”;
} else {
# Get the current VM’s power state and save for
later
my $power_state = $vm->get_execution_state();
if (!defi ned($power_state)) {
# Couldn’t get a VM’s power state,
# report the error messsage
my ($error_number, $error_string) =
$server->get_last_error();
print STDERR “Could not get execution state
of VM “ .

“$confi g: Error $error_number: “ .
“$error_string\n”;
} else {
if ($power_state == VM_EXECUTION_STATE_STUCK) {
# If stuck, try to answer the
# pending question and continue
my $question = $vm->get_pending_question();
my @choicesText = $question->get_choices();
my $choicesCount = scalar(@choicesText);
if ($choicesCount <= 1) {
$vm->answer_question($question, 0);
Marshall_AU3931_C025.indd 601Marshall_AU3931_C025.indd 601 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
602  Advanced Server Virtualization
}
}
$power_state = $vm->get_execution_state();
if ($power_state == VM_EXECUTION_STATE_ON) {
# The VM is ON, request a shutdown
$vm->stop(VM_POWEROP_MODE_TRYSOFT)
}
#############################################
##########
# Backup the fi les using your preferred backup
method #
#############################################
##########
# Recall the original power state and restore it
if ($power_state != VM_EXECUTION_STATE_OFF) {
print “Re-powering $confi g \n”;
if (!$vm->start()) {

# If an error occurs, report it
# and continue
my ($error_number, $error_string) =
$server->get_last_error();
print STDERR “Could not re-power VM “ .
“$confi g: Error “ .
“$error_number: $error_string\n”;
}
}
}
# Destroy the virtual machine object and
# disconnect from the virtual machine instance.
undef $vm;
}
}
# Destroy the server object and disconnect from the
host server.
undef $server;
Summary
Scripting is important in automating many of the daily operations when leverag-
ing virtualization. To prepare to write scripts involving virtualization, a familiar-
Marshall_AU3931_C025.indd 602Marshall_AU3931_C025.indd 602 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
Scripting with Microsoft Virtual Server, VMware GSX Server and ESX Server  603
ity with programming or at least prior scripting experience with VBScript or
Perl is valuable. In addition to scripting experience, making sure that all of the
proper reference material is at hand makes the scripting process a far easier task.
When using both VMware’s and Microsoft’s virtualization technologies, script-
ing can provide many customizable and advanced capabilities over that of the
GUI-based interfaces. Utilizing scripting is a necessity when using virtualization
in any large scale deployments.

Marshall_AU3931_C025.indd 603Marshall_AU3931_C025.indd 603 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
Marshall_AU3931_C025.indd 604Marshall_AU3931_C025.indd 604 4/13/2006 1:58:18 PM4/13/2006 1:58:18 PM
605
Chapter 26
Other Advanced Topics
Building upon the information presented up to this point, this chapter intro-
duces advanced topics including backing up and restoring virtualization host
servers and virtual machines, server clustering in a virtualized environment,
working with ISO images, and physical server to virtual server (P2V) image
conversions. Each topic is discussed from the proper planning stages through
practical implementations.
Back Up and Restore
 is section describes best practices for IT administrators and backup adminis-
trators to use when backing up and restoring virtualization host servers or vir-
tual machines. Host servers and virtual machines have the same requirements as
physical servers when it comes to back up and restore functionality. As company
or customer data is a top priority, administrators require a backup and restore
solution be easy to setup and manage, cost-eff ective, and, above all else, depend-
able.
Planning Stage
As a backup administrator in charge of validating and ensuring data integrity, it
is important to create and maintain a backup plan for a new virtualization envi-
ronment. While planning and preparing the backup solution, it is important to
consider the following questions.
• What needs to be backed up and how often?
• What solution is needed to recover individual fi les on the virtual ma-
chine?
• What solution is needed to recover the entire virtual machine?
Marshall_AU3931_C026.indd 605Marshall_AU3931_C026.indd 605 4/13/2006 2:01:26 PM4/13/2006 2:01:26 PM
606  Advanced Server Virtualization

• Is there a backup solution already in place for physical servers?
• Is backup software and licensing already owned?
• Will backup agents be needed on the virtual machines? On the host server?
Or both?
• What is the ultimate target destination for backed up data? Local storage?
Tape media? Network storage?
 ere are several possible approaches for backing up data.  e answer could
be any one of these options or a combination of these options.
Backing Up the Host Server
Backing up a host server can be accomplished in a number of ways. To com-
pletely back up the entire host server environment for a given point in time,
two of the more simple and traditional methods may be employed. By utilizing
either a server/agent backup software package such as VERITAS Backup Exec
or an imaging solution such as Symantec Ghost or Altiris Deployment Solu-
tion, the entire host server can be backed up.  ese solutions are fairly simple
to implement and are well documented.  e only exception to this is that these
packages have to be slightly adjusted in their use because there is now a virtu-
alization layer added to the mix. If the host server contains any registered and
powered on virtual machines, they must be powered off before the host server
and its virtual machine directories can be backed up.
Advantages
• Everything on the host server is backed up at one time providing ease of
back up operation.
• Back up processes and methodologies are similar to backing up a normal
physical server.
• If a catastrophic system loss occurred, the entire host server can be quickly
restored.
Disadvantages
• Any restores are to a single point in time where the data is already consid-
ered stale.

• Individual fi les in a virtual machine cannot be restored.
• Backups and restores using this method can be extremely time consuming
as well as taxing on a server’s processor and network.
• Backups require large amounts of space (either disk, tape, or DVD me-
dia).
• If not performed properly, it may result in data loss.
•  e backup is not considered live, which means all virtual machines resid-
ing on the host server that are also being backed up must be either powered
off or suspended prior to the backup taking place.
Marshall_AU3931_C026.indd 606Marshall_AU3931_C026.indd 606 4/13/2006 2:01:29 PM4/13/2006 2:01:29 PM
Other Advanced Topics  607
 e virtualization host server should not have many changes
other than periodic upgrades from the platform vendor. Back-
ing up the entire host server to simply back up the virtualiza-
tion platform is not recommended. Rather than backing up
the platform and restoring it, most platforms are either simple enough to
reinstall or they off er an automated installation path that is usually faster
than doing a full system restore. If the backup route is chosen, it will be-
come quite clear rather quickly that a full host server backup will not be
needed as frequently as a backup of the virtual machines and their associ-
ated data fi les.
Backing Up Individual Files from within the Virtual Machines
 e best way to backup individual virtual machine fi les on virtual machines that
require constant uptime (such as a typical 99.9% service level agreement or SLA,
providing 24/7 uptime) is by using traditional backup and restore processes by
installing a backup agent in each virtual machine’s guest operating system. By
connecting directly through the network to a backup server, the backup agent
on the guest operating system can completely backup and restore individual
fi les on the virtual machine.  rough either a manual or automated fashion,
the agent can be instructed to transfer the selected data from inside of the guest

operating system to a local or remote destination, such as tape, a disk array, or
writeable CD/DVD media.
 is follows the same standard procedures that would be followed when in-
stalling a backup agent onto a physical server.  ere are many backup solutions
currently on the market with one of the more popular being VERITAS Backup
Exec, which also happens to be supported by all three major virtualization plat-
forms. Most backup products today are wizard driven and provide some type
of automated scheduling method in which to archive the data. Backup archives
can be complete backups, incremental backups, or diff erential backups. Each of
these archiving schemas has advantages and disadvantages associated with them,
but selecting the right solution is dependant on the situation and the type of
data being backed up.
 e primary disadvantage to using traditional backup and restore technolo-
gies inside of a virtual machine is the time it takes to backup the data as well as
the performance hit taken in network traffi c and processor load. It is important
to realize that when the backup agent begins reading the data from the virtual
machine and transfers it across the network, the host server will be taxed quite
a bit.  e virtual machine’s guest operating system will be under a great deal
of stress and so will the virtualization layer.  e problem can be multiplied if a
large number of virtual machines residing on the same host are all scheduled to
Marshall_AU3931_C026.indd 607Marshall_AU3931_C026.indd 607 4/13/2006 2:01:29 PM4/13/2006 2:01:29 PM
608  Advanced Server Virtualization
perform their backups around the same time.  e reverse is also true, if a restore
of data is attempted using this type of method, it can be a slow and strenuous
exercise on all of the systems involved.
Advantages
• Can restore individual data fi les.
• Can restore database data via the normal database-specifi c method.
• Backups can be performed live on running virtual machines.
• A company’s normal backup and restore procedures and methodologies

can be followed.
• Most backup server or backup agent software solutions can be used as long
as it runs on the guest operating system on the virtual machine.
• It simplifi es the backup process when all machines (physical and virtual)
use the same backup strategies.
Disadvantages
•  is approach does not take advantage of the fi le encapsulation of a virtual
machine.
• A backup agent/software license must be purchased for each virtual ma-
chine, which can grow quickly and become quite costly.
• If a disaster strikes, it may take longer to fi rst restore the entire virtual
machine, load it with recovery software, and then restore the data from
each of the diff erent backups, rather than just backing up and restoring the
entire virtual machine.
• Can cause a network and processor performance hit depending on the
amount and type of data being backed up or restored, or the number of
virtual machines simultaneously backing up or restoring fi les.
Backing Up Virtual Machines with a Host
Operating System Backup Agent
Another backup method often used is one that makes use of a backup agent run-
ning on the host server.  is backup solution closely follows a standard network
backup solution and should fi t into most methodologies quite well.
Before going out to purchase a backup software package, there are a few con-
siderations to take into account when using this backup strategy. It is important
to make sure that the selected backup agent software is compatible with the
virtualization host platform and its fi le system. For example, not all backup soft-
ware is compatible with VMware ESX Server’s VMFS fi le system. Equally im-
portant, virtual machines should be powered off or suspended before a backup
agent is allowed to backup the virtual machine disk fi les, saved state fi les, con-
fi guration fi les, and any other fi les that may reside in the virtual machine direc-

tory. Otherwise, the eff ect on the virtual machine will be similar to pulling the
Marshall_AU3931_C026.indd 608Marshall_AU3931_C026.indd 608 4/13/2006 2:01:29 PM4/13/2006 2:01:29 PM
Other Advanced Topics  609
power cord from the back of the server. When the virtual machine is powered
on, it may or may not boot. If the virtual machine does boot, there is still some
chance that the data may be corrupted in some form. If a virtual machine is go-
ing to be moved from one host server to another, it is safer to power down the
virtual machine rather than suspending it as there can be problems with resum-
ing a suspended machine on a diff erent hardware platform than it was originally
suspended on. And fi nally, while some backup software packages claim to have
open fi le agents, they do not always work reliably when backing up open virtual
disks that are gigabytes in size.  e best implementation is still to power down
the virtual machine prior to backup.
 e processes discussed above can be automated in a number of diff erent
ways to provide a successful backup solution. Most backup software sold today
provides some mechanism to execute batch jobs or scripts. Using one of these
methods, virtual machines can be powered off or suspended as needed before
the backup agent begins copying its fi les. For example, VMware off ers a set of
command-lines that are useful in creating simple batch fi les to perform these
functions (powering off , starting, suspending, and resuming virtual machines).
To power off the virtual machine, the suspend batch fi le should include the
following line:
vmware-cmd <path_to_confi g_fi le>\<confi g_fi le>.vmx stop
Once the virtual machine is powered off , the backup agent can safely begin
backing up the virtual machine’s directory and fi les. Once the backup is com-
plete, the agent can launch the post-backup batch fi le containing the following
line to power on the virtual machine:
vmware-cmd <path_to_confi g_fi le>\<confi g_fi le>.vmx
start
Advantages

•  e entire virtual machine directory can be backed up at one time provid-
ing ease of backup operation.
• Backup processes and methodologies are similar to backing up fi les on a
normal physical server.
• Combining backup agents with scripting and batch fi les allows complete
automation in the backup strategy, and keeps the virtual machines error
free.
Disadvantages
• Any restores are to a single point in time where the data is already consid-
ered stale.
• Individual fi les in a virtual machine cannot be restored.
• Backups and restores using this method can be extremely time consuming
as well as taxing on a server’s processor and network.
Marshall_AU3931_C026.indd 609Marshall_AU3931_C026.indd 609 4/13/2006 2:01:30 PM4/13/2006 2:01:30 PM
610  Advanced Server Virtualization
• Backups require large amounts of space (either disk, tape, or DVD me-
dia)
• If not performed properly, it may result in data loss.
•  e backup is not considered live, which means all virtual machines resid-
ing on the host server that are being backed up must be either powered off
or suspended prior to the backup taking place.
Backing up Individual Virtual Machine Files
without Backup Agents
By far, one of the simplest methods of backing up a virtual machine is to make
use of the virtualization feature known as encapsulation.  is feature allows the
host server to view each virtual machine as a fi le with a .dsk, .vmdk, or .vhd
extension. By taking advantage of this feature, an entire virtual hard disk can be
eff ectively backed up with a simple copy command. Along with base virtual disk
fi les, backup copies of REDO or undo disks, suspended state fi les, and virtual
machine confi guration fi les can also be made. Backing up an individual virtual

machine can be a manual process that is started at any given point in time or it
can be automated through some type of scripting method. Using this simple ap-
proach, it is very easy to restore a virtual machine’s fi le to a diff erent host server
with the assurance that it will register and function just as it did on the host
server on which it was backed up.
Keep in mind that virtual disk fi les should not (typically) be
backed up while the virtual machine is powered on. When
the virtual machine is powered on, the base disk fi le is open
and being written to by the virtual machine. In most cases,
powering off or suspending the virtual machine before making a copy is
the best solution as it closes the virtual disk fi le from actively being written
to and therefore makes it safe to backup. Alternatively, there are other solu-
tions out there that attempt “live” backups, where the virtual machine can
remain powered on. Using VMware’s snapshot feature, a virtual machine
can be placed into REDO mode, where all new writes are captured by a
REDO log fi le rather than writing to the base disk.  is method allows the
base disk to be copied off , and the REDO log fi le can later be committed
back into the base disk. Other methods include “near live” backups, where
downtime may be as short as 1 minute. By using a combination of script-
ing, the virtualization suspend feature and shadow copy (using vshadow.
exe from the Volume Shadow Copy Service SDK), a virtual machine can
be backed up with minimal downtime. Scripting backup solutions is also
explained in more detail in chapter 25.
Marshall_AU3931_C026.indd 610Marshall_AU3931_C026.indd 610 4/13/2006 2:01:30 PM4/13/2006 2:01:30 PM
Other Advanced Topics  611
If the virtual machine disk images are stored on a storage area
network (SAN), use the SAN features supplied by the SAN
vendor to make backup copies of the disk images.  e SAN
management software can be used to schedule checkpoints on
the disk back end to guarantee a backup from a specifi c time frame.

VMware ESX Server 2.5 provides an easy to use tool that supports live back-
ups of running virtual machines.  e tool is named vmsnap.pl. It can list all
virtual machines that are available for backup, and it supports local or remote
backup destinations. To backup a running virtual machine registered as W2K3-
DC-01, the following command can be executed:
./vmsnap.pl –c /root/vmware/W2K3-DC-01/W2K3-DC-
01.vmx –d /virtualmachines/localbackup –l
By executing this command, a live virtual machine registered as W2K3-DC-
01 with its confi guration fi le located at /root/vmware/W2K3-DC-01 is backed
up to a local directory named /virtualmachines/localbackup.
Running ./vmsnap.pl –h provides the following in-
formation.
vmsnap [-a server_name] [-c confi g_fi le] [-d local_
dir] [-R remote_dir] | [-g] | [-h] | [-V] [-l] |
[-m] [-r]
-a server_name Specify an archive server
-c confi g_fi le Specify a VM confi guration fi le to use
for vmsnap
-d local_dir Specify a local directory for vmsnap
-R remote_dir Specify a remote directory for backup
-g List all available VM’s for backup
-h Help
-V Version
-l Perform local backup only
-m Generate the man page for this program
-r Commit the redo logs in case they are already
present
Advantages
• Backups and restores are extremely easy to perform and can be as simple as
using a fi le copy command.

• Expensive third-party software to perform backup and restore procedures
are not needed.
Marshall_AU3931_C026.indd 611Marshall_AU3931_C026.indd 611 4/13/2006 2:01:30 PM4/13/2006 2:01:30 PM
612  Advanced Server Virtualization
• Existing hardware can be used to house and restore virtual machine disk
fi les.
• If using a SAN, fi le consistency is guaranteed by SAN checkpointing.
Disadvantages
• Adds another layer of complexity to the environment since it does not
make use of current backup and restore procedures and methodologies.
• Individual fi les in a virtual machine cannot be restored. A potentially large
multi-gigabyte fi le must be restores to simply restore a single fi le, which
increases restore time.
• Need to checksum verify the fi les to make sure there is no fi le corruption
during the copy process.
• Diffi cult to perform live backups without scripting knowledge.
• Not all SAN solutions are supported by the diff erent virtualization plat-
forms.
• A SAN solution is extremely expensive.
Clustering
Clustering is used today for providing redundancy and performance over that
of a single server machine. On physical clusters the redundancy is not only in
having at least two copies of an operating system cooperatively running, but two
physical machines hosting these operating systems. By running on multiple host
machines, if there is a failure on one of the host machines, the other machine
can take over all of the activities of the failed machine. Clusters can also be
multi-node clusters. Multi-node clusters are those comprised of three or more
clustered systems. Multi-node clusters provide even greater performance and
resiliency than that of a two node cluster.
It is important to realize that the performance gained in a cluster is only

achieved when the cluster has more than one node that is active. An active clus-
ter node is one that participates in providing services to clients actively. A passive
cluster node is one that waits for the failure of another node, then upon recog-
nizing the failure replaces the failed node and becomes and active node. Clusters
with more than one active node at a time are called Active-Active Clusters. Clus-
ters with only one active node at a time are called Active-Passive Clusters. Per-
formance is only enhanced on Active-Active Clusters as they can service a larger
number of requests, due to more compute power being available.  e danger
in Active-Active Clusters is that is if all of the nodes are highly utilized and one
fails, then the people requesting services from the cluster will notice a loss of
performance.  e fl ip side of this is that it is better to see slow performance than
to see a failure in service all together.
Marshall_AU3931_C026.indd 612Marshall_AU3931_C026.indd 612 4/13/2006 2:01:30 PM4/13/2006 2:01:30 PM
Other Advanced Topics  613
Clustering Disk Technologies
 ere are several clustering disk technologies that are fundamental to clustered
environments.  ese clustering disk technologies include:
• Shared SCSI
• iSCSI
• SAN
Shared SCSI is the oldest disk clustering technology available. Shared SCSI sim-
ply ties a SCSI disk array to a pair of SCSI disk controllers.  e SCSI controllers
are said to “share the SCSI disk array across a shared bus.”  is sharing allows
for the quorum to be created and data to be simultaneously read by all clustered
nodes.  ere can only be one cluster node that can be designated to write at a
time.  e reason for only a single node being able to do a write is because if mul-
tiple nodes wrote data at the same time in the same place, data loss would occur.
Shared SCSI is commonly used for two node packaged cluster solutions.
 e newest and least expensive of the three disk technologies is iSCI. It is
based on encapsulating SCSI commands inside of IP packets. iSCSI runs across

standard Ethernet and can use standard 100Mb or 1000Mb network cards
(when 10Gb Ethernet becomes available, iSCSI will support this as well). iSCSI
is based on two components, an initiator and a target.  e target is the shared
storage location (this is analogous to the shared disk array).  e initiator is the
equivalent to the controller.  e target can be driven by software running on
Linux or Windows, or by an appliance such as a Network Appliance Filer.  e
initiator is run as a driver on a server and appears as a SCSI hard disk to the
operating system.  e initiator can also be a special iSCSI controller card.  is
card is a hybrid between an Ethernet network card and a SCSI controller. It
appears to the server’s operating system as a SCSI controller with a SCSI hard
disk connected to it; however it is actually running a special embedded software
program that allows it to communicate over the network independently of the
server’s operating system.
SAN is the most expensive solution of the three storage technologies, however
it does provide the best performance. A SAN is comprised of several components
including an HBA (Host Bus Adapter), a Fiber Channel Switch, and a disk ar-
ray. Each of these components requires special confi guration to work properly.
 e HBA acts as a SCSI hard disk controller by providing access through the Fi-
ber Channel switch to the disk array.  e Fiber Channel switch connects many
servers to the disk array.  e disk array stores all of the appropriate data just like
a standard SCSI array (in fact, some SAN arrays are comprised of SCSI-based
disks, while others are Fiber Channel-based disks.) SANs also require specialized
SAN management and confi guration software. SANs must have security and
confi guration information setup and maintained. SANs are complex and usually
Marshall_AU3931_C026.indd 613Marshall_AU3931_C026.indd 613 4/13/2006 2:01:30 PM4/13/2006 2:01:30 PM

×