|
51 | 51 | import javax.naming.ConfigurationException; |
52 | 52 | import javax.xml.parsers.ParserConfigurationException; |
53 | 53 |
|
| 54 | +import com.xensource.xenapi.VTPM; |
54 | 55 | import org.apache.cloudstack.api.ApiConstants; |
55 | 56 | import org.apache.cloudstack.diagnostics.CopyToSecondaryStorageAnswer; |
56 | 57 | import org.apache.cloudstack.diagnostics.CopyToSecondaryStorageCommand; |
@@ -5826,4 +5827,78 @@ public void destroyVm(VM vm, Connection connection, boolean forced) throws XenAP |
5826 | 5827 | public void destroyVm(VM vm, Connection connection) throws XenAPIException, XmlRpcException { |
5827 | 5828 | destroyVm(vm, connection, false); |
5828 | 5829 | } |
| 5830 | + |
| 5831 | + /** |
| 5832 | + * Configure vTPM (Virtual Trusted Platform Module) support for a VM. |
| 5833 | + * vTPM provides a virtual TPM 2.0 device for VMs, enabling features like Secure Boot and disk encryption. |
| 5834 | + * |
| 5835 | + * Requirements: |
| 5836 | + * - XenServer/XCP-ng 8.3 (and above) |
| 5837 | + * - UEFI Secure Boot enabled |
| 5838 | + * - VM in halted state |
| 5839 | + * |
| 5840 | + * @param conn XenServer connection |
| 5841 | + * @param vm The VM to configure |
| 5842 | + * @param vmSpec VM specification containing vTPM settings |
| 5843 | + */ |
| 5844 | + public void configureVTPM(Connection conn, VM vm, VirtualMachineTO vmSpec) throws XenAPIException, XmlRpcException { |
| 5845 | + if (vmSpec == null || vmSpec.getDetails() == null) { |
| 5846 | + return; |
| 5847 | + } |
| 5848 | + |
| 5849 | + String vtpmEnabled = vmSpec.getDetails().getOrDefault(VmDetailConstants.VIRTUAL_TPM_ENABLED, null); |
| 5850 | + if (!Boolean.parseBoolean(vtpmEnabled)) { |
| 5851 | + return; |
| 5852 | + } |
| 5853 | + |
| 5854 | + String bootMode = StringUtils.defaultIfEmpty(vmSpec.getDetails().get(ApiConstants.BootType.UEFI.toString()), null); |
| 5855 | + String bootType = (bootMode == null) ? ApiConstants.BootType.BIOS.toString() : ApiConstants.BootType.UEFI.toString(); |
| 5856 | + |
| 5857 | + if (!ApiConstants.BootType.UEFI.toString().equals(bootType)) { |
| 5858 | + logger.warn("vTPM requires UEFI boot mode. Skipping vTPM configuration for VM: {}", vmSpec.getName()); |
| 5859 | + return; |
| 5860 | + } |
| 5861 | + |
| 5862 | + if (!ApiConstants.BootMode.SECURE.name().equals(bootMode)) { |
| 5863 | + logger.warn("PEARL - bootMode=" + bootMode); |
| 5864 | + logger.warn("vTPM requires UEFI Secure Boot to be enabled. Skipping vTPM configuration for VM: {}", vmSpec.getName()); |
| 5865 | + return; |
| 5866 | + } |
| 5867 | + |
| 5868 | + try { |
| 5869 | + Set<VTPM> existingVtpms = vm.getVTPMs(conn); |
| 5870 | + if (!existingVtpms.isEmpty()) { |
| 5871 | + logger.debug("vTPM already exists for VM: {}", vmSpec.getName()); |
| 5872 | + return; |
| 5873 | + } |
| 5874 | + |
| 5875 | + // Creates vTPM using: xe vtpm-create vm-uuid=<uuid> |
| 5876 | + String vmUuid = vm.getUuid(conn); |
| 5877 | + String result = callHostPlugin(conn, "vmops", "create_vtpm", "vm_uuid", vmUuid); |
| 5878 | + |
| 5879 | + if (result == null || result.isEmpty() || result.startsWith("ERROR:") || result.startsWith("EXCEPTION:")) { |
| 5880 | + throw new CloudRuntimeException("Failed to create vTPM, result: " + result); |
| 5881 | + } |
| 5882 | + |
| 5883 | + logger.info("Successfully created vTPM {} for VM: {}", result.trim(), vmSpec.getName()); |
| 5884 | + } catch (Exception e) { |
| 5885 | + logger.warn("Failed to configure vTPM for VM: {}, continuing without vTPM", vmSpec.getName(), e); |
| 5886 | + } |
| 5887 | + } |
| 5888 | + |
| 5889 | + public boolean isVTPMSupported(Connection conn, Host host) { |
| 5890 | + try { |
| 5891 | + Host.Record hostRecord = host.getRecord(conn); |
| 5892 | + String productVersion = hostRecord.softwareVersion.get("product_version"); |
| 5893 | + if (productVersion == null) { |
| 5894 | + return false; |
| 5895 | + } |
| 5896 | + ComparableVersion currentVersion = new ComparableVersion(productVersion); |
| 5897 | + ComparableVersion minVersion = new ComparableVersion("8.2.0"); |
| 5898 | + return currentVersion.compareTo(minVersion) >= 0; |
| 5899 | + } catch (Exception e) { |
| 5900 | + logger.warn("Failed to check vTPM support on host", e); |
| 5901 | + return false; |
| 5902 | + } |
| 5903 | + } |
5829 | 5904 | } |
0 commit comments