Skip to content

Commit eac838d

Browse files
authored
[KDDI-4.9.1.0-shapeblue17.1] Fix for volume snapshots hardware version (#43)
* Fix for volume snapshots hardware version * Refactor number formatting * Renaming * Refactor and add unit tests
1 parent 4761ae4 commit eac838d

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,11 @@ private Ternary<String, Long, Long> createTemplateFromVolume(VirtualMachineMO vm
772772
throw new Exception(msg);
773773
}
774774

775-
String hardwareVersion = String.valueOf(vmMo.getVirtualHardwareVersion());
775+
String vmxFormattedHardwareVersion = VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(vmMo.getVirtualHardwareVersion());
776776

777777
// 4 MB is the minimum requirement for VM memory in VMware
778778
Pair<VirtualMachineMO, String[]> cloneResult =
779-
vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), hardwareVersion);
779+
vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), vmxFormattedHardwareVersion);
780780

781781
clonedVm = cloneResult.first();
782782

@@ -1101,11 +1101,11 @@ private Pair<String, String[]> exportVolumeToSecondaryStroage(VirtualMachineMO v
11011101
throw new Exception(msg);
11021102
}
11031103

1104-
String virtualHardwareVersion = String.valueOf(vmMo.getVirtualHardwareVersion());
1104+
String vmxFormattedHardwareVersion = VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(vmMo.getVirtualHardwareVersion());
11051105

11061106
// 4 MB is the minimum requirement for VM memory in VMware
11071107
Pair<VirtualMachineMO, String[]> cloneResult =
1108-
vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), virtualHardwareVersion);
1108+
vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), vmxFormattedHardwareVersion);
11091109
clonedVm = cloneResult.first();
11101110
String disks[] = cloneResult.second();
11111111

vmware-base/src/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ private static VirtualDeviceConfigSpec getControllerSpec(String diskController,
14501450

14511451
return controllerSpec;
14521452
}
1453-
public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName, String hardwareVersion) throws Exception {
1453+
public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName, String vmxFormattedHardwareVersion) throws Exception {
14541454

14551455
// Allow worker VM to float within cluster so that we will have better chance to
14561456
// create it successfully
@@ -1461,8 +1461,8 @@ public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, Da
14611461
VirtualMachineMO workingVM = null;
14621462
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
14631463
vmConfig.setName(vmName);
1464-
if (hardwareVersion != null){
1465-
vmConfig.setVersion(("vmx-" + hardwareVersion));
1464+
if (StringUtils.isNotBlank(vmxFormattedHardwareVersion)) {
1465+
vmConfig.setVersion(vmxFormattedHardwareVersion);
14661466
} else {
14671467
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), hyperHost.getHyperHostCluster());
14681468
DatacenterMO dataCenterMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());

vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.concurrent.Executors;
3535
import java.util.concurrent.Future;
3636

37+
import com.cloud.utils.exception.CloudRuntimeException;
3738
import org.apache.log4j.Logger;
3839

3940
import com.google.gson.Gson;
@@ -3016,6 +3017,18 @@ public int getVirtualHardwareVersion() throws Exception {
30163017
return vhOption.getHwVersion();
30173018
}
30183019

3020+
/**
3021+
* Return a hardware version string in the format expected by Vmware
3022+
* Format: "vmx-DD" where DD represents the hardware version number
3023+
* @param virtualHardwareVersion numeric virtual hardware version
3024+
*/
3025+
public static String getVmxFormattedVirtualHardwareVersion(int virtualHardwareVersion) {
3026+
if (virtualHardwareVersion < 1) {
3027+
throw new CloudRuntimeException("Invalid hardware version: " + virtualHardwareVersion);
3028+
}
3029+
return String.format("vmx-%02d", virtualHardwareVersion);
3030+
}
3031+
30193032
public VirtualHardwareOption getVirtualHardwareOption() throws Exception {
30203033
VirtualMachineConfigOption vmConfigOption = _context.getService().queryConfigOption(getEnvironmentBrowser(), null, null);
30213034
return vmConfigOption.getHardwareOptions();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.hypervisor.vmware.mo;
18+
19+
import com.cloud.utils.exception.CloudRuntimeException;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
public class VirtualMachineMOTest {
24+
25+
@Test
26+
public void testGetVmxFormattedVirtualHardwareVersionOneDigit() {
27+
String vmxHwVersion = VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(8);
28+
Assert.assertEquals("vmx-08", vmxHwVersion);
29+
}
30+
31+
@Test
32+
public void testGetVmxFormattedVirtualHardwareVersionTwoDigits() {
33+
String vmxHwVersion = VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(11);
34+
Assert.assertEquals("vmx-11", vmxHwVersion);
35+
}
36+
37+
@Test(expected = CloudRuntimeException.class)
38+
public void testGetVmxFormattedVirtualHardwareVersionInvalid() {
39+
VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(-1);
40+
}
41+
}

0 commit comments

Comments
 (0)