use of com.vmware.vim25.ManagedObjectReference in project cloudstack by apache.
the class VmwareResource method prepareSecondaryDatastoreOnSpecificHost.
public synchronized ManagedObjectReference prepareSecondaryDatastoreOnSpecificHost(String storeUrl, VmwareHypervisorHost hyperHost) throws Exception {
String storeName = getSecondaryDatastoreUUID(storeUrl);
URI uri = new URI(storeUrl);
ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", ""));
if (morDatastore == null)
throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl);
return morDatastore;
}
use of com.vmware.vim25.ManagedObjectReference in project cloudstack by apache.
the class VmwareResource method execute.
/**
* UnregisterNicCommand is used to remove a portgroup created for this
* specific nic. The portgroup will have the name set to the UUID of the
* nic. Introduced to cleanup the portgroups created for each nic that is
* plugged into an lswitch (Nicira NVP plugin)
*
* @param cmd
* @return
*/
protected Answer execute(UnregisterNicCommand cmd) {
s_logger.info("Executing resource UnregisterNicCommand: " + _gson.toJson(cmd));
if (_guestTrafficInfo == null) {
return new Answer(cmd, false, "No Guest Traffic Info found, unable to determine where to clean up");
}
try {
if (_guestTrafficInfo.getVirtualSwitchType() != VirtualSwitchType.StandardVirtualSwitch) {
// on the standard switches
return new Answer(cmd, true, "Nothing to do");
}
s_logger.debug("Cleaning up portgroup " + cmd.getNicUuid() + " on switch " + _guestTrafficInfo.getVirtualSwitchName());
VmwareContext context = getServiceContext();
VmwareHypervisorHost host = getHyperHost(context);
ManagedObjectReference clusterMO = host.getHyperHostCluster();
// Get a list of all the hosts in this cluster
@SuppressWarnings("unchecked") List<ManagedObjectReference> hosts = (List<ManagedObjectReference>) context.getVimClient().getDynamicProperty(clusterMO, "host");
if (hosts == null) {
return new Answer(cmd, false, "No hosts in cluster, which is pretty weird");
}
for (ManagedObjectReference hostMOR : hosts) {
HostMO hostMo = new HostMO(context, hostMOR);
hostMo.deletePortGroup(cmd.getNicUuid().toString());
s_logger.debug("Removed portgroup " + cmd.getNicUuid() + " from host " + hostMo.getHostName());
}
return new Answer(cmd, true, "Unregistered resources for NIC " + cmd.getNicUuid());
} catch (Exception e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "UnregisterVMCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
return new Answer(cmd, false, msg);
}
}
use of com.vmware.vim25.ManagedObjectReference in project cloudstack by apache.
the class VmwareResource method getDatastoreThatRootDiskIsOn.
private DatastoreMO getDatastoreThatRootDiskIsOn(HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails, DiskTO[] disks) {
Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null;
for (DiskTO vol : disks) {
if (vol.getType() == Volume.Type.ROOT) {
Map<String, String> details = vol.getDetails();
boolean managed = false;
if (details != null) {
managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
}
if (managed) {
String datastoreName = VmwareResource.getDatastoreName(details.get(DiskTO.IQN));
rootDiskDataStoreDetails = dataStoresDetails.get(datastoreName);
break;
} else {
DataStoreTO primaryStore = vol.getData().getDataStore();
rootDiskDataStoreDetails = dataStoresDetails.get(primaryStore.getUuid());
break;
}
}
}
if (rootDiskDataStoreDetails != null) {
return rootDiskDataStoreDetails.second();
}
return null;
}
use of com.vmware.vim25.ManagedObjectReference in project cloudstack by apache.
the class VmwareResource method execute.
private Answer execute(ResizeVolumeCommand cmd) {
String path = cmd.getPath();
String vmName = cmd.getInstanceName();
long newSize = cmd.getNewSize() / 1024;
long oldSize = cmd.getCurrentSize() / 1024;
boolean useWorkerVm = false;
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
String poolId = cmd.getPoolUuid();
VirtualMachineMO vmMo = null;
DatastoreMO dsMo = null;
ManagedObjectReference morDS = null;
String vmdkDataStorePath = null;
try {
if (newSize < oldSize) {
throw new Exception("VMware doesn't support shrinking volume from larger size: " + oldSize / (1024 * 1024) + " GB to a smaller size: " + newSize / (1024 * 1024) + " GB");
} else if (newSize == oldSize) {
return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
}
if (vmName.equalsIgnoreCase("none")) {
// we need to spawn a worker VM to attach the volume to and
// resize the volume.
useWorkerVm = true;
vmName = getWorkerName(getServiceContext(), cmd, 0);
morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
s_logger.info("Create worker VM " + vmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, vmName);
if (vmMo == null) {
throw new Exception("Unable to create a worker VM for volume resize");
}
synchronized (this) {
vmdkDataStorePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, path + ".vmdk");
vmMo.attachDisk(new String[] { vmdkDataStorePath }, morDS);
}
}
// find VM through datacenter (VM is not at the target host yet)
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String msg = "VM " + vmName + " does not exist in VMware datacenter";
s_logger.error(msg);
throw new Exception(msg);
}
Pair<VirtualDisk, String> vdisk = vmMo.getDiskDevice(path);
if (vdisk == null) {
if (s_logger.isTraceEnabled())
s_logger.trace("resize volume done (failed)");
throw new Exception("No such disk device: " + path);
}
// IDE virtual disk cannot be re-sized if VM is running
if (vdisk.second() != null && vdisk.second().contains("ide")) {
throw new Exception("Re-sizing a virtual disk over IDE controller is not supported in VMware hypervisor. " + "Please re-try when virtual disk is attached to a VM using SCSI controller.");
}
if (vdisk.second() != null && !vdisk.second().toLowerCase().startsWith("scsi")) {
s_logger.error("Unsupported disk device bus " + vdisk.second());
throw new Exception("Unsupported disk device bus " + vdisk.second());
}
VirtualDisk disk = vdisk.first();
if ((VirtualDiskFlatVer2BackingInfo) disk.getBacking() != null && ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent() != null) {
s_logger.error("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
throw new Exception("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
}
String vmdkAbsFile = getAbsoluteVmdkFile(disk);
if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
}
disk.setCapacityInKB(newSize);
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(disk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure VM to resize disk. vmName: " + vmName);
}
return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
} catch (Exception e) {
s_logger.error("Unable to resize volume", e);
String error = "Failed to resize volume: " + e.getMessage();
return new ResizeVolumeAnswer(cmd, false, error);
} finally {
try {
if (useWorkerVm == true) {
s_logger.info("Destroy worker VM after volume resize");
vmMo.detachDisk(vmdkDataStorePath, false);
vmMo.destroy();
}
} catch (Throwable e) {
s_logger.info("Failed to destroy worker VM: " + vmName);
}
}
}
use of com.vmware.vim25.ManagedObjectReference in project cloudstack by apache.
the class VmwareResource method prepareSecondaryDatastoreOnHost.
public synchronized ManagedObjectReference prepareSecondaryDatastoreOnHost(String storeUrl) throws Exception {
String storeName = getSecondaryDatastoreUUID(storeUrl);
URI uri = new URI(storeUrl);
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", ""));
if (morDatastore == null)
throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl);
return morDatastore;
}
Aggregations