Skip to content

Commit c9186a8

Browse files
committed
KVM: add Virtual TPM model and version
1 parent 54c1f92 commit c9186a8

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

api/src/main/java/com/cloud/vm/VmDetailConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@ public interface VmDetailConstants {
101101
String VMWARE_HOST_NAME = String.format("%s-host", VMWARE_TO_KVM_PREFIX);
102102
String VMWARE_DISK = String.format("%s-disk", VMWARE_TO_KVM_PREFIX);
103103
String VMWARE_MAC_ADDRESSES = String.format("%s-mac-addresses", VMWARE_TO_KVM_PREFIX);
104+
105+
// TPM
106+
String VIRTUAL_TPM_ENABLED = "virtual.tpm.enabled";
107+
String VIRTUAL_TPM_MODEL = "virtual.tpm.model";
108+
String VIRTUAL_TPM_VERSION = "virtual.tpm.version";
104109
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SCSIDef;
167167
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SerialDef;
168168
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TermPolicy;
169+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TpmDef;
169170
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VideoDef;
170171
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef;
171172
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef.WatchDogAction;
@@ -2660,6 +2661,11 @@ protected DevicesDef createDevicesDef(VirtualMachineTO vmTO, GuestDef guest, int
26602661
devices.addDevice(createGraphicDef(vmTO));
26612662
devices.addDevice(createTabletInputDef());
26622663

2664+
TpmDef tpmDef = createTpmDef(vmTO);
2665+
if (tpmDef != null) {
2666+
devices.addDevice(tpmDef);
2667+
}
2668+
26632669
if (isGuestAarch64()) {
26642670
createArm64UsbDef(devices);
26652671
}
@@ -2850,6 +2856,19 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
28502856
return cmd;
28512857
}
28522858

2859+
private TpmDef createTpmDef(VirtualMachineTO vmTO) {
2860+
Map<String, String> details = vmTO.getDetails();
2861+
if (MapUtils.isEmpty(details)) {
2862+
return null;
2863+
}
2864+
String tpmModel = details.get(VmDetailConstants.VIRTUAL_TPM_MODEL);
2865+
if (tpmModel == null) {
2866+
return null;
2867+
}
2868+
String tpmVersion = details.get(VmDetailConstants.VIRTUAL_TPM_VERSION);
2869+
return new TpmDef(tpmModel, tpmVersion);
2870+
}
2871+
28532872
private void configureGuestIfUefiEnabled(boolean isSecureBoot, String bootMode, GuestDef guest) {
28542873
setGuestLoader(bootMode, SECURE, guest, GuestDef.GUEST_LOADER_SECURE);
28552874
setGuestLoader(bootMode, LEGACY, guest, GuestDef.GUEST_LOADER_LEGACY);

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
@@ -2358,6 +2359,80 @@ public String toString() {
23582359
}
23592360
}
23602361

2362+
public static class TpmDef {
2363+
enum TpmModel {
2364+
TIS("tpm-tis"), // TPM Interface Specification (TIS)
2365+
CRB("tpm-crb"); // Command-Response Buffer (CRB)
2366+
2367+
final String model;
2368+
2369+
TpmModel(String model) {
2370+
this.model = model;
2371+
}
2372+
2373+
@Override
2374+
public String toString() {
2375+
return model;
2376+
}
2377+
}
2378+
2379+
enum TpmVersion {
2380+
V1_2("1.2"), // 1.2
2381+
V2_0("2.0"); // 2.0. Default version. The CRB model is only supported with version 2.0.
2382+
2383+
final String version;
2384+
2385+
TpmVersion(String version) {
2386+
this.version = version;
2387+
}
2388+
2389+
@Override
2390+
public String toString() {
2391+
return version;
2392+
}
2393+
}
2394+
2395+
private TpmModel model;
2396+
private TpmVersion version = TpmVersion.V2_0;
2397+
2398+
public TpmDef(TpmModel model, TpmVersion version) {
2399+
this.model = model;
2400+
if (version != null) {
2401+
this.version = version;
2402+
}
2403+
}
2404+
2405+
public TpmDef(String model, String version) {
2406+
this.model = Arrays.stream(TpmModel.values())
2407+
.filter(tpmModel -> tpmModel.toString().equals(model))
2408+
.findFirst()
2409+
.orElse(null);
2410+
if (version != null) {
2411+
this.version = Arrays.stream(TpmVersion.values())
2412+
.filter(tpmVersion -> tpmVersion.toString().equals(version))
2413+
.findFirst()
2414+
.orElse(null);;
2415+
}
2416+
}
2417+
2418+
public TpmModel getModel() {
2419+
return model;
2420+
}
2421+
2422+
public TpmVersion getVersion() {
2423+
return version;
2424+
}
2425+
2426+
@Override
2427+
public String toString() {
2428+
StringBuilder tpmBuidler = new StringBuilder();
2429+
tpmBuidler.append("<tpm model='").append(model).append("'>\n");
2430+
tpmBuidler.append("<backend type='emulator' version='").append(version).append("'/>\n");
2431+
tpmBuidler.append("</tpm>\n");
2432+
return tpmBuidler.toString();
2433+
}
2434+
}
2435+
23612436
public void setHvsType(String hvs) {
23622437
_hvsType = hvs;
23632438
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,11 @@ public void testTopologyNoInfo() {
571571
cpuModeDef.setTopology(-1, -1, 4);
572572
assertEquals("<cpu></cpu>", cpuModeDef.toString());
573573
}
574+
575+
@Test
576+
public void testTpmModel() {
577+
LibvirtVMDef.TpmDef tpmDef = new LibvirtVMDef.TpmDef("tpm-tis", "2.0");
578+
assertEquals(LibvirtVMDef.TpmDef.TpmModel.TIS, tpmDef.getModel());
579+
assertEquals(LibvirtVMDef.TpmDef.TpmVersion.V2_0, tpmDef.getVersion());
580+
}
574581
}

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5062,6 +5062,8 @@ private void fillVMOrTemplateDetailOptions(final Map<String, List<String>> optio
50625062
options.put(VmDetailConstants.IOTHREADS, Arrays.asList("enabled"));
50635063
options.put(VmDetailConstants.NIC_MULTIQUEUE_NUMBER, Collections.emptyList());
50645064
options.put(VmDetailConstants.NIC_PACKED_VIRTQUEUES_ENABLED, Arrays.asList("true", "false"));
5065+
options.put(VmDetailConstants.VIRTUAL_TPM_MODEL, Arrays.asList("tpm-tis", "tpm-crb"));
5066+
options.put(VmDetailConstants.VIRTUAL_TPM_VERSION, Arrays.asList("1.2", "2.0"));
50655067
}
50665068

50675069
if (HypervisorType.VMware.equals(hypervisorType)) {
@@ -5071,6 +5073,7 @@ private void fillVMOrTemplateDetailOptions(final Map<String, List<String>> optio
50715073
options.put(VmDetailConstants.NESTED_VIRTUALIZATION_FLAG, Arrays.asList("true", "false"));
50725074
options.put(VmDetailConstants.SVGA_VRAM_SIZE, Collections.emptyList());
50735075
options.put(VmDetailConstants.RAM_RESERVATION, Collections.emptyList());
5076+
options.put(VmDetailConstants.VIRTUAL_TPM_ENABLED, Arrays.asList("true", "false"));
50745077
}
50755078
}
50765079

0 commit comments

Comments
 (0)