Working with Virtual Disk Data
The virtual disk library reads and writes sectors of data. It has no interface for character or byte-oriented I/O.
Reading and Writing Local Disk
Demonstrating random I/O, this function reads a sector at a time backwards through a VMDK. If it sees the string “VmWare” it substitutes the string “VMware” in its place and writes the sector back to VMDK.
#include <string>
static void DoEdit(void)/
{
VixDisk disk(appGlobals.connection, appGlobals.diskPath, appGlobals.openFlags);
uint8 buf[VIXDISKLIB_SECTOR_SIZE];
VixDiskLibSectorType i;
string str;
for (i = appGlobals.numSectors; i >= 0; i--) {
VixError vixError;
vixError = VixDiskLib_Read(disk.Handle(), appGlobals.startSector + i, 1, buf);
CHECK_AND_THROW(vixError);
str = buf;
if (pos = str.find("VmWare", 0)) {
str.replace(pos, 5, "VMware");
buf = str;
vixError = VixDiskLib_Write(disk.Handle(), appGlobals.startSector + i, 1, buf);
CHECK_AND_THROW(vixError);
}
}
}
Reading and Writing Remote Disk
The DoEdit() function is similar for remote managed virtual disk on ESXi hosts, but beforehand you must call VixDiskLib_Connect() with authentication credentials instead of passing NULL parameters.
if (appGlobals.isRemote) {
cnxParams.vmxSpec = NULL;
cnxParams.serverName = appGlobals.host;
cnxParams.credType = VIXDISKLIB_CRED_UID;
cnxParams.creds.uid.userName = appGlobals.userName;
cnxParams.creds.uid.password = appGlobals.password;
cnxParams.port = appGlobals.port;
}
VixError vixError = VixDiskLib_Init(1, 0, NULL, NULL, NULL, NULL);
CHECK_AND_THROW(vixError);
vixError = VixDiskLib_Connect(&cnxParams, &appGlobals.connection);
Deleting a Disk (Unlink)
The function to delete virtual disk files is VixDiskLib_Unlink(). It takes two arguments: a connection and a VMDK filename.
vixError = VixDiskLib_Unlink(appGlobals.connection, appGlobals.diskPath);
Effects of Deleting a Virtual Disk
When you delete a VMDK, you lose all the information it contained. In most cases, the host operating system prevents you from doing this when a virtual machine is running. However, if you delete a VMDK with its virtual machine powered off, that guest OS becomes unbootable.
Renaming a Disk
The function to rename virtual disk files is VixDiskLib_Rename(). It takes two arguments: the old and the new VMDK filenames.
vixError = VixDiskLib_Rename(oldGlobals.diskpath, newGlobals.diskpath);
Effects of Renaming a Virtual Disk
The server expects VMDK files of its guest OS virtual machines to be in a predictable location. Any file accesses that occur during renaming might cause I/O failure and possibly cause a guest OS to fail.