Skip to content

Commit 804ab6b

Browse files
author
Zhang Wenhao
committed
<feature>[kvm]: add lastSyncReason to track VM host file sync
Add lastSyncReason column to VmHostFileVO table to track the reason for each VM host file sync operation. This helps with debugging and auditing. Update SyncVmHostFilesFromHostMsg to include syncReason parameter and set it at various sync triggers: - VM start: on prepare host file - VM stop: on release vm resource - VM migration: on post-migration - VM clone: on clone vm - Libvirt shutdown: on libvirt shutdown event Update DB schema and all sync operation calls to properly record the sync reason for better traceability. Related: ZSV-11310 Change-Id: I7978696275717271667076676b79706a646c6a66
1 parent a9e42f7 commit 804ab6b

12 files changed

Lines changed: 102 additions & 0 deletions

File tree

conf/db/zsv/V5.0.0__schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS `zstack`.`VmHostFileVO` (
1515
`hostUuid` char(32) NOT NULL,
1616
`type` varchar(64) NOT NULL COMMENT 'NvRam, TpmState',
1717
`path` varchar(1024) NOT NULL COMMENT 'Absolute path of the file on the host',
18+
`lastSyncReason` varchar(255) DEFAULT NULL COMMENT 'The reason for the last sync operation',
1819
`lastOpDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
1920
`createDate` timestamp NOT NULL DEFAULT '1999-12-31 23:59:59',
2021
PRIMARY KEY (`uuid`),

header/src/main/java/org/zstack/header/vm/additions/RestoreVmHostFileMsg.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class RestoreVmHostFileMsg extends NeedReplyMessage implements VmInstanceMessage {
77
private String vmInstanceUuid;
88
private String snapshotGroupUuid;
9+
private String syncReason;
910

1011
@Override
1112
public String getVmInstanceUuid() {
@@ -23,4 +24,12 @@ public String getSnapshotGroupUuid() {
2324
public void setSnapshotGroupUuid(String snapshotGroupUuid) {
2425
this.snapshotGroupUuid = snapshotGroupUuid;
2526
}
27+
28+
public String getSyncReason() {
29+
return syncReason;
30+
}
31+
32+
public void setSyncReason(String syncReason) {
33+
this.syncReason = syncReason;
34+
}
2635
}

header/src/main/java/org/zstack/header/vm/additions/VmHostFileInventory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class VmHostFileInventory {
1616
private String hostUuid;
1717
private String type;
1818
private String path;
19+
private String lastSyncReason;
1920
private Timestamp createDate;
2021
private Timestamp lastOpDate;
2122

@@ -29,6 +30,7 @@ public static VmHostFileInventory valueOf(VmHostFileVO vo) {
2930
inv.setHostUuid(vo.getHostUuid());
3031
inv.setType(vo.getType().toString());
3132
inv.setPath(vo.getPath());
33+
inv.setLastSyncReason(vo.getLastSyncReason());
3234
inv.setCreateDate(vo.getCreateDate());
3335
inv.setLastOpDate(vo.getLastOpDate());
3436
return inv;
@@ -78,6 +80,14 @@ public void setPath(String path) {
7880
this.path = path;
7981
}
8082

83+
public String getLastSyncReason() {
84+
return lastSyncReason;
85+
}
86+
87+
public void setLastSyncReason(String lastSyncReason) {
88+
this.lastSyncReason = lastSyncReason;
89+
}
90+
8191
public Timestamp getCreateDate() {
8292
return createDate;
8393
}
@@ -101,6 +111,7 @@ public static VmHostFileInventory __example__() {
101111
ref.setHostUuid(DocUtils.createFixedUuid(HostVO.class));
102112
ref.setType(VmHostFileType.TpmState.toString());
103113
ref.setPath("/var/lib/libvirt/swtpm/" + ref.getHostUuid() + "/");
114+
ref.setLastSyncReason("on libvirt shutdown event");
104115
ref.setCreateDate(DocUtils.timestamp());
105116
ref.setLastOpDate(DocUtils.timestamp());
106117
return ref;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.zstack.header.vm.additions;
2+
3+
public enum VmHostFileSyncReason {
4+
PrepareRead("on prepare host file (from origin host)"),
5+
PrepareReRead("on prepare host file (from dest host)"),
6+
ResourceRelease("on release vm resource"),
7+
PostMigration("on post-migration (from dest host)"),
8+
VmShutdown("on libvirt shutdown event"),
9+
PostClone("on post-clone (from dest host)"),
10+
Restore("restore"),
11+
;
12+
13+
public final String detail;
14+
15+
private VmHostFileSyncReason(String detail) {
16+
this.detail = detail;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return detail;
22+
}
23+
24+
public String reason() {
25+
return detail;
26+
}
27+
28+
public String reason(String description) {
29+
return description == null || description.length() == 0 ? reason() : detail + ": " + description;
30+
}
31+
}

header/src/main/java/org/zstack/header/vm/additions/VmHostFileVO.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class VmHostFileVO extends ResourceVO {
4040
@Column
4141
private String path;
4242
@Column
43+
private String lastSyncReason;
44+
@Column
4345
private Timestamp createDate;
4446
@Column
4547
private Timestamp lastOpDate;
@@ -76,6 +78,14 @@ public void setPath(String path) {
7678
this.path = path;
7779
}
7880

81+
public String getLastSyncReason() {
82+
return lastSyncReason;
83+
}
84+
85+
public void setLastSyncReason(String lastSyncReason) {
86+
this.lastSyncReason = lastSyncReason;
87+
}
88+
7989
public Timestamp getCreateDate() {
8090
return createDate;
8191
}
@@ -100,6 +110,7 @@ public String toString() {
100110
", hostUuid='" + hostUuid + '\'' +
101111
", type=" + type +
102112
", path='" + path + '\'' +
113+
", lastSyncReason='" + lastSyncReason + '\'' +
103114
", createDate=" + createDate +
104115
", lastOpDate=" + lastOpDate +
105116
'}';

header/src/main/java/org/zstack/header/vm/additions/VmHostFileVO_.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class VmHostFileVO_ extends ResourceVO_ {
1212
public static volatile SingularAttribute<VmHostFileVO, String> hostUuid;
1313
public static volatile SingularAttribute<VmHostFileVO, VmHostFileType> type;
1414
public static volatile SingularAttribute<VmHostFileVO, String> path;
15+
public static volatile SingularAttribute<VmHostFileVO, String> lastSyncReason;
1516
public static volatile SingularAttribute<VmHostFileVO, Timestamp> createDate;
1617
public static volatile SingularAttribute<VmHostFileVO, Timestamp> lastOpDate;
1718
}

plugin/kvm/src/main/java/org/zstack/kvm/efi/KvmSecureBootExtensions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
import static org.zstack.compute.vm.VmGlobalConfig.ENABLE_UEFI_SECURE_BOOT;
8585
import static org.zstack.core.Platform.operr;
8686
import static org.zstack.header.vm.VmMigrationType.HostMigration;
87+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.PostMigration;
88+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.PrepareReRead;
89+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.PrepareRead;
90+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.ResourceRelease;
8791
import static org.zstack.kvm.KVMConstant.*;
8892
import static org.zstack.utils.CollectionDSL.list;
8993

@@ -358,6 +362,7 @@ public static class PrepareHostFileContext {
358362
public String hostUuid;
359363
public String vmUuid;
360364
public VmHostFileType type;
365+
public String syncReason;
361366

362367
public String path;
363368
// whether the NvRam is on the same host as before
@@ -396,6 +401,7 @@ public void run(FlowTrigger trigger, Map data) {
396401
SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg();
397402
syncMsg.setHostUuid(vmHostFile.getHostUuid());
398403
syncMsg.setVmUuid(context.vmUuid);
404+
syncMsg.setSyncReason(PrepareRead.reason(context.syncReason));
399405

400406
if (vmHostFile.getType() == VmHostFileType.NvRam) {
401407
context.path = vmHostFile.getPath();
@@ -506,6 +512,7 @@ public void run(FlowTrigger trigger, Map data) {
506512
SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg();
507513
syncMsg.setHostUuid(context.hostUuid);
508514
syncMsg.setVmUuid(context.vmUuid);
515+
syncMsg.setSyncReason(PrepareReRead.reason(context.syncReason));
509516

510517
if (context.type == VmHostFileType.NvRam) {
511518
syncMsg.setNvRamPath(context.path);
@@ -549,6 +556,7 @@ private void prepareNvRamHostFileOnHost(VmInstanceSpec spec, Completion completi
549556
context.hostUuid = spec.getDestHost().getUuid();
550557
context.vmUuid = spec.getVmInventory().getUuid();
551558
context.type = VmHostFileType.NvRam;
559+
context.syncReason = "pre-instantiate VM resource";
552560
prepareHostFileOnHost(context, completion);
553561
}
554562

@@ -758,6 +766,7 @@ public void releaseVmResource(VmInstanceSpec spec, Completion completion) {
758766
SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg();
759767
syncMsg.setHostUuid(hostUuid);
760768
syncMsg.setVmUuid(vmUuid);
769+
syncMsg.setSyncReason(ResourceRelease.reason());
761770

762771
for (VmHostFileVO file : vmHostFiles) {
763772
if (file.getType() == VmHostFileType.NvRam) {
@@ -822,6 +831,7 @@ public void afterMigrateVm(VmInstanceInventory inv, String srcHostUuid, NoErrorC
822831
SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg();
823832
syncMsg.setHostUuid(destHostUuid);
824833
syncMsg.setVmUuid(vmUuid);
834+
syncMsg.setSyncReason(PostMigration.reason());
825835

826836
for (VmHostFileVO file : vmHostFiles) {
827837
if (file.getType() == VmHostFileType.NvRam) {

plugin/kvm/src/main/java/org/zstack/kvm/efi/KvmSecureBootManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
import static org.zstack.compute.vm.VmGlobalConfig.ENABLE_UEFI_SECURE_BOOT;
7171
import static org.zstack.compute.vm.VmGlobalConfig.RESET_TPM_AFTER_VM_CLONE;
7272
import static org.zstack.core.Platform.operr;
73+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.PostClone;
74+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.Restore;
75+
import static org.zstack.header.vm.additions.VmHostFileSyncReason.VmShutdown;
7376
import static org.zstack.kvm.KVMAgentCommands.*;
7477
import static org.zstack.kvm.KVMConstant.*;
7578
import static org.zstack.utils.CollectionDSL.list;
@@ -143,6 +146,7 @@ protected void run(Map tokens, Object data) {
143146
innerMessage.setVmUuid(vmUuid);
144147
innerMessage.setNvRamPath(nvRamFile == null ? null : nvRamFile.getPath());
145148
innerMessage.setTpmStateFolder(tpmStateFile == null ? null : tpmStateFile.getPath());
149+
innerMessage.setSyncReason(VmShutdown.reason());
146150
bus.makeLocalServiceId(innerMessage, VmInstanceConstant.SECURE_BOOT_SERVICE_ID);
147151
bus.send(innerMessage, new CloudBusCallBack(null) {
148152
@Override
@@ -260,6 +264,7 @@ public void success(KvmResponseWrapper wrapper) {
260264
SQL.New(VmHostFileVO.class)
261265
.eq(VmHostFileVO_.uuid, file.getUuid())
262266
.set(VmHostFileVO_.lastOpDate, now)
267+
.set(VmHostFileVO_.lastSyncReason, msg.getSyncReason())
263268
.update();
264269
} else {
265270
file = new VmHostFileVO();
@@ -268,6 +273,7 @@ public void success(KvmResponseWrapper wrapper) {
268273
file.setVmInstanceUuid(msg.getVmUuid());
269274
file.setPath(path);
270275
file.setType(type);
276+
file.setLastSyncReason(msg.getSyncReason());
271277
file.setCreateDate(now);
272278
file.setLastOpDate(now);
273279
file.setResourceName(String.format("%s file for %s", type, msg.getVmUuid()));
@@ -376,6 +382,7 @@ public void run(FlowTrigger trigger, Map data) {
376382
SyncVmHostFilesFromHostMsg syncContext = new SyncVmHostFilesFromHostMsg();
377383
syncContext.setHostUuid(hostUuid);
378384
syncContext.setVmUuid(msg.getSrcVmUuid());
385+
syncContext.setSyncReason(PostClone.reason());
379386
return syncContext;
380387
});
381388
}
@@ -728,6 +735,7 @@ public void fail(ErrorCode errorCode) {
728735
VmHostFileVO currentFile = currentFilesByType.get(type);
729736
SQL.New(VmHostFileVO.class)
730737
.eq(VmHostFileVO_.uuid, currentFile.getUuid())
738+
.set(VmHostFileVO_.lastSyncReason, Restore.reason(msg.getSyncReason()))
731739
.set(VmHostFileVO_.lastOpDate, now)
732740
.update();
733741

@@ -757,6 +765,7 @@ public void fail(ErrorCode errorCode) {
757765
newFile.setHostUuid(finalHostUuid);
758766
newFile.setType(type);
759767
newFile.setPath(buildPathForVmHostFileType(type, msg.getVmInstanceUuid()));
768+
newFile.setLastSyncReason(Restore.reason(msg.getSyncReason()));
760769
newFile.setCreateDate(now);
761770
newFile.setLastOpDate(now);
762771
databaseFacade.persist(newFile);

plugin/kvm/src/main/java/org/zstack/kvm/efi/SyncVmHostFilesFromHostMsg.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class SyncVmHostFilesFromHostMsg extends NeedReplyMessage {
77
private String vmUuid;
88
private String nvRamPath;
99
private String tpmStateFolder;
10+
private String syncReason;
1011

1112
public String getHostUuid() {
1213
return hostUuid;
@@ -39,4 +40,12 @@ public String getTpmStateFolder() {
3940
public void setTpmStateFolder(String tpmStateFolder) {
4041
this.tpmStateFolder = tpmStateFolder;
4142
}
43+
44+
public String getSyncReason() {
45+
return syncReason;
46+
}
47+
48+
public void setSyncReason(String syncReason) {
49+
this.syncReason = syncReason;
50+
}
4251
}

plugin/kvm/src/main/java/org/zstack/kvm/tpm/KvmTpmExtensions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void run(FlowTrigger trigger, Map data) {
151151
innerContext.hostUuid = spec.getDestHost().getUuid();
152152
innerContext.vmUuid = spec.getVmInventory().getUuid();
153153
innerContext.type = VmHostFileType.TpmState;
154+
innerContext.syncReason = "pre-instantiate VM resource";
154155
secureBootExtensions.prepareHostFileOnHost(innerContext, new Completion(trigger) {
155156
@Override
156157
public void success() {

0 commit comments

Comments
 (0)