config_tools: refine virtio in launch script

1. for virtio console, reference to the document
https://projectacrn.github.io/latest/developer-guides/hld/virtio-console.html,
the generated launch script will look like this:
`virtio-console,[@]stdio|tty|pty|file:portname[=portpath]\
[,[@]stdio|tty|pty|file:portname[=portpath][:socket_type]]`
  *receding with @ marks the port as a console port,
   otherwise it is a normal virtio-serial port
  *The portpath can be omitted when backend is stdio or pty.
2. for virtio input, the generated launch script as below.
`<name>:<phys>,id=<anyString>`
The launch script will automatically find the specific /dev/input/eventX
according to the event name and phys got from board.xml.

Tracked-On: #6690
Signed-off-by: Kunhui-Li <kunhuix.li@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Kunhui-Li 2022-04-11 20:51:13 +08:00 committed by acrnsi-robot
parent 76dc2b3a5e
commit 2ce914428c
2 changed files with 65 additions and 6 deletions

View File

@ -254,14 +254,34 @@ def generate_for_one_vm(board_etree, hv_scenario_etree, vm_scenario_etree, vm_id
for usb_xhci in eval_xpath_all(vm_scenario_etree, ".//usb_xhci[text() != '']/text()"):
script.add_virtual_device("xhci", options=usb_xhci)
for virtio_input in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/input[text() != '']/text()"):
script.add_virtual_device("virtio-input", options=virtio_input)
for virtio_input_etree in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/input"):
backend_device_file = eval_xpath(virtio_input_etree, "./backend_device_file[text() != '']/text()")
unique_identifier = eval_xpath(virtio_input_etree, "./id[text() != '']/text()")
if backend_device_file is not None and unique_identifier is not None:
script.add_virtual_device("virtio-input", options=f"{backend_device_file},id={unique_identifier}")
elif backend_device_file is not None:
script.add_virtual_device("virtio-input", options=backend_device_file)
for virtio_console in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/console[text() != '']/text()"):
script.add_virtual_device("virtio-console", options=virtio_console)
for backend_type in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/console/backend_type[text() != '']/text()"):
preceding_mask = ""
use_type = eval_xpath(vm_scenario_etree, ".//virtio_devices/console/use_type/text()")
if use_type == "Virtio console":
preceding_mask = "@"
for virtio_network in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/network[text() != '']/text()"):
params = virtio_network.split(",", maxsplit=1)
if backend_type == "file":
output_file_path = eval_xpath(vm_scenario_etree, ".//virtio_devices/console/output_file_path/text()")
script.add_virtual_device("virtio-console", options=f"{preceding_mask}file:file_port={output_file_path}")
elif backend_type == "tty":
tty_file_path = eval_xpath(vm_scenario_etree, ".//virtio_devices/console/tty_device_path/text()")
script.add_virtual_device("virtio-console", options=f"{preceding_mask}tty:tty_port={tty_file_path}")
elif backend_type == "sock server" or backend_type == "sock client":
sock_file_path = eval_xpath(vm_scenario_etree, ".//virtio_devices/console/sock_file_path/text()")
script.add_virtual_device("virtio-console", options=f"socket:{os.path.basename(sock_file_path).split('.')[0]}={sock_file_path}:{backend_type.replace('sock ', '')}")
else:
script.add_virtual_device("virtio-console", options=f"{preceding_mask}{backend_type}:{backend_type}_port")
for interface_name in eval_xpath_all(vm_scenario_etree, ".//virtio_devices/network/interface_name[text() != '']/text()"):
params = interface_name.split(",", maxsplit=1)
tap_conf = f"tap={params[0]}"
params = [tap_conf] + params[1:]
script.add_init_command(f"mac=$(cat /sys/class/net/e*/address)")

View File

@ -133,6 +133,45 @@ function add_virtual_device() {
fi
fi
if [ "${kind}" = "virtio-input" ]; then
if [[ "${options}" =~ id=([a-zA-Z0-9_\-]*) ]]; then
unique_identifier="${BASH_REMATCH[1]}"
options=${options/",id=${unique_identifier}"/''}
fi
local IFS=$'\n'
device_name_path=$(grep -r "${options%:*}" /sys/class/input/event*/device/name)
if [ -n "${device_name_path}" ]; then
for device_path in ${device_name_path}; do
if [ "${options%:*}" = "${options#*:}" ]; then
options="/dev/input/${device_path:17:6}"
break
else
phys_path=$(grep -r "${options#*:}" /sys/class/input/event*/device/phys)
if [ "${device_path%/device*}" = "${phys_path%/device*}" ]; then
options="/dev/input/${device_path:17:6}"
break
fi
fi
done
else
echo "${options%:*} device path is not found in the service vm." >> /dev/stderr
return
fi
if [[ ${options} =~ event([0-9]+) ]]; then
echo "${options} input device path is available in the service vm." >> /dev/stderr
else
echo "${options#*:} input phys path is not found in the service vm." >> /dev/stderr
return
fi
if [ -n "${options}" ] && [ -n "${unique_identifier}" ]; then
options="${options},${unique_identifier}"
fi
fi
echo -n "-s ${slot},${kind}"
if [ -n "${options}" ]; then
echo -n ",${options}"