|
13 | 13 | import org.zstack.core.db.SQL; |
14 | 14 | import org.zstack.core.workflow.SimpleFlowChain; |
15 | 15 | import org.zstack.header.core.Completion; |
| 16 | +import org.zstack.header.core.NoErrorCompletion; |
16 | 17 | import org.zstack.header.core.ReturnValueCompletion; |
17 | 18 | import org.zstack.header.core.workflow.Flow; |
18 | 19 | import org.zstack.header.core.workflow.FlowDoneHandler; |
|
33 | 34 | import org.zstack.header.vm.AfterReimageVmInstanceExtensionPoint; |
34 | 35 | import org.zstack.header.vm.VmInstanceDestroyExtensionPoint; |
35 | 36 | import org.zstack.header.vm.VmInstanceInventory; |
| 37 | +import org.zstack.header.vm.VmInstanceMigrateExtensionPoint; |
36 | 38 | import org.zstack.header.vm.VmInstanceSpec; |
37 | 39 | import org.zstack.header.vm.VmInstantiateResourceException; |
38 | 40 | import org.zstack.header.vm.VmMigrationType; |
@@ -89,7 +91,8 @@ public class KvmSecureBootExtensions implements KVMStartVmExtensionPoint, |
89 | 91 | VmInstanceDestroyExtensionPoint, |
90 | 92 | VmPreMigrationExtensionPoint, |
91 | 93 | AfterReimageVmInstanceExtensionPoint, |
92 | | - VmReleaseResourceExtensionPoint { |
| 94 | + VmReleaseResourceExtensionPoint, |
| 95 | + VmInstanceMigrateExtensionPoint { |
93 | 96 | private static final CLogger logger = Utils.getLogger(KvmSecureBootExtensions.class); |
94 | 97 |
|
95 | 98 | @Autowired |
@@ -780,4 +783,68 @@ public void run(MessageReply reply) { |
780 | 783 | } |
781 | 784 | }); |
782 | 785 | } |
| 786 | + |
| 787 | + @Override |
| 788 | + public void afterMigrateVm(VmInstanceInventory inv, String srcHostUuid, NoErrorCompletion completion) { |
| 789 | + String destHostUuid = inv.getHostUuid(); |
| 790 | + String vmUuid = inv.getUuid(); |
| 791 | + |
| 792 | + List<VmHostFileVO> vmHostFiles = Q.New(VmHostFileVO.class) |
| 793 | + .eq(VmHostFileVO_.vmInstanceUuid, vmUuid) |
| 794 | + .eq(VmHostFileVO_.hostUuid, srcHostUuid) |
| 795 | + .list(); |
| 796 | + if (vmHostFiles.isEmpty()) { |
| 797 | + completion.done(); |
| 798 | + return; |
| 799 | + } |
| 800 | + |
| 801 | + // clean up stale VmHostFileVO/VmHostFileContentVO on dest host |
| 802 | + // before sync creates new records |
| 803 | + List<VmHostFileVO> staleFiles = Q.New(VmHostFileVO.class) |
| 804 | + .eq(VmHostFileVO_.vmInstanceUuid, vmUuid) |
| 805 | + .eq(VmHostFileVO_.hostUuid, destHostUuid) |
| 806 | + .list(); |
| 807 | + if (!staleFiles.isEmpty()) { |
| 808 | + List<String> staleUuids = staleFiles.stream() |
| 809 | + .map(VmHostFileVO::getUuid) |
| 810 | + .collect(java.util.stream.Collectors.toList()); |
| 811 | + SQL.New(VmHostFileContentVO.class) |
| 812 | + .in(VmHostFileContentVO_.uuid, staleUuids) |
| 813 | + .delete(); |
| 814 | + SQL.New(VmHostFileVO.class) |
| 815 | + .in(VmHostFileVO_.uuid, staleUuids) |
| 816 | + .delete(); |
| 817 | + logger.debug(String.format("cleaned up %d stale VmHostFileVO/Content on dest host[uuid=%s] for VM[uuid=%s]", |
| 818 | + staleFiles.size(), destHostUuid, vmUuid)); |
| 819 | + } |
| 820 | + |
| 821 | + logger.info(String.format("sync VM host file[vmUuid=%s] from dest host[uuid=%s] after migration", |
| 822 | + vmUuid, destHostUuid)); |
| 823 | + SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg(); |
| 824 | + syncMsg.setHostUuid(destHostUuid); |
| 825 | + syncMsg.setVmUuid(vmUuid); |
| 826 | + |
| 827 | + for (VmHostFileVO file : vmHostFiles) { |
| 828 | + if (file.getType() == VmHostFileType.NvRam) { |
| 829 | + syncMsg.setNvRamPath(file.getPath()); |
| 830 | + } else if (file.getType() == VmHostFileType.TpmState) { |
| 831 | + syncMsg.setTpmStateFolder(file.getPath()); |
| 832 | + } else { |
| 833 | + logger.warn(String.format("unsupported vm host file type: %s, skip syncing for VM[uuid:%s]", |
| 834 | + file.getType(), vmUuid)); |
| 835 | + } |
| 836 | + } |
| 837 | + |
| 838 | + bus.makeLocalServiceId(syncMsg, VmInstanceConstant.SECURE_BOOT_SERVICE_ID); |
| 839 | + bus.send(syncMsg, new CloudBusCallBack(completion) { |
| 840 | + @Override |
| 841 | + public void run(MessageReply reply) { |
| 842 | + if (!reply.isSuccess()) { |
| 843 | + logger.warn(String.format("failed to sync VM host file[vmUuid=%s] from host[uuid=%s] after migration, but tolerated: %s", |
| 844 | + vmUuid, destHostUuid, reply.getError().getReadableDetails())); |
| 845 | + } |
| 846 | + completion.done(); |
| 847 | + } |
| 848 | + }); |
| 849 | + } |
783 | 850 | } |
0 commit comments