diff --git a/pkg/hostman/isolated_device/usb.go b/pkg/hostman/isolated_device/usb.go index bfc4ed29f9..8515fa26fc 100644 --- a/pkg/hostman/isolated_device/usb.go +++ b/pkg/hostman/isolated_device/usb.go @@ -16,6 +16,7 @@ package isolated_device import ( "fmt" + "strconv" "strings" "yunion.io/x/jsonutils" @@ -54,16 +55,24 @@ func GetUSBDevId(vendorId, devId, bus, addr string) string { return fmt.Sprintf("dev_%s_%s-%s_%s", vendorId, devId, bus, addr) } -func getUSBDevQemuOptions(vendorId, deviceId string, bus, addr string) map[string]interface{} { +func getUSBDevQemuOptions(vendorId, deviceId string, bus, addr string) (map[string]interface{}, error) { id := GetUSBDevId(vendorId, deviceId, bus, addr) + busI, err := strconv.Atoi(bus) + if err != nil { + return nil, errors.Wrapf(err, "parse bus to int %q", bus) + } + addrI, err := strconv.Atoi(addr) + if err != nil { + return nil, errors.Wrapf(err, "parse addr to int %q", bus) + } return map[string]interface{}{ "id": id, "bus": "usb.0", "vendorid": fmt.Sprintf("0x%s", vendorId), "productid": fmt.Sprintf("0x%s", deviceId), - "hostbus": bus, - "hostaddr": addr, - } + "hostbus": fmt.Sprintf("%d", busI), + "hostaddr": fmt.Sprintf("%d", addrI), + }, nil } func GetUSBDevQemuOptions(vendorDevId string, addr string) (map[string]interface{}, error) { @@ -81,7 +90,7 @@ func GetUSBDevQemuOptions(vendorDevId string, addr string) (map[string]interface hostBus := addrParts[0] hostAddr := addrParts[1] - return getUSBDevQemuOptions(vendorId, productId, hostBus, hostAddr), nil + return getUSBDevQemuOptions(vendorId, productId, hostBus, hostAddr) } func (dev *sUSBDevice) GetKernelDriver() (string, error) { diff --git a/pkg/hostman/isolated_device/usb_test.go b/pkg/hostman/isolated_device/usb_test.go index 39b118d67e..472bec4cc6 100644 --- a/pkg/hostman/isolated_device/usb_test.go +++ b/pkg/hostman/isolated_device/usb_test.go @@ -142,3 +142,42 @@ func Test_isUSBLinuxRootHub(t *testing.T) { }) } } + +func Test_getUSBDevQemuOptions(t *testing.T) { + type args struct { + vendorId string + deviceId string + bus string + addr string + } + tests := []struct { + name string + args args + want map[string]interface{} + }{ + { + name: "", + args: args{ + vendorId: "1d6b", + deviceId: "0001", + bus: "001", + addr: "009", + }, + want: map[string]interface{}{ + "id": "dev_1d6b_0001-001_009", + "bus": "usb.0", + "vendorid": "0x1d6b", + "productid": "0x0001", + "hostbus": "1", + "hostaddr": "9", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got, _ := getUSBDevQemuOptions(tt.args.vendorId, tt.args.deviceId, tt.args.bus, tt.args.addr); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getUSBDevQemuOptions() = %v, want %v", got, tt.want) + } + }) + } +}