Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/apis/compute/guest_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
DISK_CLONE_TASK_ID = "__disk_clone_task_id"

SSH_PORT = "__ssh_port"

DAEMON_GUEST_MANUAL_STOP = "daemon_guest_manual_stop"
)

const BASE_INSTANCE_SNAPSHOT_ID = "__base_instance_snapshot_id"
6 changes: 6 additions & 0 deletions pkg/compute/guestdrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ func (self *SKVMGuestDriver) RequestStopOnHost(ctx context.Context, guest *model
timeout = 0
}
body.Add(jsonutils.NewInt(timeout), "timeout")
if guest.IsDaemon.IsTrue() {
val := guest.GetMetadata(ctx, api.DAEMON_GUEST_MANUAL_STOP, task.GetUserCred())
if len(val) > 0 {
body.Set("daemon_guest_manual_stop", jsonutils.JSONTrue)
}
}

header := self.getTaskRequestHeader(task)

Expand Down
7 changes: 7 additions & 0 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,9 @@ func (self *SGuest) PerformStart(
}
}
if self.isAllDisksReady() {
if self.IsDaemon.IsTrue() {
self.SetMetadata(ctx, api.DAEMON_GUEST_MANUAL_STOP, "", userCred)
}
kwargs := jsonutils.Marshal(input).(*jsonutils.JSONDict)
err := self.GetDriver().PerformStart(ctx, userCred, self, kwargs, "")
return nil, err
Expand Down Expand Up @@ -3330,6 +3333,10 @@ func (self *SGuest) PerformStatus(ctx context.Context, userCred mcclient.TokenCr

func (self *SGuest) PerformStop(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject,
input api.ServerStopInput) (jsonutils.JSONObject, error) {
if self.IsDaemon.IsTrue() {
self.SetMetadata(ctx, api.DAEMON_GUEST_MANUAL_STOP, true, userCred)
}

// XXX if is force, force stop guest
if input.IsForce || utils.IsInStringArray(self.Status, []string{api.VM_RUNNING, api.VM_STOP_FAILED}) {
if err := self.ValidateEncryption(ctx, userCred); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/hostman/guestman/guesthandlers/guesthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ func guestStart(ctx context.Context, userCred mcclient.TokenCredential, sid stri
}

func guestStop(ctx context.Context, userCred mcclient.TokenCredential, sid string, body jsonutils.JSONObject) (interface{}, error) {
daemonGuestManualStop := jsonutils.QueryBoolean(body, "daemon_guest_manual_stop", false)
timeout, err := body.Int("timeout")
if err != nil {
timeout = 30
}
return nil, guestman.GetGuestManager().GuestStop(ctx, sid, timeout)
return nil, guestman.GetGuestManager().GuestStop(ctx, sid, timeout, daemonGuestManualStop)
}

func guestMonitor(ctx context.Context, userCred mcclient.TokenCredential, sid string, body jsonutils.JSONObject) (interface{}, error) {
Expand Down
33 changes: 32 additions & 1 deletion pkg/hostman/guestman/guestman.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"yunion.io/x/onecloud/pkg/hostman/storageman/remotefile"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
"yunion.io/x/onecloud/pkg/util/cgrouputils"
"yunion.io/x/onecloud/pkg/util/cgrouputils/cpuset"
Expand Down Expand Up @@ -320,6 +321,32 @@ func (m *SGuestManager) OnVerifyExistingGuestsSucc(servers []jsonutils.JSONObjec
m.RemoveCandidateServer(server)
}
}
timeutils2.AddTimeout(60*time.Second, func() { m.checkDaemonGuestsIsRunning() })
}

func (m *SGuestManager) checkDaemonGuestsIsRunning() {
m.Servers.Range(func(k, v interface{}) bool {
guest := v.(*SKVMGuestInstance)
if !guest.IsDaemon() {
return true
}
if guest.IsRunning() || guest.IsSuspend() {
return true
}

if guest.StartupTask != nil {
return true
}
if guest.isDaemonGuestManualStop() {
return true
}
if err := guest.StartGuest(context.Background(), auth.AdminCredential(), jsonutils.NewDict()); err != nil {
log.Errorf("checkDaemonGuestsIsRunning start guest %s failed: %s", guest.GetName(), err.Error())
}
return true
})

timeutils2.AddTimeout(60*time.Second, func() { m.checkDaemonGuestsIsRunning() })
}

func (m *SGuestManager) RemoveCandidateServer(server *SKVMGuestInstance) {
Expand Down Expand Up @@ -963,8 +990,12 @@ func (m *SGuestManager) GuestStart(ctx context.Context, userCred mcclient.TokenC
}
}

func (m *SGuestManager) GuestStop(ctx context.Context, sid string, timeout int64) error {
func (m *SGuestManager) GuestStop(ctx context.Context, sid string, timeout int64, daemonGuestManualStop bool) error {
if guest, ok := m.GetServer(sid); ok {
if daemonGuestManualStop {
guest.Desc.Metadata[compute.DAEMON_GUEST_MANUAL_STOP] = "true"
guest.SaveLiveDesc(guest.Desc)
}
hostutils.DelayTaskWithoutReqctx(ctx, guest.ExecStopTask, timeout)
return nil
} else {
Expand Down
8 changes: 7 additions & 1 deletion pkg/hostman/guestman/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,13 @@ func (s *SKVMGuestInstance) initIsolatedDevices(pciRoot, pciBridge *desc.PCICont

manager := s.manager.GetHost().GetIsolatedDeviceManager()
for i := 0; i < len(s.Desc.IsolatedDevices); i++ {
dev := manager.GetDeviceByAddr(s.Desc.IsolatedDevices[i].Addr)
dev := manager.GetDeviceByIdent(s.Desc.IsolatedDevices[i].VendorDeviceId, s.Desc.IsolatedDevices[i].Addr, s.Desc.IsolatedDevices[i].MdevId)
if dev == nil {
log.Errorf("failed find dev by %s %s %s",
s.Desc.IsolatedDevices[i].VendorDeviceId, s.Desc.IsolatedDevices[i].Addr, s.Desc.IsolatedDevices[i].MdevId)
continue
}

if s.Desc.IsolatedDevices[i].DevType == api.USB_TYPE {
s.Desc.IsolatedDevices[i].Usb = desc.NewUsbDevice("usb-host", dev.GetQemuId())
s.Desc.IsolatedDevices[i].Usb.Options = dev.GetPassthroughOptions()
Expand Down
1 change: 1 addition & 0 deletions pkg/hostman/guestman/qemu-kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ func (s *SKVMGuestInstance) asyncScriptStart(ctx context.Context, params interfa
}

if err != nil {
log.Errorf("asyncScriptStart init desc failed %s", err)
if ctx != nil && len(appctx.AppContextTaskId(ctx)) >= 0 {
hostutils.TaskFailed(ctx, fmt.Sprintf("Async start server failed: %s", err))
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/hostman/guestman/qemu-kvmhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (s *SKVMGuestInstance) isDisableAutoMergeSnapshots() bool {
return s.Desc.Metadata[api.VM_METADATA_DISABLE_AUTO_MERGE_SNAPSHOT] == "true"
}

func (s *SKVMGuestInstance) isDaemonGuestManualStop() bool {
return s.Desc.Metadata[api.DAEMON_GUEST_MANUAL_STOP] == "true"
}

func (s *SKVMGuestInstance) getMachine() string {
machine := s.Desc.Machine
if machine == "" {
Expand Down Expand Up @@ -856,6 +860,9 @@ func (s *SKVMGuestInstance) gpusHasVga() bool {
manager := s.manager.GetHost().GetIsolatedDeviceManager()
for i := 0; i < len(s.Desc.IsolatedDevices); i++ {
dev := manager.GetDeviceByAddr(s.Desc.IsolatedDevices[i].Addr)
if dev == nil {
continue
}
if dev.GetDeviceType() == api.GPU_VGA_TYPE {
return true
}
Expand All @@ -867,6 +874,9 @@ func (s *SKVMGuestInstance) hasGPU() bool {
manager := s.manager.GetHost().GetIsolatedDeviceManager()
for i := 0; i < len(s.Desc.IsolatedDevices); i++ {
dev := manager.GetDeviceByAddr(s.Desc.IsolatedDevices[i].Addr)
if dev == nil {
continue
}
if dev.GetDeviceType() == api.GPU_VGA_TYPE || dev.GetDeviceType() == api.GPU_HPC_TYPE {
return true
}
Expand Down
Loading