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