Skip to content

Commit

Permalink
support diable usb kbd
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Aug 11, 2020
1 parent 156f983 commit df9e47c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/climc/shell/compute/servers.go
Expand Up @@ -1140,6 +1140,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,
Expand All @@ -1151,6 +1152,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
Expand Down
4 changes: 4 additions & 0 deletions cmd/climc/shell/image/images.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/compute/api.go
Expand Up @@ -471,7 +471,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
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/image/consts.go
Expand Up @@ -50,6 +50,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"
)
Expand Down
7 changes: 7 additions & 0 deletions pkg/compute/models/guest_actions.go
Expand Up @@ -3091,6 +3091,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
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/models/guests.go
Expand Up @@ -1149,6 +1149,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)
Expand Down Expand Up @@ -1707,6 +1708,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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/hostman/guestman/qemu-kvmhelper.go
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit df9e47c

Please sign in to comment.