|
2 | 2 |
|
3 | 3 | import org.springframework.beans.factory.annotation.Autowired; |
4 | 4 | import org.zstack.compute.legacy.ComputeLegacyGlobalProperty; |
| 5 | +import org.zstack.core.Platform; |
| 6 | +import org.zstack.core.asyncbatch.While; |
| 7 | +import org.zstack.core.cloudbus.CloudBus; |
5 | 8 | import org.zstack.core.cloudbus.EventCallback; |
6 | 9 | import org.zstack.core.cloudbus.EventFacadeImpl; |
| 10 | +import org.zstack.core.db.DatabaseFacade; |
7 | 11 | import org.zstack.core.db.Q; |
8 | | -import org.zstack.header.Component; |
| 12 | +import org.zstack.core.workflow.SimpleFlowChain; |
| 13 | +import org.zstack.header.AbstractService; |
9 | 14 | import org.zstack.header.core.Completion; |
| 15 | +import org.zstack.header.core.WhileDoneCompletion; |
| 16 | +import org.zstack.header.core.workflow.FlowDoneHandler; |
| 17 | +import org.zstack.header.core.workflow.FlowErrorHandler; |
| 18 | +import org.zstack.header.core.workflow.FlowTrigger; |
| 19 | +import org.zstack.header.core.workflow.NoRollbackFlow; |
10 | 20 | import org.zstack.header.errorcode.ErrorCode; |
| 21 | +import org.zstack.header.errorcode.ErrorCodeList; |
| 22 | +import org.zstack.header.exception.CloudRuntimeException; |
| 23 | +import org.zstack.header.message.Message; |
11 | 24 | import org.zstack.header.vm.VmCanonicalEvents; |
| 25 | +import org.zstack.header.vm.VmInstanceConstant; |
12 | 26 | import org.zstack.header.vm.VmInstanceVO; |
13 | 27 | import org.zstack.header.vm.VmInstanceVO_; |
| 28 | +import org.zstack.header.vm.additions.VmHostBackupFileVO; |
| 29 | +import org.zstack.header.vm.additions.VmHostFileContentVO; |
| 30 | +import org.zstack.header.vm.additions.VmHostFileContentVO_; |
14 | 31 | import org.zstack.header.vm.additions.VmHostFileType; |
15 | 32 | import org.zstack.header.vm.additions.VmHostFileVO; |
16 | 33 | import org.zstack.header.vm.additions.VmHostFileVO_; |
| 34 | +import org.zstack.resourceconfig.ResourceConfig; |
| 35 | +import org.zstack.resourceconfig.ResourceConfigFacade; |
| 36 | +import org.zstack.utils.DebugUtils; |
17 | 37 | import org.zstack.utils.Utils; |
18 | 38 | import org.zstack.utils.logging.CLogger; |
19 | 39 |
|
20 | 40 | import javax.persistence.Tuple; |
| 41 | +import java.sql.Timestamp; |
| 42 | +import java.time.Instant; |
| 43 | +import java.util.ArrayList; |
21 | 44 | import java.util.List; |
22 | 45 | import java.util.Map; |
23 | 46 |
|
| 47 | +import static org.zstack.compute.vm.VmGlobalConfig.RESET_TPM_AFTER_VM_CLONE; |
| 48 | +import static org.zstack.kvm.efi.KvmSecureBootExtensions.*; |
24 | 49 | import static org.zstack.utils.CollectionDSL.list; |
25 | 50 | import static org.zstack.utils.CollectionUtils.findOneOrNull; |
| 51 | +import static org.zstack.utils.CollectionUtils.transform; |
26 | 52 |
|
27 | | -public class KvmSecureBootManager implements Component { |
| 53 | +public class KvmSecureBootManager extends AbstractService { |
28 | 54 | private static final CLogger logger = Utils.getLogger(KvmSecureBootManager.class); |
29 | 55 |
|
| 56 | + @Autowired |
| 57 | + private CloudBus bus; |
| 58 | + @Autowired |
| 59 | + private DatabaseFacade databaseFacade; |
30 | 60 | @Autowired |
31 | 61 | private EventFacadeImpl eventFacade; |
32 | 62 | @Autowired |
| 63 | + private ResourceConfigFacade resourceConfigFacade; |
| 64 | + @Autowired |
33 | 65 | private KvmSecureBootExtensions secureBootExtensions; |
34 | 66 |
|
35 | 67 | @Override |
@@ -101,4 +133,181 @@ public void fail(ErrorCode errorCode) { |
101 | 133 | } |
102 | 134 | }); |
103 | 135 | } |
| 136 | + |
| 137 | + @Override |
| 138 | + public String getId() { |
| 139 | + return bus.makeLocalServiceId(VmInstanceConstant.SECURE_BOOT_SERVICE_ID); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void handleMessage(Message msg) { |
| 144 | + if (msg instanceof CloneVmHostFileMsg) { |
| 145 | + handle((CloneVmHostFileMsg) msg); |
| 146 | + } else { |
| 147 | + bus.dealWithUnknownMessage(msg); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @SuppressWarnings("rawtypes") |
| 152 | + private void handle(CloneVmHostFileMsg msg) { |
| 153 | + CloneVmHostFileReply reply = new CloneVmHostFileReply(); |
| 154 | + List<VmHostFileType> needClone = list(VmHostFileType.NvRam); |
| 155 | + |
| 156 | + boolean resetTpm; |
| 157 | + if (msg.getResetTpm() == null) { |
| 158 | + ResourceConfig resourceConfig = resourceConfigFacade.getResourceConfig(RESET_TPM_AFTER_VM_CLONE.getIdentity()); |
| 159 | + resetTpm = resourceConfig.getResourceConfigValue(msg.getSrcVmUuid(), Boolean.class); |
| 160 | + } else { |
| 161 | + resetTpm = msg.getResetTpm(); |
| 162 | + } |
| 163 | + if (resetTpm) { |
| 164 | + needClone.add(VmHostFileType.TpmState); |
| 165 | + } |
| 166 | + |
| 167 | + List<VmHostFileVO> files = new ArrayList<>(); |
| 168 | + List<SyncVmHostFilesFromHostContext> syncContexts = new ArrayList<>(); |
| 169 | + |
| 170 | + SimpleFlowChain chain = new SimpleFlowChain(); |
| 171 | + chain.setName("clone-vm-host-file"); |
| 172 | + chain.then(new NoRollbackFlow() { |
| 173 | + String __name__ = "prepare-sync-vm-host-file-context-list"; |
| 174 | + |
| 175 | + @Override |
| 176 | + public void run(FlowTrigger trigger, Map data) { |
| 177 | + for (VmHostFileType type : needClone) { |
| 178 | + VmHostFileVO file = Q.New(VmHostFileVO.class) |
| 179 | + .eq(VmHostFileVO_.vmInstanceUuid, msg.getSrcVmUuid()) |
| 180 | + .eq(VmHostFileVO_.type, type) |
| 181 | + .orderByDesc(VmHostFileVO_.lastOpDate) |
| 182 | + .limit(1) |
| 183 | + .find(); |
| 184 | + if (file == null) { |
| 185 | + logger.debug(String.format("skip to read/write %s host file for VM[vmUuid=%s]: file is not registered in MN", |
| 186 | + type, msg.getSrcVmUuid())); |
| 187 | + continue; |
| 188 | + } |
| 189 | + files.add(file); |
| 190 | + } |
| 191 | + |
| 192 | + if (files.isEmpty()) { |
| 193 | + trigger.next(); |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + List<String> hostUuids = transform(files, VmHostFileVO::getHostUuid); |
| 198 | + for (String hostUuid : hostUuids) { |
| 199 | + SyncVmHostFilesFromHostContext syncContext = new SyncVmHostFilesFromHostContext(); |
| 200 | + syncContext.hostUuid = hostUuid; |
| 201 | + syncContext.vmUuid = msg.getSrcVmUuid(); |
| 202 | + syncContexts.add(syncContext); |
| 203 | + } |
| 204 | + |
| 205 | + for (VmHostFileVO file : files) { |
| 206 | + SyncVmHostFilesFromHostContext syncContext = findOneOrNull(syncContexts, |
| 207 | + item -> file.getHostUuid().equals(item.hostUuid)); |
| 208 | + DebugUtils.Assert(syncContext != null, "syncContext must be not null"); |
| 209 | + if (file.getType() == VmHostFileType.NvRam) { |
| 210 | + syncContext.nvRamPath = file.getPath(); |
| 211 | + } else if (file.getType() == VmHostFileType.TpmState) { |
| 212 | + syncContext.tpmStateFolder = file.getPath(); |
| 213 | + } else { |
| 214 | + throw new CloudRuntimeException("unsupported vm host file type: " + file.getType()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + trigger.next(); |
| 219 | + } |
| 220 | + }).then(new NoRollbackFlow() { |
| 221 | + String __name__ = "read-vm-host-file-from-origin-host"; |
| 222 | + |
| 223 | + @Override |
| 224 | + public void run(FlowTrigger trigger, Map data) { |
| 225 | + new While<>(syncContexts).each((syncContext, whileContext) -> |
| 226 | + secureBootExtensions.syncVmHostFilesFromHost(syncContext, new Completion(whileContext) { |
| 227 | + @Override |
| 228 | + public void success() { |
| 229 | + whileContext.done(); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void fail(ErrorCode errorCode) { |
| 234 | + whileContext.addError(errorCode); |
| 235 | + whileContext.done(); |
| 236 | + } |
| 237 | + }) |
| 238 | + ).run(new WhileDoneCompletion(trigger) { |
| 239 | + @Override |
| 240 | + public void done(ErrorCodeList errorCodeList) { |
| 241 | + if (!errorCodeList.isEmpty()) { |
| 242 | + logger.warn(String.format("failed to sync host file for VM[uuid=%s] but still continue:\n%s", |
| 243 | + msg.getSrcVmUuid(), |
| 244 | + String.join("\n", transform(errorCodeList.getCauses(), ErrorCode::getReadableDetails)))); |
| 245 | + } |
| 246 | + trigger.next(); |
| 247 | + } |
| 248 | + }); |
| 249 | + } |
| 250 | + }).then(new NoRollbackFlow() { |
| 251 | + String __name__ = "copy-host-content-database"; |
| 252 | + |
| 253 | + @Override |
| 254 | + public void run(FlowTrigger trigger, Map data) { |
| 255 | + List<String> uuidList = transform(files, VmHostFileVO::getUuid); |
| 256 | + List<VmHostFileVO> filesAfterSyncing = Q.New(VmHostFileVO.class) |
| 257 | + .in(VmHostFileVO_.uuid, uuidList) |
| 258 | + .list(); |
| 259 | + List<VmHostFileContentVO> contents = Q.New(VmHostFileContentVO.class) |
| 260 | + .in(VmHostFileContentVO_.uuid, uuidList) |
| 261 | + .list(); |
| 262 | + |
| 263 | + List<VmHostBackupFileVO> filesNeedPersists = new ArrayList<>(); |
| 264 | + List<VmHostFileContentVO> contentsNeedPersists = new ArrayList<>(); |
| 265 | + |
| 266 | + Timestamp now = Timestamp.from(Instant.now()); |
| 267 | + for (String vmUuid : msg.getDstVmUuidList()) { |
| 268 | + for (VmHostFileVO vmHostFileVO : filesAfterSyncing) { |
| 269 | + VmHostBackupFileVO file = new VmHostBackupFileVO(); |
| 270 | + file.setUuid(Platform.getUuid()); |
| 271 | + file.setVmInstanceUuid(vmUuid); |
| 272 | + file.setType(vmHostFileVO.getType()); |
| 273 | + file.setCreateDate(now); |
| 274 | + file.setLastOpDate(now); |
| 275 | + filesNeedPersists.add(file); |
| 276 | + |
| 277 | + VmHostFileContentVO srcContent = findOneOrNull(contents, |
| 278 | + item -> item.getUuid().equals(vmHostFileVO.getUuid())); |
| 279 | + if (srcContent == null) { |
| 280 | + continue; |
| 281 | + } |
| 282 | + VmHostFileContentVO content = new VmHostFileContentVO(); |
| 283 | + content.setUuid(file.getUuid()); |
| 284 | + content.setContent(srcContent.getContent()); |
| 285 | + content.setFormat(srcContent.getFormat()); |
| 286 | + content.setCreateDate(now); |
| 287 | + content.setLastOpDate(now); |
| 288 | + contentsNeedPersists.add(content); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + if (!filesNeedPersists.isEmpty()) { |
| 293 | + databaseFacade.persistCollection(filesNeedPersists); |
| 294 | + } |
| 295 | + if (!contentsNeedPersists.isEmpty()) { |
| 296 | + databaseFacade.persistCollection(contentsNeedPersists); |
| 297 | + } |
| 298 | + trigger.next(); |
| 299 | + } |
| 300 | + }).done(new FlowDoneHandler(msg) { |
| 301 | + @Override |
| 302 | + public void handle(Map data) { |
| 303 | + bus.reply(msg, reply); |
| 304 | + } |
| 305 | + }).error(new FlowErrorHandler(msg) { |
| 306 | + @Override |
| 307 | + public void handle(ErrorCode errCode, Map data) { |
| 308 | + reply.setError(errCode); |
| 309 | + bus.reply(msg, reply); |
| 310 | + } |
| 311 | + }).start(); |
| 312 | + } |
104 | 313 | } |
0 commit comments