fix(host): qemu usb passthrough option convert to int

This commit is contained in:
Zexi Li
2022-01-04 20:28:12 +08:00
parent be9c1760cd
commit a79b8bb73e
2 changed files with 53 additions and 5 deletions
+14 -5
View File
@@ -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) {
+39
View File
@@ -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)
}
})
}
}