use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
the class VirtualMachineMO method getHttpAccessPathInfo.
/**
* Retrieve path info to access VM files via vSphere web interface
* @return [0] vm-name, [1] data-center-name, [2] datastore-name
* @throws Exception
*/
public String[] getHttpAccessPathInfo() throws Exception {
String[] pathInfo = new String[3];
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
VirtualMachineFileInfo fileInfo = getFileInfo();
String vmxFilePath = fileInfo.getVmPathName();
String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
assert (vmxPathTokens.length == 4);
// vSphere vm name
pathInfo[1] = vmxPathTokens[1].trim();
// vSphere datacenter name
pathInfo[2] = dcInfo.second();
// vSphere datastore name
pathInfo[3] = vmxPathTokens[0].trim();
return pathInfo;
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method setSnapshotDirectory.
// snapshot directory in format of: /vmfs/volumes/<datastore name>/<path>
@Deprecated
public void setSnapshotDirectory(String snapshotDir) throws Exception {
VirtualMachineFileInfo fileInfo = getFileInfo();
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
String vmxUrl = _context.composeDatastoreBrowseUrl(dcInfo.second(), fileInfo.getVmPathName());
byte[] vmxContent = _context.getResourceContent(vmxUrl);
BufferedReader in = null;
BufferedWriter out = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean replaced = false;
try {
in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(vmxContent)));
out = new BufferedWriter(new OutputStreamWriter(bos));
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("workingDir")) {
replaced = true;
out.write(String.format("workingDir=\"%s\"", snapshotDir));
out.newLine();
} else {
out.write(line);
out.newLine();
}
}
if (!replaced) {
out.newLine();
out.write(String.format("workingDir=\"%s\"", snapshotDir));
out.newLine();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
_context.uploadResourceContent(vmxUrl, bos.toByteArray());
// It seems that I don't need to do re-registration. VMware has bug in writing the correct snapshot's VMDK path to
// its disk backing info anyway.
// redoRegistration();
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class HypervisorHostHelper method createBlankVm.
public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent) throws Exception {
if (s_logger.isInfoEnabled())
s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
// VM config basics
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
// Scsi controller
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
scsiController.setSharedBus(VirtualSCSISharing.noSharing);
scsiController.setBusNumber(0);
scsiController.setKey(1);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
DatastoreMO dsMo = new DatastoreMO(host.getContext(), morDs);
fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
vmConfig.setFiles(fileInfo);
VirtualMachineVideoCard videoCard = new VirtualMachineVideoCard();
videoCard.setControllerKey(100);
videoCard.setUseAutoDetect(true);
VirtualDeviceConfigSpec videoDeviceSpec = new VirtualDeviceConfigSpec();
videoDeviceSpec.setDevice(videoCard);
videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec, videoDeviceSpec });
if (host.createVm(vmConfig)) {
VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
assert (vmMo != null);
int ideControllerKey = -1;
while (ideControllerKey < 0) {
ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
if (ideControllerKey >= 0)
break;
s_logger.info("Waiting for IDE controller be ready in VM: " + vmName);
Thread.sleep(1000);
}
if (snapshotDirToParent) {
String snapshotDir = String.format("/vmfs/volumes/%s/", dsMo.getName());
s_logger.info("Switch snapshot working directory to " + snapshotDir + " for " + vmName);
vmMo.setSnapshotDirectory(snapshotDir);
// Don't have a good way to test if the VM is really ready for use through normal API after configuration file manipulation,
// delay 3 seconds
Thread.sleep(3000);
}
s_logger.info("Blank VM: " + vmName + " is ready for use");
return true;
}
return false;
}
use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
the class HypervisorHostHelper method createBlankVm.
public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, String vmInternalCSName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent, Pair<String, String> controllerInfo, Boolean systemVm) throws Exception {
if (s_logger.isInfoEnabled())
s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
VirtualDeviceConfigSpec controllerSpec = null;
// VM config basics
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
if (vmInternalCSName == null)
vmInternalCSName = vmName;
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
String recommendedController = host.getRecommendedDiskController(guestOsIdentifier);
String newRootDiskController = controllerInfo.first();
String newDataDiskController = controllerInfo.second();
if (DiskControllerType.getType(controllerInfo.first()) == DiskControllerType.osdefault) {
newRootDiskController = recommendedController;
}
if (DiskControllerType.getType(controllerInfo.second()) == DiskControllerType.osdefault) {
newDataDiskController = recommendedController;
}
Pair<String, String> updatedControllerInfo = new Pair<String, String>(newRootDiskController, newDataDiskController);
String scsiDiskController = HypervisorHostHelper.getScsiController(updatedControllerInfo, recommendedController);
// If there is requirement for a SCSI controller, ensure to create those.
if (scsiDiskController != null) {
int busNum = 0;
int maxControllerCount = VmwareHelper.MAX_SCSI_CONTROLLER_COUNT;
if (systemVm) {
maxControllerCount = 1;
}
while (busNum < maxControllerCount) {
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec = getControllerSpec(DiskControllerType.getType(scsiDiskController).toString(), busNum);
vmConfig.getDeviceChange().add(scsiControllerSpec);
busNum++;
}
}
if (guestOsIdentifier.startsWith("darwin")) {
//Mac OS
s_logger.debug("Add USB Controller device for blank Mac OS VM " + vmName);
//For Mac OS X systems, the EHCI+UHCI controller is enabled by default and is required for USB mouse and keyboard access.
VirtualDevice usbControllerDevice = VmwareHelper.prepareUSBControllerDevice();
VirtualDeviceConfigSpec usbControllerSpec = new VirtualDeviceConfigSpec();
usbControllerSpec.setDevice(usbControllerDevice);
usbControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(usbControllerSpec);
}
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
DatastoreMO dsMo = new DatastoreMO(host.getContext(), morDs);
fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
vmConfig.setFiles(fileInfo);
VirtualMachineVideoCard videoCard = new VirtualMachineVideoCard();
videoCard.setControllerKey(100);
videoCard.setUseAutoDetect(true);
VirtualDeviceConfigSpec videoDeviceSpec = new VirtualDeviceConfigSpec();
videoDeviceSpec.setDevice(videoCard);
videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(videoDeviceSpec);
if (host.createVm(vmConfig)) {
// Here, when attempting to find the VM, we need to use the name
// with which we created it. This is the only such place where
// we need to do this. At all other places, we always use the
// VM's internal cloudstack generated name. Here, we cannot use
// the internal name because we can set the internal name into the
// VM's custom field CLOUD_VM_INTERNAL_NAME only after we create
// the VM.
VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
assert (vmMo != null);
vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME, vmInternalCSName);
int ideControllerKey = -1;
while (ideControllerKey < 0) {
ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
if (ideControllerKey >= 0)
break;
s_logger.info("Waiting for IDE controller be ready in VM: " + vmInternalCSName);
Thread.sleep(1000);
}
return true;
}
return false;
}
use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
the class HypervisorHostHelper method createWorkerVM.
public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception {
// Allow worker VM to float within cluster so that we will have better chance to
// create it successfully
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (morCluster != null)
hyperHost = new ClusterMO(hyperHost.getContext(), morCluster);
VirtualMachineMO workingVM = null;
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
vmConfig.setMemoryMB((long) 4);
vmConfig.setNumCPUs(1);
vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
fileInfo.setVmPathName(dsMo.getDatastoreRootPath());
vmConfig.setFiles(fileInfo);
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(0);
scsiController.setKey(1);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
if (hyperHost.createVm(vmConfig)) {
// Ugly work-around, it takes time for newly created VM to appear
for (int i = 0; i < 10 && workingVM == null; i++) {
workingVM = hyperHost.findVmOnHyperHost(vmName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
s_logger.debug("[ignored] interupted while waiting to config vm.");
}
}
}
if (workingVM != null) {
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER, "true");
String workerTag = String.format("%d-%s", System.currentTimeMillis(), hyperHost.getContext().getStockObject("noderuninfo"));
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER_TAG, workerTag);
}
return workingVM;
}
Aggregations