44import org .zstack .compute .vm .BuildVmSpecExtensionPoint ;
55import org .zstack .compute .vm .VmSystemTags ;
66import org .zstack .core .db .Q ;
7+ import org .zstack .core .db .SQLBatch ;
78import org .zstack .header .tpm .entity .TpmSpec ;
89import org .zstack .header .tpm .entity .TpmVO ;
910import org .zstack .header .tpm .entity .TpmVO_ ;
@@ -25,6 +26,8 @@ public class VmTpmExtensions implements VmInstanceCreateExtensionPoint,
2526 private VmTpmManager vmTpmManager ;
2627 @ Autowired
2728 private ResourceConfigFacade resourceConfigFacade ;
29+ @ Autowired
30+ private TpmEncryptedResourceKeyBackend resourceKeyBackend ;
2831
2932 @ Override
3033 public void preCreateVmInstance (CreateVmInstanceMsg msg ) {
@@ -38,17 +41,49 @@ public void afterPersistVmInstanceVO(VmInstanceVO vo, CreateVmInstanceMsg msg) {
3841 return ;
3942 }
4043
41- vmTpmManager .persistTpmVO (null , vo .getUuid ());
44+ new SQLBatch () {
45+ @ Override
46+ protected void scripts () {
47+ final TpmVO tpm = vmTpmManager .persistTpmVO (null , vo .getUuid ());
48+ final String keyProviderUuid = spec .getTpm ().getKeyProviderUuid ();
49+ if (keyProviderUuid != null ) {
50+ resourceKeyBackend .attachKeyProviderToTpm (tpm .getUuid (), keyProviderUuid );
51+ }
52+ }
53+ }.execute ();
54+ }
55+
56+ @ Override
57+ public void afterRollbackPersistVmInstanceVO (VmInstanceVO vo , CreateVmInstanceMsg msg ) {
58+ String tpmUuid = Q .New (TpmVO .class )
59+ .eq (TpmVO_ .vmInstanceUuid , vo .getUuid ())
60+ .select (TpmVO_ .uuid )
61+ .findValue ();
62+ if (tpmUuid == null ) {
63+ return ;
64+ }
65+
66+ new SQLBatch () {
67+ @ Override
68+ protected void scripts () {
69+ try {
70+ resourceKeyBackend .detachKeyProviderFromTpm (tpmUuid );
71+ } finally {
72+ vmTpmManager .deleteTpmVO (tpmUuid );
73+ }
74+ }
75+ }.execute ();
4276 }
4377
4478 @ Override
4579 public void afterBuildVmSpec (VmInstanceSpec spec ) {
4680 String vmUuid = spec .getVmInventory ().getUuid ();
4781
48- boolean tpmExists = Q .New (TpmVO .class )
82+ String tpmUuid = Q .New (TpmVO .class )
4983 .eq (TpmVO_ .vmInstanceUuid , vmUuid )
50- .isExists ();
51- boolean needRegisterNvRam = tpmExists ;
84+ .select (TpmVO_ .uuid )
85+ .findValue ();
86+ boolean needRegisterNvRam = tpmUuid != null ;
5287 if (!needRegisterNvRam ) {
5388 String bootMode = VmSystemTags .BOOT_MODE .getTokenByResourceUuid (vmUuid , VmSystemTags .BOOT_MODE_TOKEN );
5489 if (vmTpmManager .isUefiBootMode (bootMode )) {
@@ -64,12 +99,13 @@ public void afterBuildVmSpec(VmInstanceSpec spec) {
6499 spec .setNvRamSpec (nvRamSpec );
65100 }
66101
67- if (tpmExists && (spec .getDevicesSpec () == null || spec .getDevicesSpec ().getTpm () == null )) {
102+ if (tpmUuid != null && (spec .getDevicesSpec () == null || spec .getDevicesSpec ().getTpm () == null )) {
68103 VmDevicesSpec devicesSpec = spec .getDevicesSpec () == null ? new VmDevicesSpec () : spec .getDevicesSpec ();
69104 spec .setDevicesSpec (devicesSpec );
70105
71106 devicesSpec .setTpm (new TpmSpec ());
72107 devicesSpec .getTpm ().setEnable (true );
108+ devicesSpec .getTpm ().setKeyProviderUuid (resourceKeyBackend .findKeyProviderUuidByTpm (tpmUuid ));
73109 }
74110 }
75111}
0 commit comments