3131import org .zstack .header .vm .VmInstanceVO_ ;
3232import org .zstack .header .vm .VmInstanceConstant ;
3333import org .zstack .header .vm .AfterReimageVmInstanceExtensionPoint ;
34+ import org .zstack .header .vm .BeforeHaStartVmInstanceExtensionPoint ;
3435import org .zstack .header .vm .VmInstanceInventory ;
3536import org .zstack .header .vm .VmInstanceMigrateExtensionPoint ;
3637import org .zstack .header .vm .VmInstanceSpec ;
6970import org .zstack .utils .Utils ;
7071import org .zstack .utils .logging .CLogger ;
7172
73+ import javax .persistence .Tuple ;
7274import java .sql .Timestamp ;
7375import java .time .Instant ;
7476import java .util .Base64 ;
8284import static org .zstack .header .vm .VmMigrationType .HostMigration ;
8385import static org .zstack .header .vm .VmMigrationType .PrimaryStorageMigration ;
8486import static org .zstack .header .vm .additions .VmHostFileSyncReason .PostMigration ;
87+ import static org .zstack .header .vm .additions .VmHostFileSyncReason .BeforeHaStart ;
8588import static org .zstack .header .vm .additions .VmHostFileSyncReason .PrepareReRead ;
8689import static org .zstack .header .vm .additions .VmHostFileSyncReason .PrepareRead ;
8790import static org .zstack .header .vm .additions .VmHostFileSyncReason .ResourceRelease ;
@@ -96,7 +99,8 @@ public class KvmSecureBootExtensions implements KVMStartVmExtensionPoint,
9699 AfterReimageVmInstanceExtensionPoint ,
97100 VmReleaseResourceExtensionPoint ,
98101 VmInstanceMigrateExtensionPoint ,
99- VolumeSnapshotCreationExtensionPoint {
102+ VolumeSnapshotCreationExtensionPoint ,
103+ BeforeHaStartVmInstanceExtensionPoint {
100104 private static final CLogger logger = Utils .getLogger (KvmSecureBootExtensions .class );
101105
102106 @ Autowired
@@ -600,6 +604,66 @@ public void run(MessageReply reply) {
600604 });
601605 }
602606
607+ @ Override
608+ public void beforeHaStartVmInstance (String vmUuid , String judgerClassName , List <String > softAvoidHostUuids , Completion completion ) {
609+ Tuple hostUuidTuple = Q .New (VmInstanceVO .class )
610+ .select (VmInstanceVO_ .hostUuid , VmInstanceVO_ .lastHostUuid )
611+ .eq (VmInstanceVO_ .uuid , vmUuid )
612+ .findTuple ();
613+ if (hostUuidTuple == null ) {
614+ completion .success ();
615+ return ;
616+ }
617+
618+ String hostUuid = hostUuidTuple .get (0 , String .class );
619+ final String finalHostUuid = (hostUuid == null ) ? hostUuidTuple .get (1 , String .class ) : hostUuid ;
620+ if (finalHostUuid == null ) {
621+ logger .debug ("skip HA file sync: VM has no host/lastHost record" );
622+ completion .success ();
623+ return ;
624+ }
625+
626+ List <VmHostFileVO > vmHostFiles = Q .New (VmHostFileVO .class )
627+ .eq (VmHostFileVO_ .vmInstanceUuid , vmUuid )
628+ .eq (VmHostFileVO_ .hostUuid , finalHostUuid )
629+ .list ();
630+ if (vmHostFiles .isEmpty ()) {
631+ completion .success ();
632+ return ;
633+ }
634+
635+ logger .info (String .format ("try to sync VM host file[vmUuid=%s] from last host[uuid=%s] before HA start" ,
636+ vmUuid , finalHostUuid ));
637+ SyncVmHostFilesFromHostMsg syncMsg = new SyncVmHostFilesFromHostMsg ();
638+ syncMsg .setHostUuid (finalHostUuid );
639+ syncMsg .setVmUuid (vmUuid );
640+ syncMsg .setSyncReason (BeforeHaStart .reason ());
641+
642+ for (VmHostFileVO file : vmHostFiles ) {
643+ if (file .getType () == VmHostFileType .NvRam ) {
644+ syncMsg .setNvRamPath (file .getPath ());
645+ } else if (file .getType () == VmHostFileType .TpmState ) {
646+ syncMsg .setTpmStateFolder (file .getPath ());
647+ } else {
648+ logger .warn (String .format (
649+ "unsupported vm host file type: %s, skip syncing for VM[uuid:%s] from host[uuid:%s]" ,
650+ file .getType (), vmUuid , finalHostUuid ));
651+ }
652+ }
653+
654+ bus .makeLocalServiceId (syncMsg , VmInstanceConstant .SECURE_BOOT_SERVICE_ID );
655+ bus .send (syncMsg , new CloudBusCallBack (completion ) {
656+ @ Override
657+ public void run (MessageReply reply ) {
658+ if (!reply .isSuccess ()) {
659+ logger .warn (String .format ("failed to sync VM host file[vmUuid=%s] from last host[uuid=%s] before HA start, but tolerated: %s" ,
660+ vmUuid , finalHostUuid , reply .getError ().getReadableDetails ()));
661+ }
662+ completion .success ();
663+ }
664+ });
665+ }
666+
603667 @ Override
604668 public void afterMigrateVm (VmInstanceInventory inv , String srcHostUuid , NoErrorCompletion completion ) {
605669 String destHostUuid = inv .getHostUuid ();
0 commit comments