Skip to content
Closed
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 @@ -273,6 +273,12 @@ func (self *SKVMGuestDriver) RequestStopOnHost(ctx context.Context, guest *model
body.Set("is_force", jsonutils.JSONTrue)
}
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
6 changes: 6 additions & 0 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,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)
driver, err := self.GetDriver()
if err != nil {
Expand Down Expand Up @@ -4037,6 +4040,9 @@ 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)
}
drv, err := self.GetDriver()
if err != nil {
return nil, errors.Wrap(err, "GetDriver")
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 @@ -237,12 +237,13 @@ 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
}
forceStop := jsonutils.QueryBoolean(body, "is_force", false)
return nil, guestman.GetGuestManager().GuestStop(ctx, sid, timeout, forceStop)
return nil, guestman.GetGuestManager().GuestStop(ctx, sid, timeout, forceStop, daemonGuestManualStop)
}

func guestMonitor(ctx context.Context, userCred mcclient.TokenCredential, sid string, body jsonutils.JSONObject) (interface{}, error) {
Expand Down
31 changes: 29 additions & 2 deletions pkg/hostman/guestman/guestman.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 @@ -503,6 +504,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 GuestRuntimeInstance) {
Expand Down Expand Up @@ -1185,9 +1212,9 @@ func (m *SGuestManager) GuestStart(ctx context.Context, userCred mcclient.TokenC
}
}

func (m *SGuestManager) GuestStop(ctx context.Context, sid string, timeout int64, isForce bool) error {
func (m *SGuestManager) GuestStop(ctx context.Context, sid string, timeout int64, isForce, daemonGuestManualStop bool) error {
if server, ok := m.GetServer(sid); ok {
if err := server.HandleStop(ctx, timeout, isForce); err != nil {
if err := server.HandleStop(ctx, timeout, isForce, daemonGuestManualStop); err != nil {
return errors.Wrap(err, "Do stop")
}
} 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 @@ -344,7 +344,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 == compute.USB_TYPE {
s.Desc.IsolatedDevices[i].Usb = desc.NewUsbDevice("usb-host", dev.GetQemuId())
s.Desc.IsolatedDevices[i].Usb.Options = dev.GetPassthroughOptions()
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/guestman/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ func (s *sPodGuestInstance) HandleGuestStart(ctx context.Context, userCred mccli
return nil, nil
}

func (s *sPodGuestInstance) HandleStop(ctx context.Context, timeout int64, isForce bool) error {
func (s *sPodGuestInstance) HandleStop(ctx context.Context, timeout int64, isForce, daemonGuestManualStop bool) error {
hostutils.DelayTask(ctx, func(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
err := s.stopPod(ctx, timeout)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/hostman/guestman/qemu-kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,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 Expand Up @@ -1988,11 +1989,15 @@ func (s *SKVMGuestInstance) StartGuest(ctx context.Context, userCred mcclient.To
return nil
}

func (s *SKVMGuestInstance) HandleStop(ctx context.Context, timeout int64, isForce bool) error {
func (s *SKVMGuestInstance) HandleStop(ctx context.Context, timeout int64, isForce, daemonGuestManualStop bool) error {
params := &SGuestStopParams{
IsForce: isForce,
Timeout: timeout,
}
if daemonGuestManualStop {
s.Desc.Metadata[api.DAEMON_GUEST_MANUAL_STOP] = "true"
SaveLiveDesc(s, s.Desc)
}
hostutils.DelayTaskWithoutReqctx(ctx, s.ExecStopTask, params)
return nil
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/hostman/guestman/qemu-kvmhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,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 @@ -1162,7 +1166,12 @@ func (s *SKVMGuestInstance) startMemCleaner() error {

func (s *SKVMGuestInstance) gpusHasVga() bool {
for i := 0; i < len(s.Desc.IsolatedDevices); i++ {
if s.Desc.IsolatedDevices[i].GpuType == api.GPU_VGA {
manager := s.manager.GetHost().GetIsolatedDeviceManager()
dev := manager.GetDeviceByAddr(s.Desc.IsolatedDevices[i].Addr)
if dev == nil {
continue
}
if dev.GetDeviceType() == api.GPU_VGA {
return true
}
}
Expand All @@ -1173,6 +1182,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_TYPE {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/guestman/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type GuestRuntimeInstance interface {
PostUploadStatus(resp *computeapi.HostUploadGuestStatusInput, reason string)
HandleGuestStart(ctx context.Context, userCred mcclient.TokenCredential, body jsonutils.JSONObject) (jsonutils.JSONObject, error)

HandleStop(ctx context.Context, timeout int64, isForce bool) error
HandleStop(ctx context.Context, timeout int64, isForce, daemonGuestManualStop bool) error

LoadDesc() error
PostLoad(m *SGuestManager) error
Expand Down
Loading