Virtual Disk Development Kit
This
SDK is most frequently used to implement backup and restore applications. This section is a
walk-though of code to restore a virtual machine.
The Virtual Disk Development Kit (VDDK) is a
set of libraries and utilities that enable software developers to create applications
that can access and manipulate virtual disks used for storage by products such as
vSphere, Workstation, and Fusion. VDDK is a valuable tool for developers of storage and
backup applications.
VDDK provides APIs for backup and recovery,
cloning, and other operations on virtual disks. With VDDK, developers can create
applications that perform backup and restore operations for virtual machines, as well as
applications that create clones or snapshots of virtual disks. VDDK also includes
utilities for converting virtual disks between different formats and for creating
virtual disks from scratch.
Safekeeping – a VDDK Use
Case
Based on VDDK, Safekeeping is a "proof of
concept" application to demonstrate backup and restore of vSphere VMs, vApps, and
first-class disk. You can find its source code on Github, as linked from the
developer.broadcom.com SDK landing page for VDDK. This section focuses on
RestoreVm.java
, a source code module that recovers a VM
previously saved by the BackupVm.java
module. safekeeping backup vm:myTestVM safekeeping restore vm:myTestVM
After package and interface imports,
class
RestoreVm
extends method
AbstractRestoreFcoWithDisk
, which in turn extends method
AbstractRestoreFco
for restoring a first class object (FCO) but
with disk, as in a vSphere VM. (A vApp is an FCO that includes VMs but not disk.)
RestoreVm
must be passed a VIM connection, user-specified
command options, and a log file. The
checkOptions
method
parses passed-in options, including (if specified) VM name, parent vApp, data
center, resource pool, datastore, and storage profile. If the destination VM folder
cannot be restored to, RestoreVM
uses a different location. Snapshot is a typical operation for both
backup and restore. The
createSnapshot
method takes a vSphere
snapshot so the VM can continue running while being restored, and also to quiesce
the virtual disk while it is being replaced. If a snapshot is unnecessary because
the VM does not exist, snapshot is skipped. If the VM is being restored because it no
longer runs, the
destroy
method deletes the old VM so it does not
conflict with the VM to be restored. The
disksMapping
method
is one of the most complicated in RestoreVM
. It needs to know
the number of disks to restore, the storage profile of disks, and the location of
disk data in the backup. This information comes from the
managedInfo
object, combined with the generated
profile
object. Once virtual disks are correctly mapped,
they are restored by the
disksRestore
method. This could take a lot
of time, so the final
loop blocks until disks are restored. If a failed VM was not deleted, the
manageExistingFco
method deals with naming conflicts between
the pre-existing VM and the VM to be restored. The VM might be part of a vApp, which
allows duplicate names. If the pre-existing VM is powered on, the backed-up VM
cannot be restored. With duplicate names, the user can be given a chance to rename
VM to be restored.The
networksMapping
method determines what networks the backed-up VM used for each virtual NIC, and
attempts to reformulate them using existing networks. Finally the
powerOn
method starts up the restored VM so it can be configured. A VM can be powered on
before the Guest OS is ready to run.The
reconfig
method
configures the VM based on profiles of its virtual disks and networks determined
above. Configuration may be interrupted by a fault. Similar
reconfigDisk
and reconfigNetwork
methods take
care of disk backing and network backing for the VM to be restored. The
removeSnapshot
methods cleans up the snapshot previously
created.Finally the
restore
method, the main restore function, puts this all together. It retrieves the restore
profile and calls snapshot, start-access, disksRestore
, and
end-access in an if statement. This is where the C++ functions in VixDiskLib get
called (start and end access prevent vMotion during restore).if (createSnapshot(rar) && startVddkAccess(rar, jvddk) && disksRestore(rar, jvddk) && endVddkAccess(rar, jvddk)) { final String st = rar.getFcoToString() + " Restore Success - start cleaning"; }
The restored VM could either be newly
created with the
restoreByCreate
method, or recovered with import
of virtual disk data by the restoreByImport
method. It is the
restoreMetadata
method that decides which. The
restoreManagedInfo
method takes care of items managed by
vCenter Server, such as folder groupings and resource pools.