33import org .apache .commons .lang .StringUtils ;
44import org .springframework .beans .factory .annotation .Autowired ;
55import org .zstack .compute .vm .VmGlobalConfig ;
6- import org .zstack .compute .vm .VmSystemTags ;
76import org .zstack .compute .vm .devices .VmTpmManager ;
87import org .zstack .core .Platform ;
98import org .zstack .core .cloudbus .CloudBus ;
2322import org .zstack .header .errorcode .ErrorCode ;
2423import org .zstack .header .exception .CloudRuntimeException ;
2524import org .zstack .header .message .MessageReply ;
26- import org .zstack .header .tpm .entity .TpmVO ;
27- import org .zstack .header .tpm .entity .TpmVO_ ;
2825import org .zstack .header .vm .DiskAO ;
2926import org .zstack .header .vm .PreVmInstantiateResourceExtensionPoint ;
3027import org .zstack .header .vm .VmInstanceVO ;
7976import java .util .Map ;
8077import java .util .Objects ;
8178
82- import static org .zstack .compute .vm .VmGlobalConfig .ENABLE_UEFI_SECURE_BOOT ;
8379import static org .zstack .core .Platform .operr ;
8480import static org .zstack .header .vm .additions .VmHostFileSyncReason .PostMigration ;
8581import static org .zstack .header .vm .additions .VmHostFileSyncReason .BeforeHaStart ;
8682import static org .zstack .header .vm .additions .VmHostFileSyncReason .PrepareReRead ;
8783import static org .zstack .header .vm .additions .VmHostFileSyncReason .PrepareRead ;
8884import static org .zstack .header .vm .additions .VmHostFileSyncReason .ResourceRelease ;
8985import static org .zstack .header .vm .additions .VmHostFileSyncReason .SnapshotGroupOnlineBackup ;
86+ import static org .zstack .header .vm .additions .VmHostFileType .NvRam ;
9087import static org .zstack .kvm .KVMConstant .*;
9188import static org .zstack .utils .CollectionDSL .list ;
9289
@@ -107,6 +104,8 @@ public class KvmSecureBootExtensions implements KVMStartVmExtensionPoint,
107104 private ResourceConfigFacade resourceConfigFacade ;
108105 @ Autowired
109106 private DatabaseFacade databaseFacade ;
107+ @ Autowired
108+ private KvmVmHostFileFactory vmHostFileFactory ;
110109
111110 private final Object hostFileLock = new Object ();
112111
@@ -141,15 +140,15 @@ private void prepareNvRamToStartVmCmd(KVMAgentCommands.StartVmCmd cmd, NvRamSpec
141140 final Timestamp now = Timestamp .from (Instant .now ());
142141 VmHostFileVO nvRamFile = Q .New (VmHostFileVO .class )
143142 .eq (VmHostFileVO_ .vmInstanceUuid , cmd .getVmInstanceUuid ())
144- .eq (VmHostFileVO_ .type , VmHostFileType . NvRam )
143+ .eq (VmHostFileVO_ .type , NvRam )
145144 .eq (VmHostFileVO_ .hostUuid , host .getUuid ())
146145 .find ();
147146 if (nvRamFile == null ) {
148147 nvRamFile = new VmHostFileVO ();
149148 nvRamFile .setUuid (Platform .getUuid ());
150149 nvRamFile .setHostUuid (host .getUuid ());
151150 nvRamFile .setVmInstanceUuid (cmd .getVmInstanceUuid ());
152- nvRamFile .setType (VmHostFileType . NvRam );
151+ nvRamFile .setType (NvRam );
153152 nvRamFile .setPath (volume .getInstallPath ());
154153 nvRamFile .setCreateDate (now );
155154 nvRamFile .setResourceName ("NvRam file for " + cmd .getVmInstanceUuid ());
@@ -180,29 +179,17 @@ private void prepareNvRamBeforeMigration(VmInstanceInventory vm, String dstHostU
180179 return ;
181180 }
182181
183- String tpmUuid = Q .New (TpmVO .class )
184- .eq (TpmVO_ .vmInstanceUuid , vm .getUuid ())
185- .select (TpmVO_ .uuid )
186- .findValue ();
187- boolean needRegisterNvRam = tpmUuid != null ;
182+ boolean needRegisterNvRam = vmHostFileFactory .needRegister (NvRam , vm .getUuid ());
188183 if (!needRegisterNvRam ) {
189- String bootMode = VmSystemTags .BOOT_MODE .getTokenByResourceUuid (vm .getUuid (), VmSystemTags .BOOT_MODE_TOKEN );
190- if (isUefiBootMode (bootMode )) {
191- ResourceConfig resourceConfig = resourceConfigFacade .getResourceConfig (ENABLE_UEFI_SECURE_BOOT .getIdentity ());
192- needRegisterNvRam = resourceConfig .getResourceConfigValue (vm .getUuid (), Boolean .class ) == Boolean .TRUE ;
193- }
194-
195- if (!needRegisterNvRam ) {
196- completion .success ();
197- return ;
198- }
184+ completion .success ();
185+ return ;
199186 }
200187
201188 SimpleFlowChain .of ("prepare-nvram-before-vm-" + vm .getUuid () + "-migrate" )
202189 .then ("prepare-nvram-folder-on-dest-host" , trigger -> {
203190 VmHostFileTO to = new VmHostFileTO ();
204191 to .setPath (buildNvramFilePath (vm .getUuid ()));
205- to .setType (VmHostFileType . NvRam .toString ());
192+ to .setType (NvRam .toString ());
206193 to .setOperation (VmHostFileOperation .Prepare .toString ());
207194
208195 RewriteVmHostFilesContext context = new RewriteVmHostFilesContext ();
@@ -290,7 +277,7 @@ public void preInstantiateVmResource(VmInstanceSpec spec, Completion completion)
290277 PrepareHostFileContext context = new PrepareHostFileContext ();
291278 context .hostUuid = spec .getDestHost ().getUuid ();
292279 context .vmUuid = spec .getVmInventory ().getUuid ();
293- context .type = VmHostFileType . NvRam ;
280+ context .type = NvRam ;
294281 context .backupUuid = nvRamSpec .getBackupFileUuid ();
295282 context .syncReason = "pre-instantiate VM resource" ;
296283 prepareHostFileOnHost (context , completion );
@@ -349,7 +336,7 @@ public void run(FlowTrigger trigger, Map data) {
349336 syncMsg .setVmUuid (context .vmUuid );
350337 syncMsg .setSyncReason (PrepareRead .reason (context .syncReason ));
351338
352- if (vmHostFile .getType () == VmHostFileType . NvRam ) {
339+ if (vmHostFile .getType () == NvRam ) {
353340 context .path = vmHostFile .getPath ();
354341 syncMsg .setNvRamPath (context .path );
355342 } else if (vmHostFile .getType () == VmHostFileType .TpmState ) {
@@ -471,7 +458,7 @@ public void run(FlowTrigger trigger, Map data) {
471458 syncMsg .setVmUuid (context .vmUuid );
472459 syncMsg .setSyncReason (PrepareReRead .reason (context .syncReason ));
473460
474- if (context .type == VmHostFileType . NvRam ) {
461+ if (context .type == NvRam ) {
475462 syncMsg .setNvRamPath (context .path );
476463 } else if (context .type == VmHostFileType .TpmState ) {
477464 syncMsg .setTpmStateFolder (context .path );
@@ -574,7 +561,7 @@ public void releaseVmResource(VmInstanceSpec spec, Completion completion) {
574561 syncMsg .setSyncReason (ResourceRelease .reason ());
575562
576563 for (VmHostFileVO file : vmHostFiles ) {
577- if (file .getType () == VmHostFileType . NvRam ) {
564+ if (file .getType () == NvRam ) {
578565 syncMsg .setNvRamPath (file .getPath ());
579566 } else if (file .getType () == VmHostFileType .TpmState ) {
580567 syncMsg .setTpmStateFolder (file .getPath ());
@@ -633,7 +620,7 @@ public void beforeHaStartVmInstance(String vmUuid, String judgerClassName, List<
633620 syncMsg .setSyncReason (BeforeHaStart .reason ());
634621
635622 for (VmHostFileVO file : vmHostFiles ) {
636- if (file .getType () == VmHostFileType . NvRam ) {
623+ if (file .getType () == NvRam ) {
637624 syncMsg .setNvRamPath (file .getPath ());
638625 } else if (file .getType () == VmHostFileType .TpmState ) {
639626 syncMsg .setTpmStateFolder (file .getPath ());
@@ -699,7 +686,7 @@ public void afterMigrateVm(VmInstanceInventory inv, String srcHostUuid, NoErrorC
699686 syncMsg .setSyncReason (PostMigration .reason ());
700687
701688 for (VmHostFileVO file : vmHostFiles ) {
702- if (file .getType () == VmHostFileType . NvRam ) {
689+ if (file .getType () == NvRam ) {
703690 syncMsg .setNvRamPath (file .getPath ());
704691 } else if (file .getType () == VmHostFileType .TpmState ) {
705692 syncMsg .setTpmStateFolder (file .getPath ());
@@ -759,7 +746,7 @@ public void afterVolumeLiveSnapshotGroupCreatedOnBackend(CreateVolumesSnapshotOv
759746 syncMsg .setHostUuid (hostUuid );
760747
761748 for (VmHostFileVO file : hostFiles ) {
762- if (file .getType () == VmHostFileType . NvRam ) {
749+ if (file .getType () == NvRam ) {
763750 syncMsg .setNvRamPath (buildNvramSnapshotBackupFilePath (vmUuid ));
764751 } else if (file .getType () == VmHostFileType .TpmState ) {
765752 syncMsg .setTpmStateFolder (buildTpmStateSnapshotBackupFilePath (vmUuid ));
0 commit comments