From a4c39057fbdf1f5d66372e9373cdeeaf2bf331ed Mon Sep 17 00:00:00 2001 From: wanyaoqi <18528551+wanyaoqi@users.noreply.github.com> Date: Tue, 28 Jul 2026 11:01:36 +0800 Subject: [PATCH] Automated cherry pick of #25206: fix(region,host): host register with correct hostname (#25234) * fix(region,host): host register with correct hostname * fix(region): guest migrate reuse create params schedtag --- pkg/compute/models/guest_actions.go | 11 +++++++++-- pkg/compute/models/hosts.go | 4 ++++ pkg/compute/tasks/guest/guest_live_migrate_task.go | 2 +- pkg/hostman/hostinfo/hostinfo.go | 10 ++++++---- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 69d5375d23..01bde022da 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -542,7 +542,7 @@ func (self *SGuest) PerformMigrateForecast(ctx context.Context, userCred mcclien } } - schedParams := self.GetSchedMigrateParams(userCred, input) + schedParams := self.GetSchedMigrateParams(ctx, userCred, input) if input.ConvertToKvm { schedParams.Hypervisor = api.HYPERVISOR_KVM } @@ -557,12 +557,19 @@ func (self *SGuest) PerformMigrateForecast(ctx context.Context, userCred mcclien } func (self *SGuest) GetSchedMigrateParams( - userCred mcclient.TokenCredential, + ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerMigrateForecastInput, ) *schedapi.ScheduleInput { schedDesc := self.ToSchedDesc() if input.PreferHostId != "" { schedDesc.ServerConfig.PreferHost = input.PreferHostId + } else { + createParams, err := self.GetCreateParams(ctx, userCred) + if err != nil { + log.Errorf("GetSchedMigrateParams failed get create params: %s", err) + } else { + schedDesc.Schedtags = createParams.Schedtags + } } schedDesc.ResetCpuNumaPin = input.ResetCpuNumaPin diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 15ac18e70e..38978af247 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -7402,6 +7402,10 @@ func (host *SHost) RemoteHealthStatus(ctx context.Context) string { } func (host *SHost) GetHostnameByName() string { + if host.Hostname != "" { + return host.Hostname + } + hostname := host.Name accessIp := strings.Replace(host.AccessIp, ".", "-", -1) if strings.HasSuffix(host.Name, "-"+accessIp) { diff --git a/pkg/compute/tasks/guest/guest_live_migrate_task.go b/pkg/compute/tasks/guest/guest_live_migrate_task.go index 3412a1a235..d29b628b6c 100644 --- a/pkg/compute/tasks/guest/guest_live_migrate_task.go +++ b/pkg/compute/tasks/guest/guest_live_migrate_task.go @@ -89,7 +89,7 @@ func (task *GuestMigrateTask) GetSchedParams() (*schedapi.ScheduleInput, error) input.SkipCpuCheck = skipCpuCheck input.SkipKernelCheck = skipKernelCheck } - res := guest.GetSchedMigrateParams(task.GetUserCred(), input) + res := guest.GetSchedMigrateParams(context.Background(), task.GetUserCred(), input) if devs, _ := guest.GetGuestIsolatedDevices(); len(devs) > 0 { preferNumaNodesSet := cpuset.NewBuilder() diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index af45e9f2aa..159129c03c 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -1568,12 +1568,13 @@ func (h *SHostInfo) ProbeSyncIsolatedDevices(hostId string, body jsonutils.JSONO return h.probeSyncIsolatedDevices() } -func (h *SHostInfo) setHostname(name string) { - h.FullName = name - err := sysutils.SetHostname(name) +func (h *SHostInfo) fetchOsHostname() string { + hn, err := os.Hostname() if err != nil { - log.Errorf("Fail to set system hostname: %s", err) + log.Fatalf("fail to get hostname %s", err) + return "" } + return hn } func (h *SHostInfo) fetchHostname() string { @@ -1621,6 +1622,7 @@ func (h *SHostInfo) updateOrCreateHost(hostId string) (*api.HostDetails, error) if len(hostId) == 0 { input.GenerateName = h.fetchHostname() } + input.Hostname = h.fetchOsHostname() input.AccessIp = masterIp input.AccessMac = h.GetMasterMac() var schema = "http"