I wrote a function that will extend a disk on a VM, which "works" in that it doesn't throw any exceptions, but when I boot the VM afterwards it fails to boot. I'm wondering if I possibly left out some steps I need to do or if I'm using the wrong API calls? Below is my code:
public void ExtendVirtualDisk(VirtualMachine vmGuest, int diskNum, int sizeInKB, bool eagerZero)
{
Datacenter dataCenter = (Datacenter)_vClient.FindEntityView(typeof(Datacenter), null, null, null);
if (dataCenter == null)
throw new VMwareToolsException("Unable to find datacenter object!");
VirtualDiskManager vDiskMgr = (VirtualDiskManager)_vClient.GetView(_vClient.ServiceContent.VirtualDiskManager, null);
if (vDiskMgr == null)
throw new VMwareToolsException("Unable to retrieve VirtualDiskManager");
var diskToExtend = (from device in vmGuest.Config.Hardware.Device
where (device is VirtualDisk) &&
(device.UnitNumber == diskNum)
select device).FirstOrDefault();
if(diskToExtend == null)
throw new VMwareToolsException(string.Format("Unable to locate disk #{0} on VM {1}", diskNum, vmGuest.Name));
if (((VirtualDisk)diskToExtend).CapacityInKB == sizeInKB)
{
Log.Info("Target disk matches given size. Not expanding...");
return;
}
VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)diskToExtend.Backing;
var morExtendTask = vDiskMgr.ExtendVirtualDisk_Task(backingInfo.FileName, dataCenter.MoRef, sizeInKB, eagerZero);
Task extendTask = (Task)_vClient.GetView(morExtendTask, null);
while (extendTask.Info.State == TaskInfoState.running)
{
System.Threading.Thread.Sleep(1000);
extendTask.UpdateViewData();
int progress = extendTask.Info.Progress.HasValue ? (int)extendTask.Info.Progress : 0;
this.OnExtendDiskProgress(this, new ExtendDiskEventArgs(progress));
}
extendTask.UpdateViewData();
if (extendTask.Info.State == TaskInfoState.error)
throw new VMwareToolsException(string.Format("Failed to extend disk. Reason: {0}",
extendTask.Info.Error.LocalizedMessage));
}