From d0cbc99423f3d8da1dfbee4a1a55eaf8f0203811 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 4 Nov 2021 22:26:47 +0800 Subject: [PATCH] feat(region,host): create or update server add machine option (#12605) --- pkg/apis/compute/api.go | 4 ++++ pkg/apis/compute/guest_const.go | 5 +++++ pkg/apis/compute/guests.go | 2 +- pkg/compute/guestdrivers/base.go | 4 ++++ pkg/compute/guestdrivers/virtualization.go | 21 +++++++++++++++++++++ pkg/compute/models/guestdrivers.go | 1 + pkg/compute/models/guests.go | 4 ++++ pkg/hostman/guestman/qemu-kvmhelper.go | 5 +++-- pkg/mcclient/options/servers.go | 3 +++ 9 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pkg/apis/compute/api.go b/pkg/apis/compute/api.go index 2bb893a810..5f8cc7b935 100644 --- a/pkg/apis/compute/api.go +++ b/pkg/apis/compute/api.go @@ -396,6 +396,10 @@ type ServerCreateInput struct { // emulate: BIOS, UEFI Bios string `json:"bios"` + // Machine类型 + // emulate: pc, q35 + Machine string `json:"machine"` + // 启动顺序 // c: cdrome // d: disk diff --git a/pkg/apis/compute/guest_const.go b/pkg/apis/compute/guest_const.go index ef93983433..de9b30b3cb 100644 --- a/pkg/apis/compute/guest_const.go +++ b/pkg/apis/compute/guest_const.go @@ -180,6 +180,11 @@ const ( CPU_MODE_HOST = "host" ) +const ( + VM_MACHINE_TYPE_PC = "pc" + VM_MACHINE_TYPE_Q35 = "q35" +) + var VM_RUNNING_STATUS = []string{VM_START_START, VM_STARTING, VM_RUNNING, VM_BLOCK_STREAM, VM_BLOCK_STREAM_FAIL} var VM_CREATING_STATUS = []string{VM_CREATE_NETWORK, VM_CREATE_DISK, VM_START_DEPLOY, VM_DEPLOYING} diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index 0fc9db8227..259ccf55e6 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -631,7 +631,7 @@ type GuestJsonDesc struct { Cpu int `json:"cpu"` Vga string `json:"vga"` Vdi string `json:"vdi"` - Machine string `json:"machie"` + Machine string `json:"machine"` Bios string `json:"bios"` BootOrder string `json:"boot_order"` SrcIpCheck bool `json:"src_ip_check"` diff --git a/pkg/compute/guestdrivers/base.go b/pkg/compute/guestdrivers/base.go index 095a60a329..b50940b7a2 100644 --- a/pkg/compute/guestdrivers/base.go +++ b/pkg/compute/guestdrivers/base.go @@ -411,6 +411,10 @@ func (self *SBaseGuestDriver) RequestLiveMigrate(ctx context.Context, guest *mod return fmt.Errorf("Not Implement RequestLiveMigrate") } +func (self *SBaseGuestDriver) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error { + return nil +} + func (self *SBaseGuestDriver) RequestRemoteUpdate(ctx context.Context, guest *models.SGuest, userCred mcclient.TokenCredential, replaceTags bool) error { // nil ops return nil diff --git a/pkg/compute/guestdrivers/virtualization.go b/pkg/compute/guestdrivers/virtualization.go index ccdca3332d..7a3f1b76c8 100644 --- a/pkg/compute/guestdrivers/virtualization.go +++ b/pkg/compute/guestdrivers/virtualization.go @@ -265,10 +265,31 @@ func (self *SVirtualizedGuestDriver) RequestStopGuestForDelete(ctx context.Conte return nil } +func (self *SVirtualizedGuestDriver) ValidateMachineType(machine string) error { + if !utils.IsInStringArray(machine, []string{api.VM_MACHINE_TYPE_PC, api.VM_MACHINE_TYPE_Q35}) { + return httperrors.NewBadRequestError("Invalid machine %q", machine) + } + return nil +} + func (self *SVirtualizedGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) { + if input.Machine != "" { + if err := self.ValidateMachineType(input.Machine); err != nil { + return nil, err + } + } return input, nil } +func (self *SVirtualizedGuestDriver) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error { + if input.Machine != nil { + if err := self.ValidateMachineType(*input.Machine); err != nil { + return err + } + } + return nil +} + func (self *SVirtualizedGuestDriver) ValidateCreateDataOnHost(ctx context.Context, userCred mcclient.TokenCredential, bmName string, host *models.SHost, input *api.ServerCreateInput) (*api.ServerCreateInput, error) { if host.HostStatus != api.HOST_ONLINE { return nil, httperrors.NewInvalidStatusError("Host %s is not online", bmName) diff --git a/pkg/compute/models/guestdrivers.go b/pkg/compute/models/guestdrivers.go index fa9c687f03..85cda2e3b1 100644 --- a/pkg/compute/models/guestdrivers.go +++ b/pkg/compute/models/guestdrivers.go @@ -205,6 +205,7 @@ type IGuestDriver interface { RequestMigrate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, task taskman.ITask) error RequestLiveMigrate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, task taskman.ITask) error + ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.ServerUpdateInput) error RequestRemoteUpdate(ctx context.Context, guest *SGuest, userCred mcclient.TokenCredential, replaceTags bool) error RequestOpenForward(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, req *guestdriver_types.OpenForwardRequest) (*guestdriver_types.OpenForwardResponse, error) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index c1d3a20317..112743ede2 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1017,6 +1017,10 @@ func (self *SGuest) ValidateUpdateData(ctx context.Context, userCred mcclient.To } } + if err := self.GetDriver().ValidateUpdateData(ctx, userCred, input); err != nil { + return input, err + } + var err error input.VirtualResourceBaseUpdateInput, err = self.SVirtualResourceBase.ValidateUpdateData(ctx, userCred, query, input.VirtualResourceBaseUpdateInput) if err != nil { diff --git a/pkg/hostman/guestman/qemu-kvmhelper.go b/pkg/hostman/guestman/qemu-kvmhelper.go index 6f2909246a..3bf9f97612 100644 --- a/pkg/hostman/guestman/qemu-kvmhelper.go +++ b/pkg/hostman/guestman/qemu-kvmhelper.go @@ -29,6 +29,7 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" + api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/hostman/options" "yunion.io/x/onecloud/pkg/hostman/storageman" "yunion.io/x/onecloud/pkg/util/fileutils2" @@ -120,7 +121,7 @@ func (s *SKVMGuestInstance) isWindows10() bool { func (s *SKVMGuestInstance) getMachine() string { machine, err := s.Desc.GetString("machine") if err != nil { - machine = "pc" + machine = api.VM_MACHINE_TYPE_PC } return machine } @@ -134,7 +135,7 @@ func (s *SKVMGuestInstance) getBios() string { } func (s *SKVMGuestInstance) isQ35() bool { - return s.getMachine() == "q35" + return s.getMachine() == api.VM_MACHINE_TYPE_Q35 } func (s *SKVMGuestInstance) GetVdiProtocol() string { diff --git a/pkg/mcclient/options/servers.go b/pkg/mcclient/options/servers.go index aacc7a2aac..093dfef769 100644 --- a/pkg/mcclient/options/servers.go +++ b/pkg/mcclient/options/servers.go @@ -365,6 +365,7 @@ type ServerCreateOptionalOptions struct { Vga string `help:"VGA driver" choices:"std|vmware|cirrus|qxl"` Vdi string `help:"VDI protocool" choices:"vnc|spice"` Bios string `help:"BIOS" choices:"BIOS|UEFI"` + Machine string `help:"Machine type" choices:"pc|q35"` Desc string `help:"Description" metavar:"" json:"description"` Boot string `help:"Boot device" metavar:"" choices:"disk|cdrom" json:"-"` EnableCloudInit bool `help:"Enable cloud-init service"` @@ -468,6 +469,7 @@ func (opts *ServerCreateOptionalOptions) OptionalParams() (*computeapi.ServerCre Vga: opts.Vga, Vdi: opts.Vdi, Bios: opts.Bios, + Machine: opts.Machine, ShutdownBehavior: opts.ShutdownBehavior, AutoStart: opts.AutoStart, Duration: opts.Duration, @@ -583,6 +585,7 @@ type ServerUpdateOptions struct { Boot string `help:"Boot device" choices:"disk|cdrom"` Delete string `help:"Lock server to prevent from deleting" choices:"enable|disable" json:"-"` ShutdownBehavior string `help:"Behavior after VM server shutdown" choices:"stop|terminate"` + Machine string `help:"Machine type" choices:"q35|pc"` } func (opts *ServerUpdateOptions) Params() (jsonutils.JSONObject, error) {