From bb3bc0768f038a1f5b7798bda2e1046ce18b1731 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 18 Dec 2023 13:33:17 +0800 Subject: [PATCH 1/2] fix(host): usb host device add device_id --- pkg/hostman/guestman/qemu/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/hostman/guestman/qemu/generate.go b/pkg/hostman/guestman/qemu/generate.go index 95ae585869d..301c6195fcb 100644 --- a/pkg/hostman/guestman/qemu/generate.go +++ b/pkg/hostman/guestman/qemu/generate.go @@ -520,7 +520,7 @@ func GetNicDeviceModel(name string) string { } func generateUsbDeviceOption(usbControllerId string, usb *desc.UsbDevice) string { - cmd := fmt.Sprintf("-device %s,bus=%s.0", usb.DevType, usbControllerId) + cmd := fmt.Sprintf("-device %s,bus=%s.0,id=%s", usb.DevType, usbControllerId, usb.Id) cmd += desc.OptionsToString(usb.Options) return cmd } From 75f03384a946014b38527ea042881a56a1d5b0cd Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 18 Dec 2023 15:26:56 +0800 Subject: [PATCH 2/2] fix(host): hot add usb device --- pkg/hostman/guestman/guesttasks.go | 2 +- pkg/hostman/isolated_device/isolated_device.go | 4 ++-- pkg/hostman/isolated_device/nvidia_vgpu.go | 2 +- pkg/hostman/isolated_device/usb.go | 4 +++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/hostman/guestman/guesttasks.go b/pkg/hostman/guestman/guesttasks.go index dbd8fe84af8..3083a560acb 100644 --- a/pkg/hostman/guestman/guesttasks.go +++ b/pkg/hostman/guestman/guesttasks.go @@ -995,7 +995,7 @@ func (t *SGuestIsolatedDeviceSyncTask) addDevice(dev *desc.SGuestIsolatedDevice) } } - opts, err := devObj.GetHotPlugOptions(dev) + opts, err := devObj.GetHotPlugOptions(dev, t.guest.Desc) if err != nil { cb(errors.Wrap(err, "GetHotPlugOptions").Error()) return diff --git a/pkg/hostman/isolated_device/isolated_device.go b/pkg/hostman/isolated_device/isolated_device.go index 9a643a51d45..ae5e53329bb 100644 --- a/pkg/hostman/isolated_device/isolated_device.go +++ b/pkg/hostman/isolated_device/isolated_device.go @@ -99,7 +99,7 @@ type IDevice interface { GetMdevId() string GetNVIDIAVgpuProfile() map[string]string - GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice) ([]*HotPlugOption, error) + GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice, guestDesc *desc.SGuestDesc) ([]*HotPlugOption, error) GetHotUnplugOptions(isolatedDev *desc.SGuestIsolatedDevice) ([]*HotUnplugOption, error) // Get extra PCIE information @@ -639,7 +639,7 @@ func (dev *sBaseDevice) CustomProbe(idx int) error { return nil } -func (dev *sBaseDevice) GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice) ([]*HotPlugOption, error) { +func (dev *sBaseDevice) GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice, guestDesc *desc.SGuestDesc) ([]*HotPlugOption, error) { ret := make([]*HotPlugOption, 0) var masterDevOpt *HotPlugOption diff --git a/pkg/hostman/isolated_device/nvidia_vgpu.go b/pkg/hostman/isolated_device/nvidia_vgpu.go index 0c48546e3ab..a5eec31dba3 100644 --- a/pkg/hostman/isolated_device/nvidia_vgpu.go +++ b/pkg/hostman/isolated_device/nvidia_vgpu.go @@ -160,7 +160,7 @@ func (dev *sNVIDIAVgpuDevice) GetQemuId() string { return "dev_" + dev.mdevId } -func (dev *sNVIDIAVgpuDevice) GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice) ([]*HotPlugOption, error) { +func (dev *sNVIDIAVgpuDevice) GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice, guestDesc *desc.SGuestDesc) ([]*HotPlugOption, error) { ret := make([]*HotPlugOption, 0) var masterDevOpt *HotPlugOption diff --git a/pkg/hostman/isolated_device/usb.go b/pkg/hostman/isolated_device/usb.go index 7d6f9224c86..f66e9cc9d67 100644 --- a/pkg/hostman/isolated_device/usb.go +++ b/pkg/hostman/isolated_device/usb.go @@ -121,11 +121,13 @@ func (dev *sUSBDevice) GetPassthroughCmd(index int) string { return opt } -func (dev *sUSBDevice) GetHotPlugOptions(*desc.SGuestIsolatedDevice) ([]*HotPlugOption, error) { +func (dev *sUSBDevice) GetHotPlugOptions(isolatedDev *desc.SGuestIsolatedDevice, guestDesc *desc.SGuestDesc) ([]*HotPlugOption, error) { opts, err := GetUSBDevQemuOptions(dev.dev.GetVendorDeviceId(), dev.dev.Addr) if err != nil { return nil, errors.Wrap(err, "GetUSBDevQemuOptions") } + opts["id"] = isolatedDev.Usb.Id + opts["bus"] = fmt.Sprintf("%s.0", guestDesc.Usb.Id) return []*HotPlugOption{ { Device: "usb-host",