diff --git a/pkg/apis/compute/api.go b/pkg/apis/compute/api.go index ab775eee48..8b980918be 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 9069651f2b..2aac3222dd 100644 --- a/pkg/apis/compute/guest_const.go +++ b/pkg/apis/compute/guest_const.go @@ -176,6 +176,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/compute/guestdrivers/virtualization.go b/pkg/compute/guestdrivers/virtualization.go index 6aef096b8b..607b490544 100644 --- a/pkg/compute/guestdrivers/virtualization.go +++ b/pkg/compute/guestdrivers/virtualization.go @@ -255,7 +255,29 @@ 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 *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { + machine, _ := input.GetString("machine") + if machine != "" { + if err := self.ValidateMachineType(machine); err != nil { + return nil, err + } + } return input, nil } diff --git a/pkg/hostman/guestman/qemu-kvmhelper.go b/pkg/hostman/guestman/qemu-kvmhelper.go index c200556079..6304016400 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 23cfb40398..ac7c1e457e 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) {