Skip to content

Commit

Permalink
Merge pull request #5784 from nanli1/fix_pkg_updated_for_usb_case
Browse files Browse the repository at this point in the history
fix package upated for usb case
  • Loading branch information
chunfuwen authored Sep 12, 2024
2 parents 09194f6 + 1a20a4a commit 4a2c901
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
port_num = "4000"
device_attrs = {'source': {'host': 'localhost', 'service': '4000', 'mode': 'connect'}, 'protocol': {'type': 'raw'}, 'type_name': 'tcp', 'bus': '${bus_type}', 'type': 'tcp', 'boot': {'order': '1'}}
only q35
required_cmds = ['usbredirserver', 'lsusb']
required_cmds = ['lsusb']
- hostdev_device:
device_attrs = {'type_name': 'usb', 'mode': 'subsystem', 'source': {'vendor_id': '%s', 'product_id': '%s'}, 'type': 'usb', 'managed': 'no', 'boot_order': '1'}
4 changes: 3 additions & 1 deletion libvirt/tests/cfg/usb/usb_device.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
- redirdev:
only companion
only pcie-to-pci-bridge,pci-root
pkgs_host = "usbutils,usbredir-server"
pkgs_host = "usbutils"
# pkgs_host_rhel9 = ",usbredir-server"
# pkgs_host_rhel10 = ",usbredir-tools"
device_name = "redirdev"
variants:
- spicevmc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from provider.guest_os_booting import guest_os_booting_base as guest_os
from provider.backingchain import blockcommand_base
from provider.usb import usb_base
from provider.virtual_disk import disk_base


Expand Down Expand Up @@ -66,16 +67,16 @@ def check_boot_order():
usb_device = params.get("usb_device")
bootmenu_dict = eval(params.get("bootmenu_dict", "{}"))
device_attrs = eval(params.get("device_attrs", "{}"))
port_num = params.get("port_num")
check_prompt = params.get("check_prompt")
required_cmds = eval(params.get("required_cmds", "[]"))

vm = env.get_vm(vm_name)
vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
bkxml = vmxml.copy()

test_obj = blockcommand_base.BlockCommand(test, vm, params)
disk_obj = disk_base.DiskBase(test, vm, params)
extra_usb_cmd = usb_base.get_host_pkg_and_cmd()[1]
required_cmds = eval(params.get("required_cmds", "[]")) + [extra_usb_cmd]

try:
for cmd in required_cmds:
Expand All @@ -90,11 +91,7 @@ def check_boot_order():
vendor_id, product_id = get_usb_source()
device_xml = redirdev.Redirdev()
device_xml.setup_attrs(**device_attrs)
# start usbredirserver
ps = process.SubProcess("usbredirserver -p {} {}:{}".format
(port_num, vendor_id, product_id),
shell=True)
server_id = ps.start()
usb_base.start_redirect_server(params, extra_usb_cmd, vendor_id, product_id)
if usb_device == "hostdev_device":
vendor_id, product_id = get_usb_source()
vendor_id = "0x" + vendor_id
Expand All @@ -115,5 +112,4 @@ def check_boot_order():
virsh.destroy(vm_name)
disk_obj.cleanup_disk_preparation(disk_type)
bkxml.sync()
if 'server_id' in locals():
process.run("killall usbredirserver")
usb_base.kill_redirect_server(extra_usb_cmd)
14 changes: 8 additions & 6 deletions libvirt/tests/src/usb/usb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from virttest.libvirt_xml.devices.controller import Controller
from virttest.libvirt_xml.devices.hub import Hub

from provider.usb import usb_base


# Using as lower capital is not the best way to do, but this is just a
# workaround to avoid changing the entire file.
Expand Down Expand Up @@ -57,6 +59,9 @@ def run(test, params, env):
device_type = params.get("device_type", "")
device_mode = params.get("device_mode", "")
port_num = params.get("port_num", "")
if device_name == "redirdev":
add_pkg, usb_cmd = usb_base.get_host_pkg_and_cmd()
pkgs_host = params.get("pkgs_host", "") + add_pkg
pkgs_host = params.get("pkgs_host", "")
pkgs_guest = params.get("pkgs_guest", "")
usb_hub = "yes" == params.get("usb_hub", "no")
Expand Down Expand Up @@ -321,10 +326,8 @@ def check_alias(device_alias):
# start usbredirserver
vendor_id = addr['vendor_id'].lstrip("0x")
product_id = addr['product_id'].lstrip("0x")
ps = process.SubProcess("usbredirserver -p {} {}:{}".format
(port, vendor_id, product_id),
shell=True)
server_id = ps.start()
usb_base.start_redirect_server(params, usb_cmd,
vendor_id, product_id)
if redirdev_alias:
alias_str = "ua-redir" + str(i) + random_id
device_alias[port] = alias_str
Expand Down Expand Up @@ -400,6 +403,5 @@ def check_alias(device_alias):
finally:
if 'session' in locals():
session.close()
if 'server_id' in locals():
process.run("killall usbredirserver")
usb_base.kill_redirect_server(usb_cmd)
vmxml_backup.sync()
49 changes: 49 additions & 0 deletions provider/usb/usb_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from avocado.utils import distro
from avocado.utils import process


def get_host_pkg_and_cmd():
"""
Get package and related command on the host.
:return: pkg_and_cmd, extra usb package name and command.
"""
if int(distro.detect().version) <= 9:
pkg_and_cmd = ("usbredir-server", "usbredirserver")
else:
pkg_and_cmd = ("usbredir-tools", "usbredirect")
return pkg_and_cmd


def start_redirect_server(params, usb_cmd, vendor_id, product_id):
"""
Start redirect server
:param params: Dict with test params.
:param usb_cmd, the usb command to confirm how to start server.
:param vendor_id, vendor id.
:param product_id, produce id.
:return server_id, redirect server id
"""
port_num = params.get("port_num")
if usb_cmd == "usbredirserver":
ps = process.SubProcess("usbredirserver -p {} {}:{}".format
(port_num, vendor_id, product_id),
shell=True)
elif usb_cmd == "usbredirect":
ps = process.SubProcess(
"usbredirect --device {}:{} --as {}:{}".format
(vendor_id, product_id, "127.0.0.1", port_num),
shell=True)
server_id = ps.start()
return server_id


def kill_redirect_server(usb_cmd):
"""
Kill redirect server
:param usb_cmd, the usb command to kill server.
"""
if 'server_id' in globals():
process.run("killall {}".format(usb_cmd), ignore_status=True)

0 comments on commit 4a2c901

Please sign in to comment.