diff --git a/cmd/climc/shell/images.go b/cmd/climc/shell/images.go index c620f59efd..72257f73e9 100644 --- a/cmd/climc/shell/images.go +++ b/cmd/climc/shell/images.go @@ -54,6 +54,7 @@ type ImageOptionalOptions struct { Hypervisor []string `help:"Prefer hypervisor type" choices:"kvm|esxi|baremetal|container|openstack|ctyun"` DiskDriver string `help:"Perfer disk driver" choices:"virtio|scsi|pvscsi|ide|sata"` NetDriver string `help:"Preferred network driver" choices:"virtio|e1000|vmxnet3"` + DisableUsbKbd bool `help:"Disable usb keyboard on this image(for hypervisor kvm)"` } func addImageOptionalOptions(s *mcclient.ClientSession, params *jsonutils.JSONDict, args ImageOptionalOptions) error { @@ -131,6 +132,9 @@ func addImageOptionalOptions(s *mcclient.ClientSession, params *jsonutils.JSONDi if len(args.Hypervisor) > 0 { params.Add(jsonutils.NewString(strings.Join(args.Hypervisor, ",")), "properties", "hypervisor") } + if args.DisableUsbKbd { + params.Add(jsonutils.NewString("true"), "properties", "disable_usb_kbd") + } return nil } diff --git a/cmd/climc/shell/servers.go b/cmd/climc/shell/servers.go index 7ad9c09288..821f86cc2b 100644 --- a/cmd/climc/shell/servers.go +++ b/cmd/climc/shell/servers.go @@ -1146,6 +1146,7 @@ func init() { ID string `help:"ID or name of VM"` DisableIsaSerial string `help:"disable isa serial device" choices:"true|false"` DisablePvpanic string `help:"disable pvpanic device" choices:"true|false"` + DisableUsbKbd string `help:"disable usb kbd" choices:"true|false"` } R(&ServerQemuParams{}, "server-set-qemu-params", "config qemu params", func(s *mcclient.ClientSession, @@ -1157,6 +1158,9 @@ func init() { if len(opts.DisablePvpanic) > 0 { params.Set("disable_pvpanic", jsonutils.NewString(opts.DisablePvpanic)) } + if len(opts.DisableUsbKbd) > 0 { + params.Set("disable_usb_kbd", jsonutils.NewString(opts.DisableUsbKbd)) + } result, err := modules.Servers.PerformAction(s, opts.ID, "set-qemu-params", params) if err != nil { return err diff --git a/pkg/apis/compute/api.go b/pkg/apis/compute/api.go index ebab70634a..83e343c11e 100644 --- a/pkg/apis/compute/api.go +++ b/pkg/apis/compute/api.go @@ -466,7 +466,8 @@ type ServerCreateInput struct { // swagger:ignore OsType string `json:"os_type"` - + // swagger:ignore + DisableUsbKbd bool `json:"disable_usb_kbd"` // swagger:ignore OsProfile jsonutils.JSONObject `json:"__os_profile__"` // swagger:ignore diff --git a/pkg/apis/image/consts.go b/pkg/apis/image/consts.go index 5bfa3e1b07..f4cc4ac417 100644 --- a/pkg/apis/image/consts.go +++ b/pkg/apis/image/consts.go @@ -48,6 +48,7 @@ const ( IMAGE_IS_READONLY = "is_readonly" IMAGE_PARTITION_TYPE = "partition_type" IMAGE_INSTALLED_CLOUDINIT = "installed_cloud_init" + IMAGE_DISABLE_USB_KBD = "disable_usb_kbd" IMAGE_STATUS_UPDATING = "updating" ) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 2179aaeb3a..94c5613a61 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -3056,6 +3056,13 @@ func (self *SGuest) PerformSetQemuParams(ctx context.Context, userCred mcclient. return nil, err } } + usbKbd, err := data.GetString("disable_usb_kbd") + if err == nil { + err = self.SetMetadata(ctx, "disable_usb_kbd", usbKbd, userCred) + if err != nil { + return nil, err + } + } return nil, nil } diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 83bf0d2a9d..ebd9b2fdf3 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1107,6 +1107,7 @@ func (manager *SGuestManager) validateCreateData( if len(imgProperties) == 0 { imgProperties = map[string]string{"os_type": "Linux"} } + input.DisableUsbKbd = imgProperties[imageapi.IMAGE_DISABLE_USB_KBD] == "true" osType := input.OsType osProf, err = osprofile.GetOSProfileFromImageProperties(imgProperties, hypervisor) @@ -1665,6 +1666,9 @@ func (guest *SGuest) PostCreate(ctx context.Context, userCred mcclient.TokenCred if osProfileJson != nil { guest.setOSProfile(ctx, userCred, osProfileJson) } + if jsonutils.QueryBoolean(data, imageapi.IMAGE_DISABLE_USB_KBD, false) { + guest.SetMetadata(ctx, imageapi.IMAGE_DISABLE_USB_KBD, "true", userCred) + } userData, _ := data.GetString("user_data") if len(userData) > 0 { diff --git a/pkg/hostman/guestman/qemu-kvmhelper.go b/pkg/hostman/guestman/qemu-kvmhelper.go index 3e3a27d858..dc74a71903 100644 --- a/pkg/hostman/guestman/qemu-kvmhelper.go +++ b/pkg/hostman/guestman/qemu-kvmhelper.go @@ -79,6 +79,11 @@ func (s *SKVMGuestInstance) getOsname() string { return osName } +func (s *SKVMGuestInstance) disableUsbKbd() bool { + val, _ := s.Desc.GetString("metadata", "disable_usb_kbd") + return val == "true" +} + func (s *SKVMGuestInstance) getOsDistribution() string { osDis, _ := s.Desc.GetString("metadata", "os_distribution") return osDis @@ -499,7 +504,7 @@ func (s *SKVMGuestInstance) _generateStartScript(data *jsonutils.JSONDict) (stri cmd += " -device virtio-serial" cmd += " -usb" - if !utils.IsInStringArray(s.getOsDistribution(), []string{OS_NAME_OPENWRT, OS_NAME_CIRROS}) { + if !utils.IsInStringArray(s.getOsDistribution(), []string{OS_NAME_OPENWRT, OS_NAME_CIRROS}) && !s.disableUsbKbd() { cmd += " -device usb-kbd" } // # if osname == self.OS_NAME_ANDROID: