Do not set user-configurable details for deploy-as-is VMs

This commit is contained in:
nvazquez 2020-09-19 23:23:59 -03:00 committed by Harikrishna Patnala
parent 9c162c6de9
commit 61e7625d49
3 changed files with 370 additions and 320 deletions

View File

@ -1749,38 +1749,15 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
VirtualMachineFileLayoutEx existingVmFileLayout = null;
List<DatastoreMO> existingDatastores = new ArrayList<DatastoreMO>();
DeployAsIsInfoTO deployAsIsInfo = vmSpec.getDeployAsIsInfo();
boolean deployAsIs = deployAsIsInfo != null;
Pair<String, String> names = composeVmNames(vmSpec);
String vmInternalCSName = names.first();
String vmNameOnVcenter = names.second();
String dataDiskController = vmSpec.getDetails().get(VmDetailConstants.DATA_DISK_CONTROLLER);
String rootDiskController = vmSpec.getDetails().get(VmDetailConstants.ROOT_DISK_CONTROLLER);
DiskTO rootDiskTO = null;
String bootMode = null;
if (vmSpec.getDetails().containsKey(VmDetailConstants.BOOT_MODE)) {
bootMode = vmSpec.getDetails().get(VmDetailConstants.BOOT_MODE);
}
if (null == bootMode) {
bootMode = ApiConstants.BootType.BIOS.toString();
}
// If root disk controller is scsi, then data disk controller would also be scsi instead of using 'osdefault'
// This helps avoid mix of different scsi subtype controllers in instance.
if (DiskControllerType.osdefault == DiskControllerType.getType(dataDiskController) && DiskControllerType.lsilogic == DiskControllerType.getType(rootDiskController)) {
dataDiskController = DiskControllerType.scsi.toString();
}
// Validate the controller types
dataDiskController = DiskControllerType.getType(dataDiskController).toString();
rootDiskController = DiskControllerType.getType(rootDiskController).toString();
if (DiskControllerType.getType(rootDiskController) == DiskControllerType.none) {
throw new CloudRuntimeException("Invalid root disk controller detected : " + rootDiskController);
}
if (DiskControllerType.getType(dataDiskController) == DiskControllerType.none) {
throw new CloudRuntimeException("Invalid data disk controller detected : " + dataDiskController);
}
Pair<String, String> controllerInfo = new Pair<String, String>(rootDiskController, dataDiskController);
String bootMode = getBootModeFromVmSpec(vmSpec, deployAsIs);
Pair<String, String> controllerInfo = getControllerInfoFromVmSpec(vmSpec, deployAsIs);
Boolean systemVm = vmSpec.getType().isUsedBySystem();
// Thus, vmInternalCSName always holds i-x-y, the cloudstack generated internal VM name.
@ -1802,19 +1779,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
DiskTO[] specDisks = vmSpec.getDisks();
DeployAsIsInfoTO deployAsIsInfo = vmSpec.getDeployAsIsInfo();
boolean installAsIs = deployAsIsInfo != null;
if (installAsIs && dcMo.findVm(vmInternalCSName) == null) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Deploying OVA from as is");
}
String deployAsIsTemplate = deployAsIsInfo.getTemplatePath();
String destDatastore = deployAsIsInfo.getDestStoragePool();
vmInVcenter = _storageProcessor.cloneVMFromTemplate(deployAsIsTemplate, vmInternalCSName, destDatastore);
mapSpecDisksToClonedDisks(vmInVcenter, vmInternalCSName, specDisks);
}
String guestOsId = translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs(), vmSpec.getPlatformEmulator()).value();
String guestOsId = getGuestOsIdFromVmSpec(vmSpec, deployAsIs);
DiskTO[] disks = validateDisks(vmSpec.getDisks());
assert (disks.length > 0);
NicTO[] nics = vmSpec.getNics();
@ -1840,6 +1805,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
int firstScsiControllerBusNum = 0;
int numScsiControllerForSystemVm = 1;
boolean hasSnapshot = false;
if (vmMo != null) {
s_logger.info("VM " + vmInternalCSName + " already exists, tear down devices for reconfiguration");
if (getVmPowerState(vmMo) != PowerState.PowerOff)
@ -1849,15 +1815,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
diskInfoBuilder = vmMo.getDiskInfoBuilder();
hasSnapshot = vmMo.hasSnapshot();
nicDevices = vmMo.getNicDevices();
if (!hasSnapshot)
vmMo.tearDownDevices(new Class<?>[]{VirtualDisk.class, VirtualEthernetCard.class});
else
vmMo.tearDownDevices(new Class<?>[]{VirtualEthernetCard.class});
if (systemVm) {
ensureScsiDiskControllers(vmMo, systemVmScsiControllerType.toString(), numScsiControllerForSystemVm, firstScsiControllerBusNum);
} else {
ensureDiskControllers(vmMo, controllerInfo);
}
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
} else {
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
assert (morDc != null);
@ -1875,17 +1836,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
diskInfoBuilder = vmMo.getDiskInfoBuilder();
hasSnapshot = vmMo.hasSnapshot();
if (!hasSnapshot)
vmMo.tearDownDevices(new Class<?>[]{VirtualDisk.class, VirtualEthernetCard.class});
else
vmMo.tearDownDevices(new Class<?>[]{VirtualEthernetCard.class});
if (systemVm) {
// System volumes doesn't require more than 1 SCSI controller as there is no requirement for data volumes.
ensureScsiDiskControllers(vmMo, systemVmScsiControllerType.toString(), numScsiControllerForSystemVm, firstScsiControllerBusNum);
} else {
ensureDiskControllers(vmMo, controllerInfo);
}
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
} else {
// If a VM with the same name is found in a different cluster in the DC, unregister the old VM and configure a new VM (cold-migration).
VirtualMachineMO existingVmInDc = dcMo.findVm(vmInternalCSName);
@ -1897,6 +1851,16 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
existingDatastores = existingVmInDc.getAllDatastores();
existingVmInDc.unregisterVm();
}
if (deployAsIs) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Deploying OVA from template as-is");
}
String deployAsIsTemplate = deployAsIsInfo.getTemplatePath();
String destDatastore = deployAsIsInfo.getDestStoragePool();
vmMo = _storageProcessor.cloneVMFromTemplate(deployAsIsTemplate, vmInternalCSName, destDatastore);
mapSpecDisksToClonedDisks(vmMo, vmInternalCSName, specDisks);
} else {
Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null;
for (DiskTO vol : disks) {
if (vol.getType() == Volume.Type.ROOT) {
@ -1938,6 +1902,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
throw new Exception("Failed to create VM. vmName: " + vmInternalCSName);
}
}
}
vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
if (vmMo == null) {
@ -1945,9 +1910,9 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
}
// The number of disks changed must be 0 for install as is, as the VM is a clone
//int disksChanges = !installAsIs ? disks.length : 0;
int totalChangeDevices = disks.length + nics.length;
// The number of disks changed must be 0 for install as is, as the VM is a clone from the template as-is
int disksChanges = !deployAsIs ? disks.length : 0;
int totalChangeDevices = disksChanges + nics.length;
int hackDeviceCount = 0;
if (diskInfoBuilder != null) {
hackDeviceCount += diskInfoBuilder.getDiskCount();
@ -1964,7 +1929,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
if (vmSpec.getType() != VirtualMachine.Type.User) {
// system VM needs a patch ISO
totalChangeDevices++;
} else {
} else if (!deployAsIs) {
volIso = getIsoDiskTO(disks);
if (volIso == null)
totalChangeDevices++;
@ -1972,9 +1937,18 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VmwareHelper.setBasicVmConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), getReservedCpuMHZ(vmSpec), (int) (vmSpec.getMaxRam() / (1024 * 1024)),
getReservedMemoryMb(vmSpec), guestOsId, vmSpec.getLimitCpuUse());
int i = 0;
int ideUnitNumber = 0;
int scsiUnitNumber = 0;
int ideControllerKey = vmMo.getIDEDeviceControllerKey();
int scsiControllerKey = vmMo.getScsiDeviceControllerKeyNoException();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[totalChangeDevices];
DiskTO[] sortedDisks = sortVolumesByDeviceId(disks);
VmwareHelper.setBasicVmConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), getReservedCpuMHZ(vmSpec), (int) (vmSpec.getMaxRam() / (1024 * 1024)),
getReservedMemoryMb(vmSpec), guestOsId, vmSpec.getLimitCpuUse(), deployAsIs);
if (!deployAsIs) {
// Check for multi-cores per socket settings
int numCoresPerSocket = 1;
String coresPerSocket = vmSpec.getDetails().get(VmDetailConstants.CPU_CORE_PER_SOCKET);
@ -2001,12 +1975,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
configNestedHVSupport(vmMo, vmSpec, vmConfigSpec);
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[totalChangeDevices];
int i = 0;
int ideUnitNumber = 0;
int scsiUnitNumber = 0;
int ideControllerKey = vmMo.getIDEDeviceControllerKey();
int scsiControllerKey = vmMo.getScsiDeviceControllerKeyNoException();
int controllerKey;
//
@ -2106,14 +2074,13 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
//
// Setup ROOT/DATA disk devices
//
DiskTO[] sortedDisks = sortVolumesByDeviceId(disks);
for (DiskTO vol : sortedDisks) {
if (vol.getType() == Volume.Type.ISO)
continue;
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(diskInfoBuilder, vol, hyperHost, context);
controllerKey = getDiskController(matchingExistingDisk, vol, vmSpec, ideControllerKey, scsiControllerKey);
String diskController = getDiskController(vmMo, matchingExistingDisk, vol, new Pair<String, String>(rootDiskController, dataDiskController));
String diskController = getDiskController(vmMo, matchingExistingDisk, vol, controllerInfo);
if (DiskControllerType.getType(diskController) == DiskControllerType.osdefault) {
diskController = vmMo.getRecommendedDiskController(null);
@ -2214,6 +2181,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
s_logger.debug("USB Controller device exists on VM Start for Mac OS VM " + vmInternalCSName);
}
}
}
//
// Setup NIC devices
@ -2318,6 +2286,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
// pass boot arguments through machine.id & perform customized options to VMX
ArrayList<OptionValue> extraOptions = new ArrayList<OptionValue>();
configBasicExtraOption(extraOptions, vmSpec);
if (deployAsIs) {
setDeployAsIsProperties(vmMo, deployAsIsInfo, vmConfigSpec);
configureVNC(vmSpec, extraOptions, vmConfigSpec, hyperHost, vmInternalCSName);
} else {
configNvpExtraOption(extraOptions, vmSpec, nicUuidToDvSwitchUuid);
configCustomExtraOption(extraOptions, vmSpec);
@ -2331,19 +2304,13 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
extraOptions.add(option);
}
// config VNC
String keyboardLayout = null;
if (vmSpec.getDetails() != null)
keyboardLayout = vmSpec.getDetails().get(VmDetailConstants.KEYBOARD);
vmConfigSpec.getExtraConfig()
.addAll(Arrays.asList(configureVnc(extraOptions.toArray(new OptionValue[0]), hyperHost, vmInternalCSName, vmSpec.getVncPassword(), keyboardLayout)));
configureVNC(vmSpec, extraOptions, vmConfigSpec, hyperHost, vmInternalCSName);
// config video card
configureVideoCard(vmMo, vmSpec, vmConfigSpec);
setDeployAsIsProperties(vmMo, deployAsIsInfo, vmConfigSpec);
setBootOptions(vmSpec, bootMode, vmConfigSpec);
}
//
// Configure VM
@ -2358,7 +2325,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
// Resizing root disk only when explicit requested by user
final Map<String, String> vmDetails = cmd.getVirtualMachine().getDetails();
if (rootDiskTO != null && !hasSnapshot && (vmDetails != null && vmDetails.containsKey(ApiConstants.ROOT_DISK_SIZE))) {
if (!deployAsIs && rootDiskTO != null && !hasSnapshot && (vmDetails != null && vmDetails.containsKey(ApiConstants.ROOT_DISK_SIZE))) {
resizeRootDiskOnVMStart(vmMo, rootDiskTO, hyperHost, context);
}
@ -2432,6 +2399,85 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
}
/**
* Configure VNC
*/
private void configureVNC(VirtualMachineTO vmSpec, ArrayList<OptionValue> extraOptions, VirtualMachineConfigSpec vmConfigSpec, VmwareHypervisorHost hyperHost, String vmInternalCSName) throws Exception {
String keyboardLayout = null;
if (vmSpec.getDetails() != null)
keyboardLayout = vmSpec.getDetails().get(VmDetailConstants.KEYBOARD);
vmConfigSpec.getExtraConfig()
.addAll(Arrays.asList(configureVnc(extraOptions.toArray(new OptionValue[0]), hyperHost, vmInternalCSName, vmSpec.getVncPassword(), keyboardLayout)));
}
private void ensureDiskControllersInternal(VirtualMachineMO vmMo, Boolean systemVm,
Pair<String, String> controllerInfo,
DiskControllerType systemVmScsiControllerType,
int numScsiControllerForSystemVm,
int firstScsiControllerBusNum, boolean deployAsIs) throws Exception {
if (systemVm) {
ensureScsiDiskControllers(vmMo, systemVmScsiControllerType.toString(), numScsiControllerForSystemVm, firstScsiControllerBusNum);
} else if (!deployAsIs) {
ensureDiskControllers(vmMo, controllerInfo);
}
}
private void tearDownVmDevices(VirtualMachineMO vmMo, boolean hasSnapshot, boolean deployAsIs) throws Exception {
if (deployAsIs) {
vmMo.tearDownDevices(new Class<?>[]{VirtualEthernetCard.class});
} else if (!hasSnapshot) {
vmMo.tearDownDevices(new Class<?>[]{VirtualDisk.class, VirtualEthernetCard.class});
} else {
vmMo.tearDownDevices(new Class<?>[]{VirtualEthernetCard.class});
}
}
private String getGuestOsIdFromVmSpec(VirtualMachineTO vmSpec, boolean deployAsIs) {
String guestOsId = null;
if (!deployAsIs) {
guestOsId = translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs(), vmSpec.getPlatformEmulator()).value();
}
return guestOsId;
}
private Pair<String, String> getControllerInfoFromVmSpec(VirtualMachineTO vmSpec, boolean deployAsIs) throws CloudRuntimeException {
String dataDiskController = vmSpec.getDetails().get(VmDetailConstants.DATA_DISK_CONTROLLER);
String rootDiskController = vmSpec.getDetails().get(VmDetailConstants.ROOT_DISK_CONTROLLER);
// If root disk controller is scsi, then data disk controller would also be scsi instead of using 'osdefault'
// This helps avoid mix of different scsi subtype controllers in instance.
if (DiskControllerType.osdefault == DiskControllerType.getType(dataDiskController) && DiskControllerType.lsilogic == DiskControllerType.getType(rootDiskController)) {
dataDiskController = DiskControllerType.scsi.toString();
}
// Validate the controller types
dataDiskController = DiskControllerType.getType(dataDiskController).toString();
rootDiskController = DiskControllerType.getType(rootDiskController).toString();
if (DiskControllerType.getType(rootDiskController) == DiskControllerType.none) {
throw new CloudRuntimeException("Invalid root disk controller detected : " + rootDiskController);
}
if (DiskControllerType.getType(dataDiskController) == DiskControllerType.none) {
throw new CloudRuntimeException("Invalid data disk controller detected : " + dataDiskController);
}
return new Pair<String, String>(rootDiskController, dataDiskController);
}
private String getBootModeFromVmSpec(VirtualMachineTO vmSpec, boolean deployAsIs) {
String bootMode = null;
if (!deployAsIs) {
if (vmSpec.getDetails().containsKey(VmDetailConstants.BOOT_MODE)) {
bootMode = vmSpec.getDetails().get(VmDetailConstants.BOOT_MODE);
}
if (null == bootMode) {
bootMode = ApiConstants.BootType.BIOS.toString();
}
}
return bootMode;
}
/**
* Set OVF properties (if available)
*/

View File

@ -1479,7 +1479,7 @@ public class HypervisorHostHelper {
if (vmInternalCSName == null)
vmInternalCSName = vmName;
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse, false);
String recommendedController = host.getRecommendedDiskController(guestOsIdentifier);
String newRootDiskController = controllerInfo.first();

View File

@ -534,7 +534,7 @@ public class VmwareHelper {
}
public static void setBasicVmConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, int memoryMB, int memoryReserveMB,
String guestOsIdentifier, boolean limitCpuUse) {
String guestOsIdentifier, boolean limitCpuUse, boolean deployAsIs) {
// VM config basics
vmConfig.setMemoryMB((long)memoryMB);
@ -560,9 +560,13 @@ public class VmwareHelper {
memInfo.setReservation((long)memoryReserveMB);
vmConfig.setMemoryAllocation(memInfo);
if (!deployAsIs) {
// Deploy as-is uses the cloned VM guest OS
vmConfig.setGuestId(guestOsIdentifier);
}
}
public static VirtualDevice prepareUSBControllerDevice() {
s_logger.debug("Preparing USB controller(EHCI+UHCI) device");
VirtualUSBController usbController = new VirtualUSBController(); //EHCI+UHCI