88import org .zstack .core .cloudbus .EventCallback ;
99import org .zstack .core .cloudbus .EventFacadeImpl ;
1010import org .zstack .core .cloudbus .MessageSafe ;
11+ import org .zstack .core .cloudbus .ResourceDestinationMaker ;
1112import org .zstack .core .db .DatabaseFacade ;
1213import org .zstack .core .db .Q ;
1314import org .zstack .core .db .SQL ;
@@ -104,6 +105,8 @@ public class KvmSecureBootManager extends AbstractService {
104105 private KvmVmHostFileFactory vmHostFileFactory ;
105106 @ Autowired
106107 private TimeHelper timeHelper ;
108+ @ Autowired
109+ private ResourceDestinationMaker resourceDestinationMaker ;
107110
108111 @ Override
109112 public boolean start () {
@@ -118,10 +121,28 @@ public boolean stop() {
118121
119122 @ SuppressWarnings ("rawtypes" )
120123 private void setupCanonicalEvents () {
124+ eventFacade .on (VmCanonicalEvents .VM_LIBVIRT_REPORT_START , new EventCallback <Object >() {
125+ @ Override
126+ protected void run (Map tokens , Object data ) {
127+ String vmUuid = (String ) data ;
128+ boolean managedByMe = resourceDestinationMaker .isManagedByUs (vmUuid );
129+ if (!managedByMe ) {
130+ return ;
131+ }
132+ markVmHostFilesChanged (vmUuid );
133+ }
134+ });
135+
121136 eventFacade .on (VmCanonicalEvents .VM_LIBVIRT_REPORT_SHUTDOWN , new EventCallback <Object >() {
122137 @ Override
123138 protected void run (Map tokens , Object data ) {
124139 String vmUuid = (String ) data ;
140+ boolean managedByMe = resourceDestinationMaker .isManagedByUs (vmUuid );
141+ if (!managedByMe ) {
142+ return ;
143+ }
144+ markVmHostFilesChanged (vmUuid );
145+
125146 Tuple tuple = Q .New (VmInstanceVO .class )
126147 .select (VmInstanceVO_ .hostUuid , VmInstanceVO_ .lastHostUuid )
127148 .eq (VmInstanceVO_ .uuid , vmUuid )
@@ -173,6 +194,49 @@ public void run(MessageReply reply) {
173194 });
174195 }
175196
197+ /**
198+ * Preemptive judgment: when a VM with TPM starts or shuts down,
199+ * the NvRam/TpmState data must have changed, so mark the corresponding
200+ * VmHostFileVO.changeDate to current time.
201+ */
202+ private void markVmHostFilesChanged (String vmUuid ) {
203+ boolean hasTpm = Q .New (TpmVO .class )
204+ .eq (TpmVO_ .vmInstanceUuid , vmUuid )
205+ .isExists ();
206+ if (!hasTpm ) {
207+ return ;
208+ }
209+
210+ Tuple tuple = Q .New (VmInstanceVO .class )
211+ .select (VmInstanceVO_ .hostUuid , VmInstanceVO_ .lastHostUuid )
212+ .eq (VmInstanceVO_ .uuid , vmUuid )
213+ .findTuple ();
214+ if (tuple == null ) {
215+ return ;
216+ }
217+
218+ String hostUuid = (String ) tuple .get (0 );
219+ if (hostUuid == null ) {
220+ hostUuid = (String ) tuple .get (1 );
221+ }
222+ if (hostUuid == null ) {
223+ return ;
224+ }
225+
226+ Timestamp now = new Timestamp (timeHelper .getCurrentTimeMillis ());
227+ long updated = SQL .New (VmHostFileVO .class )
228+ .eq (VmHostFileVO_ .vmInstanceUuid , vmUuid )
229+ .eq (VmHostFileVO_ .hostUuid , hostUuid )
230+ .in (VmHostFileVO_ .type , list (VmHostFileType .NvRam , VmHostFileType .TpmState ))
231+ .set (VmHostFileVO_ .changeDate , now )
232+ .update ();
233+
234+ if (updated > 0 ) {
235+ logger .debug (String .format ("preemptively marked VmHostFiles as changed for TPM vm[uuid:%s] on host[uuid:%s], %d records updated" ,
236+ vmUuid , hostUuid , updated ));
237+ }
238+ }
239+
176240 @ Override
177241 public String getId () {
178242 return bus .makeLocalServiceId (VmInstanceConstant .SECURE_BOOT_SERVICE_ID );
0 commit comments