|
37 | 37 | import org.zstack.header.vm.VmInstantiateResourceException; |
38 | 38 | import org.zstack.header.vm.VmMigrationType; |
39 | 39 | import org.zstack.header.vm.VmPreMigrationExtensionPoint; |
| 40 | +import org.zstack.header.vm.VmReleaseResourceExtensionPoint; |
40 | 41 | import org.zstack.header.vm.additions.VmHostBackupFileVO; |
41 | 42 | import org.zstack.header.vm.additions.VmHostBackupFileVO_; |
42 | 43 | import org.zstack.header.vm.additions.VmHostFileContentVO; |
@@ -87,7 +88,8 @@ public class KvmSecureBootExtensions implements KVMStartVmExtensionPoint, |
87 | 88 | PreVmInstantiateResourceExtensionPoint, |
88 | 89 | VmInstanceDestroyExtensionPoint, |
89 | 90 | VmPreMigrationExtensionPoint, |
90 | | - AfterReimageVmInstanceExtensionPoint { |
| 91 | + AfterReimageVmInstanceExtensionPoint, |
| 92 | + VmReleaseResourceExtensionPoint { |
91 | 93 | private static final CLogger logger = Utils.getLogger(KvmSecureBootExtensions.class); |
92 | 94 |
|
93 | 95 | @Autowired |
@@ -731,4 +733,51 @@ public void afterReimageVmInstance(VolumeInventory inventory) { |
731 | 733 | logger.debug(String.format("reset TPM state for VM[uuid:%s] after reimage: " + |
732 | 734 | "deleted all VmHostFileVO and VmHostBackupFileVO records", vmUuid)); |
733 | 735 | } |
| 736 | + |
| 737 | + @Override |
| 738 | + public void releaseVmResource(VmInstanceSpec spec, Completion completion) { |
| 739 | + if (spec.getDestHost() == null) { |
| 740 | + completion.success(); |
| 741 | + return; |
| 742 | + } |
| 743 | + |
| 744 | + String hostUuid = spec.getDestHost().getUuid(); |
| 745 | + String vmUuid = spec.getVmInventory().getUuid(); |
| 746 | + List<VmHostFileVO> vmHostFiles = Q.New(VmHostFileVO.class) |
| 747 | + .eq(VmHostFileVO_.vmInstanceUuid, vmUuid) |
| 748 | + .eq(VmHostFileVO_.hostUuid, hostUuid) |
| 749 | + .list(); |
| 750 | + if (vmHostFiles.isEmpty()) { |
| 751 | + completion.success(); |
| 752 | + return; |
| 753 | + } |
| 754 | + |
| 755 | + logger.info(String.format("try to sync VM host file[vmUuid=%s] from host[uuid=%s]", vmUuid, hostUuid)); |
| 756 | + SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg(); |
| 757 | + syncMsg.setHostUuid(hostUuid); |
| 758 | + syncMsg.setVmUuid(vmUuid); |
| 759 | + |
| 760 | + for (VmHostFileVO file : vmHostFiles) { |
| 761 | + if (file.getType() == VmHostFileType.NvRam) { |
| 762 | + syncMsg.setNvRamPath(file.getPath()); |
| 763 | + } else if (file.getType() == VmHostFileType.TpmState) { |
| 764 | + syncMsg.setTpmStateFolder(file.getPath()); |
| 765 | + } else { |
| 766 | + logger.warn(String.format("unsupported vm host file type: %s, skip syncing for VM[uuid:%s] from host[uuid:%s]", |
| 767 | + file.getType(), vmUuid, hostUuid)); |
| 768 | + } |
| 769 | + } |
| 770 | + |
| 771 | + bus.makeLocalServiceId(syncMsg, VmInstanceConstant.SECURE_BOOT_SERVICE_ID); |
| 772 | + bus.send(syncMsg, new CloudBusCallBack(completion) { |
| 773 | + @Override |
| 774 | + public void run(MessageReply reply) { |
| 775 | + if (!reply.isSuccess()) { |
| 776 | + logger.warn(String.format("failed to sync VM host file[vmUuid=%s] from host[uuid=%s] but still continue: %s", |
| 777 | + vmUuid, hostUuid, reply.getError().getReadableDetails())); |
| 778 | + } |
| 779 | + completion.success(); |
| 780 | + } |
| 781 | + }); |
| 782 | + } |
734 | 783 | } |
0 commit comments